-#ifdef PCI_COMPRESSED_IDS
-#include <zlib.h>
-typedef gzFile pci_file;
-#define pci_gets(f, l, s) gzgets(f, l, s)
-#define pci_eof(f) gzeof(f)
-
-static pci_file pci_open(struct pci_access *a)
-{
- pci_file result;
- size_t len;
- char *new_name;
-
- result = gzopen(a->id_file_name, "rb");
- if (result)
- return result;
- len = strlen(a->id_file_name);
- if (len >= 3 && memcmp(a->id_file_name + len - 3, ".gz", 3) != 0)
- return result;
- new_name = malloc(len - 2);
- memcpy(new_name, a->id_file_name, len - 3);
- new_name[len - 3] = 0;
- pci_set_name_list_path(a, new_name, 1);
- return gzopen(a->id_file_name, "rb");
-}
-
-#define pci_close(f) gzclose(f)
-#define PCI_ERROR(f, err) \
- if (!err) { \
- int errnum; \
- err = gzerror(f, &errnum); \
- if (errnum == Z_ERRNO) err = "I/O error"; \
- else if (errnum >= 0) err = NULL; \
- }
-#else
-typedef FILE * pci_file;
-#define pci_gets(f, l, s) fgets(l, s, f)
-#define pci_eof(f) feof(f)
-#define pci_open(a) fopen(a->id_file_name, "r")
-#define pci_close(f) fclose(f)
-#define PCI_ERROR(f, err) if (!err && ferror(f)) err = "I/O error";
-#endif
-
-struct id_entry {
- struct id_entry *next;
- u32 id12, id34;
- byte cat;
- char name[1];
-};
-
-enum id_entry_type {
- ID_UNKNOWN,
- ID_VENDOR,
- ID_DEVICE,
- ID_SUBSYSTEM,
- ID_GEN_SUBSYSTEM,
- ID_CLASS,
- ID_SUBCLASS,
- ID_PROGIF
-};
-
-struct id_bucket {
- struct id_bucket *next;
- unsigned int full;
-};
-
-#define MAX_LINE 1024
-#define BUCKET_SIZE 8192
-#define HASH_SIZE 4099
-
-#ifdef __GNUC__
-#define BUCKET_ALIGNMENT __alignof__(struct id_bucket)
-#else
-union id_align {
- struct id_bucket *next;
- unsigned int full;
-};
-#define BUCKET_ALIGNMENT sizeof(union id_align)
-#endif
-#define BUCKET_ALIGN(n) ((n)+BUCKET_ALIGNMENT-(n)%BUCKET_ALIGNMENT)
-
-static void *id_alloc(struct pci_access *a, unsigned int size)
-{
- struct id_bucket *buck = a->current_id_bucket;
- unsigned int pos;
- if (!buck || buck->full + size > BUCKET_SIZE)
- {
- buck = pci_malloc(a, BUCKET_SIZE);
- buck->next = a->current_id_bucket;
- a->current_id_bucket = buck;
- buck->full = BUCKET_ALIGN(sizeof(struct id_bucket));
- }
- pos = buck->full;
- buck->full = BUCKET_ALIGN(buck->full + size);
- return (byte *)buck + pos;
-}
-
-static inline u32 id_pair(unsigned int x, unsigned int y)
-{
- return ((x << 16) | y);
-}
-
-static inline unsigned int id_hash(int cat, u32 id12, u32 id34)
-{
- unsigned int h;
-
- h = id12 ^ (id34 << 3) ^ (cat << 5);
- return h % HASH_SIZE;
-}
-
-static char *id_lookup(struct pci_access *a, int cat, int id1, int id2, int id3, int id4)
-{
- struct id_entry *n;
- u32 id12 = id_pair(id1, id2);
- u32 id34 = id_pair(id3, id4);
-
- if (!a->id_hash)
- return NULL;
- n = a->id_hash[id_hash(cat, id12, id34)];
- while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
- n = n->next;
- return n ? n->name : NULL;
-}
-
-static int id_insert(struct pci_access *a, int cat, int id1, int id2, int id3, int id4, char *text)
-{
- u32 id12 = id_pair(id1, id2);
- u32 id34 = id_pair(id3, id4);
- unsigned int h = id_hash(cat, id12, id34);
- struct id_entry *n = a->id_hash[h];
- int len = strlen(text);
-
- while (n && (n->id12 != id12 || n->id34 != id34 || n->cat != cat))
- n = n->next;
- if (n)
- return 1;
- n = id_alloc(a, sizeof(struct id_entry) + len);
- n->id12 = id12;
- n->id34 = id34;
- n->cat = cat;
- memcpy(n->name, text, len+1);
- n->next = a->id_hash[h];
- a->id_hash[h] = n;
- return 0;
-}
-
-static int id_hex(char *p, int cnt)
-{
- int x = 0;
- while (cnt--)
- {
- x <<= 4;
- if (*p >= '0' && *p <= '9')
- x += (*p - '0');
- else if (*p >= 'a' && *p <= 'f')
- x += (*p - 'a' + 10);
- else if (*p >= 'A' && *p <= 'F')
- x += (*p - 'A' + 10);
- else
- return -1;
- p++;
- }
- return x;
-}
-
-static inline int id_white_p(int c)
-{
- return (c == ' ') || (c == '\t');
-}
-
-static const char *id_parse_list(struct pci_access *a, pci_file f, int *lino)