file/src/print.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Ian F. Darwin 1986-1995.
00003  * Software written by Ian F. Darwin and others;
00004  * maintained 1995-present by Christos Zoulas and others.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice immediately at the beginning of the file, without modification,
00011  *    this list of conditions, and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  *  
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
00020  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00022  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00023  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00024  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00025  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00026  * SUCH DAMAGE.
00027  */
00028 /*
00029  * print.c - debugging printout routines
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("@(#)$File: print.c,v 1.58 2007/01/16 14:58:48 ljt Exp $")
00045 #endif  /* lint */
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         private const char optyp[] = { FILE_OPS };
00054 
00055         (void) fprintf(stderr, "[%zu", m->lineno);
00056         (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
00057                        m->offset);
00058 
00059         if (m->flag & INDIR) {
00060                 (void) fprintf(stderr, "(%s,",
00061                                /* Note: type is unsigned */
00062                                (m->in_type < file_nnames) ? 
00063                                         file_names[m->in_type] : "*bad*");
00064                 if (m->in_op & FILE_OPINVERSE)
00065                         (void) fputc('~', stderr);
00066                 (void) fprintf(stderr, "%c%d),",
00067                                ((m->in_op & FILE_OPS_MASK) < SZOF(optyp)) ? 
00068                                         optyp[m->in_op & FILE_OPS_MASK] : '?',
00069                                 m->in_offset);
00070         }
00071         (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
00072                        /* Note: type is unsigned */
00073                        (m->type < file_nnames) ? file_names[m->type] : "*bad*");
00074         if (m->mask_op & FILE_OPINVERSE)
00075                 (void) fputc('~', stderr);
00076 
00077         if (IS_STRING(m->type)) {
00078                 if (m->str_flags) {
00079                         (void) fputc('/', stderr);
00080                         if (m->str_flags & STRING_COMPACT_BLANK) 
00081                                 (void) fputc(CHAR_COMPACT_BLANK, stderr);
00082                         if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK) 
00083                                 (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
00084                                 stderr);
00085                         if (m->str_flags & STRING_IGNORE_LOWERCASE) 
00086                                 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
00087                         if (m->str_flags & STRING_IGNORE_UPPERCASE) 
00088                                 (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
00089                         if (m->str_flags & REGEX_OFFSET_START) 
00090                                 (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
00091                 }
00092                 if (m->str_count)
00093                         (void) fprintf(stderr, "/%u", m->str_count);
00094         }
00095         else {
00096                 if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
00097                         (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
00098                 else
00099                         (void) fputc('?', stderr);
00100 
00101                 if (m->num_mask) {
00102                         (void) fprintf(stderr, "%.8llx",
00103                             (unsigned long long)m->num_mask);
00104                 }
00105         }
00106         (void) fprintf(stderr, ",%c", m->reln);
00107 
00108         if (m->reln != 'x') {
00109                 switch (m->type) {
00110                 case FILE_BYTE:
00111                 case FILE_SHORT:
00112                 case FILE_LONG:
00113                 case FILE_LESHORT:
00114                 case FILE_LELONG:
00115                 case FILE_MELONG:
00116                 case FILE_BESHORT:
00117                 case FILE_BELONG:
00118                         (void) fprintf(stderr, "%d", m->value.l);
00119                         break;
00120                 case FILE_BEQUAD:
00121                 case FILE_LEQUAD:
00122                 case FILE_QUAD:
00123                         (void) fprintf(stderr, "%lld",
00124                             (unsigned long long)m->value.q);
00125                         break;
00126                 case FILE_PSTRING:
00127                 case FILE_STRING:
00128                 case FILE_REGEX:
00129                 case FILE_BESTRING16:
00130                 case FILE_LESTRING16:
00131                 case FILE_SEARCH:
00132                         file_showstr(stderr, m->value.s, (size_t)m->vallen);
00133                         break;
00134                 case FILE_DATE:
00135                 case FILE_LEDATE:
00136                 case FILE_BEDATE:
00137                 case FILE_MEDATE:
00138                         (void)fprintf(stderr, "%s,",
00139                             file_fmttime(m->value.l, 1));
00140                         break;
00141                 case FILE_LDATE:
00142                 case FILE_LELDATE:
00143                 case FILE_BELDATE:
00144                 case FILE_MELDATE:
00145                         (void)fprintf(stderr, "%s,",
00146                             file_fmttime(m->value.l, 0));
00147                         break;
00148                 case FILE_QDATE:
00149                 case FILE_LEQDATE:
00150                 case FILE_BEQDATE:
00151                         (void)fprintf(stderr, "%s,",
00152                             file_fmttime((uint32_t)m->value.q, 1));
00153                         break;
00154                 case FILE_QLDATE:
00155                 case FILE_LEQLDATE:
00156                 case FILE_BEQLDATE:
00157                         (void)fprintf(stderr, "%s,",
00158                             file_fmttime((uint32_t)m->value.q, 0));
00159                         break;
00160                 case FILE_DEFAULT:
00161                         /* XXX - do anything here? */
00162                         break;
00163                 default:
00164                         (void) fputs("*bad*", stderr);
00165                         break;
00166                 }
00167         }
00168         (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
00169 }
00170 #endif
00171 
00172 /*VARARGS*/
00173 protected void
00174 file_magwarn(struct magic_set *ms, const char *f, ...)
00175 {
00176         va_list va;
00177         va_start(va, f);
00178 
00179         /* cuz we use stdout for most, stderr here */
00180         (void) fflush(stdout); 
00181 
00182         (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
00183             (unsigned long)ms->line);
00184         (void) vfprintf(stderr, f, va);
00185         va_end(va);
00186         (void) fputc('\n', stderr);
00187 }
00188 
00189 protected const char *
00190 file_fmttime(uint32_t v, int local)
00191 {
00192         char *pp, *rt;
00193         time_t t = (time_t)v;
00194         struct tm *tm;
00195 
00196         if (local) {
00197                 pp = ctime(&t);
00198         } else {
00199 #ifndef HAVE_DAYLIGHT
00200                 private int daylight = 0;
00201 #ifdef HAVE_TM_ISDST
00202                 private time_t now = (time_t)0;
00203 
00204                 if (now == (time_t)0) {
00205                         struct tm *tm1;
00206                         (void)time(&now);
00207                         tm1 = localtime(&now);
00208                         if (tm1 == NULL)
00209                                 return "*Invalid time*";
00210                         daylight = tm1->tm_isdst;
00211                 }
00212 #endif /* HAVE_TM_ISDST */
00213 #endif /* HAVE_DAYLIGHT */
00214                 if (daylight)
00215                         t += 3600;
00216                 tm = gmtime(&t);
00217                 if (tm == NULL)
00218                         return "*Invalid time*";
00219                 pp = asctime(tm);
00220         }
00221 
00222         if ((rt = strchr(pp, '\n')) != NULL)
00223                 *rt = '\0';
00224         return pp;
00225 }

Generated on Fri May 25 21:18:11 2007 for rpm by  doxygen 1.5.2