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