]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - backend/ieee1284.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / backend / ieee1284.c
index a9346d7a52bd9d3cd696138778ac89a05c75b383..821315ff34252586b112c7755e6ec97ab6399026 100644 (file)
@@ -1,24 +1,11 @@
 /*
- * "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $"
+ * IEEE-1284 support functions for CUPS.
  *
- *   IEEE-1284 support functions for CUPS.
+ * Copyright © 2007-2015 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
  *
- *   Copyright 2007-2011 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/".
- *
- *   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.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
@@ -26,7 +13,7 @@
  */
 
 #include "backend-private.h"
-#include <cups/cups-private.h>
+#include <cups/ppd-private.h>
 
 
 /*
@@ -65,21 +52,15 @@ 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, "
-                "make_model=%p, make_model_size=%d, scheme=\"%s\", "
-               "uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
-               make_model, make_model_size, scheme ? scheme : "(null)",
-               uri, uri_size));
-
  /*
   * Range check input...
   */
 
   if (!device_id || device_id_size < 32)
   {
-    DEBUG_puts("backendGetDeviceID: Bad args!");
     return (-1);
   }
 
@@ -95,7 +76,7 @@ backendGetDeviceID(
     *device_id = '\0';
 
 #  ifdef __linux
-    if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
+    if (ioctl(fd, LPIOC_GET_DEVICE_ID((unsigned)device_id_size), device_id))
     {
      /*
       * Linux has to implement things differently for every device it seems.
@@ -147,8 +128,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;
@@ -177,8 +157,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
@@ -186,9 +165,8 @@ backendGetDeviceID(
       * and then limit the length to the size of our buffer...
       */
 
-      if (length > device_id_size)
-       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)
        length = device_id_size;
@@ -220,15 +198,14 @@ backendGetDeviceID(
 
        length -= 2;
 
-       memmove(device_id, device_id + 2, length);
+       memmove(device_id, device_id + 2, (size_t)length);
        device_id[length] = '\0';
       }
     }
-#    ifdef DEBUG
     else
-      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
-                    strerror(errno)));
-#    endif /* DEBUG */
+    {
+      *device_id = '\0';
+    }
 #  endif /* __linux */
 
 #   if defined(__sun) && defined(ECPPIOC_GETDEVID)
@@ -248,15 +225,22 @@ backendGetDeviceID(
       else
        device_id[device_id_size - 1] = '\0';
     }
-#    ifdef DEBUG
-    else
-      DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
-                    strerror(errno)));
-#    endif /* DEBUG */
 #  endif /* __sun && ECPPIOC_GETDEVID */
   }
 
-  DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
+ /*
+  * 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)
+    {
+      *device_id = '\0';
+      break;
+    }
 
   if (scheme && uri)
     *uri = '\0';
@@ -269,7 +253,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...
@@ -354,7 +338,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 */
@@ -363,19 +347,12 @@ 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));
-
  /*
   * Range check input...
   */
 
   if (!device_id || !*device_id || !make_model || make_model_size < 32)
-  {
-    DEBUG_puts("backendGetMakeModel: Bad args!");
     return (-1);
-  }
 
   *make_model = '\0';
 
@@ -466,8 +443,3 @@ backendGetMakeModel(
 
   return (0);
 }
-
-
-/*
- * End of "$Id: ieee1284.c 7687 2008-06-24 01:28:36Z mike $".
- */