2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice immediately at the beginning of the file, without modification,
10 * this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 FILE_RCSID("@(#)$Id: funcs.c,v 1.14 2005/01/07 19:17:27 christos Exp $")
38 * Like printf, only we print to a buffer and advance it.
41 file_printf(struct magic_set
*ms
, const char *fmt
, ...)
49 if ((len
= vsnprintf(ms
->o
.ptr
, ms
->o
.len
, fmt
, ap
)) >= ms
->o
.len
) {
51 if ((buf
= realloc(ms
->o
.buf
, len
+ 1024)) == NULL
) {
55 ms
->o
.ptr
= buf
+ (ms
->o
.ptr
- ms
->o
.buf
);
57 ms
->o
.len
= ms
->o
.size
- (ms
->o
.ptr
- ms
->o
.buf
);
58 ms
->o
.size
= len
+ 1024;
61 len
= vsnprintf(ms
->o
.ptr
, ms
->o
.len
, fmt
, ap
);
70 * error - print best error message possible
74 file_error(struct magic_set
*ms
, int error
, const char *f
, ...)
77 /* Only the first error is ok */
81 (void)vsnprintf(ms
->o
.buf
, ms
->o
.size
, f
, va
);
84 size_t len
= strlen(ms
->o
.buf
);
85 (void)snprintf(ms
->o
.buf
+ len
, ms
->o
.size
- len
, " (%s)",
94 file_oomem(struct magic_set
*ms
)
96 file_error(ms
, errno
, "cannot allocate memory");
100 file_badseek(struct magic_set
*ms
)
102 file_error(ms
, errno
, "error seeking");
106 file_badread(struct magic_set
*ms
)
108 file_error(ms
, errno
, "error reading");
113 file_buffer(struct magic_set
*ms
, int fd
, const void *buf
, size_t nb
)
116 /* try compression stuff */
117 if ((m
= file_zmagic(ms
, fd
, buf
, nb
)) == 0) {
118 /* Check if we have a tar file */
119 if ((m
= file_is_tar(ms
, buf
, nb
)) == 0) {
120 /* try tests in /etc/magic (or surrogate magic file) */
121 if ((m
= file_softmagic(ms
, buf
, nb
)) == 0) {
122 /* try known keywords, check whether it is ASCII */
123 if ((m
= file_ascmagic(ms
, buf
, nb
)) == 0) {
124 /* abandon hope, all ye who remain here */
125 if (file_printf(ms
, ms
->flags
& MAGIC_MIME
?
126 "application/octet-stream" : "data") == -1)
138 file_reset(struct magic_set
*ms
)
140 if (ms
->mlist
== NULL
) {
141 file_error(ms
, 0, "no magic files loaded");
144 ms
->o
.ptr
= ms
->o
.buf
;
150 protected const char *
151 file_getbuffer(struct magic_set
*ms
)
153 char *nbuf
, *op
, *np
;
159 if (ms
->flags
& MAGIC_RAW
)
162 nsize
= ms
->o
.len
* 4 + 1;
163 if (ms
->o
.psize
< nsize
) {
164 if ((nbuf
= realloc(ms
->o
.pbuf
, nsize
)) == NULL
) {
172 for (np
= ms
->o
.pbuf
, op
= ms
->o
.buf
; *op
; op
++) {
173 if (isprint((unsigned char)*op
)) {
177 *np
++ = ((*op
>> 6) & 3) + '0';
178 *np
++ = ((*op
>> 3) & 7) + '0';
179 *np
++ = ((*op
>> 0) & 7) + '0';