Update to file 4.13. Put the contrib files into contrib/file-4 instead
[dragonfly.git] / contrib / file-4 / src / funcs.c
bloba2ceff7f8e44db83220c96656dafb58c0d92b224
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.
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.
27 #include "file.h"
28 #include "magic.h"
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
34 #ifndef lint
35 FILE_RCSID("@(#)$Id: funcs.c,v 1.14 2005/01/07 19:17:27 christos Exp $")
36 #endif /* lint */
38 * Like printf, only we print to a buffer and advance it.
40 protected int
41 file_printf(struct magic_set *ms, const char *fmt, ...)
43 va_list ap;
44 size_t len;
45 char *buf;
47 va_start(ap, fmt);
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;
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;
60 va_start(ap, fmt);
61 len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap);
63 ms->o.ptr += len;
64 ms->o.len -= len;
65 va_end(ap);
66 return 0;
70 * error - print best error message possible
72 /*VARARGS*/
73 protected void
74 file_error(struct magic_set *ms, int error, const char *f, ...)
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));
88 ms->haderr++;
89 ms->error = error;
93 protected void
94 file_oomem(struct magic_set *ms)
96 file_error(ms, errno, "cannot allocate memory");
99 protected void
100 file_badseek(struct magic_set *ms)
102 file_error(ms, errno, "error seeking");
105 protected void
106 file_badread(struct magic_set *ms)
108 file_error(ms, errno, "error reading");
111 #ifndef COMPILE_ONLY
112 protected int
113 file_buffer(struct magic_set *ms, int fd, const void *buf, size_t nb)
115 int m;
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)
127 return -1;
128 m = 1;
133 return m;
135 #endif
137 protected int
138 file_reset(struct magic_set *ms)
140 if (ms->mlist == NULL) {
141 file_error(ms, 0, "no magic files loaded");
142 return -1;
144 ms->o.ptr = ms->o.buf;
145 ms->haderr = 0;
146 ms->error = -1;
147 return 0;
150 protected const char *
151 file_getbuffer(struct magic_set *ms)
153 char *nbuf, *op, *np;
154 size_t nsize;
156 if (ms->haderr)
157 return NULL;
159 if (ms->flags & MAGIC_RAW)
160 return ms->o.buf;
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;
168 ms->o.psize = nsize;
169 ms->o.pbuf = nbuf;
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';
182 *np = '\0';
183 return ms->o.pbuf;