Deleted Added
full compact
funcs.c (133359) funcs.c (139368)
1/*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
1/*
2 * Copyright (c) Christos Zoulas 2003.
3 * All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include "file.h"
30#include "magic.h"
31#include <stdarg.h>
32#include <stdlib.h>
33#include <string.h>
34#include <ctype.h>
35
36#ifndef lint
14 *
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
25 * SUCH DAMAGE.
26 */
27#include "file.h"
28#include "magic.h"
29#include <stdarg.h>
30#include <stdlib.h>
31#include <string.h>
32#include <ctype.h>
33
34#ifndef lint
37FILE_RCSID("@(#)$Id: funcs.c,v 1.12 2004/06/04 14:40:20 christos Exp $")
35FILE_RCSID("@(#)$Id: funcs.c,v 1.13 2004/09/11 19:15:57 christos Exp $")
38#endif /* lint */
39/*
40 * Like printf, only we print to a buffer and advance it.
41 */
42protected int
43file_printf(struct magic_set *ms, const char *fmt, ...)
44{
45 va_list ap;
46 size_t len;
47 char *buf;
48
49 va_start(ap, fmt);
50
51 if ((len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap)) >= ms->o.len) {
52 va_end(ap);
53 if ((buf = realloc(ms->o.buf, len + 1024)) == NULL) {
54 file_oomem(ms);
55 return -1;
56 }
57 ms->o.ptr = buf + (ms->o.ptr - ms->o.buf);
58 ms->o.buf = buf;
59 ms->o.len = ms->o.size - (ms->o.ptr - ms->o.buf);
60 ms->o.size = len + 1024;
61
62 va_start(ap, fmt);
63 len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap);
64 }
65 ms->o.ptr += len;
66 ms->o.len -= len;
67 va_end(ap);
68 return 0;
69}
70
71/*
72 * error - print best error message possible
73 */
74/*VARARGS*/
75protected void
76file_error(struct magic_set *ms, int error, const char *f, ...)
77{
78 va_list va;
79 /* Only the first error is ok */
80 if (ms->haderr)
81 return;
82 va_start(va, f);
83 (void)vsnprintf(ms->o.buf, ms->o.size, f, va);
84 va_end(va);
85 if (error > 0) {
86 size_t len = strlen(ms->o.buf);
87 (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
88 strerror(error));
89 }
90 ms->haderr++;
91 ms->error = error;
92}
93
94
95protected void
96file_oomem(struct magic_set *ms)
97{
98 file_error(ms, errno, "cannot allocate memory");
99}
100
101protected void
102file_badseek(struct magic_set *ms)
103{
104 file_error(ms, errno, "error seeking");
105}
106
107protected void
108file_badread(struct magic_set *ms)
109{
110 file_error(ms, errno, "error reading");
111}
112
113#ifndef COMPILE_ONLY
114protected int
115file_buffer(struct magic_set *ms, const void *buf, size_t nb)
116{
117 int m;
118 /* try compression stuff */
119 if ((m = file_zmagic(ms, buf, nb)) == 0) {
120 /* Check if we have a tar file */
121 if ((m = file_is_tar(ms, buf, nb)) == 0) {
122 /* try tests in /etc/magic (or surrogate magic file) */
123 if ((m = file_softmagic(ms, buf, nb)) == 0) {
124 /* try known keywords, check whether it is ASCII */
125 if ((m = file_ascmagic(ms, buf, nb)) == 0) {
126 /* abandon hope, all ye who remain here */
127 if (file_printf(ms, ms->flags & MAGIC_MIME ?
128 "application/octet-stream" : "data") == -1)
129 return -1;
130 m = 1;
131 }
132 }
133 }
134 }
135 return m;
136}
137#endif
138
139protected int
140file_reset(struct magic_set *ms)
141{
142 if (ms->mlist == NULL) {
143 file_error(ms, 0, "no magic files loaded");
144 return -1;
145 }
146 ms->o.ptr = ms->o.buf;
147 ms->haderr = 0;
148 ms->error = -1;
149 return 0;
150}
151
152protected const char *
153file_getbuffer(struct magic_set *ms)
154{
155 char *nbuf, *op, *np;
156 size_t nsize;
157
158 if (ms->haderr)
159 return NULL;
160
161 if (ms->flags & MAGIC_RAW)
162 return ms->o.buf;
163
164 nsize = ms->o.len * 4 + 1;
165 if (ms->o.psize < nsize) {
166 if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) {
167 file_oomem(ms);
168 return NULL;
169 }
170 ms->o.psize = nsize;
171 ms->o.pbuf = nbuf;
172 }
173
174 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
175 if (isprint((unsigned char)*op)) {
176 *np++ = *op;
177 } else {
178 *np++ = '\\';
179 *np++ = ((*op >> 6) & 3) + '0';
180 *np++ = ((*op >> 3) & 7) + '0';
181 *np++ = ((*op >> 0) & 7) + '0';
182 }
183 }
184 *np = '\0';
185 return ms->o.pbuf;
186}
36#endif /* lint */
37/*
38 * Like printf, only we print to a buffer and advance it.
39 */
40protected int
41file_printf(struct magic_set *ms, const char *fmt, ...)
42{
43 va_list ap;
44 size_t len;
45 char *buf;
46
47 va_start(ap, fmt);
48
49 if ((len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap)) >= ms->o.len) {
50 va_end(ap);
51 if ((buf = realloc(ms->o.buf, len + 1024)) == NULL) {
52 file_oomem(ms);
53 return -1;
54 }
55 ms->o.ptr = buf + (ms->o.ptr - ms->o.buf);
56 ms->o.buf = buf;
57 ms->o.len = ms->o.size - (ms->o.ptr - ms->o.buf);
58 ms->o.size = len + 1024;
59
60 va_start(ap, fmt);
61 len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap);
62 }
63 ms->o.ptr += len;
64 ms->o.len -= len;
65 va_end(ap);
66 return 0;
67}
68
69/*
70 * error - print best error message possible
71 */
72/*VARARGS*/
73protected void
74file_error(struct magic_set *ms, int error, const char *f, ...)
75{
76 va_list va;
77 /* Only the first error is ok */
78 if (ms->haderr)
79 return;
80 va_start(va, f);
81 (void)vsnprintf(ms->o.buf, ms->o.size, f, va);
82 va_end(va);
83 if (error > 0) {
84 size_t len = strlen(ms->o.buf);
85 (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
86 strerror(error));
87 }
88 ms->haderr++;
89 ms->error = error;
90}
91
92
93protected void
94file_oomem(struct magic_set *ms)
95{
96 file_error(ms, errno, "cannot allocate memory");
97}
98
99protected void
100file_badseek(struct magic_set *ms)
101{
102 file_error(ms, errno, "error seeking");
103}
104
105protected void
106file_badread(struct magic_set *ms)
107{
108 file_error(ms, errno, "error reading");
109}
110
111#ifndef COMPILE_ONLY
112protected int
113file_buffer(struct magic_set *ms, const void *buf, size_t nb)
114{
115 int m;
116 /* try compression stuff */
117 if ((m = file_zmagic(ms, 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)
127 return -1;
128 m = 1;
129 }
130 }
131 }
132 }
133 return m;
134}
135#endif
136
137protected int
138file_reset(struct magic_set *ms)
139{
140 if (ms->mlist == NULL) {
141 file_error(ms, 0, "no magic files loaded");
142 return -1;
143 }
144 ms->o.ptr = ms->o.buf;
145 ms->haderr = 0;
146 ms->error = -1;
147 return 0;
148}
149
150protected const char *
151file_getbuffer(struct magic_set *ms)
152{
153 char *nbuf, *op, *np;
154 size_t nsize;
155
156 if (ms->haderr)
157 return NULL;
158
159 if (ms->flags & MAGIC_RAW)
160 return ms->o.buf;
161
162 nsize = ms->o.len * 4 + 1;
163 if (ms->o.psize < nsize) {
164 if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) {
165 file_oomem(ms);
166 return NULL;
167 }
168 ms->o.psize = nsize;
169 ms->o.pbuf = nbuf;
170 }
171
172 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
173 if (isprint((unsigned char)*op)) {
174 *np++ = *op;
175 } else {
176 *np++ = '\\';
177 *np++ = ((*op >> 6) & 3) + '0';
178 *np++ = ((*op >> 3) & 7) + '0';
179 *np++ = ((*op >> 0) & 7) + '0';
180 }
181 }
182 *np = '\0';
183 return ms->o.pbuf;
184}