]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ieee1284.c
Fix build errors on Fedora.
[thirdparty/cups.git] / backend / ieee1284.c
index 8b8579d8066fac5a9c99988f3b015022a2a2b035..b4ded43ee394389fd9bdde70de05477ec2e9ce4d 100644 (file)
@@ -1,24 +1,18 @@
 /*
- * "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $"
+ * "$Id$"
  *
- *   IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
+ * IEEE-1284 support functions for CUPS.
  *
- *   Copyright 2007 by Apple Inc.
- *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   These coded instructions, statements, and computer programs are the
- *   property of Apple Inc. and are protected by Federal copyright
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   "LICENSE" which should have been included with this file.  If this
- *   file is missing or damaged, see the license at "http://www.cups.org/".
+ * These coded instructions, statements, and computer programs are the
+ * property of Apple Inc. and are protected by Federal copyright
+ * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ * "LICENSE" which should have been included with this file.  If this
+ * file is missing or damaged, see the license at "http://www.cups.org/".
  *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   backendGetDeviceID()  - Get the IEEE-1284 device ID string and
- *                           corresponding URI.
- *   backendGetMakeModel() - Get the make and model string from the device ID.
+ * This file is subject to the Apple OS-Developed Software exception.
  */
 
 /*
 
 #include "backend-private.h"
 
-#ifdef __linux
-#  include <sys/ioctl.h>
-#  include <linux/lp.h>
-#  define IOCNR_GET_DEVICE_ID          1
-#  define LPIOC_GET_DEVICE_ID(len)     _IOC(_IOC_READ, 'P', IOCNR_GET_DEVICE_ID, len)
-#  include <linux/parport.h>
-#  include <linux/ppdev.h>
-#  include <unistd.h>
-#  include <fcntl.h>
-#endif /* __linux */
-
-#ifdef __sun
-#  ifdef __sparc
-#    include <sys/ecppio.h>
-#  else
-#    include <sys/ioccom.h>
-#    include <sys/ecppsys.h>
-#  endif /* __sparc */
-#endif /* __sun */
-
 
 /*
  * 'backendGetDeviceID()' - Get the IEEE-1284 device ID string and
@@ -65,6 +39,15 @@ backendGetDeviceID(
     int        uri_size)               /* I - Size of buffer */
 {
 #ifdef __APPLE__ /* This function is a no-op */
+  (void)fd;
+  (void)device_id;
+  (void)device_id_size;
+  (void)make_model;
+  (void)make_model_size;
+  (void)scheme;
+  (void)uri;
+  (void)uri_size;
+
   return (-1);
 
 #else /* Get the device ID from the specified file descriptor... */
@@ -75,6 +58,7 @@ backendGetDeviceID(
 #  if defined(__sun) && defined(ECPPIOC_GETDEVID)
   struct ecpp_device_id did;           /* Device ID buffer */
 #  endif /* __sun && ECPPIOC_GETDEVID */
+  char *ptr;                           /* Pointer into device ID */
 
 
   DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
@@ -157,8 +141,7 @@ backendGetDeviceID(
                * Read the 1284 device ID...
                */
 
-               if ((length = read(devparportfd, device_id,
-                                  device_id_size - 1)) >= 2)
+               if ((length = read(devparportfd, device_id, (size_t)device_id_size - 1)) >= 2)
                {
                  device_id[length] = '\0';
                  got_id = 1;
@@ -187,8 +170,7 @@ backendGetDeviceID(
       * bytes.  The 1284 spec says the length is stored MSB first...
       */
 
-      length = (((unsigned)device_id[0] & 255) << 8) +
-              ((unsigned)device_id[1] & 255);
+      length = (int)((((unsigned)device_id[0] & 255) << 8) + ((unsigned)device_id[1] & 255));
 
      /*
       * Check to see if the length is larger than our buffer; first
@@ -196,25 +178,49 @@ backendGetDeviceID(
       * and then limit the length to the size of our buffer...
       */
 
-      if (length > (device_id_size - 2))
-       length = (((unsigned)device_id[1] & 255) << 8) +
-                ((unsigned)device_id[0] & 255);
+      if (length > device_id_size || length < 14)
+       length = (int)((((unsigned)device_id[1] & 255) << 8) + ((unsigned)device_id[0] & 255));
 
-      if (length > (device_id_size - 2))
-       length = device_id_size - 2;
+      if (length > device_id_size)
+       length = device_id_size;
 
      /*
-      * Copy the device ID text to the beginning of the buffer and
-      * nul-terminate.
+      * The length field counts the number of bytes in the string
+      * including the length field itself (2 bytes).  The minimum
+      * length for a valid/usable device ID is 14 bytes:
+      *
+      *     <LENGTH> MFG: <MFG> ;MDL: <MDL> ;
+      *        2  +   4  +  1  +  5 +  1 +  1
       */
 
-      memmove(device_id, device_id + 2, length);
-      device_id[length] = '\0';
+      if (length < 14)
+      {
+       /*
+       * Can't use this device ID, so don't try to copy it...
+       */
+
+       device_id[0] = '\0';
+       got_id       = 0;
+      }
+      else
+      {
+       /*
+       * Copy the device ID text to the beginning of the buffer and
+       * nul-terminate.
+       */
+
+       length -= 2;
+
+       memmove(device_id, device_id + 2, (size_t)length);
+       device_id[length] = '\0';
+      }
     }
-#    ifdef DEBUG
     else
-      printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno));
-#    endif /* DEBUG */
+    {
+      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
+                    strerror(errno)));
+      *device_id = '\0';
+    }
 #  endif /* __linux */
 
 #   if defined(__sun) && defined(ECPPIOC_GETDEVID)
@@ -236,11 +242,28 @@ backendGetDeviceID(
     }
 #    ifdef DEBUG
     else
-      printf("backendGetDeviceID: ioctl failed - %s\n", strerror(errno));
+      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
+                    strerror(errno)));
 #    endif /* DEBUG */
 #  endif /* __sun && ECPPIOC_GETDEVID */
   }
 
+ /*
+  * Check whether device ID is valid. Turn line breaks and tabs to spaces and
+  * reject device IDs with non-printable characters.
+  */
+
+  for (ptr = device_id; *ptr; ptr ++)
+    if (_cups_isspace(*ptr))
+      *ptr = ' ';
+    else if ((*ptr & 255) < ' ' || *ptr == 127)
+    {
+      DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
+                    *ptr & 255));
+      *device_id = '\0';
+      break;
+    }
+
   DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
 
   if (scheme && uri)
@@ -254,7 +277,7 @@ backendGetDeviceID(
   */
 
   if (make_model)
-    backendGetMakeModel(device_id, make_model, make_model_size);
+    backendGetMakeModel(device_id, make_model, (size_t)make_model_size);
 
  /*
   * Then generate a device URI...
@@ -275,7 +298,7 @@ backendGetDeviceID(
     * Get the make, model, and serial numbers...
     */
 
-    num_values = _ppdGet1284Values(device_id, &values);
+    num_values = _cupsGet1284Values(device_id, &values);
 
     if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
       if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
@@ -289,9 +312,9 @@ backendGetDeviceID(
 
     if (mfg)
     {
-      if (!strcasecmp(mfg, "Hewlett-Packard"))
+      if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
         mfg = "HP";
-      else if (!strcasecmp(mfg, "Lexmark International"))
+      else if (!_cups_strcasecmp(mfg, "Lexmark International"))
         mfg = "Lexmark";
     }
     else
@@ -304,6 +327,17 @@ backendGetDeviceID(
       mfg = temp;
     }
 
+    if (!mdl)
+      mdl = "";
+
+    if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
+    {
+      mdl += strlen(mfg);
+
+      while (isspace(*mdl & 255))
+        mdl ++;
+    }
+
    /*
     * Generate the device URI from the manufacturer, make_model, and
     * serial number strings.
@@ -328,7 +362,7 @@ int                                 /* O - 0 on success, -1 on failure */
 backendGetMakeModel(
     const char *device_id,             /* O - 1284 device ID */
     char       *make_model,            /* O - Make/model */
-    int        make_model_size)                /* I - Size of buffer */
+    size_t     make_model_size)                /* I - Size of buffer */
 {
   int          num_values;             /* Number of keys and values */
   cups_option_t        *values;                /* Keys and values */
@@ -337,9 +371,7 @@ backendGetMakeModel(
                *des;                   /* Description string */
 
 
-  DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
-                "make_model=%p, make_model_size=%d)\n", device_id,
-               make_model, make_model_size));
+  DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", make_model=%p, make_model_size=" CUPS_LLFMT ")\n", device_id, make_model, CUPS_LLCAST make_model_size));
 
  /*
   * Range check input...
@@ -357,7 +389,7 @@ backendGetMakeModel(
   * Look for the description field...
   */
 
-  num_values = _ppdGet1284Values(device_id, &values);
+  num_values = _cupsGet1284Values(device_id, &values);
 
   if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
     mdl = cupsGetOption("MDL", num_values, values);
@@ -371,7 +403,7 @@ backendGetMakeModel(
     if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
       mfg = cupsGetOption("MFG", num_values, values);
 
-    if (!mfg || !strncasecmp(mdl, mfg, strlen(mfg)))
+    if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg)))
     {
      /*
       * Just copy the model string, since it has the manufacturer...
@@ -388,6 +420,7 @@ backendGetMakeModel(
       char     temp[1024];             /* Temporary make and model */
 
       snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
+
       _ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
     }
   }
@@ -442,5 +475,5 @@ backendGetMakeModel(
 
 
 /*
- * End of "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $".
+ * End of "$Id$".
  */