names.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * names.c -- USB name database manipulation routines
  4. *
  5. * Copyright (C) 1999, 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
  6. *
  7. * Copyright (C) 2005 Takahiro Hirofuchi
  8. * - names_deinit() is added.
  9. */
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <dirent.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include "names.h"
  21. #include "usbip_common.h"
  22. struct vendor {
  23. struct vendor *next;
  24. u_int16_t vendorid;
  25. char name[1];
  26. };
  27. struct product {
  28. struct product *next;
  29. u_int16_t vendorid, productid;
  30. char name[1];
  31. };
  32. struct class {
  33. struct class *next;
  34. u_int8_t classid;
  35. char name[1];
  36. };
  37. struct subclass {
  38. struct subclass *next;
  39. u_int8_t classid, subclassid;
  40. char name[1];
  41. };
  42. struct protocol {
  43. struct protocol *next;
  44. u_int8_t classid, subclassid, protocolid;
  45. char name[1];
  46. };
  47. struct genericstrtable {
  48. struct genericstrtable *next;
  49. unsigned int num;
  50. char name[1];
  51. };
  52. #define HASH1 0x10
  53. #define HASH2 0x02
  54. #define HASHSZ 16
  55. static unsigned int hashnum(unsigned int num)
  56. {
  57. unsigned int mask1 = HASH1 << 27, mask2 = HASH2 << 27;
  58. for (; mask1 >= HASH1; mask1 >>= 1, mask2 >>= 1)
  59. if (num & mask1)
  60. num ^= mask2;
  61. return num & (HASHSZ-1);
  62. }
  63. static struct vendor *vendors[HASHSZ] = { NULL, };
  64. static struct product *products[HASHSZ] = { NULL, };
  65. static struct class *classes[HASHSZ] = { NULL, };
  66. static struct subclass *subclasses[HASHSZ] = { NULL, };
  67. static struct protocol *protocols[HASHSZ] = { NULL, };
  68. const char *names_vendor(u_int16_t vendorid)
  69. {
  70. struct vendor *v;
  71. v = vendors[hashnum(vendorid)];
  72. for (; v; v = v->next)
  73. if (v->vendorid == vendorid)
  74. return v->name;
  75. return NULL;
  76. }
  77. const char *names_product(u_int16_t vendorid, u_int16_t productid)
  78. {
  79. struct product *p;
  80. p = products[hashnum((vendorid << 16) | productid)];
  81. for (; p; p = p->next)
  82. if (p->vendorid == vendorid && p->productid == productid)
  83. return p->name;
  84. return NULL;
  85. }
  86. const char *names_class(u_int8_t classid)
  87. {
  88. struct class *c;
  89. c = classes[hashnum(classid)];
  90. for (; c; c = c->next)
  91. if (c->classid == classid)
  92. return c->name;
  93. return NULL;
  94. }
  95. const char *names_subclass(u_int8_t classid, u_int8_t subclassid)
  96. {
  97. struct subclass *s;
  98. s = subclasses[hashnum((classid << 8) | subclassid)];
  99. for (; s; s = s->next)
  100. if (s->classid == classid && s->subclassid == subclassid)
  101. return s->name;
  102. return NULL;
  103. }
  104. const char *names_protocol(u_int8_t classid, u_int8_t subclassid,
  105. u_int8_t protocolid)
  106. {
  107. struct protocol *p;
  108. p = protocols[hashnum((classid << 16) | (subclassid << 8)
  109. | protocolid)];
  110. for (; p; p = p->next)
  111. if (p->classid == classid && p->subclassid == subclassid &&
  112. p->protocolid == protocolid)
  113. return p->name;
  114. return NULL;
  115. }
  116. /* add a cleanup function by takahiro */
  117. struct pool {
  118. struct pool *next;
  119. void *mem;
  120. };
  121. static struct pool *pool_head;
  122. static void *my_malloc(size_t size)
  123. {
  124. struct pool *p;
  125. p = calloc(1, sizeof(struct pool));
  126. if (!p)
  127. return NULL;
  128. p->mem = calloc(1, size);
  129. if (!p->mem) {
  130. free(p);
  131. return NULL;
  132. }
  133. p->next = pool_head;
  134. pool_head = p;
  135. return p->mem;
  136. }
  137. void names_free(void)
  138. {
  139. struct pool *pool;
  140. if (!pool_head)
  141. return;
  142. for (pool = pool_head; pool != NULL; ) {
  143. struct pool *tmp;
  144. if (pool->mem)
  145. free(pool->mem);
  146. tmp = pool;
  147. pool = pool->next;
  148. free(tmp);
  149. }
  150. }
  151. static int new_vendor(const char *name, u_int16_t vendorid)
  152. {
  153. struct vendor *v;
  154. unsigned int h = hashnum(vendorid);
  155. v = vendors[h];
  156. for (; v; v = v->next)
  157. if (v->vendorid == vendorid)
  158. return -1;
  159. v = my_malloc(sizeof(struct vendor) + strlen(name));
  160. if (!v)
  161. return -1;
  162. strcpy(v->name, name);
  163. v->vendorid = vendorid;
  164. v->next = vendors[h];
  165. vendors[h] = v;
  166. return 0;
  167. }
  168. static int new_product(const char *name, u_int16_t vendorid,
  169. u_int16_t productid)
  170. {
  171. struct product *p;
  172. unsigned int h = hashnum((vendorid << 16) | productid);
  173. p = products[h];
  174. for (; p; p = p->next)
  175. if (p->vendorid == vendorid && p->productid == productid)
  176. return -1;
  177. p = my_malloc(sizeof(struct product) + strlen(name));
  178. if (!p)
  179. return -1;
  180. strcpy(p->name, name);
  181. p->vendorid = vendorid;
  182. p->productid = productid;
  183. p->next = products[h];
  184. products[h] = p;
  185. return 0;
  186. }
  187. static int new_class(const char *name, u_int8_t classid)
  188. {
  189. struct class *c;
  190. unsigned int h = hashnum(classid);
  191. c = classes[h];
  192. for (; c; c = c->next)
  193. if (c->classid == classid)
  194. return -1;
  195. c = my_malloc(sizeof(struct class) + strlen(name));
  196. if (!c)
  197. return -1;
  198. strcpy(c->name, name);
  199. c->classid = classid;
  200. c->next = classes[h];
  201. classes[h] = c;
  202. return 0;
  203. }
  204. static int new_subclass(const char *name, u_int8_t classid, u_int8_t subclassid)
  205. {
  206. struct subclass *s;
  207. unsigned int h = hashnum((classid << 8) | subclassid);
  208. s = subclasses[h];
  209. for (; s; s = s->next)
  210. if (s->classid == classid && s->subclassid == subclassid)
  211. return -1;
  212. s = my_malloc(sizeof(struct subclass) + strlen(name));
  213. if (!s)
  214. return -1;
  215. strcpy(s->name, name);
  216. s->classid = classid;
  217. s->subclassid = subclassid;
  218. s->next = subclasses[h];
  219. subclasses[h] = s;
  220. return 0;
  221. }
  222. static int new_protocol(const char *name, u_int8_t classid, u_int8_t subclassid,
  223. u_int8_t protocolid)
  224. {
  225. struct protocol *p;
  226. unsigned int h = hashnum((classid << 16) | (subclassid << 8)
  227. | protocolid);
  228. p = protocols[h];
  229. for (; p; p = p->next)
  230. if (p->classid == classid && p->subclassid == subclassid
  231. && p->protocolid == protocolid)
  232. return -1;
  233. p = my_malloc(sizeof(struct protocol) + strlen(name));
  234. if (!p)
  235. return -1;
  236. strcpy(p->name, name);
  237. p->classid = classid;
  238. p->subclassid = subclassid;
  239. p->protocolid = protocolid;
  240. p->next = protocols[h];
  241. protocols[h] = p;
  242. return 0;
  243. }
  244. static void parse(FILE *f)
  245. {
  246. char buf[512], *cp;
  247. unsigned int linectr = 0;
  248. int lastvendor = -1;
  249. int lastclass = -1;
  250. int lastsubclass = -1;
  251. int lasthut = -1;
  252. int lastlang = -1;
  253. unsigned int u;
  254. while (fgets(buf, sizeof(buf), f)) {
  255. linectr++;
  256. /* remove line ends */
  257. cp = strchr(buf, '\r');
  258. if (cp)
  259. *cp = 0;
  260. cp = strchr(buf, '\n');
  261. if (cp)
  262. *cp = 0;
  263. if (buf[0] == '#' || !buf[0])
  264. continue;
  265. cp = buf;
  266. if (buf[0] == 'P' && buf[1] == 'H' && buf[2] == 'Y' &&
  267. buf[3] == 'S' && buf[4] == 'D' &&
  268. buf[5] == 'E' && buf[6] == 'S' && /*isspace(buf[7])*/
  269. buf[7] == ' ') {
  270. continue;
  271. }
  272. if (buf[0] == 'P' && buf[1] == 'H' &&
  273. buf[2] == 'Y' && /*isspace(buf[3])*/ buf[3] == ' ') {
  274. continue;
  275. }
  276. if (buf[0] == 'B' && buf[1] == 'I' && buf[2] == 'A' &&
  277. buf[3] == 'S' && /*isspace(buf[4])*/ buf[4] == ' ') {
  278. continue;
  279. }
  280. if (buf[0] == 'L' && /*isspace(buf[1])*/ buf[1] == ' ') {
  281. lasthut = lastclass = lastvendor = lastsubclass = -1;
  282. /*
  283. * set 1 as pseudo-id to indicate that the parser is
  284. * in a `L' section.
  285. */
  286. lastlang = 1;
  287. continue;
  288. }
  289. if (buf[0] == 'C' && /*isspace(buf[1])*/ buf[1] == ' ') {
  290. /* class spec */
  291. cp = buf+2;
  292. while (isspace(*cp))
  293. cp++;
  294. if (!isxdigit(*cp)) {
  295. err("Invalid class spec at line %u", linectr);
  296. continue;
  297. }
  298. u = strtoul(cp, &cp, 16);
  299. while (isspace(*cp))
  300. cp++;
  301. if (!*cp) {
  302. err("Invalid class spec at line %u", linectr);
  303. continue;
  304. }
  305. if (new_class(cp, u))
  306. err("Duplicate class spec at line %u class %04x %s",
  307. linectr, u, cp);
  308. dbg("line %5u class %02x %s", linectr, u, cp);
  309. lasthut = lastlang = lastvendor = lastsubclass = -1;
  310. lastclass = u;
  311. continue;
  312. }
  313. if (buf[0] == 'A' && buf[1] == 'T' && isspace(buf[2])) {
  314. /* audio terminal type spec */
  315. continue;
  316. }
  317. if (buf[0] == 'H' && buf[1] == 'C' && buf[2] == 'C'
  318. && isspace(buf[3])) {
  319. /* HID Descriptor bCountryCode */
  320. continue;
  321. }
  322. if (isxdigit(*cp)) {
  323. /* vendor */
  324. u = strtoul(cp, &cp, 16);
  325. while (isspace(*cp))
  326. cp++;
  327. if (!*cp) {
  328. err("Invalid vendor spec at line %u", linectr);
  329. continue;
  330. }
  331. if (new_vendor(cp, u))
  332. err("Duplicate vendor spec at line %u vendor %04x %s",
  333. linectr, u, cp);
  334. dbg("line %5u vendor %04x %s", linectr, u, cp);
  335. lastvendor = u;
  336. lasthut = lastlang = lastclass = lastsubclass = -1;
  337. continue;
  338. }
  339. if (buf[0] == '\t' && isxdigit(buf[1])) {
  340. /* product or subclass spec */
  341. u = strtoul(buf+1, &cp, 16);
  342. while (isspace(*cp))
  343. cp++;
  344. if (!*cp) {
  345. err("Invalid product/subclass spec at line %u",
  346. linectr);
  347. continue;
  348. }
  349. if (lastvendor != -1) {
  350. if (new_product(cp, lastvendor, u))
  351. err("Duplicate product spec at line %u product %04x:%04x %s",
  352. linectr, lastvendor, u, cp);
  353. dbg("line %5u product %04x:%04x %s", linectr,
  354. lastvendor, u, cp);
  355. continue;
  356. }
  357. if (lastclass != -1) {
  358. if (new_subclass(cp, lastclass, u))
  359. err("Duplicate subclass spec at line %u class %02x:%02x %s",
  360. linectr, lastclass, u, cp);
  361. dbg("line %5u subclass %02x:%02x %s", linectr,
  362. lastclass, u, cp);
  363. lastsubclass = u;
  364. continue;
  365. }
  366. if (lasthut != -1) {
  367. /* do not store hut */
  368. continue;
  369. }
  370. if (lastlang != -1) {
  371. /* do not store langid */
  372. continue;
  373. }
  374. err("Product/Subclass spec without prior Vendor/Class spec at line %u",
  375. linectr);
  376. continue;
  377. }
  378. if (buf[0] == '\t' && buf[1] == '\t' && isxdigit(buf[2])) {
  379. /* protocol spec */
  380. u = strtoul(buf+2, &cp, 16);
  381. while (isspace(*cp))
  382. cp++;
  383. if (!*cp) {
  384. err("Invalid protocol spec at line %u",
  385. linectr);
  386. continue;
  387. }
  388. if (lastclass != -1 && lastsubclass != -1) {
  389. if (new_protocol(cp, lastclass, lastsubclass,
  390. u))
  391. err("Duplicate protocol spec at line %u class %02x:%02x:%02x %s",
  392. linectr, lastclass, lastsubclass,
  393. u, cp);
  394. dbg("line %5u protocol %02x:%02x:%02x %s",
  395. linectr, lastclass, lastsubclass, u, cp);
  396. continue;
  397. }
  398. err("Protocol spec without prior Class and Subclass spec at line %u",
  399. linectr);
  400. continue;
  401. }
  402. if (buf[0] == 'H' && buf[1] == 'I' &&
  403. buf[2] == 'D' && /*isspace(buf[3])*/ buf[3] == ' ') {
  404. continue;
  405. }
  406. if (buf[0] == 'H' && buf[1] == 'U' &&
  407. buf[2] == 'T' && /*isspace(buf[3])*/ buf[3] == ' ') {
  408. lastlang = lastclass = lastvendor = lastsubclass = -1;
  409. /*
  410. * set 1 as pseudo-id to indicate that the parser is
  411. * in a `HUT' section.
  412. */
  413. lasthut = 1;
  414. continue;
  415. }
  416. if (buf[0] == 'R' && buf[1] == ' ')
  417. continue;
  418. if (buf[0] == 'V' && buf[1] == 'T')
  419. continue;
  420. err("Unknown line at line %u", linectr);
  421. }
  422. }
  423. int names_init(char *n)
  424. {
  425. FILE *f;
  426. f = fopen(n, "r");
  427. if (!f)
  428. return errno;
  429. parse(f);
  430. fclose(f);
  431. return 0;
  432. }