summaryrefslogtreecommitdiff
path: root/scheduler/type.c
blob: 2cceeda58c8ed43dd6abc6c337aa0dc457aaf2e2 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
/*
 * MIME typing routines for CUPS.
 *
 * Copyright 2007-2016 by Apple Inc.
 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
 *
 * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
 */

/*
 * Include necessary headers...
 */

#include <cups/string-private.h>
#include <locale.h>
#include "mime.h"


/*
 * Debug macros that used to be private API...
 */

#define DEBUG_puts(x)
#define DEBUG_printf(...)


/*
 * Local types...
 */

typedef struct _mime_filebuf_s		/**** File buffer for MIME typing ****/
{
  cups_file_t	*fp;			/* File pointer */
  int		offset,			/* Offset in file */
		length;			/* Length of buffered data */
  unsigned char	buffer[MIME_MAX_BUFFER];/* Buffered data */
} _mime_filebuf_t;


/*
 * Local functions...
 */

static int	mime_compare_types(mime_type_t *t0, mime_type_t *t1);
static int	mime_check_rules(const char *filename, _mime_filebuf_t *fb,
		                 mime_magic_t *rules);
static int	mime_patmatch(const char *s, const char *pat);


/*
 * Local globals...
 */

#ifdef MIME_DEBUG
static const char * const debug_ops[] =
		{			/* Test names... */
		  "NOP",		/* No operation */
		  "AND",		/* Logical AND of all children */
		  "OR",			/* Logical OR of all children */
		  "MATCH",		/* Filename match */
		  "ASCII",		/* ASCII characters in range */
		  "PRINTABLE",		/* Printable characters (32-255) */
		  "STRING",		/* String matches */
		  "CHAR",		/* Character/byte matches */
		  "SHORT",		/* Short/16-bit word matches */
		  "INT",		/* Integer/32-bit word matches */
		  "LOCALE",		/* Current locale matches string */
		  "CONTAINS",		/* File contains a string */
		  "ISTRING",		/* Case-insensitive string matches */
		  "REGEX"		/* Regular expression matches */
		};
#endif /* DEBUG */


/*
 * 'mimeAddType()' - Add a MIME type to a database.
 */

mime_type_t *				/* O - New (or existing) MIME type */
mimeAddType(mime_t     *mime,		/* I - MIME database */
            const char *super,		/* I - Super-type name */
	    const char *type)		/* I - Type name */
{
  mime_type_t	*temp;			/* New MIME type */
  size_t	typelen;		/* Length of type name */


  DEBUG_printf(("mimeAddType(mime=%p, super=\"%s\", type=\"%s\")", mime, super,
                type));

 /*
  * Range check input...
  */

  if (!mime || !super || !type)
  {
    DEBUG_puts("1mimeAddType: Returning NULL (bad arguments).");
    return (NULL);
  }

 /*
  * See if the type already exists; if so, return the existing type...
  */

  if ((temp = mimeType(mime, super, type)) != NULL)
  {
    DEBUG_printf(("1mimeAddType: Returning %p (existing).", temp));
    return (temp);
  }

 /*
  * The type doesn't exist; add it...
  */

  if (!mime->types)
    mime->types = cupsArrayNew((cups_array_func_t)mime_compare_types, NULL);

  if (!mime->types)
  {
    DEBUG_puts("1mimeAddType: Returning NULL (no types).");
    return (NULL);
  }

  typelen = strlen(type) + 1;

  if ((temp = calloc(1, sizeof(mime_type_t) - MIME_MAX_TYPE + typelen)) == NULL)
  {
    DEBUG_puts("1mimeAddType: Returning NULL (out of memory).");
    return (NULL);
  }

  strlcpy(temp->super, super, sizeof(temp->super));
  memcpy(temp->type, type, typelen);
  temp->priority = 100;

  cupsArrayAdd(mime->types, temp);

  DEBUG_printf(("1mimeAddType: Returning %p (new).", temp));
  return (temp);
}


/*
 * 'mimeAddTypeRule()' - Add a detection rule for a file type.
 */

int					/* O - 0 on success, -1 on failure */
mimeAddTypeRule(mime_type_t *mt,	/* I - Type to add to */
                const char  *rule)	/* I - Rule to add */
{
  int		num_values,		/* Number of values seen */
		op,			/* Operation code */
		logic,			/* Logic for next rule */
		invert;			/* Invert following rule? */
  char		name[255],		/* Name in rule string */
		value[3][255],		/* Value in rule string */
		*ptr,			/* Position in name or value */
		quote;			/* Quote character */
  int		length[3];		/* Length of each parameter */
  mime_magic_t	*temp,			/* New rule */
		*current;  		/* Current rule */


  DEBUG_printf(("mimeAddTypeRule(mt=%p(%s/%s), rule=\"%s\")", mt,
                mt ? mt->super : "???", mt ? mt->type : "???", rule));

 /*
  * Range check input...
  */

  if (!mt || !rule)
    return (-1);

 /*
  * Find the last rule in the top-level of the rules tree.
  */

  for (current = mt->rules; current; current = current->next)
    if (!current->next)
      break;

 /*
  * Parse the rules string.  Most rules are either a file extension or a
  * comparison function:
  *
  *    extension
  *    function(parameters)
  */

  logic  = MIME_MAGIC_NOP;
  invert = 0;

  while (*rule != '\0')
  {
    while (isspace(*rule & 255))
      rule ++;

    if (*rule == '(')
    {
      DEBUG_puts("1mimeAddTypeRule: New parenthesis group");
      logic = MIME_MAGIC_NOP;
      rule ++;
    }
    else if (*rule == ')')
    {
      DEBUG_puts("1mimeAddTypeRule: Close paren...");
      if (current == NULL || current->parent == NULL)
        return (-1);

      current = current->parent;

      if (current->parent == NULL)
        logic = MIME_MAGIC_OR;
      else
        logic = current->parent->op;

      rule ++;
    }
    else if (*rule == '+' && current != NULL)
    {
      if (logic != MIME_MAGIC_AND &&
          current != NULL && current->prev != NULL)
      {
       /*
        * OK, we have more than 1 rule in the current tree level...  Make a
	* new group tree and move the previous rule to it...
	*/

	if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
	  return (-1);

        temp->op            = MIME_MAGIC_AND;
        temp->child         = current;
        temp->parent        = current->parent;
	current->prev->next = temp;
	temp->prev          = current->prev;

        current->prev   = NULL;
	current->parent = temp;

        DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p.", temp));
      }
      else if (current->parent)
      {
        DEBUG_printf(("1mimeAddTypeRule: Setting group %p op to AND.",
	              current->parent));
        current->parent->op = MIME_MAGIC_AND;
      }

      logic = MIME_MAGIC_AND;
      rule ++;
    }
    else if (*rule == ',')
    {
      if (logic != MIME_MAGIC_OR && current != NULL)
      {
       /*
        * OK, we have two possibilities; either this is the top-level rule or
	* we have a bunch of AND rules at this level.
	*/

	if (current->parent == NULL)
	{
	 /*
	  * This is the top-level rule; we have to move *all* of the AND rules
	  * down a level, as AND has precedence over OR.
	  */

	  if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
	    return (-1);

          DEBUG_printf(("1mimeAddTypeRule: Creating new AND group %p inside OR "
	                "group.", temp));

          while (current->prev != NULL)
	  {
	    current->parent = temp;
	    current         = current->prev;
	  }

          current->parent = temp;
          temp->op        = MIME_MAGIC_AND;
          temp->child     = current;

          mt->rules = current = temp;
	}
	else
	{
	 /*
	  * This isn't the top rule, so go up one level...
	  */

          DEBUG_puts("1mimeAddTypeRule: Going up one level.");
	  current = current->parent;
	}
      }

      logic = MIME_MAGIC_OR;
      rule ++;
    }
    else if (*rule == '!')
    {
      DEBUG_puts("1mimeAddTypeRule: NOT");
      invert = 1;
      rule ++;
    }
    else if (isalnum(*rule & 255))
    {
     /*
      * Read an extension name or a function...
      */

      ptr = name;
      while (isalnum(*rule & 255) && (size_t)(ptr - name) < (sizeof(name) - 1))
        *ptr++ = *rule++;

      *ptr = '\0';

      if (*rule == '(')
      {
       /*
        * Read function parameters...
	*/

	rule ++;
	for (num_values = 0;
	     num_values < (int)(sizeof(value) / sizeof(value[0]));
	     num_values ++)
	{
	  ptr = value[num_values];

	  while ((size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1) &&
	         *rule != '\0' && *rule != ',' && *rule != ')')
	  {
	    if (isspace(*rule & 255))
	    {
	     /*
	      * Ignore whitespace...
	      */

	      rule ++;
	      continue;
	    }
	    else if (*rule == '\"' || *rule == '\'')
	    {
	     /*
	      * Copy quoted strings literally...
	      */

	      quote = *rule++;

	      while (*rule != '\0' && *rule != quote &&
	             (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
	        *ptr++ = *rule++;

              if (*rule == quote)
	        rule ++;
	      else
		return (-1);
	    }
	    else if (*rule == '<')
	    {
	      rule ++;

	      while (*rule != '>' && *rule != '\0' &&
	             (size_t)(ptr - value[num_values]) < (sizeof(value[0]) - 1))
	      {
	        if (isxdigit(rule[0] & 255) && isxdigit(rule[1] & 255))
		{
		  if (isdigit(*rule))
		    *ptr = (char)((*rule++ - '0') << 4);
		  else
		    *ptr = (char)((tolower(*rule++) - 'a' + 10) << 4);

		  if (isdigit(*rule))
		    *ptr++ |= *rule++ - '0';
		  else
		    *ptr++ |= tolower(*rule++) - 'a' + 10;
		}
		else
	          return (-1);
	      }

              if (*rule == '>')
	        rule ++;
	      else
		return (-1);
	    }
	    else
	      *ptr++ = *rule++;
	  }

          *ptr = '\0';
	  length[num_values] = ptr - value[num_values];

          if (*rule != ',')
	  {
	    num_values ++;
	    break;
	  }

          rule ++;
	}

        if (*rule != ')')
	  return (-1);

	rule ++;

       /*
        * Figure out the function...
	*/

        if (!strcmp(name, "match"))
	  op = MIME_MAGIC_MATCH;
	else if (!strcmp(name, "ascii"))
	  op = MIME_MAGIC_ASCII;
	else if (!strcmp(name, "printable"))
	  op = MIME_MAGIC_PRINTABLE;
	else if (!strcmp(name, "regex"))
	  op = MIME_MAGIC_REGEX;
	else if (!strcmp(name, "string"))
	  op = MIME_MAGIC_STRING;
	else if (!strcmp(name, "istring"))
	  op = MIME_MAGIC_ISTRING;
	else if (!strcmp(name, "char"))
	  op = MIME_MAGIC_CHAR;
	else if (!strcmp(name, "short"))
	  op = MIME_MAGIC_SHORT;
	else if (!strcmp(name, "int"))
	  op = MIME_MAGIC_INT;
	else if (!strcmp(name, "locale"))
	  op = MIME_MAGIC_LOCALE;
	else if (!strcmp(name, "contains"))
	  op = MIME_MAGIC_CONTAINS;
	else if (!strcmp(name, "priority") && num_values == 1)
	{
	  mt->priority = atoi(value[0]);
	  continue;
	}
	else
	  return (-1);
      }
      else
      {
       /*
        * This is just a filename match on the extension...
	*/

	snprintf(value[0], sizeof(value[0]), "*.%s", name);
	length[0]  = (int)strlen(value[0]);
	op         = MIME_MAGIC_MATCH;
      }

     /*
      * Add a rule for this operation.
      */

      if ((temp = calloc(1, sizeof(mime_magic_t))) == NULL)
	return (-1);

      temp->invert = (short)invert;
      if (current != NULL)
      {
	temp->parent  = current->parent;
	current->next = temp;
      }
      else
        mt->rules = temp;

      temp->prev = current;

      if (logic == MIME_MAGIC_NOP)
      {
       /*
        * Add parenthetical grouping...
	*/

        DEBUG_printf(("1mimeAddTypeRule: Making new OR group %p for "
	              "parenthesis.", temp));

        temp->op = MIME_MAGIC_OR;

	if ((temp->child = calloc(1, sizeof(mime_magic_t))) == NULL)
	  return (-1);

	temp->child->parent = temp;
	temp->child->invert = temp->invert;
	temp->invert        = 0;

	temp  = temp->child;
        logic = MIME_MAGIC_OR;
      }

      DEBUG_printf(("1mimeAddTypeRule: Adding %p: %s, op=MIME_MAGIC_%s(%d), "
		    "logic=MIME_MAGIC_%s, invert=%d.", temp, name,
		    debug_ops[op], op, debug_ops[logic], invert));

     /*
      * Fill in data for the rule...
      */

      current  = temp;
      temp->op = (short)op;
      invert   = 0;

      switch (op)
      {
        case MIME_MAGIC_MATCH :
	    if ((size_t)length[0] > (sizeof(temp->value.matchv) - 1))
	      return (-1);
	    strlcpy(temp->value.matchv, value[0], sizeof(temp->value.matchv));
	    break;
	case MIME_MAGIC_ASCII :
	case MIME_MAGIC_PRINTABLE :
	    temp->offset = strtol(value[0], NULL, 0);
	    temp->length = strtol(value[1], NULL, 0);
	    if (temp->length > MIME_MAX_BUFFER)
	      temp->length = MIME_MAX_BUFFER;
	    break;
	case MIME_MAGIC_REGEX :
	    temp->offset = strtol(value[0], NULL, 0);
	    temp->length = MIME_MAX_BUFFER;
	    if (regcomp(&(temp->value.rev), value[1], REG_NOSUB | REG_EXTENDED))
	      return (-1);
	    break;
	case MIME_MAGIC_STRING :
	case MIME_MAGIC_ISTRING :
	    temp->offset = strtol(value[0], NULL, 0);
	    if ((size_t)length[1] > sizeof(temp->value.stringv))
	      return (-1);
	    temp->length = length[1];
	    memcpy(temp->value.stringv, value[1], (size_t)length[1]);
	    break;
	case MIME_MAGIC_CHAR :
	    temp->offset = strtol(value[0], NULL, 0);
	    if (length[1] == 1)
	      temp->value.charv = (unsigned char)value[1][0];
	    else
	      temp->value.charv = (unsigned char)strtol(value[1], NULL, 0);

	    DEBUG_printf(("1mimeAddTypeRule: CHAR(%d,0x%02x)", temp->offset,
	                  temp->value.charv));
	    break;
	case MIME_MAGIC_SHORT :
	    temp->offset       = strtol(value[0], NULL, 0);
	    temp->value.shortv = (unsigned short)strtol(value[1], NULL, 0);
	    break;
	case MIME_MAGIC_INT :
	    temp->offset     = strtol(value[0], NULL, 0);
	    temp->value.intv = (unsigned)strtol(value[1], NULL, 0);
	    break;
	case MIME_MAGIC_LOCALE :
	    if ((size_t)length[0] > (sizeof(temp->value.localev) - 1))
	      return (-1);

	    strlcpy(temp->value.localev, value[0], sizeof(temp->value.localev));
	    break;
	case MIME_MAGIC_CONTAINS :
	    temp->offset = strtol(value[0], NULL, 0);
	    temp->region = strtol(value[1], NULL, 0);
	    if ((size_t)length[2] > sizeof(temp->value.stringv))
	      return (-1);
	    temp->length = length[2];
	    memcpy(temp->value.stringv, value[2], (size_t)length[2]);
	    break;
      }
    }
    else
      break;
  }

  return (0);
}


/*
 * 'mimeFileType()' - Determine the type of a file.
 */

mime_type_t *				/* O - Type of file */
mimeFileType(mime_t     *mime,		/* I - MIME database */
             const char *pathname,	/* I - Name of file to check on disk */
	     const char *filename,	/* I - Original filename or NULL */
	     int        *compression)	/* O - Is the file compressed? */
{
  _mime_filebuf_t	fb;		/* File buffer */
  const char		*base;		/* Base filename of file */
  mime_type_t		*type,		/* File type */
			*best;		/* Best match */


  DEBUG_printf(("mimeFileType(mime=%p, pathname=\"%s\", filename=\"%s\", "
                "compression=%p)", mime, pathname, filename, compression));

 /*
  * Range check input parameters...
  */

  if (!mime || !pathname)
  {
    DEBUG_puts("1mimeFileType: Returning NULL.");
    return (NULL);
  }

 /*
  * Try to open the file...
  */

  if ((fb.fp = cupsFileOpen(pathname, "r")) == NULL)
  {
    DEBUG_printf(("1mimeFileType: Unable to open \"%s\": %s", pathname,
                  strerror(errno)));
    DEBUG_puts("1mimeFileType: Returning NULL.");
    return (NULL);
  }

 /*
  * Then preload the first MIME_MAX_BUFFER bytes of the file into the file
  * buffer, returning an error if we can't read anything...
  */

  fb.offset = 0;
  fb.length = (int)cupsFileRead(fb.fp, (char *)fb.buffer, MIME_MAX_BUFFER);

  if (fb.length <= 0)
  {
    DEBUG_printf(("1mimeFileType: Unable to read from \"%s\": %s", pathname, strerror(errno)));
    DEBUG_puts("1mimeFileType: Returning NULL.");

    cupsFileClose(fb.fp);

    return (NULL);
  }

 /*
  * Figure out the base filename (without directory portion)...
  */

  if (filename)
  {
    if ((base = strrchr(filename, '/')) != NULL)
      base ++;
    else
      base = filename;
  }
  else if ((base = strrchr(pathname, '/')) != NULL)
    base ++;
  else
    base = pathname;

 /*
  * Then check it against all known types...
  */

  for (type = (mime_type_t *)cupsArrayFirst(mime->types), best = NULL;
       type;
       type = (mime_type_t *)cupsArrayNext(mime->types))
    if (mime_check_rules(base, &fb, type->rules))
    {
      if (!best || type->priority > best->priority)
        best = type;
    }

 /*
  * Finally, close the file and return a match (if any)...
  */

  if (compression)
  {
    *compression = cupsFileCompression(fb.fp);
    DEBUG_printf(("1mimeFileType: *compression=%d", *compression));
  }

  cupsFileClose(fb.fp);

  DEBUG_printf(("1mimeFileType: Returning %p(%s/%s).", best,
                best ? best->super : "???", best ? best->type : "???"));
  return (best);
}


/*
 * 'mimeType()' - Lookup a file type.
 */

mime_type_t *				/* O - Matching file type definition */
mimeType(mime_t     *mime,		/* I - MIME database */
         const char *super,		/* I - Super-type name */
	 const char *type)		/* I - Type name */
{
  mime_type_t	key,			/* MIME type search key */
		*mt;			/* Matching type */


  DEBUG_printf(("mimeType(mime=%p, super=\"%s\", type=\"%s\")", mime, super,
                type));

 /*
  * Range check input...
  */

  if (!mime || !super || !type)
  {
    DEBUG_puts("1mimeType: Returning NULL.");
    return (NULL);
  }

 /*
  * Lookup the type in the array...
  */

  strlcpy(key.super, super, sizeof(key.super));
  strlcpy(key.type, type, sizeof(key.type));

  mt = (mime_type_t *)cupsArrayFind(mime->types, &key);
  DEBUG_printf(("1mimeType: Returning %p.", mt));
  return (mt);
}


/*
 * 'mime_compare_types()' - Compare two MIME super/type names.
 */

static int				/* O - Result of comparison */
mime_compare_types(mime_type_t *t0,	/* I - First type */
                   mime_type_t *t1)	/* I - Second type */
{
  int	i;				/* Result of comparison */


  if ((i = _cups_strcasecmp(t0->super, t1->super)) == 0)
    i = _cups_strcasecmp(t0->type, t1->type);

  return (i);
}


/*
 * 'mime_check_rules()' - Check each rule in a list.
 */

static int				/* O - 1 if match, 0 if no match */
mime_check_rules(
    const char      *filename,		/* I - Filename */
    _mime_filebuf_t *fb,		/* I - File to check */
    mime_magic_t    *rules)		/* I - Rules to check */
{
  int		n;			/* Looping var */
  int		region;			/* Region to look at */
  int		logic,			/* Logic to apply */
		result;			/* Result of test */
  unsigned	intv;			/* Integer value */
  short		shortv;			/* Short value */
  unsigned char	*bufptr;		/* Pointer into buffer */


  DEBUG_printf(("4mime_check_rules(filename=\"%s\", fb=%p, rules=%p)", filename,
                fb, rules));

  if (rules == NULL)
    return (0);

  if (rules->parent == NULL)
    logic = MIME_MAGIC_OR;
  else
    logic = rules->parent->op;

  result = 0;

  while (rules != NULL)
  {
   /*
    * Compute the result of this rule...
    */

    switch (rules->op)
    {
      case MIME_MAGIC_MATCH :
          result = mime_patmatch(filename, rules->value.matchv);
	  break;

      case MIME_MAGIC_ASCII :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->length) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ASCII fb->length=%d", fb->length));
	  }

         /*
	  * Test for ASCII printable characters plus standard control chars.
	  */

	  if ((rules->offset + rules->length) > (fb->offset + fb->length))
	    n = fb->offset + fb->length - rules->offset;
	  else
	    n = rules->length;

          bufptr = fb->buffer + rules->offset - fb->offset;
	  while (n > 0)
	    if ((*bufptr >= 32 && *bufptr <= 126) ||
	        (*bufptr >= 8 && *bufptr <= 13) ||
		*bufptr == 26 || *bufptr == 27)
	    {
	      n --;
	      bufptr ++;
	    }
	    else
	      break;

	  result = (n == 0);
	  break;

      case MIME_MAGIC_PRINTABLE :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->length) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_PRINTABLE fb->length=%d", fb->length));
	  }

         /*
	  * Test for 8-bit printable characters plus standard control chars.
	  */

	  if ((rules->offset + rules->length) > (fb->offset + fb->length))
	    n = fb->offset + fb->length - rules->offset;
	  else
	    n = rules->length;

          bufptr = fb->buffer + rules->offset - fb->offset;

	  while (n > 0)
	    if (*bufptr >= 128 ||
	        (*bufptr >= 32 && *bufptr <= 126) ||
	        (*bufptr >= 8 && *bufptr <= 13) ||
		*bufptr == 26 || *bufptr == 27)
	    {
	      n --;
	      bufptr ++;
	    }
	    else
	      break;

	  result = (n == 0);
	  break;

      case MIME_MAGIC_REGEX :
          DEBUG_printf(("5mime_check_rules: regex(%d, \"%s\")", rules->offset,
	                rules->value.stringv));

         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->length) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_REGEX fb->length=%d", fb->length));

            DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
	                  "with \"%c%c%c%c\".",
	                  fb->length, fb->offset, fb->buffer[0], fb->buffer[1],
			  fb->buffer[2], fb->buffer[3]));
	  }

         /*
	  * Compare the buffer against the string.  If the file is too
	  * short then don't compare - it can't match...
	  */

          if (fb->length > 0)
          {
            char temp[MIME_MAX_BUFFER + 1];
					/* Temporary buffer */

            memcpy(temp, fb->buffer, (size_t)fb->length);
            temp[fb->length] = '\0';
            result = !regexec(&(rules->value.rev), temp, 0, NULL, 0);
          }

          DEBUG_printf(("5mime_check_rules: result=%d", result));
	  break;

      case MIME_MAGIC_STRING :
          DEBUG_printf(("5mime_check_rules: string(%d, \"%s\")", rules->offset,
	                rules->value.stringv));

         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->length) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_STRING fb->length=%d", fb->length));

            DEBUG_printf(("5mime_check_rules: loaded %d byte fb->buffer at %d, starts "
	                  "with \"%c%c%c%c\".",
	                  fb->length, fb->offset, fb->buffer[0], fb->buffer[1],
			  fb->buffer[2], fb->buffer[3]));
	  }

         /*
	  * Compare the buffer against the string.  If the file is too
	  * short then don't compare - it can't match...
	  */

	  if ((rules->offset + rules->length) > (fb->offset + fb->length))
	    result = 0;
	  else
            result = !memcmp(fb->buffer + rules->offset - fb->offset, rules->value.stringv, (size_t)rules->length);
          DEBUG_printf(("5mime_check_rules: result=%d", result));
	  break;

      case MIME_MAGIC_ISTRING :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->length) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_ISTRING fb->length=%d", fb->length));
	  }

         /*
	  * Compare the buffer against the string.  If the file is too
	  * short then don't compare - it can't match...
	  */

	  if ((rules->offset + rules->length) > (fb->offset + fb->length))
	    result = 0;
	  else
            result = !_cups_strncasecmp((char *)fb->buffer + rules->offset - fb->offset, rules->value.stringv, (size_t)rules->length);
	  break;

      case MIME_MAGIC_CHAR :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset)
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CHAR fb->length=%d", fb->length));
	  }

	 /*
	  * Compare the character values; if the file is too short, it
	  * can't match...
	  */

	  if (fb->length < 1)
	    result = 0;
	  else
	    result = (fb->buffer[rules->offset - fb->offset] ==
	                  rules->value.charv);
	  break;

      case MIME_MAGIC_SHORT :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + 2) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_SHORT fb->length=%d", fb->length));
	  }

	 /*
	  * Compare the short values; if the file is too short, it
	  * can't match...
	  */

	  if (fb->length < 2)
	    result = 0;
	  else
	  {
	    bufptr = fb->buffer + rules->offset - fb->offset;
	    shortv = (short)((bufptr[0] << 8) | bufptr[1]);
	    result = (shortv == rules->value.shortv);
	  }
	  break;

      case MIME_MAGIC_INT :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + 4) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_INT fb->length=%d", fb->length));
	  }

	 /*
	  * Compare the int values; if the file is too short, it
	  * can't match...
	  */

	  if (fb->length < 4)
	    result = 0;
	  else
	  {
	    bufptr = fb->buffer + rules->offset - fb->offset;
	    intv   = (unsigned)((((((bufptr[0] << 8) | bufptr[1]) << 8) | bufptr[2]) << 8) | bufptr[3]);
	    result = (intv == rules->value.intv);
	  }
	  break;

      case MIME_MAGIC_LOCALE :
#if defined(_WIN32) || defined(__EMX__) || defined(__APPLE__)
          result = !strcmp(rules->value.localev, setlocale(LC_ALL, ""));
#else
          result = !strcmp(rules->value.localev, setlocale(LC_MESSAGES, ""));
#endif /* __APPLE__ */
	  break;

      case MIME_MAGIC_CONTAINS :
         /*
	  * Load the buffer if necessary...
	  */

          if (fb->offset < 0 || rules->offset < fb->offset ||
	      (rules->offset + rules->region) > (fb->offset + fb->length))
	  {
	   /*
	    * Reload file buffer...
	    */

            cupsFileSeek(fb->fp, rules->offset);
	    fb->length = cupsFileRead(fb->fp, (char *)fb->buffer,
	                              sizeof(fb->buffer));
	    fb->offset = rules->offset;

	    DEBUG_printf(("4mime_check_rules: MIME_MAGIC_CONTAINS fb->length=%d", fb->length));
	  }

         /*
	  * Compare the buffer against the string.  If the file is too
	  * short then don't compare - it can't match...
	  */

	  if ((rules->offset + rules->length) > (fb->offset + fb->length))
	    result = 0;
	  else
	  {
	    if (fb->length > rules->region)
	      region = rules->region - rules->length;
	    else
	      region = fb->length - rules->length;

	    for (n = 0; n < region; n ++)
	      if ((result = (memcmp(fb->buffer + rules->offset - fb->offset + n, rules->value.stringv, (size_t)rules->length) == 0)) != 0)
		break;
          }
	  break;

      default :
          if (rules->child != NULL)
	    result = mime_check_rules(filename, fb, rules->child);
	  else
	    result = 0;
	  break;
    }

   /*
    * If the logic is inverted, invert the result...
    */

    if (rules->invert)
      result = !result;

   /*
    * OK, now if the current logic is OR and this result is true, the this
    * rule set is true.  If the current logic is AND and this result is false,
    * the the rule set is false...
    */

    DEBUG_printf(("5mime_check_rules: result of test %p (MIME_MAGIC_%s) is %d",
                  rules, debug_ops[rules->op], result));

    if ((result && logic == MIME_MAGIC_OR) ||
        (!result && logic == MIME_MAGIC_AND))
      return (result);

   /*
    * Otherwise the jury is still out on this one, so move to the next rule.
    */

    rules = rules->next;
  }

  return (result);
}


/*
 * 'mime_patmatch()' - Pattern matching.
 */

static int				/* O - 1 if match, 0 if no match */
mime_patmatch(const char *s,		/* I - String to match against */
              const char *pat)		/* I - Pattern to match against */
{
 /*
  * Range check the input...
  */

  if (s == NULL || pat == NULL)
    return (0);

 /*
  * Loop through the pattern and match strings, and stop if we come to a
  * point where the strings don't match or we find a complete match.
  */

  while (*s != '\0' && *pat != '\0')
  {
    if (*pat == '*')
    {
     /*
      * Wildcard - 0 or more characters...
      */

      pat ++;
      if (*pat == '\0')
        return (1);	/* Last pattern char is *, so everything matches... */

     /*
      * Test all remaining combinations until we get to the end of the string.
      */

      while (*s != '\0')
      {
        if (mime_patmatch(s, pat))
	  return (1);

	s ++;
      }
    }
    else if (*pat == '?')
    {
     /*
      * Wildcard - 1 character...
      */

      pat ++;
      s ++;
      continue;
    }
    else if (*pat == '[')
    {
     /*
      * Match a character from the input set [chars]...
      */

      pat ++;
      while (*pat != ']' && *pat != '\0')
        if (*s == *pat)
	  break;
	else
	  pat ++;

      if (*pat == ']' || *pat == '\0')
        return (0);

      while (*pat != ']' && *pat != '\0')
        pat ++;

      if (*pat == ']')
        pat ++;

      continue;
    }
    else if (*pat == '\\')
    {
     /*
      * Handle quoted characters...
      */

      pat ++;
    }

   /*
    * Stop if the pattern and string don't match...
    */

    if (*pat++ != *s++)
      return (0);
  }

 /*
  * Done parsing the pattern and string; return 1 if the last character
  * matches and 0 otherwise...
  */

  return (*s == *pat);
}