Hi,
This is a patch for pyusb 0.4.1.

Currently, getString(index, len, -1) returns a plain string, but when
providing a langid like in getString(index, len, 0x409) it returns the
raw string descriptor which then has to be converted into a plain
string. The patch makes the latter case return a plain string like in
the former case. The conversion is similar to the one done in the
libusb-0.1 usb_get_string_simple() function.

I tried to check how this is done in pyusb 1.x, but AFAICS the
getString() method is not implemented there yet.

Tormod
diff -u pyusb-0.4.1/pyusb.c.orig pyusb-0.4.1/pyusb.c
--- pyusb-0.4.1.orig/pyusb.c
+++ pyusb-0.4.1/pyusb.c
@@ -1686,6 +1686,7 @@
 	unsigned long len;
 	PyObject *retStr;
 	char *buffer;
+	char *rawbuffer;
 	int ret;
 	Py_usb_DeviceHandle *_self = (Py_usb_DeviceHandle *) self;
 
@@ -1713,19 +1714,33 @@
 	++len;	/* for NULL termination */
 	buffer = (char *) PyMem_Malloc(len);
 	if (!buffer) return NULL;
+	rawbuffer = (char *) PyMem_Malloc(len*2); /* for string descriptor */
+	if (!rawbuffer) return NULL;
 
 	Py_BEGIN_ALLOW_THREADS
 
 	if (-1 == langid) {
 		ret = usb_get_string_simple(_self->deviceHandle, index, buffer, len);
 	} else {
-		ret = usb_get_string(_self->deviceHandle, index, langid, buffer, len);
+		int di, si;
+
+		ret = usb_get_string(_self->deviceHandle, index, langid, rawbuffer, len*2);
+		/* convert string descriptor to ascii string */
+		for (di = 0, si = 2; si < rawbuffer[0] && si < ret-1; si += 2) {
+			if (rawbuffer[si + 1])   /* high byte */
+				buffer[di++] = '?';
+			else
+				buffer[di++] = rawbuffer[si];
+		}
+		buffer[di] = 0;
+		ret = di;
 	}
 
 	Py_END_ALLOW_THREADS
 
 	if (ret < 0) {
 		PyMem_Free(buffer);
+		PyMem_Free(rawbuffer);
 		PyUSB_Error();
 		return NULL;
 	}
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to