00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "file.h"
00033 #include <stdio.h>
00034 #include <errno.h>
00035 #include <string.h>
00036 #include <stdarg.h>
00037 #include <stdlib.h>
00038 #ifdef HAVE_UNISTD_H
00039 #include <unistd.h>
00040 #endif
00041 #include <time.h>
00042
00043 #ifndef lint
00044 FILE_RCSID("@(#)$Id: print.c,v 1.47 2005/03/14 16:56:25 christos Exp $")
00045 #endif
00046
00047 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
00048
00049 #ifndef COMPILE_ONLY
00050 protected void
00051 file_mdump(struct magic *m)
00052 {
00053
00054 private const char *typ[] = { "invalid", "byte", "short", "invalid",
00055 "long", "string", "date", "beshort",
00056 "belong", "bedate", "leshort", "lelong",
00057 "ledate", "pstring", "ldate", "beldate",
00058 "leldate", "regex" };
00059 private const char optyp[] = { '@', '&', '|', '^', '+', '-',
00060 '*', '/', '%' };
00061 (void) fputc('[', stderr);
00062 (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
00063 m->offset);
00064
00065 if (m->flag & INDIR) {
00066 (void) fprintf(stderr, "(%s,",
00067
00068 (m->in_type < SZOF(typ)) ?
00069 typ[m->in_type] : "*bad*");
00070 if (m->in_op & FILE_OPINVERSE)
00071 (void) fputc('~', stderr);
00072 (void) fprintf(stderr, "%c%d),",
00073 ((m->in_op&0x7F) < SZOF(optyp)) ?
00074 optyp[m->in_op&0x7F] : '?',
00075 m->in_offset);
00076 }
00077 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
00078
00079 (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
00080 if (m->mask_op & FILE_OPINVERSE)
00081 (void) fputc('~', stderr);
00082 if (m->mask) {
00083 if ((m->mask_op & 0x7F) < SZOF(optyp))
00084 fputc(optyp[m->mask_op&0x7F], stderr);
00085 else
00086 fputc('?', stderr);
00087 if(FILE_STRING != m->type || FILE_PSTRING != m->type)
00088 (void) fprintf(stderr, "%.8x", m->mask);
00089 else {
00090 if (m->mask & STRING_IGNORE_LOWERCASE)
00091 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
00092 if (m->mask & STRING_COMPACT_BLANK)
00093 (void) fputc(CHAR_COMPACT_BLANK, stderr);
00094 if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
00095 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
00096 stderr);
00097 }
00098 }
00099
00100 (void) fprintf(stderr, ",%c", m->reln);
00101
00102 if (m->reln != 'x') {
00103 switch (m->type) {
00104 case FILE_BYTE:
00105 case FILE_SHORT:
00106 case FILE_LONG:
00107 case FILE_LESHORT:
00108 case FILE_LELONG:
00109 case FILE_BESHORT:
00110 case FILE_BELONG:
00111 (void) fprintf(stderr, "%d", m->value.l);
00112 break;
00113 case FILE_STRING:
00114 case FILE_PSTRING:
00115 case FILE_REGEX:
00116 file_showstr(stderr, m->value.s, ~0U);
00117 break;
00118 case FILE_DATE:
00119 case FILE_LEDATE:
00120 case FILE_BEDATE:
00121 (void)fprintf(stderr, "%s,",
00122 file_fmttime(m->value.l, 1));
00123 break;
00124 case FILE_LDATE:
00125 case FILE_LELDATE:
00126 case FILE_BELDATE:
00127 (void)fprintf(stderr, "%s,",
00128 file_fmttime(m->value.l, 0));
00129 break;
00130 default:
00131 (void) fputs("*bad*", stderr);
00132 break;
00133 }
00134 }
00135 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
00136 }
00137 #endif
00138
00139
00140 protected void
00141 file_magwarn(struct magic_set *ms, const char *f, ...)
00142 {
00143 va_list va;
00144 va_start(va, f);
00145
00146
00147 (void) fflush(stdout);
00148
00149 (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
00150 (unsigned long)ms->line);
00151 (void) vfprintf(stderr, f, va);
00152 va_end(va);
00153 fputc('\n', stderr);
00154 }
00155
00156 protected const char *
00157 file_fmttime(uint32_t v, int local)
00158 {
00159 char *pp, *rt;
00160 time_t t = (time_t)v;
00161 struct tm *tm;
00162
00163 if (local) {
00164 pp = ctime(&t);
00165 } else {
00166 #ifndef HAVE_DAYLIGHT
00167 private int daylight = 0;
00168 #ifdef HAVE_TM_ISDST
00169 private time_t now = (time_t)0;
00170
00171 if (now == (time_t)0) {
00172 struct tm *tm1;
00173 (void)time(&now);
00174 tm1 = localtime(&now);
00175 if (tm1 == NULL)
00176 return "*Invalid time*";
00177 daylight = tm1->tm_isdst;
00178 }
00179 #endif
00180 #endif
00181 if (daylight)
00182 t += 3600;
00183 tm = gmtime(&t);
00184 if (tm == NULL)
00185 return "*Invalid time*";
00186 pp = asctime(tm);
00187 }
00188
00189 if ((rt = strchr(pp, '\n')) != NULL)
00190 *rt = '\0';
00191 return pp;
00192 }