Deleted Added
full compact
print.c (133359) print.c (139368)
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:
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.
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:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Ian F. Darwin and others.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33/*
34 * print.c - debugging printout routines
35 */
36
37#include "file.h"
38#include <stdio.h>
39#include <errno.h>
40#include <string.h>
41#include <stdarg.h>
42#include <stdlib.h>
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
46#include <time.h>
47
48#ifndef lint
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28/*
29 * print.c - debugging printout routines
30 */
31
32#include "file.h"
33#include <stdio.h>
34#include <errno.h>
35#include <string.h>
36#include <stdarg.h>
37#include <stdlib.h>
38#ifdef HAVE_UNISTD_H
39#include <unistd.h>
40#endif
41#include <time.h>
42
43#ifndef lint
49FILE_RCSID("@(#)$Id: print.c,v 1.44 2003/09/12 19:39:44 christos Exp $")
44FILE_RCSID("@(#)$Id: print.c,v 1.46 2004/11/13 08:11:39 christos Exp $")
50#endif /* lint */
51
52#define SZOF(a) (sizeof(a) / sizeof(a[0]))
53
54#ifndef COMPILE_ONLY
55protected void
56file_mdump(struct magic *m)
57{
58 private const char *typ[] = { "invalid", "byte", "short", "invalid",
59 "long", "string", "date", "beshort",
60 "belong", "bedate", "leshort", "lelong",
61 "ledate", "pstring", "ldate", "beldate",
62 "leldate", "regex" };
63 private const char optyp[] = { '@', '&', '|', '^', '+', '-',
64 '*', '/', '%' };
65 (void) fputc('[', stderr);
66 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
67 m->offset);
68
69 if (m->flag & INDIR) {
70 (void) fprintf(stderr, "(%s,",
71 /* Note: type is unsigned */
72 (m->in_type < SZOF(typ)) ?
73 typ[m->in_type] : "*bad*");
74 if (m->in_op & FILE_OPINVERSE)
75 (void) fputc('~', stderr);
76 (void) fprintf(stderr, "%c%d),",
77 ((m->in_op&0x7F) < SZOF(optyp)) ?
78 optyp[m->in_op&0x7F] : '?',
79 m->in_offset);
80 }
81 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
82 /* Note: type is unsigned */
83 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
84 if (m->mask_op & FILE_OPINVERSE)
85 (void) fputc('~', stderr);
86 if (m->mask) {
87 if ((m->mask_op & 0x7F) < SZOF(optyp))
88 fputc(optyp[m->mask_op&0x7F], stderr);
89 else
90 fputc('?', stderr);
91 if(FILE_STRING != m->type || FILE_PSTRING != m->type)
92 (void) fprintf(stderr, "%.8x", m->mask);
93 else {
94 if (m->mask & STRING_IGNORE_LOWERCASE)
95 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
96 if (m->mask & STRING_COMPACT_BLANK)
97 (void) fputc(CHAR_COMPACT_BLANK, stderr);
98 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
99 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
100 stderr);
101 }
102 }
103
104 (void) fprintf(stderr, ",%c", m->reln);
105
106 if (m->reln != 'x') {
107 switch (m->type) {
108 case FILE_BYTE:
109 case FILE_SHORT:
110 case FILE_LONG:
111 case FILE_LESHORT:
112 case FILE_LELONG:
113 case FILE_BESHORT:
114 case FILE_BELONG:
115 (void) fprintf(stderr, "%d", m->value.l);
116 break;
117 case FILE_STRING:
118 case FILE_PSTRING:
119 case FILE_REGEX:
120 file_showstr(stderr, m->value.s, ~0U);
121 break;
122 case FILE_DATE:
123 case FILE_LEDATE:
124 case FILE_BEDATE:
125 (void)fprintf(stderr, "%s,",
126 file_fmttime(m->value.l, 1));
127 break;
128 case FILE_LDATE:
129 case FILE_LELDATE:
130 case FILE_BELDATE:
131 (void)fprintf(stderr, "%s,",
132 file_fmttime(m->value.l, 0));
133 break;
134 default:
135 (void) fputs("*bad*", stderr);
136 break;
137 }
138 }
139 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
140}
141#endif
142
143/*VARARGS*/
144protected void
45#endif /* lint */
46
47#define SZOF(a) (sizeof(a) / sizeof(a[0]))
48
49#ifndef COMPILE_ONLY
50protected void
51file_mdump(struct magic *m)
52{
53 private const char *typ[] = { "invalid", "byte", "short", "invalid",
54 "long", "string", "date", "beshort",
55 "belong", "bedate", "leshort", "lelong",
56 "ledate", "pstring", "ldate", "beldate",
57 "leldate", "regex" };
58 private const char optyp[] = { '@', '&', '|', '^', '+', '-',
59 '*', '/', '%' };
60 (void) fputc('[', stderr);
61 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
62 m->offset);
63
64 if (m->flag & INDIR) {
65 (void) fprintf(stderr, "(%s,",
66 /* Note: type is unsigned */
67 (m->in_type < SZOF(typ)) ?
68 typ[m->in_type] : "*bad*");
69 if (m->in_op & FILE_OPINVERSE)
70 (void) fputc('~', stderr);
71 (void) fprintf(stderr, "%c%d),",
72 ((m->in_op&0x7F) < SZOF(optyp)) ?
73 optyp[m->in_op&0x7F] : '?',
74 m->in_offset);
75 }
76 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
77 /* Note: type is unsigned */
78 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
79 if (m->mask_op & FILE_OPINVERSE)
80 (void) fputc('~', stderr);
81 if (m->mask) {
82 if ((m->mask_op & 0x7F) < SZOF(optyp))
83 fputc(optyp[m->mask_op&0x7F], stderr);
84 else
85 fputc('?', stderr);
86 if(FILE_STRING != m->type || FILE_PSTRING != m->type)
87 (void) fprintf(stderr, "%.8x", m->mask);
88 else {
89 if (m->mask & STRING_IGNORE_LOWERCASE)
90 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
91 if (m->mask & STRING_COMPACT_BLANK)
92 (void) fputc(CHAR_COMPACT_BLANK, stderr);
93 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
94 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
95 stderr);
96 }
97 }
98
99 (void) fprintf(stderr, ",%c", m->reln);
100
101 if (m->reln != 'x') {
102 switch (m->type) {
103 case FILE_BYTE:
104 case FILE_SHORT:
105 case FILE_LONG:
106 case FILE_LESHORT:
107 case FILE_LELONG:
108 case FILE_BESHORT:
109 case FILE_BELONG:
110 (void) fprintf(stderr, "%d", m->value.l);
111 break;
112 case FILE_STRING:
113 case FILE_PSTRING:
114 case FILE_REGEX:
115 file_showstr(stderr, m->value.s, ~0U);
116 break;
117 case FILE_DATE:
118 case FILE_LEDATE:
119 case FILE_BEDATE:
120 (void)fprintf(stderr, "%s,",
121 file_fmttime(m->value.l, 1));
122 break;
123 case FILE_LDATE:
124 case FILE_LELDATE:
125 case FILE_BELDATE:
126 (void)fprintf(stderr, "%s,",
127 file_fmttime(m->value.l, 0));
128 break;
129 default:
130 (void) fputs("*bad*", stderr);
131 break;
132 }
133 }
134 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
135}
136#endif
137
138/*VARARGS*/
139protected void
145file_magwarn(const char *f, ...)
140file_magwarn(struct magic_set *ms, const char *f, ...)
146{
147 va_list va;
148 va_start(va, f);
149
150 /* cuz we use stdout for most, stderr here */
151 (void) fflush(stdout);
152
141{
142 va_list va;
143 va_start(va, f);
144
145 /* cuz we use stdout for most, stderr here */
146 (void) fflush(stdout);
147
153 (void) fprintf(stderr, "WARNING: ");
148 (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
149 (unsigned long)ms->line);
154 (void) vfprintf(stderr, f, va);
155 va_end(va);
156 fputc('\n', stderr);
157}
158
159protected char *
160file_fmttime(uint32_t v, int local)
161{
162 char *pp, *rt;
163 time_t t = (time_t)v;
164 struct tm *tm;
165
166 if (local) {
167 pp = ctime(&t);
168 } else {
169#ifndef HAVE_DAYLIGHT
170 private int daylight = 0;
171#ifdef HAVE_TM_ISDST
172 private time_t now = (time_t)0;
173
174 if (now == (time_t)0) {
175 struct tm *tm1;
176 (void)time(&now);
177 tm1 = localtime(&now);
178 daylight = tm1->tm_isdst;
179 }
180#endif /* HAVE_TM_ISDST */
181#endif /* HAVE_DAYLIGHT */
182 if (daylight)
183 t += 3600;
184 tm = gmtime(&t);
185 pp = asctime(tm);
186 }
187
188 if ((rt = strchr(pp, '\n')) != NULL)
189 *rt = '\0';
190 return pp;
191}
150 (void) vfprintf(stderr, f, va);
151 va_end(va);
152 fputc('\n', stderr);
153}
154
155protected char *
156file_fmttime(uint32_t v, int local)
157{
158 char *pp, *rt;
159 time_t t = (time_t)v;
160 struct tm *tm;
161
162 if (local) {
163 pp = ctime(&t);
164 } else {
165#ifndef HAVE_DAYLIGHT
166 private int daylight = 0;
167#ifdef HAVE_TM_ISDST
168 private time_t now = (time_t)0;
169
170 if (now == (time_t)0) {
171 struct tm *tm1;
172 (void)time(&now);
173 tm1 = localtime(&now);
174 daylight = tm1->tm_isdst;
175 }
176#endif /* HAVE_TM_ISDST */
177#endif /* HAVE_DAYLIGHT */
178 if (daylight)
179 t += 3600;
180 tm = gmtime(&t);
181 pp = asctime(tm);
182 }
183
184 if ((rt = strchr(pp, '\n')) != NULL)
185 *rt = '\0';
186 return pp;
187}