summaryrefslogtreecommitdiff
path: root/magick/magic.c
blob: 66998e4005f2612c64980a16a6aed3af0b92d392 (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
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
/*
% Copyright (C) 2003-2019 GraphicsMagick Group
% Copyright (C) 2002 ImageMagick Studio
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                    M   M   AAA    GGGG  IIIII   CCCC                        %
%                    MM MM  A   A  G        I    C                            %
%                    M M M  AAAAA  G GGG    I    C                            %
%                    M   M  A   A  G   G    I    C                            %
%                    M   M  A   A   GGGG  IIIII   CCCC                        %
%                                                                             %
%                                                                             %
%                    GraphicsMagick Image Magic Methods                       %
%                                                                             %
%                                                                             %
%                              Software Design                                %
%                              Bob Friesenhahn                                %
%                                 July 2000                                   %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/

/*
  Include declarations.
*/
#include "magick/studio.h"
#include "magick/magic.h"
#include "magick/utility.h"

/*
  Define declarations.
*/

/*
  Static declarations.
*/
static const struct
{
  const char
    name[10];

  const unsigned char
    * const magic;

  unsigned short
    length,
    offset;
}
StaticMagic[] =
{
#define MAGIC(name,offset,magic) {name,(const unsigned char * const)magic,sizeof(magic)-1,offset}
  MAGIC("WEBP", 8, "WEBP"),
  /* MAGIC("AVI", 0, "RIFF"), */
  MAGIC("8BIMWTEXT", 0, "8\000B\000I\000M\000#"),
  MAGIC("8BIMTEXT", 0, "8BIM#"),
  MAGIC("8BIM", 0, "8BIM"),
  MAGIC("BMP", 0, "BA"),
  MAGIC("BMP", 0, "BM"),
  MAGIC("BMP", 0, "CI"),
  MAGIC("BMP", 0, "CP"),
  MAGIC("BMP", 0, "IC"),
  MAGIC("BMP", 0, "PI"),
  MAGIC("CALS", 21, "version: MIL-STD-1840"),
  MAGIC("CALS", 0, "srcdocid:"),
  MAGIC("CALS", 9, "srcdocid:"),
  MAGIC("CALS", 8, "rorient:"),
  MAGIC("CGM", 0, "BEGMF"),
  MAGIC("CIN", 0, "\200\052\137\327"),
  MAGIC("DCM", 128, "DICM"),
  MAGIC("DCX", 0, "\261\150\336\72"),
  MAGIC("DIB", 0, "\050\000"),
  MAGIC("DOT", 0, "digraph"),
  MAGIC("DPX", 0, "SDPX"),
  MAGIC("DPX", 0, "XPDS"),
  MAGIC("EMF", 40, "\040\105\115\106\000\000\001\000"),
  MAGIC("EPT", 0, "\305\320\323\306"),
  MAGIC("FAX", 0, "DFAX"),
  MAGIC("FIG", 0, "#FIG"),
  MAGIC("FITS", 0, "IT0"),
  MAGIC("FITS", 0, "SIMPLE"),
  MAGIC("FPX", 0, "\320\317\021\340"),
  MAGIC("GIF", 0, "GIF8"),
  MAGIC("HDF", 1, "HDF"),
  MAGIC("HPGL", 0, "IN;"),
  MAGIC("HPGL", 0, "\033E\033"),
  MAGIC("HTML", 1, "HTML"),
  MAGIC("HTML", 1, "html"),
  MAGIC("ILBM", 8, "ILBM"),
  MAGIC("IPTCWTEXT", 0, "\062\000#\000\060\000=\000\042\000&\000#\000\060\000;\000&\000#\000\062\000;\000\042\000"),
  MAGIC("IPTCTEXT", 0, "2#0=\042�\042"),
  MAGIC("IPTC", 0, "\034\002"),
  MAGIC("JNG", 0, "\213JNG\r\n\032\n"),
  MAGIC("JPEG", 0, "\377\330\377"),
  MAGIC("JPC", 0, "\377\117"),
  MAGIC("JP2", 4, "\152\120\040\040\015"),
  MAGIC("MAT", 0, "MATLAB 5.0 MAT-file,"),
  MAGIC("MIFF", 0, "Id=ImageMagick"),
  MAGIC("MIFF", 0, "id=ImageMagick"),
  MAGIC("MNG", 0, "\212MNG\r\n\032\n"),
  MAGIC("MPC", 0, "id=MagickCache"),
  MAGIC("MPEG", 0, "\000\000\001\263"),
  MAGIC("PCD", 2048, "PCD_"),
  MAGIC("PCL", 0, "\033E\033"),
  MAGIC("PCX", 0, "\012\002"),
  MAGIC("PCX", 0, "\012\005"),
  MAGIC("PDB", 60, "vIMGView"),
  MAGIC("PDF", 0, "%PDF-"),
  MAGIC("PFA", 0, "%!PS-AdobeFont-1.0"),
  MAGIC("PFB", 6, "%!PS-AdobeFont-1.0"),
  MAGIC("PGX", 0, "PG ML"),
  MAGIC("PGX", 0, "PG LM"),
  MAGIC("PICT", 522, "\000\021\002\377\014\000"),
  MAGIC("PNG", 0, "\211PNG\r\n\032\n"),
  MAGIC("PBM", 0, "P1"),
  MAGIC("PGM", 0, "P2"),
  MAGIC("PPM", 0, "P3"),
  MAGIC("PBM", 0, "P4"),
  MAGIC("PGM", 0, "P5"),
  MAGIC("PPM", 0, "P6"),
  MAGIC("P7", 0, "P7 332"), /* XV Thumbnail */
  MAGIC("PAM", 0, "P7"), /* Should be listed after "P7 332" */
  MAGIC("PS", 0, "%!"),
  MAGIC("PS", 0, "\004%!"),
  MAGIC("PS", 0, "\305\320\323\306"),
  MAGIC("PSD", 0, "8BPS"),
  MAGIC("PWP", 0, "SFW95"),
  MAGIC("RAD", 0, "#?RADIANCE"),
  MAGIC("RAD", 0, "VIEW= "),
  MAGIC("RLE", 0, "\122\314"),
  MAGIC("SCT", 0, "CT"),
  MAGIC("SFW", 0, "SFW94"),
  MAGIC("SGI", 0, "\001\332"),
  MAGIC("SUN", 0, "\131\246\152\225"),
  MAGIC("SVG", 1, "?XML"),
  MAGIC("SVG", 1, "?xml"),
  MAGIC("TIFF", 0, "\115\115\000\052"),
  MAGIC("TIFF", 0, "\111\111\052\000"),
  MAGIC("BIGTIFF", 0, "\115\115\000\053\000\010\000\000"),
  MAGIC("BIGTIFF", 0, "\111\111\053\000\010\000\000\000"),
  MAGIC("VICAR", 0, "LBLSIZE"),
  MAGIC("VICAR", 0, "NJPL1I"),
  MAGIC("VIFF", 0, "\253\001"),
  MAGIC("WMF", 0, "\327\315\306\232"),
  MAGIC("WMF", 0, "\001\000\011\000"),
  MAGIC("WPG", 0, "\377WPC"),
  MAGIC("XBM", 0, "#define"),
  MAGIC("XCF", 0, "gimp xcf"),
  MAGIC("XPM", 1, "* XPM *"),
  MAGIC("XWD", 4, "\007\000\000"),
  MAGIC("XWD", 5, "\000\000\007")
};

/*
  Forward declarations.
*/

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
+   D e s t r o y M a g i c I n f o                                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method DestroyMagicInfo deallocates memory associated with the magic list.
%
%  The format of the DestroyMagicInfo method is:
%
%      DestroyMagicInfo(void)
%
%
*/
MagickExport void
DestroyMagicInfo(void)
{
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
+   G e t M a g i c k F i l e F o r m a t                                     %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method GetMagickFileFormat inspects the provided image file/blob header
%  bytes and sets/updates the provided file format string buffer. The value
%  MagickPass is returned if the format was successfully identified.  The
%  value MagickFail is returned if the format was not identified or an
%  exception occured.
%
%
%  The format of the GetMagickFileFormat method is:
%
%      MagickPassFail GetMagickFileFormat(
%        const unsigned char *header, const size_t header_length,
%        char *postulate, const size_t postulate_length,
%        ExceptionInfo *exception)
%
%  A description of each parameter follows:
%
%    o header: A binary string representing the first several
%        thousand bytes of the file/blob header to test.
%
%    o header_length: The length of the binary signature.  Currently
%        2*MaxTextExtent (4106 bytes).
%
%    o format: Format buffer. Updated with actual format if it can be
%        determined.
%
%    o format_length: Format buffer length.
%
%    o exception: Return any errors or warnings in this structure.
%
%
*/
MagickExport MagickPassFail
GetMagickFileFormat(const unsigned char *header, const size_t header_length,
                    char *format, const size_t format_length,
                    ExceptionInfo *exception)
{
  register unsigned int
    i;

  MagickPassFail
    status=MagickFail;

  ARG_NOT_USED(exception);

  if (!((header == (const unsigned char *) NULL) || (header_length == 0) ||
        (format_length < 2)))
    {
      /*
        Search for requested magic.
      */
      for (i=0; i < ArraySize(StaticMagic); i++)
        {
          if (StaticMagic[i].offset+StaticMagic[i].length <= header_length)
            {
              if ((header[StaticMagic[i].offset] == StaticMagic[i].magic[0]) &&
                  (memcmp(header+StaticMagic[i].offset,StaticMagic[i].magic,
                          StaticMagic[i].length) == 0))
                {
                  if (strlcpy(format,StaticMagic[i].name,format_length) < format_length)
                    status=MagickPass;
                  break;
                }
            }
        }
    }
  return status;
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
+   I n i t i a l i z e M a g i c I n f o                                     %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method InitializeMagicInfo initializes the file header magic detection
%  facility.
%
%  The format of the InitializeMagicInfo method is:
%
%      MagickPassFail InitializeMagicInfo(void)
%
%
*/
MagickExport MagickPassFail
InitializeMagicInfo(void)
{
  return MagickPass;
}

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  L i s t M a g i c I n f o                                                  %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Method ListMagicInfo lists the magic info to a file.
%
%  The format of the ListMagicInfo method is:
%
%      unsigned int ListMagicInfo(FILE *file,ExceptionInfo *exception)
%
%  A description of each parameter follows.
%
%    o file:  An pointer to a FILE.
%
%    o exception: Return any errors or warnings in this structure.
%
%
*/
MagickExport MagickPassFail
ListMagicInfo(FILE *file,ExceptionInfo *exception)
{
  register unsigned int
    i,
    j;

  ARG_NOT_USED(exception);

  if (file == (const FILE *) NULL)
    file=stdout;

  (void) fprintf(file,"Name      Offset Target\n");
  (void) fprintf(file,"-------------------------------------------------"
                 "------------------------------\n");
  for (i=0; i < ArraySize(StaticMagic); i++)
    {
      register const unsigned char
        *c;

      (void) fprintf(file,"%.1024s",StaticMagic[i].name);
      for (j= (unsigned int) strlen(StaticMagic[i].name); j <= 9; j++)
        (void) fprintf(file," ");
      (void) fprintf(file,"%6u ",StaticMagic[i].offset);

      (void) fprintf(file,"\"");
      c=StaticMagic[i].magic;
      for (j=0; j < StaticMagic[i].length; j++)
        {
/*          else if ('\b' == c[j]) */
/*            (void) fprintf(file,"\\b"); */
/*          else if ('\f' == c[j]) */
/*            (void) fprintf(file,"\\f"); */
          if ('\n' == c[j])
            (void) fprintf(file,"\\n");
          else if ('\r' == c[j])
            (void) fprintf(file,"\\r");
          else if ('\t' == c[j])
              (void) fprintf(file,"\\t");
/*          else if ('\v' == c[j]) */
/*            (void) fprintf(file,"\\v"); */
/*          else if ('\a' == c[j]) */
/*            (void) fprintf(file,"\\a"); */
          else if ('\\' == c[j])
            (void) fprintf(file,"\\");
          else if ('\?' == c[j])
            (void) fprintf(file,"\\?");
          else if ('"' == c[j])
            (void) fprintf(file,"\\\"");
          else if (isprint((int) c[j]))
            (void) fprintf(file,"%c",(int) c[j]);
          else
            {
              (void) fprintf(file,"\\%03o",c[j]);
            }
        }
      (void) fprintf(file,"\"\n");
    }
  (void) fflush(file);
  return(MagickPass);
}