mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-04-26 15:14:42 +00:00
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:
parent
a75069be35
commit
55a0670a6d
@ -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;
|
||||||
virReportError(VIR_ERR_XML_ERROR,
|
|
||||||
_("Cannot parse USB version %s"), version);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
hex = (major / 10) << 12 | (major % 10) << 8;
|
return 0;
|
||||||
if (fraction_len == 1)
|
|
||||||
hex |= (minor % 10) << 4;
|
|
||||||
else
|
|
||||||
hex |= (minor / 10) << 4 | (minor % 10) << 0;
|
|
||||||
|
|
||||||
def->version = hex;
|
error:
|
||||||
ret = 0;
|
virReportError(VIR_ERR_XML_ERROR,
|
||||||
|
_("Cannot parse USB device version %s"), version);
|
||||||
cleanup:
|
return -1;
|
||||||
VIR_FREE(version_copy);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static virDomainRedirFilterUSBDevDefPtr
|
static virDomainRedirFilterUSBDevDefPtr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user