Deleted Added
full compact
compress.c (298192) compress.c (299736)
1/*
2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
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:

--- 21 unchanged lines hidden (view full) ---

30 * zmagic() - returns 0 if not recognized, uncompresses and prints
31 * information if recognized
32 * uncompress(method, old, n, newch) - uncompress old into new,
33 * using method, return sizeof new
34 */
35#include "file.h"
36
37#ifndef lint
1/*
2 * Copyright (c) Ian F. Darwin 1986-1995.
3 * Software written by Ian F. Darwin and others;
4 * maintained 1995-present by Christos Zoulas and others.
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:

--- 21 unchanged lines hidden (view full) ---

30 * zmagic() - returns 0 if not recognized, uncompresses and prints
31 * information if recognized
32 * uncompress(method, old, n, newch) - uncompress old into new,
33 * using method, return sizeof new
34 */
35#include "file.h"
36
37#ifndef lint
38FILE_RCSID("@(#)$File: compress.c,v 1.93 2016/03/31 17:51:12 christos Exp $")
38FILE_RCSID("@(#)$File: compress.c,v 1.96 2016/04/20 00:00:26 christos Exp $")
39#endif
40
41#include "magic.h"
42#include <stdlib.h>
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46#include <string.h>

--- 135 unchanged lines hidden (view full) ---

182protected int
183file_zmagic(struct magic_set *ms, int fd, const char *name,
184 const unsigned char *buf, size_t nbytes)
185{
186 unsigned char *newbuf = NULL;
187 size_t i, nsz;
188 char *rbuf;
189 file_pushbuf_t *pb;
39#endif
40
41#include "magic.h"
42#include <stdlib.h>
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46#include <string.h>

--- 135 unchanged lines hidden (view full) ---

182protected int
183file_zmagic(struct magic_set *ms, int fd, const char *name,
184 const unsigned char *buf, size_t nbytes)
185{
186 unsigned char *newbuf = NULL;
187 size_t i, nsz;
188 char *rbuf;
189 file_pushbuf_t *pb;
190 int rv = 0;
190 int urv, prv, rv = 0;
191 int mime = ms->flags & MAGIC_MIME;
192#ifdef HAVE_SIGNAL_H
193 sig_t osigpipe;
194#endif
195
196 if ((ms->flags & MAGIC_COMPRESS) == 0)
197 return 0;
198

--- 10 unchanged lines hidden (view full) ---

209 CCAST(void *, compr[i].magic)))(buf);
210 else
211#endif
212 zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0;
213
214 if (!zm)
215 continue;
216 nsz = nbytes;
191 int mime = ms->flags & MAGIC_MIME;
192#ifdef HAVE_SIGNAL_H
193 sig_t osigpipe;
194#endif
195
196 if ((ms->flags & MAGIC_COMPRESS) == 0)
197 return 0;
198

--- 10 unchanged lines hidden (view full) ---

209 CCAST(void *, compr[i].magic)))(buf);
210 else
211#endif
212 zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0;
213
214 if (!zm)
215 continue;
216 nsz = nbytes;
217 rv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
218 DPRINTF("uncompressbuf = %d, %s, %zu\n", rv, (char *)newbuf,
217 urv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz);
218 DPRINTF("uncompressbuf = %d, %s, %zu\n", urv, (char *)newbuf,
219 nsz);
219 nsz);
220 switch (rv) {
220 switch (urv) {
221 case OKDATA:
222 case ERRDATA:
223
224 ms->flags &= ~MAGIC_COMPRESS;
221 case OKDATA:
222 case ERRDATA:
223
224 ms->flags &= ~MAGIC_COMPRESS;
225 if (rv == ERRDATA)
226 rv = file_printf(ms, "%s ERROR: %s",
225 if (urv == ERRDATA)
226 prv = file_printf(ms, "%s ERROR: %s",
227 methodname(i), newbuf);
228 else
227 methodname(i), newbuf);
228 else
229 rv = file_buffer(ms, -1, name, newbuf, nsz);
230 if (rv == -1)
229 prv = file_buffer(ms, -1, name, newbuf, nsz);
230 if (prv == -1)
231 goto error;
231 goto error;
232 DPRINTF("rv = %d\n", rv);
232 rv = 1;
233 if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
234 goto out;
235 if (mime != MAGIC_MIME && mime != 0)
236 goto out;
237 if ((file_printf(ms,
238 mime ? " compressed-encoding=" : " (")) == -1)
239 goto error;
240 if ((pb = file_push_buffer(ms)) == NULL)
241 goto error;
233 if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0)
234 goto out;
235 if (mime != MAGIC_MIME && mime != 0)
236 goto out;
237 if ((file_printf(ms,
238 mime ? " compressed-encoding=" : " (")) == -1)
239 goto error;
240 if ((pb = file_push_buffer(ms)) == NULL)
241 goto error;
242 /*
243 * XXX: If file_buffer fails here, we overwrite
244 * the compressed text. FIXME.
245 */
242 if (file_buffer(ms, -1, NULL, buf, nbytes) == -1)
243 goto error;
244 if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
245 if (file_printf(ms, "%s", rbuf) == -1) {
246 free(rbuf);
247 goto error;
248 }
249 free(rbuf);
250 }
251 if (!mime && file_printf(ms, ")") == -1)
252 goto error;
246 if (file_buffer(ms, -1, NULL, buf, nbytes) == -1)
247 goto error;
248 if ((rbuf = file_pop_buffer(ms, pb)) != NULL) {
249 if (file_printf(ms, "%s", rbuf) == -1) {
250 free(rbuf);
251 goto error;
252 }
253 free(rbuf);
254 }
255 if (!mime && file_printf(ms, ")") == -1)
256 goto error;
253 goto out;
257 /*FALLTHROUGH*/
254 case NODATA:
258 case NODATA:
255 goto out;
259 break;
256 default:
257 abort();
260 default:
261 abort();
262 /*NOTREACHED*/
263 error:
264 rv = -1;
265 break;
258 }
259 }
260out:
266 }
267 }
268out:
261 rv = 1;
262error:
269 DPRINTF("rv = %d\n", rv);
270
263#ifdef HAVE_SIGNAL_H
264 (void)signal(SIGPIPE, osigpipe);
265#endif
266 free(newbuf);
267 ms->flags |= MAGIC_COMPRESS;
268 DPRINTF("Zmagic returns %d\n", rv);
269 return rv;
270}

--- 480 unchanged lines hidden ---
271#ifdef HAVE_SIGNAL_H
272 (void)signal(SIGPIPE, osigpipe);
273#endif
274 free(newbuf);
275 ms->flags |= MAGIC_COMPRESS;
276 DPRINTF("Zmagic returns %d\n", rv);
277 return rv;
278}

--- 480 unchanged lines hidden ---