/*
- * "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $"
+ * IEEE-1284 support functions for CUPS.
*
- * IEEE-1284 support functions for the Common UNIX Printing System (CUPS).
+ * Copyright © 2007-2015 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
*
- * Copyright 2007 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.
*/
/*
*/
#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 */
+#include <cups/ppd-private.h>
/*
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... */
# 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, "
*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.
* 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;
* 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
* 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)
}
# 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)
*/
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...
* 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)
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
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.
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 */
*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...
* 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);
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...
char temp[1024]; /* Temporary make and model */
snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
+
_ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
}
}
return (0);
}
-
-
-/*
- * End of "$Id: ieee1284.c 7019 2007-10-10 22:48:52Z mike $".
- */