src/perl-mmagic-xs.c

filter: Statementstmt 62.6% 407 / 650 Branchbran 46.4% 171 / 368 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 56.7% 578 / 1018 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/MMagicST.c ↑ Summary src/MMagic.xs »
1 /*
2 * Daisuke Maki <dmaki@cpan.org>
3 * All rights reserved.
4 *
5 * This is a complete port of the apache module mod_mime_magic.
6 * This is based on httpd-2.0.52's mod_mime_magic.c -- portions of this
7 * code was shamelessly borrowed from there.
8 *
9 * fmm_mime_magic(file)
10 * -> fsmagic(file)
11 * -> read HOWMANY bytes
12 * -> apply softmagic(buf)
13 * -> apply ascmagic(buf)
14 *
15 * fmm_append_buf -> appends raw string to a buffer
16 * fmm_append_mime -> appends mime string
17 *
18 */
19  
20 /* Copyright 1999-2004 The Apache Software Foundation
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34  
35 /*
36 * mod_mime_magic: MIME type lookup via file magic numbers
37 * Copyright (c) 1996-1997 Cisco Systems, Inc.
38 *
39 * This software was submitted by Cisco Systems to the Apache Software Foundation in July
40 * 1997. Future revisions and derivatives of this source code must
41 * acknowledge Cisco Systems as the original contributor of this module.
42 * All other licensing and usage conditions are those of the Apache Software Foundation.
43 *
44 * Some of this code is derived from the free version of the file command
45 * originally posted to comp.sources.unix. Copyright info for that program
46 * is included below as required.
47 * ---------------------------------------------------------------------------
48 * - Copyright (c) Ian F. Darwin, 1987. Written by Ian F. Darwin.
49 *
50 * This software is not subject to any license of the American Telephone and
51 * Telegraph Company or of the Regents of the University of California.
52 *
53 * Permission is granted to anyone to use this software for any purpose on any
54 * computer system, and to alter it and redistribute it freely, subject to
55 * the following restrictions:
56 *
57 * 1. The author is not responsible for the consequences of use of this
58 * software, no matter how awful, even if they arise from flaws in it.
59 *
60 * 2. origin of this software must not be misrepresented, either by
61 * explicit claim or by omission. Since few users ever read sources, credits
62 * must appear in the documentation.
63 *
64 * 3. Altered versions must be plainly marked as such, and must not be
65 * misrepresented as being the original software. Since few users ever read
66 * sources, credits must appear in the documentation.
67 *
68 * 4. This notice may not be removed or altered.
69 * -------------------------------------------------------------------------
70 *
71 * For compliance with Mr Darwin's terms: this has been very significantly
72 * modified from the free "file" command.
73 * - all-in-one file for compilation convenience when moving from one
74 * version of Apache to the next.
75 * - Memory allocation is done through the Apache API's apr_pool_t structure.
76 * - All functions have had necessary Apache API request or server
77 * structures passed to them where necessary to call other Apache API
78 * routines. (i.e. usually for logging, files, or memory allocation in
79 * itself or a called function.)
80 * - struct magic has been converted from an array to a single-ended linked
81 * list because it only grows one record at a time, it's only accessed
82 * sequentially, and the Apache API has no equivalent of realloc().
83 * - Functions have been changed to get their parameters from the server
84 * configuration instead of globals. (It should be reentrant now but has
85 * not been tested in a threaded environment.)
86 * - Places where it used to print results to stdout now saves them in a
87 * list where they're used to set the MIME type in the Apache request
88 * record.
89 * - Command-line flags have been removed since they will never be used here.
90 *
91 * Ian Kluft <ikluft@cisco.com>
92 * Engineering Information Framework
93 * Central Engineering
94 * Cisco Systems, Inc.
95 * San Jose, CA, USA
96 *
97 * Initial installation July/August 1996
98 * Misc bug fixes May 1997
99 * Submission to Apache Software Foundation July 1997
100 *
101 */
102  
103 #ifndef __PERL_MMAGIC_XS_C__
104 #define __PERL_MMAGIC_XS_C__
105 #include "perl-mmagic-xs.h"
106  
107 /*
108 * data structures for tar file recognition
109 * --------------------------------------------------------------------------
110 * Header file for public domain tar (tape archive) program.
111 *
112 * @(#)tar.h 1.20 86/10/29 Public Domain. Created 25 August 1985 by John
113 * Gilmore, ihnp4!hoptoad!gnu.
114 *
115 * Header block on tape.
116 *
117 * I'm going to use traditional DP naming conventions here. A "block" is a big
118 * chunk of stuff that we do I/O on. A "record" is a piece of info that we
119 * care about. Typically many "record"s fit into a "block".
120 */
121 #define RECORDSIZE 512
122 #define NAMSIZ 100
123 #define TUNMLEN 32
124 #define TGNMLEN 32
125  
126 union record {
127 char charptr[RECORDSIZE];
128 struct header {
129 char name[NAMSIZ];
130 char mode[8];
131 char uid[8];
132 char gid[8];
133 char size[12];
134 char mtime[12];
135 char chksum[8];
136 char linkflag;
137 char linkname[NAMSIZ];
138 char magic[8];
139 char uname[TUNMLEN];
140 char gname[TGNMLEN];
141 char devmajor[8];
142 char devminor[8];
143 } header;
144 };
145  
146 /* The magic field is filled with this if uname and gname are valid. */
147 #define TMAGIC "ustar " /* 7 chars and a null */
148  
149 /*
150 * includes for ASCII substring recognition formerly "names.h" in file
151 * command
152 *
153 * Original notes: names and types used by ascmagic in file(1). These tokens are
154 * here because they can appear anywhere in the first HOWMANY bytes, while
155 * tokens in /etc/magic must appear at fixed offsets into the file. Don't
156 * make HOWMANY too high unless you have a very fast CPU.
157 */
158  
159 /* these types are used to index the apr_table_t 'types': keep em in sync! */
160 /* HTML inserted in first because this is a web server module now */
161 #define L_HTML 0 /* HTML */
162 #define L_C 1 /* first and foremost on UNIX */
163 #define L_FORT 2 /* the oldest one */
164 #define L_MAKE 3 /* Makefiles */
165 #define L_PLI 4 /* PL/1 */
166 #define L_MACH 5 /* some kinda assembler */
167 #define L_ENG 6 /* English */
168 #define L_PAS 7 /* Pascal */
169 #define L_MAIL 8 /* Electronic mail */
170 #define L_NEWS 9 /* Usenet Netnews */
171  
172 static char *types[] =
173 {
174 "text/html", /* HTML */
175 "text/plain", /* "c program text", */
176 "text/plain", /* "fortran program text", */
177 "text/plain", /* "make commands text", */
178 "text/plain", /* "pl/1 program text", */
179 "text/plain", /* "assembler program text", */
180 "text/plain", /* "English text", */
181 "text/plain", /* "pascal program text", */
182 "message/rfc822", /* "mail text", */
183 "message/news", /* "news text", */
184 "application/binary", /* "can't happen error on names.h/types", */
185 0
186 };
187  
188 static struct names {
189 char *name;
190 short type;
191 } names[] = {
192  
193 /* These must be sorted by eye for optimal hit rate */
194 /* Add to this list only after substantial meditation */
195 {
196 "<html>", L_HTML
197 },
198 {
199 "<HTML>", L_HTML
200 },
201 {
202 "<head>", L_HTML
203 },
204 {
205 "<HEAD>", L_HTML
206 },
207 {
208 "<title>", L_HTML
209 },
210 {
211 "<TITLE>", L_HTML
212 },
213 {
214 "<h1>", L_HTML
215 },
216 {
217 "<H1>", L_HTML
218 },
219 {
220 "<!--", L_HTML
221 },
222 {
223 "<!DOCTYPE HTML", L_HTML
224 },
225 {
226 "</html>", L_HTML
227 },
228 {
229 "/*", L_C
230 }, /* must precede "The", "the", etc. */
231 {
232 "#include", L_C
233 },
234 {
235 "char", L_C
236 },
237 {
238 "The", L_ENG
239 },
240 {
241 "the", L_ENG
242 },
243 {
244 "double", L_C
245 },
246 {
247 "extern", L_C
248 },
249 {
250 "float", L_C
251 },
252 {
253 "real", L_C
254 },
255 {
256 "struct", L_C
257 },
258 {
259 "union", L_C
260 },
261 {
262 "CFLAGS", L_MAKE
263 },
264 {
265 "LDFLAGS", L_MAKE
266 },
267 {
268 "all:", L_MAKE
269 },
270 {
271 ".PRECIOUS", L_MAKE
272 },
273 /*
274 * Too many files of text have these words in them. Find another way to
275 * recognize Fortrash.
276 */
277 #ifdef NOTDEF
278 {
279 "subroutine", L_FORT
280 },
281 {
282 "function", L_FORT
283 },
284 {
285 "block", L_FORT
286 },
287 {
288 "common", L_FORT
289 },
290 {
291 "dimension", L_FORT
292 },
293 {
294 "integer", L_FORT
295 },
296 {
297 "data", L_FORT
298 },
299 #endif /* NOTDEF */
300 {
301 ".ascii", L_MACH
302 },
303 {
304 ".asciiz", L_MACH
305 },
306 {
307 ".byte", L_MACH
308 },
309 {
310 ".even", L_MACH
311 },
312 {
313 ".globl", L_MACH
314 },
315 {
316 "clr", L_MACH
317 },
318 {
319 "(input,", L_PAS
320 },
321 {
322 "dcl", L_PLI
323 },
324 {
325 "Received:", L_MAIL
326 },
327 {
328 ">From", L_MAIL
329 },
330 {
331 "Return-Path:", L_MAIL
332 },
333 {
334 "Cc:", L_MAIL
335 },
336 {
337 "Newsgroups:", L_NEWS
338 },
339 {
340 "Path:", L_NEWS
341 },
342 {
343 "Organization:", L_NEWS
344 },
345 {
346 NULL, 0
347 }
348 };
349  
350 #define NNAMES ((sizeof(names)/sizeof(struct names)) - 1)
351  
352 /* append string to an existing buffer, using printf fashion */
353 /* Will refuse to append anything after MAXMIMESTRING into dst*/
354 static void
355 43 fmm_append_buf(PerlFMM *state, char **dst, char *str, ...)
356 {
357 va_list ap;
358 char buf[MAXMIMESTRING];
359 SV *err;
360  
361 43 strcpy( buf, str );
362
363 43 va_start(ap, str);
364 43 vsnprintf(buf, sizeof(buf), str, ap);
365 43 va_end(ap);
366  
367 43 ▶ if (strlen(buf) + 1 > MAXMIMESTRING - strlen(*dst)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(buf) + 1 > MAXMIMESTRING - strlen(*dst)) { 0 43 43
368 0 err = newSVpv("detected truncation in fmm_append_buf. refusing to append", 0);
369 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
370 0 return;
371 }
372 #ifdef FMM_DEBUG
373 PerlIO_printf(PerlIO_stderr(), "dst = %s, buf = %s\n", *dst, buf);
374 #endif
375 43 strncat(*dst, buf, strlen(buf));
376 }
377  
378 /* APR_CTIME_LEN is defined in apr_time.h */
379 #define CTIME_LEN 25
380 #define CTIME_FMT "%a %b %d %H:%M:%S %Y"
381  
382 /*
383 * Convert the byte order of the data we are looking at
384 */
385 static int
386 10393 fmm_mconvert(PerlFMM *state, union VALUETYPE *p, fmmagic *m)
387 {
388 char *rt;
389 SV *err;
390  
391 10393 switch (m->type) {
392 312 case BYTE:
393 case SHORT:
394 case LONG:
395 case DATE:
396 312 return 1;
397 8617 case STRING:
398 /* Null terminate and eat the return */
399 8617 p->s[sizeof(p->s) - 1] = '\0';
400 8617 ▶ if ((rt = strchr(p->s, '\n')) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if ((rt = strchr(p->s, '\n')) != NULL) 7724 893 8617
401 7724 *rt = '\0';
402 8617 return 1;
403 408 case BESHORT:
404 408 p->h = (short) ((p->hs[0] << 8) | (p->hs[1]));
405 408 return 1;
406 398 case BELONG:
407 case BEDATE:
408 398 p->l = (long)
409 398 ((p->hl[0] << 24) | (p->hl[1] << 16) | (p->hl[2] << 8) | (p->hl[3]));
410 398 return 1;
411 134 case LESHORT:
412 134 p->h = (short) ((p->hs[1] << 8) | (p->hs[0]));
413 134 return 1;
414 524 case LELONG:
415 case LEDATE:
416 524 p->l = (long)
417 524 ((p->hl[3] << 24) | (p->hl[2] << 16) | (p->hl[1] << 8) | (p->hl[0]));
418 524 return 1;
419 0 default:
420 0 err = newSVpvf(
421 "fmm_mconvert : invalid type %d in mconvert().",
422 m->type
423 );
424 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
425 0 return 0;
426 }
427 }
428  
429 static int
430 10435 fmm_mget(PerlFMM *state, union VALUETYPE *p, unsigned char *s, fmmagic *m, size_t nbytes)
431 {
432 10435 long offset = m->offset;
433  
434 10435 ▶ if (offset + sizeof(union VALUETYPE) > nbytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset + sizeof(union VALUETYPE) > nbytes) { 42 10393 10435
435 42 return 0;
436 }
437  
438 10393 memcpy(p, s + offset, sizeof(union VALUETYPE));
439  
440 10393 ▶ if (!fmm_mconvert(state, p, m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fmm_mconvert(state, p, m)) { 0 10393 10393
441 0 return 0;
442 }
443  
444 10393 ▶ if (m->flag & INDIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->flag & INDIR) { 0 10393 10393
445 0 switch (m->in.type) {
446 0 case BYTE:
447 0 offset = p->b + m->in.offset;
448 0 break;
449 0 case SHORT:
450 0 offset = p->h + m->in.offset;
451 0 break;
452 0 case LONG:
453 0 offset = p->l + m->in.offset;
454 0 break;
455 }
456
457 0 ▶ if (offset + sizeof(union VALUETYPE) > nbytes)
Branch: 1 branch
BranchTrueFalseTotal
if (offset + sizeof(union VALUETYPE) > nbytes) 0 0 0
458 0 return 0;
459
460 0 memcpy(p, s + offset, sizeof(union VALUETYPE));
461
462 0 ▶ if (!fmm_mconvert(state, p, m)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fmm_mconvert(state, p, m)) { 0 0 0
463 0 return 0;
464 }
465 }
466  
467 10393 return 1;
468 }
469  
470 #define isODIGIT(c) (((unsigned char)(c) >= '0') && ((unsigned char)(c) <= '7'))
471  
472 /*
473 * Quick and dirty octal conversion.
474 *
475 * Result is -1 if the field is invalid (all blank, or nonoctal).
476 */
477 static long
478 21 from_oct(int digs, char *where)
479 {
480 register long value;
481  
482 21 ▶ while (isSPACE(*where)) { /* Skip spaces */
Branch: 1 branch
BranchTrueFalseTotal
while (isSPACE(*where)) { /* Skip spaces */ 0 21 21
483 0 where++;
484 0 ▶ if (--digs <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (--digs <= 0) 0 0 0
485 0 return -1; /* All blank field */
486 }
487 21 value = 0;
488 21 ▶ while (digs > 0 && isODIGIT(*where)) { /* Scan til nonoctal */
Branch: 3 branches
BranchTrueFalseTotal
while (digs > 0 && isODIGIT(*where)) { /* Scan til nonoctal */ 21 0 21
while (digs > 0 && isODIGIT(*where)) { /* Scan til nonoctal */ 1 20 21
while (digs > 0 && isODIGIT(*where)) { /* Scan til nonoctal */ 0 1 1
489 0 value = (value << 3) | (*where++ - '0');
490 0 --digs;
491 }
492  
493 21 ▶ if (digs > 0 && *where && !isSPACE(*where))
Branch: 3 branches
BranchTrueFalseTotal
if (digs > 0 && *where && !isSPACE(*where)) 21 0 21
if (digs > 0 && *where && !isSPACE(*where)) 21 0 21
if (digs > 0 && *where && !isSPACE(*where)) 21 0 21
494 21 return -1; /* Ended on non-space/nul */
495  
496 0 return value;
497 }
498  
499 /*
500 * is_tar() -- figure out whether file is a tar archive.
501 *
502 * Stolen (by author of file utility) from the public domain tar program: Public
503 * Domain version written 26 Aug 1985 John Gilmore (ihnp4!hoptoad!gnu).
504 *
505 * @(#)list.c 1.18 9/23/86 Public Domain - gnu $Id: mod_mime_magic.c,v 1.7
506 * 1997/06/24 00:41:02 ikluft Exp ikluft $
507 *
508 * Comments changed and some code/comments reformatted for file command by Ian
509 * Darwin.
510 */
511  
512  
513 /*
514 * Return 0 if the checksum is bad (i.e., probably not a tar archive), 1 for
515 * old UNIX tar file, 2 for Unix Std (POSIX) tar file.
516 */
517  
518 static int
519 21 is_tar(unsigned char *buf, size_t nbytes)
520 {
521 21 register union record *header = (union record *) buf;
522 register int i;
523 register long sum, recsum;
524 register char *p;
525  
526 21 ▶ if (nbytes < sizeof(union record))
Branch: 1 branch
BranchTrueFalseTotal
if (nbytes < sizeof(union record)) 0 21 21
527 0 return 0;
528  
529 21 recsum = from_oct(8, header->header.chksum);
530 21 sum = 0;
531 21 p = header->charptr;
532 10773 ▶ for (i = sizeof(union record); --i >= 0;) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = sizeof(union record); --i >= 0;) { 10752 21 10773
533 /*
534 * We can't use unsigned char here because of old compilers, e.g. V7.
535 */
536 10752 sum += 0xFF & *p++;
537 }
538  
539 /* Adjust checksum to count the "chksum" field as blanks. */
540 189 ▶ for (i = sizeof(header->header.chksum); --i >= 0;)
Branch: 1 branch
BranchTrueFalseTotal
for (i = sizeof(header->header.chksum); --i >= 0;) 168 21 189
541 168 sum -= 0xFF & header->header.chksum[i];
542 21 sum += ' ' * sizeof header->header.chksum;
543  
544 21 ▶ if (sum != recsum)
Branch: 1 branch
BranchTrueFalseTotal
if (sum != recsum) 21 0 21
545 21 return 0; /* Not a tar archive */
546  
547 0 ▶ if (0 == strcmp(header->header.magic, TMAGIC))
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strcmp(header->header.magic, TMAGIC)) 0 0 0
548 0 return 2; /* Unix Standard tar archive */
549  
550 0 return 1; /* Old fashioned tar archive */
551 }
552  
553 /*
554 * extend the sign bit if the comparison is to be signed
555 */
556 static unsigned long
557 11243 fmm_signextend(PerlFMM *state, fmmagic *m, unsigned long v)
558 {
559 SV *err;
560  
561 11243 ▶ if (!(m->flag & UNSIGNED))
Branch: 1 branch
BranchTrueFalseTotal
if (!(m->flag & UNSIGNED)) 11243 0 11243
562 11243 switch (m->type) {
563 /*
564 * Do not remove the casts below. They are vital. When later
565 * compared with the data, the sign extension must have happened.
566 */
567 120 case BYTE:
568 120 v = (char) v;
569 120 break;
570 1104 case SHORT:
571 case BESHORT:
572 case LESHORT:
573 1104 v = (short) v;
574 1104 break;
575 1402 case DATE:
576 case BEDATE:
577 case LEDATE:
578 case LONG:
579 case BELONG:
580 case LELONG:
581 1402 v = (long) v;
582 1402 break;
583 8617 case STRING:
584 8617 break;
585 0 default:
586 0 err = newSVpvf(
587 "fmm_signextend: can't happen: m->type=%d\n", m->type);
588 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
589 0 return -1;
590 }
591 11243 return v;
592 }
593  
594 static void
595 43 fmm_append_mime(PerlFMM *state, char **buf, union VALUETYPE *p, fmmagic *m)
596 {
597 char *pp;
598 unsigned long v;
599 char *time_str;
600 SV *err;
601  
602 #ifdef FMM_DEBUG
603 PerlIO_printf(PerlIO_stderr(), "fmm_append_mime: buf = %s\n", buf);
604 #endif
605 43 switch (m->type) {
606 0 case BYTE:
607 0 v = p->b;
608 0 break;
609 2 case SHORT:
610 case BESHORT:
611 case LESHORT:
612 2 v = p->h;
613 2 break;
614 41 case STRING:
615 41 ▶ if (m->reln == '=') {
Branch: 1 branch
BranchTrueFalseTotal
if (m->reln == '=') { 41 0 41
616 41 fmm_append_buf(state, buf, m->desc, m->value.s );
617 } else {
618 0 fmm_append_buf(state, buf, m->desc, p->s);
619 }
620 41 return;
621 0 case DATE:
622 case BEDATE:
623 case LEDATE:
624 0 Newz(1234, time_str, CTIME_LEN, char);
625 0 strftime(time_str, CTIME_LEN, CTIME_FMT,
626 0 localtime((const time_t *) &p->l));
627 0 pp = time_str;
628 0 fmm_append_buf(state, buf, m->desc, pp);
629 0 Safefree(time_str);
630 0 return;
631 0 default:
632 0 err = newSVpvf(
633 "fmm_append_mime: invalud m->type (%d) in fmm_append_mime().\n", m->type);
634 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
635 0 return;
636 }
637  
638 2 v = fmm_signextend(state, m, v) & m->mask;
639 2 fmm_append_buf(state, buf, m->desc, (unsigned long) v);
640 }
641  
642 static int
643 10393 fmm_mcheck(PerlFMM *state, union VALUETYPE *p, fmmagic *m)
644 {
645 10393 register unsigned long l = m->value.l;
646 register unsigned long v;
647 register unsigned char *a;
648 register unsigned char *b;
649 register int len;
650 int matched;
651 SV *err;
652  
653 10393 ▶ if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
Branch: 2 branches
BranchTrueFalseTotal
if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) { 24 10369 10393
if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) { 0 24 24
654 /* XXX - WTF does this mean?? */
655 0 PerlIO_printf(PerlIO_stderr(), "fmm_mcheck: BOINK\n");
656 0 return 1;
657 }
658  
659 10393 switch (m->type) {
660 48 case BYTE:
661 48 v = p->b;
662 48 break;
663 806 case SHORT:
664 case BESHORT:
665 case LESHORT:
666 806 v = p->h;
667 806 break;
668 922 case LONG:
669 case BELONG:
670 case LELONG:
671 case DATE:
672 case BEDATE:
673 case LEDATE:
674 922 v = p->l;
675 922 break;
676 8617 case STRING:
677 8617 l = 0;
678 /* What we want here is: v = strncmp(m->value.s, p->s, m->vallen)
679 * but ignoring any nulls. bcmp doesn't give -/+/0 and isn't
680 * universally available anyway
681 */
682 8617 v = 0;
683 {
684 8617 a = (unsigned char *) m->value.s;
685 8617 b = (unsigned char *) p->s;
686 8617 len = m->vallen;
687  
688 10669 ▶ while (--len >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (--len >= 0) { 10628 41 10669
689 10628 ▶ if ((v = *b++ - *a++) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = *b++ - *a++) != 0) { 8576 2052 10628
690 8576 break;
691 }
692 }
693 }
694 8617 break;
695 0 default:
696 /* bogosity, pretend that it just wan't a match*/
697 0 err = newSVpvf(
698 "fmm_mcheck: invalid type %d in mcheck().\n", m->type);
699 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
700 0 return 0;
701 }
702  
703 10393 v = fmm_signextend(state, m, v) & m->mask;
704  
705 10393 switch (m->reln) {
706 0 case 'x':
707 0 matched = 1;
708 0 break;
709 0 case '!':
710 0 matched = v != l;
711 0 break;
712 10393 case '=':
713 10393 matched = v == l;
714 10393 break;
715 0 case '>':
716 0 ▶ if (m->flag & UNSIGNED) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->flag & UNSIGNED) { 0 0 0
717 0 matched = v > l;
718 } else {
719 0 matched = (long) v > (long) l;
720 }
721 0 break;
722 0 case '<':
723 0 ▶ if (m->flag & UNSIGNED) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->flag & UNSIGNED) { 0 0 0
724 0 matched = v < l;
725 } else {
726 0 matched = (long) v < (long) l;
727 }
728 0 break;
729 0 case '&':
730 0 matched = (v & l) == l;
731 0 break;
732 0 case '^':
733 0 matched = (v & l) != l;
734 0 break;
735 0 default:
736 /* bogosity, pretend it didn't match */
737 0 matched = 0;
738 0 err = newSVpvf(
739 "fmm_mcheck: Can't happen: invalid relation %d.\n", m->reln);
740 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
741 }
742 10393 return matched;
743 }
744  
745  
746  
747 /* Single hex char to int; -1 if not a hex char. */
748 static int
749 72 fmm_hextoint(int c)
750 {
751 72 ▶ if (isDIGIT(c))
Branch: 1 branch
BranchTrueFalseTotal
if (isDIGIT(c)) 32 40 72
752 32 return c - '0';
753 40 ▶ if ((c >= 'a') && (c <= 'f'))
Branch: 2 branches
BranchTrueFalseTotal
if ((c >= 'a') && (c <= 'f')) 16 24 40
if ((c >= 'a') && (c <= 'f')) 16 0 16
754 16 return c + 10 - 'a';
755 24 ▶ if ((c >= 'A') && (c <= 'F'))
Branch: 2 branches
BranchTrueFalseTotal
if ((c >= 'A') && (c <= 'F')) 24 0 24
if ((c >= 'A') && (c <= 'F')) 0 24 24
756 0 return c + 10 - 'A';
757 24 return -1;
758 }
759  
760 /*
761 * Convert a string containing C character escapes. Stop at an unescaped
762 * space or tab. Copy the converted version to "p", returning its length in
763 * *slen. Return updated scan pointer as function result.
764 */
765 static char *
766 1931 fmm_getstr(PerlFMM *state, register char *s, register char *p, int plen, int *slen)
767 {
768 1931 char *origs = s, *origp = p;
769 1931 char *pmax = p + plen - 1;
770 register int c;
771 register int val;
772 SV *err;
773  
774 18164 ▶ while ((c = *s++) != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
while ((c = *s++) != '\0') { 18060 104 18164
775 18060 ▶ if (isSPACE(c))
Branch: 2 branches
BranchTrueFalseTotal
if (isSPACE(c)) 18060 0 18060
if (isSPACE(c)) 1827 16233 18060
776 1827 break;
777 16233 ▶ if (p >= pmax) {
Branch: 1 branch
BranchTrueFalseTotal
if (p >= pmax) { 0 16233 16233
778 0 err = newSVpvf(
779 "fmm_getstr: string too long: %s", origs);
780 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
781 0 break;
782 }
783 16233 ▶ if (c == '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\\') { 1499 14734 16233
784 1499 switch (c = *s++) {
785  
786 0 case '\0':
787 0 goto out;
788  
789 819 default:
790 819 *p++ = (char) c;
791 819 break;
792  
793 8 case 'n':
794 8 *p++ = '\n';
795 8 break;
796  
797 8 case 'r':
798 8 *p++ = '\r';
799 8 break;
800  
801 0 case 'b':
802 0 *p++ = '\b';
803 0 break;
804  
805 0 case 't':
806 0 *p++ = '\t';
807 0 break;
808  
809
810 0 case 'f':
811 0 *p++ = '\f';
812 0 break;
813
814 0 case 'v':
815 0 *p++ = '\v';
816 0 break;
817
818 /* \ and up to 3 octal digits */
819 640 case '0':
820 case '1':
821 case '2':
822 case '3':
823 case '4':
824 case '5':
825 case '6':
826 case '7':
827 640 val = c - '0';
828 640 c = *s++; /* try for 2 */
829 640 ▶ if (c >= '0' && c <= '7') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '7') { 608 32 640
if (c >= '0' && c <= '7') { 496 112 608
830 496 val = (val << 3) | (c - '0');
831 496 c = *s++; /* try for 3 */
832 496 ▶ if (c >= '0' && c <= '7')
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '7') 496 0 496
if (c >= '0' && c <= '7') 480 16 496
833 480 val = (val << 3) | (c - '0');
834 else
835 16 --s;
836 }
837 else
838 144 --s;
839 640 *p++ = (char) val;
840 640 break;
841
842 /* \x and up to 3 hex digits */
843 24 case 'x':
844 24 val = 'x'; /* Default if no digits */
845 24 c = fmm_hextoint(*s++); /* Get next char */
846 24 ▶ if (c >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 0) { 24 0 24
847 24 val = c;
848 24 c = fmm_hextoint(*s++);
849 24 ▶ if (c >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 0) { 24 0 24
850 24 val = (val << 4) + c;
851 24 c = fmm_hextoint(*s++);
852 24 ▶ if (c >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c >= 0) { 0 24 24
853 0 val = (val << 4) + c;
854 }
855 else
856 24 --s;
857 }
858 else
859 0 --s;
860 }
861 else
862 0 --s;
863 24 *p++ = (char) val;
864 24 break;
865 }
866 }
867 else
868 14734 *p++ = (char) c;
869 }
870 104 out:
871 1931 *p = '\0';
872 1931 *slen = p - origp;
873 1931 return s;
874 }
875  
876 /*
877 * Read a numeric value from a pointer, into the value union of a magic
878 * pointer, according to the magic type. Update the string pointer to point
879 * just after the number read. Return 0 for success, non-zero for failure.
880 */
881 2643 static int fmm_getvalue(PerlFMM *state, fmmagic *m, char **p)
882 {
883 int slen;
884  
885 2643 ▶ if (m->type == STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->type == STRING) { 1931 712 2643
886 1931 *p = fmm_getstr(state, *p, m->value.s, sizeof(m->value.s), &slen);
887 1931 m->vallen = slen;
888 }
889 712 ▶ else if (m->reln != 'x')
Branch: 1 branch
BranchTrueFalseTotal
else if (m->reln != 'x') 712 0 712
890 712 m->value.l = fmm_signextend(state, m, strtol(*p, p, 0));
891 2643 return 0;
892 }
893  
894 /* maps to mod_mime_magic::parse */
895 static int
896 2651 fmm_parse_magic_line(PerlFMM *state, char *l, int lineno)
897 {
898 char *t;
899 char *s;
900 fmmagic *m;
901 SV *err;
902  
903 2651 Newz(1234, m, 1, fmmagic);
904 2651 m->next = NULL;
905 2651 m->flag = 0;
906 2651 m->cont_level = 0;
907 2651 m->lineno = lineno;
908  
909 2651 ▶ if (! state->magic || !state->last) {
Branch: 2 branches
BranchTrueFalseTotal
if (! state->magic || !state->last) { 2643 8 2651
if (! state->magic || !state->last) { 0 2643 2643
910 8 state->magic = state->last = m;
911 } else {
912 2643 state->last->next = m;
913 2643 state->last = m;
914 }
915
916 3115 ▶ while (*l == '>') {
Branch: 1 branch
BranchTrueFalseTotal
while (*l == '>') { 464 2651 3115
917 464 l++; /* step over */
918 464 m->cont_level++;
919 }
920  
921 2651 ▶ if (m->cont_level != 0 && *l == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (m->cont_level != 0 && *l == '(') { 464 2187 2651
if (m->cont_level != 0 && *l == '(') { 0 464 464
922 0 l++; /* step over */
923 0 m->flag |= INDIR;
924 }
925  
926 /* get offset, then skip over it */
927 2651 m->offset = (int) strtol(l, &t, 0);
928 2651 ▶ if (l == t) {
Branch: 1 branch
BranchTrueFalseTotal
if (l == t) { 0 2651 2651
929 0 err = newSVpvf("Invalid offset in mime magic file, line %d: %s", lineno, l);
930 0 goto error;
931 }
932  
933 2651 l = t;
934  
935 2651 ▶ if (m->flag & INDIR) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->flag & INDIR) { 0 2651 2651
936 0 m->in.type = LONG;
937 0 m->in.offset = 0;
938 /* read [.lbs][+=]nnnnn) */
939 0 ▶ if (*l == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (*l == '.') { 0 0 0
940 0 switch (*++l) {
941 0 case 'l':
942 0 m->in.type = LONG;
943 0 break;
944 0 case 's':
945 0 m->in.type = SHORT;
946 0 break;
947 0 case 'b':
948 0 m->in.type = BYTE;
949 0 break;
950 0 default:
951 0 err = newSVpvf(
952 "Invalid indirect offset type in mime magic file, line %d: %c", lineno, *l);
953 0 goto error;
954 }
955 0 l++;
956 }
957 0 s = l;
958 0 ▶ if (*l == '+' || *l == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (*l == '+' || *l == '-') { 0 0 0
if (*l == '+' || *l == '-') { 0 0 0
959 0 l++;
960 }
961 0 ▶ if (isdigit((unsigned char) *l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isdigit((unsigned char) *l)) { 0 0 0
962 0 m->in.offset = strtol(l, &t, 0);
963 0 ▶ if (*s == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '-') { 0 0 0
964 0 m->in.offset = -(m->in.offset);
965 }
966 } else {
967 0 t = l;
968 }
969 0 ▶ if (*t++ != ')') {
Branch: 1 branch
BranchTrueFalseTotal
if (*t++ != ')') { 0 0 0
970 0 err = newSVpvf(
971 "Missing ')' in indirect offset in mime magic file, line %d", lineno);
972 0 goto error;
973 }
974 0 l = t;
975 }
976  
977 2651 ▶ while (isdigit((unsigned char) *l)) {
Branch: 1 branch
BranchTrueFalseTotal
while (isdigit((unsigned char) *l)) { 0 2651 2651
978 0 ++l;
979 }
980 9118 ▶ EATAB(l);
Branch: 1 branch
BranchTrueFalseTotal
EATAB(l); 6467 2651 9118
981  
982 #define NBYTE 4
983 #define NSHORT 5
984 #define NLONG 4
985 #define NSTRING 6
986 #define NDATE 4
987 #define NBESHORT 7
988 #define NBELONG 6
989 #define NBEDATE 6
990 #define NLESHORT 7
991 #define NLELONG 6
992 #define NLEDATE 6
993  
994 2651 ▶ if (*l == 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if (*l == 'u') { 0 2651 2651
995 0 ++l;
996 0 m->flag |= UNSIGNED;
997 }
998  
999 /* get type, skip it */
1000 2651 ▶ if (strncmp(l, "byte", NBYTE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(l, "byte", NBYTE) == 0) { 72 2579 2651
1001 72 m->type = BYTE;
1002 72 l += NBYTE;
1003 }
1004 2579 ▶ else if (strncmp(l, "short", NSHORT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "short", NSHORT) == 0) { 56 2523 2579
1005 56 m->type = SHORT;
1006 56 l += NSHORT;
1007 }
1008 2523 ▶ else if (strncmp(l, "long", NLONG) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "long", NLONG) == 0) { 0 2523 2523
1009 0 m->type = LONG;
1010 0 l += NLONG;
1011 }
1012 2523 ▶ else if (strncmp(l, "string", NSTRING) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "string", NSTRING) == 0) { 1931 592 2523
1013 1931 m->type = STRING;
1014 1931 l += NSTRING;
1015 }
1016 592 ▶ else if (strncmp(l, "date", NDATE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "date", NDATE) == 0) { 0 592 592
1017 0 m->type = DATE;
1018 0 l += NDATE;
1019 }
1020 592 ▶ else if (strncmp(l, "beshort", NBESHORT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "beshort", NBESHORT) == 0) { 144 448 592
1021 144 m->type = BESHORT;
1022 144 l += NBESHORT;
1023 }
1024 448 ▶ else if (strncmp(l, "belong", NBELONG) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "belong", NBELONG) == 0) { 184 264 448
1025 184 m->type = BELONG;
1026 184 l += NBELONG;
1027 }
1028 264 ▶ else if (strncmp(l, "bedate", NBEDATE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "bedate", NBEDATE) == 0) { 0 264 264
1029 0 m->type = BEDATE;
1030 0 l += NBEDATE;
1031 }
1032 264 ▶ else if (strncmp(l, "leshort", NLESHORT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "leshort", NLESHORT) == 0) { 72 192 264
1033 72 m->type = LESHORT;
1034 72 l += NLESHORT;
1035 }
1036 192 ▶ else if (strncmp(l, "lelong", NLELONG) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "lelong", NLELONG) == 0) { 192 0 192
1037 192 m->type = LELONG;
1038 192 l += NLELONG;
1039 }
1040 0 ▶ else if (strncmp(l, "ledate", NLEDATE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
else if (strncmp(l, "ledate", NLEDATE) == 0) { 0 0 0
1041 0 m->type = LEDATE;
1042 0 l += NLEDATE;
1043 }
1044 else {
1045 0 err = newSVpvf("Invalid type in mime magic file, line %d: %s", lineno, l);
1046 0 goto error;
1047 }
1048 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
1049 2651 ▶ if (*l == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (*l == '&') { 136 2515 2651
1050 136 ++l;
1051 136 m->mask = fmm_signextend(state, m, strtol(l, &l, 0));
1052 }
1053 else {
1054 2515 m->mask = ~0L;
1055 }
1056 11334 ▶ EATAB(l);
Branch: 1 branch
BranchTrueFalseTotal
EATAB(l); 8683 2651 11334
1057  
1058 2651 switch (*l) {
1059 48 case '>':
1060 case '<':
1061 /* Old-style anding: "0 byte &0x80 dynamically linked" */
1062 case '&':
1063 case '^':
1064 case '=':
1065 48 m->reln = *l;
1066 48 ++l;
1067 48 break;
1068 0 case '!':
1069 0 ▶ if (m->type != STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->type != STRING) { 0 0 0
1070 0 m->reln = *l;
1071 0 ++l;
1072 0 break;
1073 }
1074 /* FALL THROUGH */
1075 default:
1076 2603 ▶ if (*l == 'x' && isSPACE(l[1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (*l == 'x' && isSPACE(l[1])) { 8 2595 2603
if (*l == 'x' && isSPACE(l[1])) { 8 0 8
1077 8 m->reln = *l;
1078 8 ++l;
1079 8 goto GetDesc; /* Bill The Cat */
1080 }
1081 2595 m->reln = '=';
1082 2595 break;
1083 }
1084 2643 ▶ EATAB(l);
Branch: 1 branch
BranchTrueFalseTotal
EATAB(l); 0 2643 2643
1085
1086 2643 ▶ if (fmm_getvalue(state, m, &l))
Branch: 1 branch
BranchTrueFalseTotal
if (fmm_getvalue(state, m, &l)) 0 2643 2643
1087 0 return -1;
1088  
1089 /*
1090 * now get last part - the description
1091 */
1092 2643 GetDesc:
1093 9875 ▶ EATAB(l);
Branch: 1 branch
BranchTrueFalseTotal
EATAB(l); 7224 2651 9875
1094 2651 ▶ if (l[0] == '\b') {
Branch: 1 branch
BranchTrueFalseTotal
if (l[0] == '\b') { 0 2651 2651
1095 0 ++l;
1096 0 m->nospflag = 1;
1097 }
1098 2651 ▶ else if ((l[0] == '\\') && (l[1] == 'b')) {
Branch: 2 branches
BranchTrueFalseTotal
else if ((l[0] == '\\') && (l[1] == 'b')) { 48 2603 2651
else if ((l[0] == '\\') && (l[1] == 'b')) { 0 48 48
1099 0 ++l;
1100 0 ++l;
1101 0 m->nospflag = 1;
1102 }
1103 else {
1104 2651 m->nospflag = 0;
1105 }
1106 2651 strncpy(m->desc, l, sizeof(m->desc) - 1);
1107 2651 m->desc[sizeof(m->desc) - 1] = '\0';
1108  
1109 2651 return 0;
1110  
1111 0 error:
1112 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
1113 0 croak(SvPV_nolen(err));
1114 }
1115  
1116 /* maps to mod_mime_magic::apprentice */
1117 static int
1118 8 fmm_parse_magic_file(PerlFMM *state, char *file)
1119 {
1120 int ws_offset;
1121 int lineno;
1122 int errs;
1123 /* char line[BUFSIZ + 1];*/
1124 PerlIO *fhandle;
1125 SV *err;
1126 8 SV *sv = sv_2mortal(newSV(BUFSIZ));
1127 8 SV *PL_rs_orig = newSVsv(PL_rs);
1128 char *line;
1129  
1130 8 fhandle = PerlIO_open(file, "r");
1131 8 ▶ if (! fhandle) {
Branch: 1 branch
BranchTrueFalseTotal
if (! fhandle) { 0 8 8
1132 0 err = newSVpvf(
1133 "Failed to open %s: %s", file, strerror(errno));
1134 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
1135 0 PerlIO_close(fhandle);
1136 0 return -1;
1137 }
1138  
1139 /*
1140 * Parse it line by line
1141 * $/ (slurp mode) is needed here
1142 */
1143 8 PL_rs = sv_2mortal(newSVpvn("\n", 1));
1144 7480 ▶ for(lineno = 1; sv_gets(sv, fhandle, 0) != NULL; lineno++) {
Branch: 1 branch
BranchTrueFalseTotal
for(lineno = 1; sv_gets(sv, fhandle, 0) != NULL; lineno++) { 7472 8 7480
1145 7472 line = SvPV_nolen(sv);
1146 /* delete newline */
1147 7472 ▶ if (line[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (line[0]) { 7472 0 7472
1148 7472 line[strlen(line) - 1] = '\0';
1149 }
1150  
1151 /* skip leading whitespace */
1152 7472 ws_offset = 0;
1153 7472 ▶ while (line[ws_offset] && isSPACE(line[ws_offset])) {
Branch: 2 branches
BranchTrueFalseTotal
while (line[ws_offset] && isSPACE(line[ws_offset])) { 6384 1088 7472
while (line[ws_offset] && isSPACE(line[ws_offset])) { 0 6384 6384
1154 0 ws_offset++;
1155 }
1156  
1157 /* skip blank lines */
1158 7472 ▶ if (line[ws_offset] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (line[ws_offset] == 0) { 1088 6384 7472
1159 1088 continue;
1160 }
1161  
1162 6384 ▶ if (line[ws_offset] == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (line[ws_offset] == '#') { 3736 2648 6384
1163 3736 continue;
1164 }
1165  
1166 2648 ▶ if (fmm_parse_magic_line(state, line, lineno) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmm_parse_magic_line(state, line, lineno) != 0) { 0 2648 2648
1167 0 ++errs;
1168 }
1169 }
1170 8 PerlIO_close(fhandle);
1171 8 PL_rs = PL_rs_orig;
1172  
1173 8 return 1;
1174 }
1175  
1176 /* fmm_fsmagic
1177 *
1178 * Checks the file's attribute by checking stat() and populates the
1179 * mime_type variable with a mime type. If no appropriate mime type is
1180 * found then returns -1 on error, 1 if undetermined because we
1181 * saw that it's a regular file which needs further processing to
1182 * determine its file type
1183 */
1184  
1185 #define DIR_MAGIC_TYPE "x-system/x-unix; directory"
1186 #define FIFO_MAGIC_TYPE "x-system/x-unix; named pipe"
1187 #define SOCKET_MAGIC_TYPE "x-system/x-unix; socket"
1188 #define BLOCK_MAGIC_TYPE "x-system/x-unix; block special file"
1189 #define CHAR_MAGIC_TYPE "x-system/x-unix; character special file"
1190 #define EMPTY_MAGIC_TYPE "x-system/x-unix; empty"
1191 #define BROKEN_SYMLINK_MAGIC_TYPE "x-system/x-unix; broken symlink"
1192 static int
1193 26 fmm_fsmagic_stat(PerlFMM *state, struct stat *sb, char **mime_type)
1194 {
1195 SV *err;
1196  
1197 26 ▶ if (sb->st_mode & S_IFREG) {
Branch: 1 branch
BranchTrueFalseTotal
if (sb->st_mode & S_IFREG) { 26 0 26
1198 /* Regular file. Need to check for emptiness */
1199 26 ▶ if (sb->st_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sb->st_size == 0) { 0 26 26
1200 0 strcpy(*mime_type, EMPTY_MAGIC_TYPE);
1201 0 return 0;
1202 }
1203 26 return 1;
1204 }
1205  
1206 /* it's not a regular file, so check other possibilities... */
1207 0 ▶ if (sb->st_mode & S_IFIFO) {
Branch: 1 branch
BranchTrueFalseTotal
if (sb->st_mode & S_IFIFO) { 0 0 0
1208 0 strcpy(*mime_type, FIFO_MAGIC_TYPE);
1209 0 ▶ } else if (sb->st_mode & S_IFCHR) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sb->st_mode & S_IFCHR) { 0 0 0
1210 0 strcpy(*mime_type, CHAR_MAGIC_TYPE);
1211 0 ▶ } else if (sb->st_mode & S_IFDIR) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sb->st_mode & S_IFDIR) { 0 0 0
1212 0 strcpy(*mime_type, DIR_MAGIC_TYPE);
1213 0 ▶ } else if (sb->st_mode & S_IFBLK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sb->st_mode & S_IFBLK) { 0 0 0
1214 0 strcpy(*mime_type, BLOCK_MAGIC_TYPE);
1215 0 ▶ } else if (sb->st_mode & S_IFLNK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sb->st_mode & S_IFLNK) { 0 0 0
1216 /* According to mod_mime_magic.c, the only reason stat() will return
1217 * a S_IFLNK in st_mode is that the symlink is broken
1218 */
1219 0 strcpy(*mime_type, BROKEN_SYMLINK_MAGIC_TYPE);
1220 0 ▶ } else if (sb->st_mode & S_IFSOCK) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (sb->st_mode & S_IFSOCK) { 0 0 0
1221 0 strcpy(*mime_type, SOCKET_MAGIC_TYPE);
1222 } else {
1223 /* Unknown type? */
1224 0 err = newSVpv("fmm_fsmagic: invalid file type", 0);
1225 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
1226 0 return -1;
1227 }
1228  
1229 0 return 0;
1230 }
1231  
1232 static int
1233 27 fmm_fsmagic(PerlFMM *state, char *filename, char **mime_type)
1234 {
1235 struct stat sb;
1236 SV *err;
1237  
1238 27 ▶ if (stat(filename, &sb) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(filename, &sb) == -1) { 1 26 27
1239 1 err = newSVpvf(
1240 "Failed to stat file %s: %s", filename, strerror(errno));
1241 1 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 1 0 1
FMM_SET_ERROR(state, err); 0 1 1
1242 1 return -1;
1243 }
1244  
1245 26 ▶ if (fmm_fsmagic_stat(state, &sb, mime_type) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmm_fsmagic_stat(state, &sb, mime_type) == 0) { 0 26 26
1246 0 return 0;
1247 }
1248  
1249 26 return 1;
1250 }
1251  
1252 static int
1253 21 fmm_ascmagic(unsigned char *buf, size_t nbytes, char **mime_type)
1254 {
1255 21 int has_escapes = 0;
1256 unsigned char *s;
1257 char nbuf[HOWMANY + 1]; /* one extra for terminating '\0' */
1258 char *token;
1259 register struct names *p;
1260 int small_nbytes;
1261 char *strtok_state;
1262 unsigned char *tp;
1263  
1264 /* these are easy, do them first */
1265  
1266 /*
1267 * for troff, look for . + letter + letter or .\"; this must be done to
1268 * disambiguate tar archives' ./file and other trash from real troff
1269 * input.
1270 */
1271 21 ▶ if (*buf == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (*buf == '.') { 0 21 21
1272 0 tp = buf + 1;
1273 0 ▶ while (isSPACE(*tp))
Branch: 1 branch
BranchTrueFalseTotal
while (isSPACE(*tp)) 0 0 0
1274 0 ++tp; /* skip leading whitespace */
1275 0 ▶ if ((isALNUM(*tp) || *tp == '\\') && (isALNUM(*(tp + 1)) || *tp == '"')) {
Branch: 4 branches
BranchTrueFalseTotal
if ((isALNUM(*tp) || *tp == '\\') && (isALNUM(*(tp + 1)) || *tp == '"')) { 0 0 0
if ((isALNUM(*tp) || *tp == '\\') && (isALNUM(*(tp + 1)) || *tp == '"')) { 0 0 0
if ((isALNUM(*tp) || *tp == '\\') && (isALNUM(*(tp + 1)) || *tp == '"')) { 0 0 0
if ((isALNUM(*tp) || *tp == '\\') && (isALNUM(*(tp + 1)) || *tp == '"')) { 0 0 0
1276 0 strcpy(*mime_type, "application/x-troff");
1277 0 return 0;
1278 }
1279 }
1280  
1281 21 ▶ if ((*buf == 'c' || *buf == 'C') && isSPACE(*(buf + 1))) {
Branch: 3 branches
BranchTrueFalseTotal
if ((*buf == 'c' || *buf == 'C') && isSPACE(*(buf + 1))) { 21 0 21
if ((*buf == 'c' || *buf == 'C') && isSPACE(*(buf + 1))) { 0 21 21
if ((*buf == 'c' || *buf == 'C') && isSPACE(*(buf + 1))) { 0 0 0
1282 /* Fortran */
1283 0 strcpy(*mime_type, "text/plain");
1284 0 return 0;
1285 }
1286  
1287 /* look for tokens from names.h - this is expensive!, so we'll limit
1288 * ourselves to only SMALL_HOWMANY bytes */
1289 21 small_nbytes = (nbytes > SMALL_HOWMANY) ? SMALL_HOWMANY : nbytes;
1290  
1291 /* make a copy of the buffer here because strtok() will destroy it */
1292 21 s = (unsigned char *) memcpy(nbuf, buf, small_nbytes);
1293 21 s[small_nbytes] = '\0';
1294 21 has_escapes = (memchr(s, '\033', small_nbytes) != NULL);
1295 2712 ▶ while ((token = strtok_r((char *) s, " \t\n\r\f", &strtok_state)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((token = strtok_r((char *) s, " \t\n\r\f", &strtok_state)) != NULL) { 2691 21 2712
1296 2691 s = NULL; /* make strtok() keep on tokin' */
1297 113022 ▶ for (p = names; p < names + NNAMES; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = names; p < names + NNAMES; p++) { 110331 2691 113022
1298 110331 ▶ if (strEQ(p->name, token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p->name, token)) { 0 110331 110331
1299 0 strcpy(*mime_type, types[p->type]);
1300 0 ▶ if (has_escapes)
Branch: 1 branch
BranchTrueFalseTotal
if (has_escapes) 0 0 0
1301 0 strcat(*mime_type, " (with escape sequences)");
1302 0 return 0;
1303 }
1304 }
1305 }
1306  
1307 21 int is_tarball = is_tar(buf, nbytes);
1308 21 ▶ if ( is_tarball == 1 || is_tarball == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( is_tarball == 1 || is_tarball == 2 ) { 21 0 21
if ( is_tarball == 1 || is_tarball == 2 ) { 0 21 21
1309 /* 1: V7 tar archive */
1310 /* 2: POSIX tar archive */
1311 0 strcpy(*mime_type, "application/x-tar");
1312 0 return 0;
1313 }
1314  
1315 /* all else fails, but it is ascii... */
1316 21 strcpy(*mime_type, "text/plain");
1317 21 return 0;
1318 }
1319  
1320 static int
1321 62 fmm_softmagic(PerlFMM *state, unsigned char **buf, int size, char **mime_type)
1322 {
1323 62 int cont_level = 0;
1324 62 int need_separator = 0;
1325 union VALUETYPE p;
1326 fmmagic *m_cont;
1327 62 fmmagic *m = state->magic;
1328  
1329 10454 ▶ for (; m; m = m->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (; m; m = m->next) { 10435 19 10454
1330 /* check if main entry matches */
1331 10435 ▶ if (! fmm_mget(state, &p, *buf, m, size) || !fmm_mcheck(state, &p, m)) {
Branch: 2 branches
BranchTrueFalseTotal
if (! fmm_mget(state, &p, *buf, m, size) || !fmm_mcheck(state, &p, m)) { 10393 42 10435
if (! fmm_mget(state, &p, *buf, m, size) || !fmm_mcheck(state, &p, m)) { 10350 43 10393
1332 /* main entry didn't match, flush its continuations */
1333 10392 ▶ if (! m->next || (m->next->cont_level == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if (! m->next || (m->next->cont_level == 0)) { 10373 19 10392
if (! m->next || (m->next->cont_level == 0)) { 9649 724 10373
1334 9668 continue;
1335 }
1336  
1337 724 m_cont = m->next;
1338 2632 ▶ while (m_cont && (m_cont->cont_level != 0)) {
Branch: 2 branches
BranchTrueFalseTotal
while (m_cont && (m_cont->cont_level != 0)) { 2632 0 2632
while (m_cont && (m_cont->cont_level != 0)) { 1908 724 2632
1339 /* this trick allows us to keep *m in sync when the continue
1340 * advances the pointer
1341 */
1342 1908 m = m_cont;
1343 1908 m_cont = m_cont->next;
1344 }
1345 724 continue;
1346 }
1347 /* if we get here, the main entry rule was a match */
1348 /* this will be the last run through the loop */
1349  
1350 /* print the match */
1351 43 fmm_append_mime(state, mime_type, &p, m);
1352  
1353 /* if we printed something, we'll need to print a blank before
1354 * we print something else */
1355 43 ▶ if (m->desc[0])
Branch: 1 branch
BranchTrueFalseTotal
if (m->desc[0]) 43 0 43
1356 43 need_separator = 1;
1357  
1358 /* and any continuations that match */
1359 43 cont_level++;
1360  
1361 43 m = m->next;
1362 43 ▶ while (m && (m->cont_level != 0)) {
Branch: 2 branches
BranchTrueFalseTotal
while (m && (m->cont_level != 0)) { 38 5 43
while (m && (m->cont_level != 0)) { 0 38 38
1363 0 ▶ if (cont_level >= m->cont_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (cont_level >= m->cont_level) { 0 0 0
1364 0 ▶ if (cont_level > m->cont_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (cont_level > m->cont_level) { 0 0 0
1365 /* We're at the end of the level "cont_level"
1366 * continuations.
1367 */
1368 0 cont_level = m->cont_level;
1369 }
1370  
1371 0 ▶ if (fmm_mget(state, &p, *buf, m, size) && fmm_mcheck(state, &p, m)) {
Branch: 2 branches
BranchTrueFalseTotal
if (fmm_mget(state, &p, *buf, m, size) && fmm_mcheck(state, &p, m)) { 0 0 0
if (fmm_mget(state, &p, *buf, m, size) && fmm_mcheck(state, &p, m)) { 0 0 0
1372 /* This continuation matched. Print its message, with a
1373 * blank before it if the previous item printed and this
1374 * isn't empty.
1375 */
1376 /* space if previous printed */
1377 0 ▶ if (need_separator && (m->nospflag == 0) && (m->desc[0] != '\0')) {
Branch: 3 branches
BranchTrueFalseTotal
if (need_separator && (m->nospflag == 0) && (m->desc[0] != '\0')) { 0 0 0
if (need_separator && (m->nospflag == 0) && (m->desc[0] != '\0')) { 0 0 0
if (need_separator && (m->nospflag == 0) && (m->desc[0] != '\0')) { 0 0 0
1378 /* putchar ' ' */
1379 0 fmm_append_buf(state, mime_type, " ");
1380 0 need_separator = 0;
1381 }
1382 0 fmm_append_mime(state, mime_type, &p, m);
1383 0 ▶ if (m->desc[0])
Branch: 1 branch
BranchTrueFalseTotal
if (m->desc[0]) 0 0 0
1384 0 need_separator = 1;
1385  
1386 /* If we see any continuations at a higher level,
1387 * process them.
1388 */
1389 0 cont_level++;
1390 }
1391 }
1392 /* move to next continuation record */
1393 0 m = m->next;
1394 }
1395 43 return 0;
1396 }
1397 19 return 1;
1398 }
1399  
1400 /* Perform mime magic on a PerlIO handle */
1401 /* Perform mime magic on a buffer */
1402 static int
1403 62 fmm_bufmagic(PerlFMM *state, unsigned char **buffer, char **mime_type)
1404 {
1405 62 ▶ if (fmm_softmagic(state, buffer, HOWMANY, mime_type) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmm_softmagic(state, buffer, HOWMANY, mime_type) == 0) { 43 19 62
1406 #ifdef FMM_DEBUG
1407 PerlIO_printf(PerlIO_stderr(), "[fmm_bufmagic]: fmm_softmagic returns 0\n");
1408 #endif
1409 43 return 0;
1410 }
1411  
1412 19 ▶ if (fmm_ascmagic(*buffer, HOWMANY, mime_type) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmm_ascmagic(*buffer, HOWMANY, mime_type) == 0) { 19 0 19
1413 #ifdef FMM_DEBUG
1414 PerlIO_printf(PerlIO_stderr(), "[fmm_bufmagic]: fmm_ascmagic returns 0\n");
1415 #endif
1416 19 return 0;
1417 }
1418  
1419 0 return 1;
1420 }
1421  
1422 static int
1423 44 fmm_fhmagic(PerlFMM *state, PerlIO *fhandle, char **mime_type)
1424 {
1425 SV *err;
1426 unsigned char *data;
1427 44 int ret = -1;
1428  
1429 44 Newz(1234, data, HOWMANY + 1, unsigned char);
1430 44 ▶ if (! PerlIO_read(fhandle, data, HOWMANY)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! PerlIO_read(fhandle, data, HOWMANY)) { 0 44 44
1431 0 err = newSVpvf(
1432 "Failed to read from handle: %s",
1433 strerror(errno)
1434 );
1435 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
1436 0 Safefree(data);
1437 0 return -1;
1438 }
1439  
1440 44 ret = fmm_bufmagic(state, &data, mime_type);
1441 44 Safefree(data);
1442  
1443 44 return ret;
1444 }
1445  
1446 static int
1447 0 fmm_ext_magic(PerlFMM *state, char *file, char **mime_type)
1448 {
1449 char ext[BUFSIZ];
1450 char *temp_mimetype;
1451 /* Look for the last dot */
1452 0 char *dot = rindex(file, '.');
1453 0 ▶ if (dot == 0x00) {
Branch: 1 branch
BranchTrueFalseTotal
if (dot == 0x00) { 0 0 0
1454 0 return 0;
1455 }
1456  
1457 0 strncpy(ext, dot + 1, BUFSIZ);
1458 0 ▶ if (st_lookup(state->ext, (st_data_t) ext, (st_data_t *) &temp_mimetype) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (st_lookup(state->ext, (st_data_t) ext, (st_data_t *) &temp_mimetype) == 0) { 0 0 0
1459 0 return 1;
1460 }
1461 0 strncpy(*mime_type, temp_mimetype, MAXMIMESTRING);
1462 0 return 0;
1463 }
1464  
1465 static int
1466 26 fmm_mime_magic(PerlFMM *state, char *file, char **mime_type)
1467 {
1468 PerlIO *fhandle;
1469 SV *err;
1470 int ret;
1471  
1472 26 ▶ if ((ret = fmm_fsmagic(state, file, mime_type)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = fmm_fsmagic(state, file, mime_type)) == 0) { 0 26 26
1473 0 return 0;
1474 }
1475 26 ▶ if (ret == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == -1) { 0 26 26
1476 0 return -1;
1477 }
1478  
1479 26 fhandle = PerlIO_open(file, "r");
1480 26 ▶ if (!fhandle) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fhandle) { 0 26 26
1481 0 err = newSVpvf(
1482 "Failed to open file %s: %s", file, strerror(errno));
1483 0 ▶ FMM_SET_ERROR(state, err);
Branch: 2 branches
BranchTrueFalseTotal
FMM_SET_ERROR(state, err); 0 0 0
FMM_SET_ERROR(state, err); 0 0 0
1484 0 return -1;
1485 }
1486  
1487 26 ▶ if ((ret = fmm_fhmagic(state, fhandle, mime_type)) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ret = fmm_fhmagic(state, fhandle, mime_type)) == 0) { 26 0 26
1488 #ifdef FMM_DEBUG
1489 PerlIO_printf(PerlIO_stderr(), "[fmm_mime_magic]: fmm_fhmagic returns 0\n");
1490 #endif
1491 26 PerlIO_close(fhandle);
1492 26 return 0;
1493 }
1494 0 PerlIO_close(fhandle);
1495  
1496 0 return fmm_ext_magic(state, file, mime_type);
1497 }
1498  
1499 PerlFMM*
1500 9 PerlFMM_create(SV *class_sv) {
1501 PerlFMM *state;
1502  
1503 PERL_UNUSED_VAR(class_sv);
1504 9 Newz(1234, state, 1, PerlFMM);
1505 9 state->magic = NULL;
1506 9 state->error = NULL;
1507 9 state->ext = st_init_strtable();
1508  
1509 9 return state;
1510 }
1511  
1512 PerlFMM *
1513 1 PerlFMM_clone(PerlFMM *self)
1514 {
1515 PerlFMM *state;
1516 fmmagic *d, *s;
1517  
1518 1 state = PerlFMM_create(NULL);
1519 1 st_free_table(state->ext);
1520 1 state->ext = st_copy( self->ext );
1521  
1522 1 s = self->magic;
1523 1 Newxz(d, 1, fmmagic);
1524 1 memcpy(d, s, sizeof(fmmagic));
1525 1 state->magic = d;
1526 331 ▶ while (s->next != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (s->next != NULL) { 330 1 331
1527 330 Newxz(d->next, 1, fmmagic);
1528 330 memcpy(d->next, s->next, sizeof(struct _fmmagic));
1529 330 d = d->next;
1530 330 s = s->next;
1531 }
1532 1 state->last = d;
1533 1 state->last->next = NULL;
1534  
1535 1 return state;
1536 }
1537  
1538 SV *
1539 8 PerlFMM_parse_magic_file(PerlFMM *self, char *file)
1540 {
1541 8 FMM_SET_ERROR(self, NULL);
1542 8 return fmm_parse_magic_file(self, file) ?
1543 8 ▶ &PL_sv_yes : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
&PL_sv_yes : &PL_sv_undef; 8 0 8
1544 }
1545  
1546 SV *
1547 3 PerlFMM_add_magic(PerlFMM *self, char *magic)
1548 {
1549 3 return fmm_parse_magic_line(self, magic, 0) == 0 ?
1550 3 ▶ &PL_sv_yes : &PL_sv_undef
Branch: 1 branch
BranchTrueFalseTotal
&PL_sv_yes : &PL_sv_undef 3 0 3
1551 ;
1552 }
1553  
1554 SV *
1555 2 PerlFMM_add_file_ext(PerlFMM *self, char *ext, char *mime)
1556 {
1557 char *dummy;
1558 SV *ret;
1559  
1560 2 ▶ if (st_lookup(self->ext, (st_data_t) ext, (st_data_t *) &dummy)) {
Branch: 1 branch
BranchTrueFalseTotal
if (st_lookup(self->ext, (st_data_t) ext, (st_data_t *) &dummy)) { 0 2 2
1561 0 ret = &PL_sv_no;
1562 } else {
1563 2 st_insert(self->ext, (st_data_t) ext, (st_data_t) mime);
1564 2 ret = &PL_sv_yes;
1565 }
1566 2 return ret;
1567 }
1568  
1569 SV *
1570 18 PerlFMM_fhmagic(PerlFMM *self, SV *svio)
1571 {
1572 PerlIO *io;
1573 char *type;
1574 int rc;
1575 SV *ret;
1576  
1577 18 ▶ if (! SvROK(svio))
Branch: 1 branch
BranchTrueFalseTotal
if (! SvROK(svio)) 0 18 18
1578 0 croak("Usage: self->fhmagic(*handle))");
1579  
1580 18 io = IoIFP(sv_2io(SvRV(svio)));
1581 18 ▶ if (! io)
Branch: 1 branch
BranchTrueFalseTotal
if (! io) 0 18 18
1582 0 croak("Not a handle");
1583  
1584 18 FMM_SET_ERROR(self, NULL);
1585 18 Newz(1234, type, BUFSIZ, char);
1586 18 rc = fmm_fhmagic(self, io, &type);
1587 18 ▶ ret = FMM_RESULT(type, rc);
Branch: 1 branch
BranchTrueFalseTotal
ret = FMM_RESULT(type, rc); 18 0 18
1588 18 Safefree(type);
1589 18 return ret;
1590 }
1591  
1592 SV *
1593 1 PerlFMM_fsmagic(PerlFMM *self, char *filename)
1594 {
1595 char *type;
1596 int rc;
1597 SV *ret;
1598  
1599 1 FMM_SET_ERROR(self, NULL);
1600  
1601 1 Newz(1234, type, BUFSIZ, char);
1602  
1603 1 rc = fmm_fsmagic(self, filename, &type);
1604 1 ▶ ret = FMM_RESULT(type, rc);
Branch: 1 branch
BranchTrueFalseTotal
ret = FMM_RESULT(type, rc); 0 1 1
1605 1 Safefree(type);
1606 1 return ret;
1607 }
1608  
1609 SV *
1610 18 PerlFMM_bufmagic(PerlFMM *self, SV *buf)
1611 {
1612 unsigned char *buffer;
1613 char *type;
1614 int rc;
1615 SV *ret;
1616  
1617 /* rt #28040, allow RV to SVs to be passed here */
1618 18 ▶ if (SvROK(buf) && SvTYPE(SvRV(buf)) == SVt_PV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(buf) && SvTYPE(SvRV(buf)) == SVt_PV) { 18 0 18
if (SvROK(buf) && SvTYPE(SvRV(buf)) == SVt_PV) { 18 0 18
1619 36 buffer = (unsigned char *) SvPV_nolen( SvRV( buf ) );
1620 } else {
1621 0 buffer = (unsigned char *) SvPV_nolen(buf);
1622 }
1623  
1624 18 FMM_SET_ERROR(self, NULL);
1625  
1626 18 Newz(1234, type, BUFSIZ, char);
1627  
1628 18 rc = fmm_bufmagic(self, &buffer, &type);
1629 18 ▶ ret = FMM_RESULT(type, rc);
Branch: 1 branch
BranchTrueFalseTotal
ret = FMM_RESULT(type, rc); 18 0 18
1630 18 Safefree(type);
1631 18 return ret;
1632 }
1633  
1634 SV *
1635 2 PerlFMM_ascmagic(PerlFMM *self, unsigned char *data)
1636 {
1637 char *type;
1638 int rc;
1639 SV *ret;
1640  
1641 2 Newz(1234, type, BUFSIZ, char);
1642  
1643 2 FMM_SET_ERROR(self, NULL);
1644  
1645 2 rc = fmm_ascmagic(data, strlen(data), &type);
1646 2 ▶ ret = FMM_RESULT(type, rc);
Branch: 1 branch
BranchTrueFalseTotal
ret = FMM_RESULT(type, rc); 2 0 2
1647 2 Safefree(type);
1648 2 return ret;
1649 }
1650  
1651 SV *
1652 26 PerlFMM_get_mime(PerlFMM *self, char *filename)
1653 {
1654 char *type;
1655 int rc;
1656 SV *ret;
1657  
1658 26 Newz(1234, type, MAXMIMESTRING, char);
1659  
1660 26 FMM_SET_ERROR(self, NULL);
1661 26 rc = fmm_mime_magic(self, filename, &type);
1662 26 ▶ ret = FMM_RESULT(type, rc);
Branch: 1 branch
BranchTrueFalseTotal
ret = FMM_RESULT(type, rc); 26 0 26
1663 26 Safefree(type);
1664 26 return ret;
1665 }
1666  
1667 #endif
« src/MMagicST.c ↑ Summary src/MMagic.xs »