| 1071 | */ |
| 1072 | |
| 1073 | static int /* O - 0 on success, -1 on error */ |
| 1074 | get_device_id(usb_printer_t *printer, /* I - Printer */ |
| 1075 | char *buffer, /* I - String buffer */ |
| 1076 | size_t bufsize) /* I - Number of bytes in buffer */ |
| 1077 | { |
| 1078 | size_t length; /* Length of device ID */ |
| 1079 | |
| 1080 | |
| 1081 | if (libusb_control_transfer(printer->handle, |
| 1082 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_ENDPOINT_IN | |
| 1083 | LIBUSB_RECIPIENT_INTERFACE, |
| 1084 | 0, printer->conf, |
| 1085 | (printer->iface << 8) | printer->altset, |
| 1086 | (unsigned char *)buffer, bufsize, 5000) < 0) |
| 1087 | { |
| 1088 | *buffer = '\0'; |
| 1089 | return (-1); |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Extract the length of the device ID string from the first two |
| 1094 | * bytes. The 1284 spec says the length is stored MSB first... |
| 1095 | */ |
| 1096 | |
| 1097 | length = (((unsigned)buffer[0] & 255) << 8) | ((unsigned)buffer[1] & 255); |
| 1098 | |
| 1099 | /* |
| 1100 | * Check to see if the length is larger than our buffer or less than 14 bytes |
| 1101 | * (the minimum valid device ID is "MFG:x;MDL:y;" with 2 bytes for the length). |
| 1102 | * |
| 1103 | * If the length is out-of-range, assume that the vendor incorrectly |
| 1104 | * implemented the 1284 spec and re-read the length as LSB first,.. |
| 1105 | */ |
| 1106 | |
| 1107 | if (length > bufsize || length < 14) |
| 1108 | length = (((unsigned)buffer[1] & 255) << 8) | ((unsigned)buffer[0] & 255); |
| 1109 | |
| 1110 | if (length > bufsize) |
| 1111 | length = bufsize; |
| 1112 | |
| 1113 | if (length < 14) |
| 1114 | { |
| 1115 | /* |
| 1116 | * Invalid device ID, clear it! |
| 1117 | */ |
| 1118 | |
| 1119 | *buffer = '\0'; |
| 1120 | return (-1); |
| 1121 | } |
| 1122 | |
| 1123 | length -= 2; |
| 1124 | |
| 1125 | /* |
| 1126 | * Copy the device ID text to the beginning of the buffer and |
| 1127 | * nul-terminate. |
| 1128 | */ |
| 1129 | |
| 1130 | memmove(buffer, buffer + 2, length); |
no outgoing calls
no test coverage detected