1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
/*****************************************************************************/
/*
* names.c -- USB name database manipulation routines
*
* Copyright (C) 1999, 2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*/
/*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_LIBZ
#include <zlib.h>
#define usb_file gzFile
#define usb_fopen(path, mode) gzopen(path, mode)
#define usb_fgets(s, size, stream) gzgets(stream, s, size)
#define usb_close(f) gzclose(f)
#else
#define usb_file FILE *
#define usb_fopen(path, mode) fopen(path, mode)
#define usb_fgets(s, size, stream) fgets(s, size, stream)
#define usb_close(f) fclose(f)
#endif
#include "names.h"
/* ---------------------------------------------------------------------- */
struct vendor {
struct vendor *next;
u_int16_t vendorid;
char name[1];
};
struct product {
struct product *next;
u_int16_t vendorid, productid;
char name[1];
};
struct class {
struct class *next;
u_int8_t classid;
char name[1];
};
struct subclass {
struct subclass *next;
u_int8_t classid, subclassid;
char name[1];
};
struct protocol {
struct protocol *next;
u_int8_t classid, subclassid, protocolid;
char name[1];
};
struct audioterminal {
struct audioterminal *next;
u_int16_t termt;
char name[1];
};
struct videoterminal {
struct videoterminal *next;
u_int16_t termt;
char name[1];
};
struct genericstrtable {
struct genericstrtable *next;
unsigned int num;
char name[1];
};
/* ---------------------------------------------------------------------- */
#define HASH1 0x10
#define HASH2 0x02
#define HASHSZ 16
static unsigned int hashnum(unsigned int num)
{
unsigned int mask1 = HASH1 << 27, mask2 = HASH2 << 27;
for (; mask1 >= HASH1; mask1 >>= 1, mask2 >>= 1)
if (num & mask1)
num ^= mask2;
return num & (HASHSZ-1);
}
/* ---------------------------------------------------------------------- */
static struct vendor *vendors[HASHSZ] = { NULL, };
static struct product *products[HASHSZ] = { NULL, };
static struct class *classes[HASHSZ] = { NULL, };
static struct subclass *subclasses[HASHSZ] = { NULL, };
static struct protocol *protocols[HASHSZ] = { NULL, };
static struct audioterminal *audioterminals[HASHSZ] = { NULL, };
static struct videoterminal *videoterminals[HASHSZ] = { NULL, };
static struct genericstrtable *hiddescriptors[HASHSZ] = { NULL, };
static struct genericstrtable *reports[HASHSZ] = { NULL, };
static struct genericstrtable *huts[HASHSZ] = { NULL, };
static struct genericstrtable *biass[HASHSZ] = { NULL, };
static struct genericstrtable *physdess[HASHSZ] = { NULL, };
static struct genericstrtable *hutus[HASHSZ] = { NULL, };
static struct genericstrtable *langids[HASHSZ] = { NULL, };
static struct genericstrtable *countrycodes[HASHSZ] = { NULL, };
/* ---------------------------------------------------------------------- */
static const char *names_genericstrtable(struct genericstrtable *t[HASHSZ],
unsigned int idx)
{
struct genericstrtable *h;
for (h = t[hashnum(idx)]; h; h = h->next)
if (h->num == idx)
return h->name;
return NULL;
}
const char *names_hid(u_int8_t hidd)
{
return names_genericstrtable(hiddescriptors, hidd);
}
const char *names_reporttag(u_int8_t rt)
{
return names_genericstrtable(reports, rt);
}
const char *names_huts(unsigned int data)
{
return names_genericstrtable(huts, data);
}
const char *names_hutus(unsigned int data)
{
return names_genericstrtable(hutus, data);
}
const char *names_langid(u_int16_t langid)
{
return names_genericstrtable(langids, langid);
}
const char *names_physdes(u_int8_t ph)
{
return names_genericstrtable(physdess, ph);
}
const char *names_bias(u_int8_t b)
{
return names_genericstrtable(biass, b);
}
const char *names_countrycode(unsigned int countrycode)
{
return names_genericstrtable(countrycodes, countrycode);
}
const char *names_vendor(u_int16_t vendorid)
{
struct vendor *v;
v = vendors[hashnum(vendorid)];
for (; v; v = v->next)
if (v->vendorid == vendorid)
return v->name;
return NULL;
}
const char *names_product(u_int16_t vendorid, u_int16_t productid)
{
struct product *p;
p = products[hashnum((vendorid << 16) | productid)];
for (; p; p = p->next)
if (p->vendorid == vendorid && p->productid == productid)
return p->name;
return NULL;
}
const char *names_class(u_int8_t classid)
{
struct class *c;
c = classes[hashnum(classid)];
for (; c; c = c->next)
if (c->classid == classid)
return c->name;
return NULL;
}
const char *names_subclass(u_int8_t classid, u_int8_t subclassid)
{
struct subclass *s;
s = subclasses[hashnum((classid << 8) | subclassid)];
for (; s; s = s->next)
if (s->classid == classid && s->subclassid == subclassid)
return s->name;
return NULL;
}
const char *names_protocol(u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
{
struct protocol *p;
p = protocols[hashnum((classid << 16) | (subclassid << 8) | protocolid)];
for (; p; p = p->next)
if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
return p->name;
return NULL;
}
const char *names_audioterminal(u_int16_t termt)
{
struct audioterminal *at;
at = audioterminals[hashnum(termt)];
for (; at; at = at->next)
if (at->termt == termt)
return at->name;
return NULL;
}
const char *names_videoterminal(u_int16_t termt)
{
struct videoterminal *vt;
vt = videoterminals[hashnum(termt)];
for (; vt; vt = vt->next)
if (vt->termt == termt)
return vt->name;
return NULL;
}
/* ---------------------------------------------------------------------- */
int get_vendor_string(char *buf, size_t size, u_int16_t vid)
{
const char *cp;
if (size < 1)
return 0;
*buf = 0;
if (!(cp = names_vendor(vid)))
return 0;
return snprintf(buf, size, "%s", cp);
}
int get_product_string(char *buf, size_t size, u_int16_t vid, u_int16_t pid)
{
const char *cp;
if (size < 1)
return 0;
*buf = 0;
if (!(cp = names_product(vid, pid)))
return 0;
return snprintf(buf, size, "%s", cp);
}
/* ---------------------------------------------------------------------- */
static int new_vendor(const char *name, u_int16_t vendorid)
{
struct vendor *v;
unsigned int h = hashnum(vendorid);
v = vendors[h];
for (; v; v = v->next)
if (v->vendorid == vendorid)
return -1;
v = malloc(sizeof(struct vendor) + strlen(name));
if (!v)
return -1;
strcpy(v->name, name);
v->vendorid = vendorid;
v->next = vendors[h];
vendors[h] = v;
return 0;
}
static int new_product(const char *name, u_int16_t vendorid, u_int16_t productid)
{
struct product *p;
unsigned int h = hashnum((vendorid << 16) | productid);
p = products[h];
for (; p; p = p->next)
if (p->vendorid == vendorid && p->productid == productid)
return -1;
p = malloc(sizeof(struct product) + strlen(name));
if (!p)
return -1;
strcpy(p->name, name);
p->vendorid = vendorid;
p->productid = productid;
p->next = products[h];
products[h] = p;
return 0;
}
static int new_class(const char *name, u_int8_t classid)
{
struct class *c;
unsigned int h = hashnum(classid);
c = classes[h];
for (; c; c = c->next)
if (c->classid == classid)
return -1;
c = malloc(sizeof(struct class) + strlen(name));
if (!c)
return -1;
strcpy(c->name, name);
c->classid = classid;
c->next = classes[h];
classes[h] = c;
return 0;
}
static int new_subclass(const char *name, u_int8_t classid, u_int8_t subclassid)
{
struct subclass *s;
unsigned int h = hashnum((classid << 8) | subclassid);
s = subclasses[h];
for (; s; s = s->next)
if (s->classid == classid && s->subclassid == subclassid)
return -1;
s = malloc(sizeof(struct subclass) + strlen(name));
if (!s)
return -1;
strcpy(s->name, name);
s->classid = classid;
s->subclassid = subclassid;
s->next = subclasses[h];
subclasses[h] = s;
return 0;
}
static int new_protocol(const char *name, u_int8_t classid, u_int8_t subclassid, u_int8_t protocolid)
{
struct protocol *p;
unsigned int h = hashnum((classid << 16) | (subclassid << 8) | protocolid);
p = protocols[h];
for (; p; p = p->next)
if (p->classid == classid && p->subclassid == subclassid && p->protocolid == protocolid)
return -1;
p = malloc(sizeof(struct protocol) + strlen(name));
if (!p)
return -1;
strcpy(p->name, name);
p->classid = classid;
p->subclassid = subclassid;
p->protocolid = protocolid;
p->next = protocols[h];
protocols[h] = p;
return 0;
}
static int new_audioterminal(const char *name, u_int16_t termt)
{
struct audioterminal *at;
unsigned int h = hashnum(termt);
at = audioterminals[h];
for (; at; at = at->next)
if (at->termt == termt)
return -1;
at = malloc(sizeof(struct audioterminal) + strlen(name));
if (!at)
return -1;
strcpy(at->name, name);
at->termt = termt;
at->next = audioterminals[h];
audioterminals[h] = at;
return 0;
}
static int new_videoterminal(const char *name, u_int16_t termt)
{
struct videoterminal *vt;
unsigned int h = hashnum(termt);
vt = videoterminals[h];
for (; vt; vt = vt->next)
if (vt->termt == termt)
return -1;
vt = malloc(sizeof(struct videoterminal) + strlen(name));
if (!vt)
return -1;
strcpy(vt->name, name);
vt->termt = termt;
vt->next = videoterminals[h];
videoterminals[h] = vt;
return 0;
}
static int new_genericstrtable(struct genericstrtable *t[HASHSZ],
const char *name, unsigned int idx)
{
struct genericstrtable *g;
unsigned int h = hashnum(idx);
for (g = t[h]; g; g = g->next)
if (g->num == idx)
return -1;
g = malloc(sizeof(struct genericstrtable) + strlen(name));
if (!g)
return -1;
strcpy(g->name, name);
g->num = idx;
g->next = t[h];
t[h] = g;
return 0;
}
static int new_hid(const char *name, u_int8_t hidd)
{
return new_genericstrtable(hiddescriptors, name, hidd);
}
static int new_reporttag(const char *name, u_int8_t rt)
{
return new_genericstrtable(reports, name, rt);
}
static int new_huts(const char *name, unsigned int data)
{
return new_genericstrtable(huts, name, data);
}
static int new_hutus(const char *name, unsigned int data)
{
return new_genericstrtable(hutus, name, data);
}
static int new_langid(const char *name, u_int16_t langid)
{
return new_genericstrtable(langids, name, langid);
}
static int new_physdes(const char *name, u_int8_t ph)
{
return new_genericstrtable(physdess, name, ph);
}
static int new_bias(const char *name, u_int8_t b)
{
return new_genericstrtable(biass, name, b);
}
static int new_countrycode(const char *name, unsigned int countrycode)
{
return new_genericstrtable(countrycodes, name, countrycode);
}
/* ---------------------------------------------------------------------- */
static void free_vendor(void)
{
struct vendor *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = vendors[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_product(void)
{
struct product *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = products[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_class(void)
{
struct class *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = classes[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_subclass(void)
{
struct subclass *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = subclasses[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_protocol(void)
{
struct protocol *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = protocols[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_audioterminal(void)
{
struct audioterminal *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = audioterminals[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
return;
}
static void free_videoterminal(void)
{
struct videoterminal *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = videoterminals[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void _free_genericstrtable(struct genericstrtable *t[HASHSZ])
{
struct genericstrtable *cur, *tmp;
int i;
for (i = 0; i < HASHSZ; i++) {
cur = t[i];
while (cur) {
tmp = cur;
cur = cur->next;
free(tmp);
}
}
}
static void free_genericstrtable(void)
{
_free_genericstrtable(hiddescriptors);
_free_genericstrtable(reports);
_free_genericstrtable(huts);
_free_genericstrtable(biass);
_free_genericstrtable(physdess);
_free_genericstrtable(hutus);
_free_genericstrtable(langids);
_free_genericstrtable(countrycodes);
}
#define DBG(x)
static void parse(usb_file f)
{
char buf[512], *cp;
unsigned int linectr = 0;
int lastvendor = -1;
int lastclass = -1;
int lastsubclass = -1;
int lasthut = -1;
int lastlang = -1;
unsigned int u;
while (usb_fgets(buf, sizeof(buf), f)) {
linectr++;
/* remove line ends */
cp = strchr(buf, 13);
if (cp)
*cp = 0;
cp = strchr(buf, 10);
if (cp)
*cp = 0;
if (buf[0] == '#' || !buf[0])
continue;
cp = buf;
if (buf[0] == 'P' && buf[1] == 'H' && buf[2] == 'Y' && buf[3] == 'S' && buf[4] == 'D' &&
buf[5] == 'E' && buf[6] == 'S' && /*isspace(buf[7])*/ buf[7] == ' ') {
cp = buf + 8;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid Physdes type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid Physdes type at line %u\n", linectr);
continue;
}
if (new_physdes(cp, u))
fprintf(stderr, "Duplicate Physdes type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u physdes type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'P' && buf[1] == 'H' && buf[2] == 'Y' && /*isspace(buf[3])*/ buf[3] == ' ') {
cp = buf + 4;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid PHY type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid PHY type at line %u\n", linectr);
continue;
}
if (new_physdes(cp, u))
fprintf(stderr, "Duplicate PHY type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u PHY type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'B' && buf[1] == 'I' && buf[2] == 'A' && buf[3] == 'S' && /*isspace(buf[4])*/ buf[4] == ' ') {
cp = buf + 5;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid BIAS type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid BIAS type at line %u\n", linectr);
continue;
}
if (new_bias(cp, u))
fprintf(stderr, "Duplicate BIAS type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u BIAS type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'L' && /*isspace(buf[1])*/ buf[1] == ' ') {
cp = buf+2;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid LANGID spec at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid LANGID spec at line %u\n", linectr);
continue;
}
if (new_langid(cp, u))
fprintf(stderr, "Duplicate LANGID spec at line %u language-id %04x %s\n", linectr, u, cp);
DBG(printf("line %5u LANGID %02x %s\n", linectr, u, cp));
lasthut = lastclass = lastvendor = lastsubclass = -1;
lastlang = u;
continue;
}
if (buf[0] == 'C' && /*isspace(buf[1])*/ buf[1] == ' ') {
/* class spec */
cp = buf+2;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid class spec at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid class spec at line %u\n", linectr);
continue;
}
if (new_class(cp, u))
fprintf(stderr, "Duplicate class spec at line %u class %04x %s\n", linectr, u, cp);
DBG(printf("line %5u class %02x %s\n", linectr, u, cp));
lasthut = lastlang = lastvendor = lastsubclass = -1;
lastclass = u;
continue;
}
if (buf[0] == 'A' && buf[1] == 'T' && isspace(buf[2])) {
/* audio terminal type spec */
cp = buf+3;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid audio terminal type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid audio terminal type at line %u\n", linectr);
continue;
}
if (new_audioterminal(cp, u))
fprintf(stderr, "Duplicate audio terminal type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u audio terminal type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'V' && buf[1] == 'T' && isspace(buf[2])) {
/* video terminal type spec */
cp = buf+3;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid video terminal type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid video terminal type at line %u\n", linectr);
continue;
}
if (new_videoterminal(cp, u))
fprintf(stderr, "Duplicate video terminal type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u video terminal type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'H' && buf[1] == 'C' && buf[2] == 'C' && isspace(buf[3])) {
/* HID Descriptor bCountryCode */
cp = buf+3;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid HID country code at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 10);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid HID country code at line %u\n", linectr);
continue;
}
if (new_countrycode(cp, u))
fprintf(stderr, "Duplicate HID country code at line %u country %02u %s\n", linectr, u, cp);
DBG(printf("line %5u keyboard country code %02u %s\n", linectr, u, cp));
continue;
}
if (isxdigit(*cp)) {
/* vendor */
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid vendor spec at line %u\n", linectr);
continue;
}
if (new_vendor(cp, u))
fprintf(stderr, "Duplicate vendor spec at line %u vendor %04x %s\n", linectr, u, cp);
DBG(printf("line %5u vendor %04x %s\n", linectr, u, cp));
lastvendor = u;
lasthut = lastlang = lastclass = lastsubclass = -1;
continue;
}
if (buf[0] == '\t' && isxdigit(buf[1])) {
/* product or subclass spec */
u = strtoul(buf+1, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid product/subclass spec at line %u\n", linectr);
continue;
}
if (lastvendor != -1) {
if (new_product(cp, lastvendor, u))
fprintf(stderr, "Duplicate product spec at line %u product %04x:%04x %s\n", linectr, lastvendor, u, cp);
DBG(printf("line %5u product %04x:%04x %s\n", linectr, lastvendor, u, cp));
continue;
}
if (lastclass != -1) {
if (new_subclass(cp, lastclass, u))
fprintf(stderr, "Duplicate subclass spec at line %u class %02x:%02x %s\n", linectr, lastclass, u, cp);
DBG(printf("line %5u subclass %02x:%02x %s\n", linectr, lastclass, u, cp));
lastsubclass = u;
continue;
}
if (lasthut != -1) {
if (new_hutus(cp, (lasthut << 16)+u))
fprintf(stderr, "Duplicate HUT Usage Spec at line %u\n", linectr);
continue;
}
if (lastlang != -1) {
if (new_langid(cp, lastlang+(u<<10)))
fprintf(stderr, "Duplicate LANGID Usage Spec at line %u\n", linectr);
continue;
}
fprintf(stderr, "Product/Subclass spec without prior Vendor/Class spec at line %u\n", linectr);
continue;
}
if (buf[0] == '\t' && buf[1] == '\t' && isxdigit(buf[2])) {
/* protocol spec */
u = strtoul(buf+2, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid protocol spec at line %u\n", linectr);
continue;
}
if (lastclass != -1 && lastsubclass != -1) {
if (new_protocol(cp, lastclass, lastsubclass, u))
fprintf(stderr, "Duplicate protocol spec at line %u class %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp);
DBG(printf("line %5u protocol %02x:%02x:%02x %s\n", linectr, lastclass, lastsubclass, u, cp));
continue;
}
fprintf(stderr, "Protocol spec without prior Class and Subclass spec at line %u\n", linectr);
continue;
}
if (buf[0] == 'H' && buf[1] == 'I' && buf[2] == 'D' && /*isspace(buf[3])*/ buf[3] == ' ') {
cp = buf + 4;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid HID type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid HID type at line %u\n", linectr);
continue;
}
if (new_hid(cp, u))
fprintf(stderr, "Duplicate HID type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u HID type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'H' && buf[1] == 'U' && buf[2] == 'T' && /*isspace(buf[3])*/ buf[3] == ' ') {
cp = buf + 4;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid HUT type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid HUT type at line %u\n", linectr);
continue;
}
if (new_huts(cp, u))
fprintf(stderr, "Duplicate HUT type spec at line %u terminal type %04x %s\n", linectr, u, cp);
lastlang = lastclass = lastvendor = lastsubclass = -1;
lasthut = u;
DBG(printf("line %5u HUT type %02x %s\n", linectr, u, cp));
continue;
}
if (buf[0] == 'R' && buf[1] == ' ') {
cp = buf + 2;
while (isspace(*cp))
cp++;
if (!isxdigit(*cp)) {
fprintf(stderr, "Invalid Report type at line %u\n", linectr);
continue;
}
u = strtoul(cp, &cp, 16);
while (isspace(*cp))
cp++;
if (!*cp) {
fprintf(stderr, "Invalid Report type at line %u\n", linectr);
continue;
}
if (new_reporttag(cp, u))
fprintf(stderr, "Duplicate Report type spec at line %u terminal type %04x %s\n", linectr, u, cp);
DBG(printf("line %5u Report type %02x %s\n", linectr, u, cp));
continue;
}
fprintf(stderr, "Unknown line at line %u\n", linectr);
}
}
/* ---------------------------------------------------------------------- */
int names_init(char *n)
{
usb_file f;
f = usb_fopen(n, "r");
if (!f)
return errno;
parse(f);
usb_close(f);
return 0;
}
void names_exit(void)
{
free_vendor();
free_product();
free_class();
free_subclass();
free_protocol();
free_audioterminal();
free_videoterminal();
free_genericstrtable();
}
|