Exemple #1
0
char *get_dev_string(libusb_device_handle *dev, u_int8_t id)
{
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
	int ret;
	char *buf, unicode_buf[254];
	u_int16_t langid;
#endif

	if (!dev || !id) return strdup("");

#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
	langid = get_any_langid(dev);
	if (!langid) return strdup("(error)");

	ret = libusb_get_string_descriptor(dev, id, langid,
	                                   (unsigned char *) unicode_buf,
	                                   sizeof unicode_buf);
	if (ret < 2) return strdup("(error)");

	if ((unsigned char)unicode_buf[0] < 2 || unicode_buf[1] != LIBUSB_DT_STRING)
		return strdup("(error)");

	buf = usb_string_to_native(unicode_buf + 2,
	                           ((unsigned char) unicode_buf[0] - 2) / 2);

	if (!buf) return get_dev_string_ascii(dev, 127, id);

	return buf;
#else
	return get_dev_string_ascii(dev, 127, id);
#endif
}
Exemple #2
0
int get_dev_string(char *buf, size_t size, libusb_device_handle *hdev, u_int8_t id)
{
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
	int ret;
	unsigned char unicode_buf[254];
	u_int16_t langid;
#endif

	if (!hdev || !id) {
		return 0;
	}
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
	langid = get_any_langid(hdev);
	if (!langid) {
		return snprintf(buf, size, "%s", "(error)");
	}

	ret = libusb_get_string_descriptor(hdev, id, langid,
					   (unsigned char *) unicode_buf,
					   sizeof unicode_buf);
	if (ret < 2) {
		return snprintf(buf, size, "%s", "(error)");
	}
	char *tmp = usb_string_to_native(unicode_buf + 2,
				   ((unsigned char) unicode_buf[0] - 2) / 2);

	if (tmp) {
		snprintf(buf, size, "%s", tmp);
		free(tmp);
	} else {
		get_dev_string_ascii(buf, size, hdev, id);
	}
	return strlen(buf);
#else
	get_dev_string_ascii(buf, size, hdev, id);
	return strlen(buf);
#endif
}