source: trunk/src/sh_filetype.c@ 367

Last change on this file since 367 was 310, checked in by katerina, 14 years ago

Add option to skip checksum for certain files (ticket #232). Also fix for #231 (missing warning on invalid recursion depth).

File size: 18.7 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2011 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#ifndef NULL
23#if !defined(__cplusplus)
24#define NULL ((void*)0)
25#else
26#define NULL (0)
27#endif
28#endif
29
30#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
31
32#include "samhain.h"
33#include "sh_mem.h"
34#include "sh_error_min.h"
35#include "sh_utils.h"
36
37#define FIL__ _("sh_filetype.c")
38
39#include <string.h>
40#include <stdlib.h>
41#include <ctype.h>
42
43/* #define SH_FILE_MAIN 1 */
44#ifdef SH_FILE_MAIN
45#include <stdio.h>
46#define _(a) a
47#define N_(a) a
48#define sl_strlcpy strncpy
49#endif
50
51#define SH_FTYPE_MAX 32
52
53/* List of filetype description, in the format:
54 * offset : type(0=text, 1=binary) : length(if binary) : G1 : G2 : G3 : Name : Teststring
55 *
56 * This list is mostly taken from the 'filetype' library by Paul L Daniels.
57 *
58 * Copyright (c) 2003, PLD
59 * All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or
62 * without modification, are permitted provided that the
63 * following conditions are met:
64 *
65 * * Redistributions of source code must retain the above
66 * copyright notice, this list of conditions and the following
67 * disclaimer.
68 *
69 * * Redistributions in binary form must reproduce the above
70 * copyright notice, this list of conditions and the following
71 * disclaimer in the documentation and/or other materials provided
72 * with the distribution.
73 *
74 * * Neither the name of the PLD nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
79 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
80 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
81 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
82 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
83 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
84 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
85 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
86 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
88 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
89 * POSSIBILITY OF SUCH DAMAGE.
90 */
91
92
93char * sh_ftype_list[] = {
94
95 N_("6:0:0:IMAGE:COMPRESSED:JPG:JFIF Jpeg:JFIF"),
96 N_("0:0:0:IMAGE:COMPRESSED:PNG:PNG:=89PNG=0d=0a=1a=0a"),
97 N_("0:0:0:IMAGE:COMPRESSED:JPG:JFIF Jpeg:=FF=D8=FF"),
98 N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF97a"),
99 N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF89a"),
100 N_("0:0:0:IMAGE:COMPRESSED:GIF:GIF:GIF87a"),
101 N_("0:1:4:IMAGE:COMPRESSED:TIFF:TIFF-LE:II=2A=00"),
102 N_("0:1:4:IMAGE:COMPRESSED:TIFF:TIFF-BE:MM=00=2A"),
103 N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX25:=0A=00"),
104 N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX28WP:=0A=02"),
105 N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX28NP:=0A=03"),
106 N_("0:1:2:IMAGE:COMPRESSED:PCX:PCX30:=0A=05"),
107 N_("0:0:0:IMAGE:RAW:BMP:Bitmap:BM"),
108 N_("0:0:0:IMAGE:RAW:XPM:XPM:/* XPM */"),
109 N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWT:AC=31=30=31"),
110 N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWF:(DWF V"),
111 N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:WMF:=D7=CD=C6=9A"),
112 N_("0:0:0:IMAGE:SPECIAL:AUTOCAD:DWG:AC10"),
113 N_("8:0:0:IMAGE:SPECIAL:COREL:CorelDraw:CDR"),
114 N_("0:0:0:IMAGE:SPECIAL:FITS:Fits file:SIMPLE=20=20="),
115 N_("1536:0:0:IMAGE:SPECIAL:VISIO:VisioDraw:Visio"),
116 N_("128:0:0:IMAGE:SPECIAL:DICM:DICOM medical:DICM"),
117 N_("0:0:0:IMAGE:SPECIAL:PHS:Photoshop:8BPS"),
118 N_("0:0:0:IMAGE:SPECIAL:XCF:Gimp XCF:gimp xcf"),
119 N_("0:0:0:MOVIE:COMPRESSED:RIFF:RIFF/AVI Movie:RIFF"),
120 N_("0:0:0:MOVIE:RAW:MOV:SGI Movie:MOVI:.mov SGI Movie"),
121 N_("0:1:4:MOVIE:COMPRESSED:MPG:Mpeg 2:=00=00=01=BA"),
122 N_("0:1:4:MOVIE:COMPRESSED:MPG:Mpeg 2:=00=00=01=B3"),
123 N_("4:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
124 N_("4:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
125 N_("36:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
126 N_("36:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
127 N_("68:0:0:MOVIE:COMPRESSED:QT:QuickTime:moov"),
128 N_("68:0:0:MOVIE:COMPRESSED:QT:QuickTime:mdat"),
129 N_("0:1:3:MOVIE:COMPRESSED:FLI:FLIC animation:=00=11=AF"),
130 N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash data:FWS"),
131 N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash data:CWS"),
132 N_("0:0:0:MOVIE:COMPRESSED:FLASH:Flash video:FLV"),
133 N_("0:0:0:MOVIE:COMPRESSED:WMV:WMV:=30=26=B2=75=8E=66=CF"),
134 N_("0:0:0:AUDIO:RAW:SND:Sun Audio:.snd"),
135 N_("0:0:0:AUDIO:RAW:EMOD:EMOD:Mod"),
136 N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (.M.K):.M.K"),
137 N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M.K.):M.K."),
138 N_("1080:0:0:AUDIO:RAW:MOD:NoiseTracker:N.T."),
139 N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M!K!):M!K!"),
140 N_("1080:0:0:AUDIO:RAW:MOD:SoundTracker (M&K!):M&K!"),
141 N_("8:0:0:AUDIO:RAW:WAVE:Wave:WAVE"),
142 N_("0:1:4:AUDIO:RAW:DEC:DEC-Audio:=00=64=73=2E"),
143 N_("0:0:0:AUDIO:STANDARD:MIDI:Midi:MThd"),
144 N_("0:0:0:AUDIO:COMPRESSED:REAL:RealMedia:.RMF"),
145 N_("0:0:0:AUDIO:COMPRESSED:OGG:Ogg Vorbis:OggS"),
146 N_("0:0:0:AUDIO:COMPRESSED:FLAC:Flac:fLaC"),
147 N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=02=00"),
148 N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=03=00"),
149 N_("0:1:5:AUDIO:COMPRESSED:MP3:MP3 Audio:=49=44=33=04=00"),
150 N_("0:1:2:AUDIO:COMPRESSED:MP3:MP3 Audio:=ff=fb"),
151 N_("0:1:2:AUDIO:COMPRESSED:MP3:MP3 Audio:=ff=fa"),
152 N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 0:-lh0-"),
153 N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 1:-lh1-"),
154 N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 4:-lz4-"),
155 N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha z5:-lz5-"),
156 N_("2:0:0:ARCHIVE:COMPRESSED:LHA:Lha 5:-lh5-"),
157 N_("0:0:0:ARCHIVE:COMPRESSED:RAR:RarArchive:Rar!"),
158 N_("0:0:0:ARCHIVE:COMPRESSED:ZIP:PkZip:PK=03=04"),
159 N_("0:0:0:ARCHIVE:COMPRESSED:7Z:7-Zip:=37=7A=BC=AF=27=1C"),
160 N_("0:0:0:ARCHIVE:COMPRESSED:COMPRESS:Compress:=1F=89"),
161 N_("0:0:0:ARCHIVE:COMPRESSED:GZIP:Gzip:=1F=8B"),
162 N_("0:0:0:ARCHIVE:COMPRESSED:BZIP2:Bzip2:BZh"),
163 N_("0:0:0:ARCHIVE:COMPRESSED:ARJ:ARJ:=60=ea"),
164 N_("0:0:0:ARCHIVE:COMPRESSED:ARJ:ARJ:=ea=60"),
165 N_("0:0:0:ARCHIVE:COMPRESSED:HPAK:HPack:HPAK"),
166 N_("0:0:0:ARCHIVE:COMPRESSED:JAM:Jam:=E9,=01JAM"),
167 N_("0:0:0:ARCHIVE:COMPRESSED:SQUISH:Squish:SQSH"),
168 N_("0:1:8:ARCHIVE:COMPRESSED:CAB:MS Cabinet:MSCF=00=00=00=00"),
169 N_("20:0:0:ARCHIVE:COMPRESSED:ZOO:Zoo:=FD=C4=A7=DC"),
170 N_("0:0:0:ARCHIVE:COMPRESSED:XPK:Amiga XPK Archive:XPKF"),
171 N_("0:0:0:ARCHIVE:PACKAGE:RPM:RPM:=ED=AB=EE=DB"),
172 N_("0:0:0:ARCHIVE:PACKAGE:DEB:DEB:!<arch>=0A""debian"),
173 N_("0:0:0:ARCHIVE:UNIX:AR:AR:!<arch>"),
174 N_("0:0:0:ARCHIVE:UNIX:AR:AR:<ar>"),
175 N_("257:1:8:ARCHIVE:UNIX:TAR:TAR:ustar=20=20=00"),
176 N_("257:1:6:ARCHIVE:UNIX:TAR:TAR:ustar=00"),
177 N_("0:0:0:LIBRARY:JAVA:CLASS:Java:=CA=FE=BA=BE"),
178 N_("2108:0:0:DOCUMENT:OFFICE:WORD:Word v5:MSWordDoc"),
179 N_("2112:0:0:DOCUMENT:OFFICE:WORD:Word v5:MSWordDoc"),
180 N_("2080:0:0:DOCUMENT:OFFICE:EXCEL:Excel v4:Microsoft Excel"),
181 N_("2080:0:0:DOCUMENT:OFFICE:WORD:MS Word:Microsoft Word"),
182 N_("0:0:0:DOCUMENT:OFFICE:WORD:Word:=94=A6=2E"),
183 N_("512:1:19:DOCUMENT:OFFICE:WORD:Word:R=00o=00o=00t=00 =00""E=00n=00t=00r=00y"),
184 N_("0:1:9:DOCUMENT:OFFICE:ALL:MSOffice:=D0=CF=11=E0=A1=B1=1A=E1=00"),
185 N_("0:0:0:DOCUMENT:ADOBE:PDF:PortableDocument:%PDF-"),
186 N_("0:0:0:DOCUMENT:ADOBE:EPS:EncapsulatedPS:%!PS-ADOBE EPS"),
187 N_("0:0:0:DOCUMENT:STANDARD:RTF:RichText:{\\rtf"),
188 N_("6:1:4:DOCUMENT:STANDARD:RTF:RichText Compressed:=00=00LZ"),
189 N_("6:0:0:DOCUMENT:ID:VCARD:VCARD:vcard"),
190 N_("0:0:0:EXECUTABLE:DOS:EXE:DosExe:MZ"),
191 N_("0:0:0:EXECUTABLE:DOS:EXE:DosExe:LZ"),
192 N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 1:=E9"),
193 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/bin/sh"),
194 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /bin/sh"),
195 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/bin/bash"),
196 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /bin/bash"),
197 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#!/usr/bin/bash"),
198 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Bourne:#! /usr/bin/bash"),
199 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#!/usr/bin/csh"),
200 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#! /usr/bin/csh"),
201 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#!/bin/csh"),
202 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Csh:#! /bin/csh"),
203 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#! /usr/bin/ksh"),
204 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#!/usr/bin/ksh"),
205 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#! /bin/ksh"),
206 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Korn:#!/bin/ksh"),
207 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#!/usr/bin/tcsh"),
208 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#! /usr/bin/tcsh"),
209 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#!/bin/tcsh"),
210 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Tenex:#! /bin/tcsh"),
211 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#!/usr/bin/zsh"),
212 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#! /usr/bin/zsh"),
213 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#!/bin/zsh"),
214 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Zsh:#! /bin/zsh"),
215 N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#!/usr/bin/ash"),
216 N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#! /usr/bin/ash"),
217 N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#!/bin/ash"),
218 N_("0:0:0:EXECUTABLE:UNIX:SHELL:ash:#! /bin/ash"),
219 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/usr/bin/nawk"),
220 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /usr/bin/nawk"),
221 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/nawk"),
222 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/nawk"),
223 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/gawk"),
224 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/gawk"),
225 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#!/bin/awk"),
226 N_("0:0:0:EXECUTABLE:UNIX:SHELL:awk:#! /bin/awk"),
227 N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#!/usr/bin/perl"),
228 N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#! /usr/bin/perl"),
229 N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#!/bin/perl"),
230 N_("0:0:0:EXECUTABLE:UNIX:SHELL:perl:#! /bin/perl"),
231 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Shell script:#!/"),
232 N_("0:0:0:EXECUTABLE:UNIX:SHELL:Shell script:#! /"),
233 N_("0:0:0:EXECUTABLE:UNIX:ELF:Linux ELF32:=7f""ELF=01"),
234 N_("0:0:0:EXECUTABLE:UNIX:ELF:Linux ELF:=7f""ELF=02"),
235 N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 2:=8c"),
236 N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 3:=eb"),
237 N_("0:0:0:EXECUTABLE:DOS:COM:DosCom 4:=b8"),
238 N_("0:1:4:EXECUTABLE:AMIGAOS:EXECUTABLE:AmigaOS Executable:=00=00=03=F3"),
239 N_("0:1:20:DATABASE:ANY:ACCESS:MSAccess:=00=01=00=00Standard=20Jet=20""DB=00"),
240 N_("0:1:2:DATABASE:ANY:MYSQL:MySQL database:=fe=01"),
241 N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=03=00"),
242 N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=07=00"),
243 N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=05=00"),
244 N_("0:1:4:DATABASE:ANY:MYSQL:MySQL database:=fe=fe=06=00"),
245
246 NULL,
247 NULL,
248 NULL,
249 NULL,
250
251 NULL,
252 NULL,
253 NULL,
254 NULL,
255
256 NULL,
257 NULL,
258 NULL,
259 NULL,
260
261 NULL,
262 NULL,
263 NULL,
264 NULL,
265
266 NULL,
267};
268
269static unsigned int sh_ftype_def = 0;
270
271#define SH_FTYPE_ADD 16
272
273struct sh_ftype_rec {
274 size_t offset;
275 size_t length;
276 char pattern[SH_FTYPE_MAX];
277
278 char type[SH_FTYPE_MAX];
279};
280
281
282struct sh_ftype_rec ** sh_ftype_arr = NULL;
283static unsigned int sh_ftype_nn = 0;
284
285#if !defined(SH_FILE_MAIN)
286
287static unsigned int sh_ftype_usr = 0;
288
289extern char * unquote_string (const char * str, size_t len);
290
291int sh_restrict_add_ftype(const char * str)
292{
293 size_t len;
294 char * cond;
295
296 if (sh_ftype_def == 0)
297 {
298 while(sh_ftype_list[sh_ftype_def] != NULL) ++sh_ftype_def;
299 }
300
301 if (!str)
302 {
303 if (sh_ftype_usr > 0)
304 {
305 unsigned int i, j = sh_ftype_def;
306
307 for (i = 0; i < sh_ftype_usr; ++i)
308 {
309 SH_FREE(sh_ftype_list[j+i]);
310 sh_ftype_list[j+i] = NULL;
311 }
312 sh_ftype_usr = 0;
313 }
314
315 if (sh_ftype_arr)
316 {
317 unsigned int i = 0;
318
319 while(sh_ftype_arr[i] != NULL)
320 {
321 SH_FREE(sh_ftype_arr[i]);
322 ++i;
323 }
324 SH_FREE(sh_ftype_arr);
325 sh_ftype_arr = NULL;
326 }
327 }
328 else if (sh_ftype_usr < SH_FTYPE_ADD)
329 {
330 len = strlen(str);
331 cond = unquote_string(str, len);
332 sh_ftype_list[sh_ftype_def+sh_ftype_usr] = cond;
333 ++sh_ftype_usr;
334 }
335 else
336 {
337 return -1;
338 }
339 return 0;
340}
341
342
343#endif
344
345
346static int init_record(unsigned int n, char * define,
347 struct sh_ftype_rec * record)
348{
349 unsigned int offset, dtype, length, i = 0, xn = 0;
350 char type[SH_FTYPE_MAX];
351 char pattern[SH_FTYPE_MAX];
352
353 char * end;
354 char * start;
355
356 offset = strtoul(define, &end, 0);
357 if (*end != ':')
358 return -1;
359
360 start = end; ++start;
361 dtype = strtoul(start, &end, 0);
362 if (*end != ':')
363 return -1;
364
365 start = end; ++start;
366 length = strtoul(start, &end, 0);
367 if (*end != ':')
368 return -1;
369
370 start = end; ++start;
371
372 while (*start && (i < sizeof(type)))
373 {
374 type[i] = *start; ++start;
375 if (type[i] == ':')
376 ++xn;
377 if (xn == 3)
378 {
379 type[i] = '\0';
380 break;
381 }
382 ++i;
383 }
384 if (xn != 3)
385 return -1;
386
387 start = strchr(start, ':');
388
389 if (!start)
390 return -1;
391
392 ++start;
393
394 if (dtype == 0)
395 {
396 sl_strlcpy(pattern, start, sizeof(pattern));
397 length = strlen(pattern);
398 }
399 else if (length <= sizeof(pattern))
400 {
401 memcpy(pattern, start, length);
402 }
403 else
404 {
405 return -1;
406 }
407
408 /* fprintf(stderr, "FIXME: %d %d %s ", dtype, length, type); */
409 /**
410 if (dtype == 0)
411 fprintf(stderr, "%s\n", pattern);
412 else
413 {
414 int k;
415 for (k = 0; k < length; ++k)
416 fprintf(stderr, "0x%X", (unsigned int) (pattern[k]));
417 fprintf(stderr, "\n");
418 }
419 **/
420
421 for (i = 0; i < n; ++i)
422 {
423 if (sh_ftype_arr[i]->length <= length &&
424 sh_ftype_arr[i]->offset == offset)
425 {
426 if (0 == memcmp(sh_ftype_arr[i]->pattern, pattern,
427 sh_ftype_arr[i]->length))
428 {
429#ifdef SH_FILE_MAIN
430 fprintf(stderr,
431 "Pattern %d (%s / %s) override by earlier pattern %d (%s / %s)\n",
432 n, type, pattern,
433 i, sh_ftype_arr[i]->type, sh_ftype_arr[i]->pattern);
434#else
435 char errbuf[256];
436
437 sl_snprintf(errbuf, sizeof(errbuf),
438 _("Pattern %d (%s) override by earlier pattern %d (%s)"),
439 n, type,
440 i, sh_ftype_arr[i]->type);
441 sh_error_handle((-1), FIL__, __LINE__, -1, MSG_E_SUBGEN,
442 errbuf,
443 _("init_record"));
444#endif
445 }
446 }
447 }
448
449
450 record->offset = offset;
451 record->length = length;
452 memcpy(record->pattern, pattern, length);
453 sl_strlcpy(record->type, type, SH_FTYPE_MAX);
454
455 return 0;
456}
457
458static void file_arr_init()
459{
460 unsigned int i, nn = 0;
461
462 if (sh_ftype_def == 0)
463 {
464 while(sh_ftype_list[sh_ftype_def] != NULL) ++sh_ftype_def;
465 }
466
467 while (sh_ftype_list[nn] != NULL) ++nn;
468
469#ifdef SH_FILE_MAIN
470 printf("%d definitions found, defined = %d\n", nn, sh_ftype_def);
471#endif
472
473#ifdef SH_FILE_MAIN
474 sh_ftype_arr = malloc((nn+1) * sizeof(struct sh_ftype_rec *));
475#else
476 sh_ftype_arr = SH_ALLOC((nn+1) * sizeof(struct sh_ftype_rec *));
477#endif
478
479 for(i = 0; i < nn; i++)
480 {
481#ifdef SH_FILE_MAIN
482 sh_ftype_arr[i] = malloc(sizeof(struct sh_ftype_rec));
483#else
484 sh_ftype_arr[i] = SH_ALLOC(sizeof(struct sh_ftype_rec));
485#endif
486
487 memset(sh_ftype_arr[i], 0, sizeof(struct sh_ftype_rec));
488 if (i < sh_ftype_def)
489 {
490 char * p = _(sh_ftype_list[i]);
491 size_t len = strlen(p);
492 char * cond = unquote_string(p, len);
493
494 init_record(i, cond, sh_ftype_arr[i]);
495 }
496 else
497 {
498 init_record(i, sh_ftype_list[i], sh_ftype_arr[i]);
499 }
500 }
501 sh_ftype_arr[nn] = NULL;
502 sh_ftype_nn = nn;
503
504 return;
505}
506
507static char * check_filetype(char * filetype,
508 const char * buffer, size_t buflen)
509{
510 unsigned int i;
511 const char * p;
512
513 if (!sh_ftype_arr)
514 {
515 file_arr_init();
516 }
517
518 for (i = 0; i < sh_ftype_nn; ++i)
519 {
520 if (sh_ftype_arr[i]->length > 0 &&
521 (sh_ftype_arr[i]->length + sh_ftype_arr[i]->offset) < buflen)
522 {
523 p = &buffer[sh_ftype_arr[i]->offset];
524
525#if 0
526 {
527 int dd;
528 /* fprintf(stderr, "FIXME: %03d comp %d:%d ", i,
529 sh_ftype_arr[i]->offset, sh_ftype_arr[i]->length); */
530 for (dd = 0; dd < sh_ftype_arr[i]->length; ++dd) {
531 fprintf(stderr, "0x%X ", sh_ftype_arr[i]->pattern[dd]);
532 }
533 for (dd = 0; dd < sh_ftype_arr[i]->length; ++dd) {
534 fprintf(stderr, "0x%X ", p[dd]);
535 }
536 fprintf(stderr, "\n");
537 }
538#endif
539
540 if (0 == memcmp(p, sh_ftype_arr[i]->pattern, sh_ftype_arr[i]->length))
541 {
542 sl_strlcpy(filetype, sh_ftype_arr[i]->type, SH_FTYPE_MAX);
543 return (filetype);
544 }
545 }
546 }
547
548 if (buflen > 0) {
549
550 int flag = 0;
551
552 p = buffer;
553 for (i = 0; i < buflen; ++i) {
554 if (*p == '\0')
555 {
556 sl_strlcpy(filetype, _("FILE:BINARY:UNKNOWN"), SH_FTYPE_MAX);
557 goto out;
558 }
559 else if (!isgraph((int)*p) && !isspace((int)*p))
560 {
561 flag = 1;
562 }
563 ++p;
564 }
565 if (flag == 0)
566 {
567 sl_strlcpy(filetype, _("FILE:TEXT:ASCII"), SH_FTYPE_MAX);
568 goto out;
569 }
570 }
571 sl_strlcpy(filetype, _("FILE:UNKNOWN:UNKNOWN"), SH_FTYPE_MAX);
572 out:
573 return filetype;
574}
575
576#if !defined(SH_FILE_MAIN)
577
578int matches_filetype(SL_TICKET ft, char * test_type)
579{
580 char buffer[3072];
581 char filetype[SH_FTYPE_MAX];
582 long len;
583
584 len = sl_read_timeout (ft, buffer, sizeof(buffer), 12, SL_TRUE);
585
586 sl_rewind(ft);
587
588 if (len > 0)
589 {
590 check_filetype(filetype, buffer, len);
591 }
592 else
593 {
594 sl_strlcpy(filetype, _("FILE:UNKNOWN:UNKNOWN"), SH_FTYPE_MAX);
595 }
596
597 if (0 == strcmp(filetype, test_type))
598 {
599 return 1;
600 }
601
602 return 0;
603}
604
605#else
606/* SH_FILE_MAIN */
607#include <unistd.h>
608
609int main (int argc, char * argv[])
610{
611 char buffer[3072];
612 char filetype[SH_FTYPE_MAX];
613 size_t len;
614
615 FILE * fh = fopen(argv[1], "r");
616
617 if (fh)
618 {
619 int fd = fileno(fh);
620
621 len = read(fd, buffer, 3072);
622
623 check_filetype(filetype, buffer, len);
624
625 fprintf(stdout, "%s: %s\n", argv[1], filetype);
626
627 return 0;
628 }
629 return 1;
630}
631#endif
632
633#endif
634/* #if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE) */
Note: See TracBrowser for help on using the repository browser.