Rewrite usb device version parsing

Simplify the function by leaving out the local copy and checking
return values of virStrToLong.
This commit is contained in:
Ján Tomko 2015-04-10 15:50:53 +02:00
parent a75069be35
commit 55a0670a6d

View File

@ -11345,66 +11345,41 @@ virDomainRedirdevDefParseXML(xmlNodePtr node,
} }
/* /*
* This is the helper function to convert USB version from a * This is the helper function to convert USB device version from a
* format of JJ.MN to a format of 0xJJMN where JJ is the major * format of JJ.MN to a format of 0xJJMN where JJ is the major
* version number, M is the minor version number and N is the * version number, M is the minor version number and N is the
* sub minor version number. * sub minor version number.
* e.g. USB 2.0 is reported as 0x0200, * e.g. USB version 2.0 is reported as 0x0200,
* USB 1.1 as 0x0110 and USB 1.0 as 0x0100. * USB version 4.07 as 0x0407
*/ */
static int static int
virDomainRedirFilterUSBVersionHelper(const char *version, virDomainRedirFilterUSBVersionHelper(const char *version,
virDomainRedirFilterUSBDevDefPtr def) virDomainRedirFilterUSBDevDefPtr def)
{ {
char *version_copy = NULL; unsigned int major, minor;
char *temp = NULL; char *s = NULL;
int ret = -1;
size_t len;
size_t fraction_len;
unsigned int major;
unsigned int minor;
unsigned int hex;
if (VIR_STRDUP(version_copy, version) < 0) if ((virStrToLong_ui(version, &s, 10, &major)) < 0 ||
return -1; *s++ != '.' ||
(virStrToLong_ui(s, NULL, 10, &minor)) < 0)
goto error;
len = strlen(version_copy); if (major >= 100 || minor >= 100)
/* goto error;
* The valid format of version is like 01.10, 1.10, 1.1, etc.
*/
if (len > 5 ||
!(temp = strchr(version_copy, '.')) ||
temp - version_copy < 1 ||
temp - version_copy > 2 ||
!(fraction_len = strlen(temp + 1)) ||
fraction_len > 2) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Incorrect USB version format %s"), version);
goto cleanup;
}
*temp = '\0'; /* Treat JJ.M as JJ.M0, not JJ.0M */
temp++; if (strlen(s) == 1)
minor *= 10;
if ((virStrToLong_ui(version_copy, NULL, 10, &major)) < 0 || def->version = (major / 10) << 12 | (major % 10) << 8 |
(virStrToLong_ui(temp, NULL, 10, &minor)) < 0) { (minor / 10) << 4 | (minor % 10) << 0;
return 0;
error:
virReportError(VIR_ERR_XML_ERROR, virReportError(VIR_ERR_XML_ERROR,
_("Cannot parse USB version %s"), version); _("Cannot parse USB device version %s"), version);
goto cleanup; return -1;
}
hex = (major / 10) << 12 | (major % 10) << 8;
if (fraction_len == 1)
hex |= (minor % 10) << 4;
else
hex |= (minor / 10) << 4 | (minor % 10) << 0;
def->version = hex;
ret = 0;
cleanup:
VIR_FREE(version_copy);
return ret;
} }
static virDomainRedirFilterUSBDevDefPtr static virDomainRedirFilterUSBDevDefPtr