📄 names.c
字号:
/*****************************************************************************//* * 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. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * *//* * Copyright (C) 2005 Takahiro Hirofuchi * - names_deinit() is added. *//*****************************************************************************/#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>#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 genericstrtable { struct genericstrtable *next; unsigned int num; char name[1];};/* ---------------------------------------------------------------------- */#define HASH1 0x10#define HASH2 0x02#define HASHSZ 16static 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 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 index){ struct genericstrtable *h; for (h = t[hashnum(index)]; h; h = h->next) if (h->num == index) 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;}/* ---------------------------------------------------------------------- *//* add a cleanup function by takahiro */struct pool { struct pool *next; void *mem;};static struct pool *pool_head = NULL;static void *my_malloc(size_t size){ struct pool *p; p = calloc(1, sizeof(struct pool)); if (!p) { free(p); return NULL; } p->mem = calloc(1, size); if (!p->mem) return NULL; p->next = pool_head; pool_head = p; return p->mem;}void names_free(void){ struct pool *pool; if (!pool_head) return; for (pool = pool_head; pool != NULL; ) { struct pool *tmp; if (pool->mem) free(pool->mem); tmp = pool; pool = pool->next; free(tmp); }}/* ---------------------------------------------------------------------- */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 = my_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 = my_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 = my_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 = my_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 = my_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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -