summaryrefslogtreecommitdiff
path: root/src/bin/xi/xifile.c
blob: fb7f2f7cb9866a71259fcb4ada31129ceea822d7 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <err.h>

static const char *stdin_name = "/dev/stdin";

static FILE*
fopen_or_die(const char *path, const char *mode)
{
   assert(path && mode);

   FILE *f;
   if (!(f = fopen(path, mode)))
      err(EXIT_FAILURE"fopen(%s%s)", path, mode);

   return f;
}

static void
detect(const char *path)
{
   assert(path);

   uint8_t buf[32] = {0};
   const char *name = (!strcmp(path, "-") ? stdin_name : path);
   FILE *f = (name == stdin_name ? stdin : fopen_or_die(name, "rb"));
   fread(buf, 1sizeof(buf), f);

   const struct info {
      const char *name;
      const uint8_t *header;
      size_t chunk;
   } map[] = {
#define HDR(...) .header = (const uint8_t[]){__VA_ARGS__}, .chunk = sizeof((const uint8_t[]){__VA_ARGS__})
      {
         .name = "BGMStream",
         HDR('B''G''M''S''t''r''e''a''m')
      }, {
         .name = "SeWave",
         HDR('S''e''W''a''v''e')
      }, {
         .name = "PMUS",
         HDR('P''M''U''S')
      }, {
         .name = "RIFF/WAVE",
         HDR('R''I''F''F'0x240xB30xCF0x04'W''A''V''E''f''m''t')
      }, {
         .name = "RIFF/ACON",
         HDR('R''I''F''F'0x580x2300'A''C''O''N''a''n''i''h')
      }, {
         .name = "name",
         HDR('n''o''n''e'0000000000000000000000000000)
      }, {
         .name = "syst",
         HDR('s''y''s''t')
      }, {
         .name = "menu",
         HDR('m''e''n''u')
      }, {
         .name = "lobb",
         HDR('l''o''b''b')
      }, {
         .name = "wave",
         HDR('w''a''v''e')
      }, {
         .name = "ability",
         HDR(0000x1700000x80)
      }, {
         .name = "spell",
         HDR(00000x0300x9F00x10)
      }, {
         .name = "mgc_",
         HDR('m''g''c''_')
      }, {
         .name = "win0",
         HDR('w''i''n''0')
      }, {
         .name = "titl",
         HDR('t''i''t''l')
      }, {
         .name = "sel_",
         HDR('s''e''l''_')
      }, {
         .name = "damv",
         HDR('d''a''m''v')
      }, {
         .name = "XISTRING",
         HDR('X''I''S''T''R''I''N''G')
      }, {
         .name = "prvd",
         HDR('p''r''v''d')
      }, {
         .name = "selp",
         HDR('s''e''l''p')
      }, {
         .name = "dun",
         HDR('d''u''n')
      }, {
         .name = "town",
         HDR('t''o''w''n')
      }, {
         .name = "dese",
         HDR('d''e''s''e')
      }, {
         .name = "fore",
         HDR('f''o''r''e')
      }, {
         .name = "tree",
         HDR('t''r''e''e')
      }, {
         .name = "unka",
         HDR('u''n''k''a')
      }, {
         .name = "moun",
         HDR('m''o''u''n')
      }, {
         .name = "cast",
         HDR('c''a''s''t')
      }, {
         .name = "fuji",
         HDR('f''u''j''i')
      }, {
         .name = "view",
         HDR('v''i''e''w')
      }
#undef HDR
   };

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))

   const struct info *info = NULL;
   for (size_t i = 0; i < ARRAY_SIZE(map); ++i) {
      assert(map[i].chunk <= sizeof(buf));
      if (memcmp(buf, map[i].header, map[i].chunk))
         continue;

      info = &map[i];
      break;
   }

   if (info) {
      printf("%s%s\n", name, info->name);
   } else {
      int i;
      for (i = 0; i < 32 && isprint(buf[i]); ++i);
      if (i > 0) {
         printf("%s: unknown (%.*s)\n", name, i, buf);
      } else {
         printf("%s: unknown\n", name);
      }
   }

   fclose(f);
}

int
main(int argc, char *argv[])
{
   if (argc < 2)
      errx(EXIT_FAILURE"usage: %s file ...", argv[0]);

   for (int i = 1; i < argc; ++i)
      detect(argv[i]);

   return EXIT_SUCCESS;
}