common: Fix more USB descriptor parsing bugs.

This commit is contained in:
Solomon Peachy 2013-07-06 20:58:23 -04:00
parent 6093b65481
commit fec7e6d776
2 changed files with 19 additions and 6 deletions

View file

@ -28,7 +28,7 @@
#include <libusb-1.0/libusb.h>
#include <arpa/inet.h>
#define BACKEND_VERSION "0.2"
#define BACKEND_VERSION "0.3"
#define STR_LEN_MAX 64
#define DEBUG( ... ) fprintf(stderr, "DEBUG: " __VA_ARGS__ )
@ -149,6 +149,16 @@ static void sigterm_handler(int signum) {
INFO("Job Cancelled");
}
static char *sanitize_string(char *str) {
int len = strlen(str);
while(len && (str[len-1] <= 0x20)) {
str[len-1] = 0;
len--;
}
return str;
}
static int print_scan_output(struct libusb_device *device,
struct libusb_device_descriptor *desc,
char *prefix, char *manuf2,
@ -170,12 +180,15 @@ static int print_scan_output(struct libusb_device *device,
/* Query detailed info */
if (desc->iManufacturer) {
libusb_get_string_descriptor_ascii(dev, desc->iManufacturer, manuf, STR_LEN_MAX);
sanitize_string((char*)manuf);
}
if (desc->iProduct) {
libusb_get_string_descriptor_ascii(dev, desc->iProduct, product, STR_LEN_MAX);
sanitize_string((char*)product);
}
if (desc->iSerialNumber) {
libusb_get_string_descriptor_ascii(dev, desc->iSerialNumber, serial, STR_LEN_MAX);
sanitize_string((char*)serial);
}
if (!strlen((char*)serial))
@ -190,9 +203,9 @@ static int print_scan_output(struct libusb_device *device,
/* URL-ify model. */
char buf[128]; // XXX ugly..
int j = 0, k = 0;
char *ieee_id;
char *ieee_id = get_device_id(dev);
while (*(product + j + strlen(manuf2))) {
buf[k] = *(product + j + strlen(manuf2) + 1);
buf[k] = *(product + j + (strlen(manuf2) ? (strlen(manuf2) + 1) : 0));
if(buf[k] == ' ') {
buf[k++] = '%';
buf[k++] = '2';
@ -201,10 +214,10 @@ static int print_scan_output(struct libusb_device *device,
k++;
j++;
}
ieee_id = get_device_id(dev);
buf[k] = 0;
fprintf(stdout, "direct %s%s/%s?serial=%s \"%s\" \"%s\" \"%s\" \"\"\n",
prefix, manuf2,
prefix, strlen(manuf2) ? manuf2 : (char*)manuf,
buf, serial, product, product,
ieee_id);

View file

@ -436,7 +436,7 @@ static int find_and_enumerate(struct libusb_context *ctx,
}
found = print_scan_output((*list)[i], &desc,
URI_PREFIX, "Shinko",
URI_PREFIX, "",
found, (found == i), 1,
scan_only, match_serno);
}