ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/header.c
Revision: 2.32
Committed: Thu Aug 2 18:33:42 2018 UTC (5 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2
Changes since 2.31: +6 -5 lines
Log Message:
Created MAXFMTLEN to guard against buffer overrun attacks in header input

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.32 static const char RCSid[] = "$Id: header.c,v 2.31 2017/05/10 18:02:08 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * header.c - routines for reading and writing information headers.
6     *
7 greg 2.11 * Externals declared in resolu.h
8 greg 1.3 *
9 greg 2.4 * newheader(t,fp) start new information header identified by string t
10     * headidval(r,s) copy header identifier value in s to r
11 greg 2.25 * dateval(t,s) get capture date value as UTC
12     * gmtval(t,s) get GMT as UTC
13     * fputdate(t,fp) put out the given UTC
14 greg 2.11 * fputnow(fp) put out the current date and time
15 greg 2.3 * printargs(ac,av,fp) print an argument list to fp, followed by '\n'
16 greg 1.3 * formatval(r,s) copy the format value in s to r
17     * fputformat(s,fp) write "FORMAT=%s" to fp
18     * getheader(fp,f,p) read header from fp, calling f(s,p) on each line
19 greg 2.6 * globmatch(pat, str) check for glob match of str against pat
20 greg 1.3 * checkheader(i,p,o) check header format from i against p and copy to o
21     *
22     * To copy header from input to output, use getheader(fin, fputs, fout)
23 greg 1.1 */
24    
25 greg 2.12 #include "copyright.h"
26 greg 2.11
27 greg 1.3 #include <ctype.h>
28 schorsch 2.17
29 greg 2.18 #include "rtio.h"
30     #include "resolu.h"
31 schorsch 2.14
32 greg 2.24 #define MAXLINE 2048
33 greg 2.2
34 greg 2.26 extern time_t timegm(struct tm *tm);
35    
36 greg 2.25 const char HDRSTR[] = "#?"; /* information header magic number */
37 greg 1.2
38 greg 2.25 const char FMTSTR[] = "FORMAT="; /* format identifier */
39 greg 1.2
40 greg 2.25 const char TMSTR[] = "CAPDATE="; /* capture date identifier */
41     const char GMTSTR[] = "GMT="; /* GMT identifier */
42 greg 2.16
43 schorsch 2.22 static gethfunc mycheck;
44 greg 2.4
45 greg 2.11
46 greg 2.29 void
47 schorsch 2.22 newheader( /* identifying line of information header */
48 greg 2.31 const char *s,
49 greg 2.25 FILE *fp
50 schorsch 2.22 )
51 greg 2.4 {
52     fputs(HDRSTR, fp);
53     fputs(s, fp);
54     putc('\n', fp);
55     }
56    
57    
58 greg 2.29 int
59 schorsch 2.22 headidval( /* get header id (return true if is id) */
60 greg 2.25 char *r,
61 greg 2.31 const char *s
62 schorsch 2.22 )
63 greg 2.4 {
64 greg 2.25 const char *cp = HDRSTR;
65 greg 2.4
66     while (*cp) if (*cp++ != *s++) return(0);
67     if (r == NULL) return(1);
68 gregl 2.9 while (*s && !isspace(*s)) *r++ = *s++;
69 greg 2.4 *r = '\0';
70     return(1);
71     }
72    
73    
74 greg 2.29 int
75 greg 2.25 dateval( /* convert capture date line to UTC */
76 schorsch 2.22 time_t *tloc,
77 greg 2.31 const char *s
78 schorsch 2.22 )
79 greg 2.11 {
80     struct tm tms;
81 greg 2.25 const char *cp = TMSTR;
82 greg 2.11
83     while (*cp) if (*cp++ != *s++) return(0);
84     while (isspace(*s)) s++;
85     if (!*s) return(0);
86     if (sscanf(s, "%d:%d:%d %d:%d:%d",
87     &tms.tm_year, &tms.tm_mon, &tms.tm_mday,
88     &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6)
89     return(0);
90     if (tloc == NULL)
91     return(1);
92     tms.tm_mon--;
93     tms.tm_year -= 1900;
94     tms.tm_isdst = -1; /* ask mktime() to figure out DST */
95     *tloc = mktime(&tms);
96     return(1);
97     }
98    
99    
100 greg 2.29 int
101 greg 2.25 gmtval( /* convert GMT date line to UTC */
102     time_t *tloc,
103 greg 2.31 const char *s
104 schorsch 2.22 )
105 greg 2.11 {
106 greg 2.25 struct tm tms;
107     const char *cp = GMTSTR;
108    
109     while (*cp) if (*cp++ != *s++) return(0);
110     while (isspace(*s)) s++;
111     if (!*s) return(0);
112     if (sscanf(s, "%d:%d:%d %d:%d:%d",
113     &tms.tm_year, &tms.tm_mon, &tms.tm_mday,
114     &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6)
115     return(0);
116     if (tloc == NULL)
117     return(1);
118     tms.tm_mon--;
119     tms.tm_year -= 1900;
120     *tloc = timegm(&tms);
121     return(1);
122 greg 2.11 }
123    
124    
125 greg 2.29 void
126 greg 2.25 fputdate( /* write out the given time value (local & GMT) */
127 schorsch 2.22 time_t tv,
128     FILE *fp
129     )
130 greg 2.11 {
131 greg 2.25 struct tm *tms;
132    
133     tms = localtime(&tv);
134     if (tms != NULL)
135     fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR,
136     tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday,
137     tms->tm_hour, tms->tm_min, tms->tm_sec);
138     tms = gmtime(&tv);
139     if (tms != NULL)
140     fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", GMTSTR,
141     tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday,
142     tms->tm_hour, tms->tm_min, tms->tm_sec);
143 greg 2.11 }
144    
145    
146 greg 2.29 void
147 schorsch 2.22 fputnow( /* write out the current time */
148     FILE *fp
149     )
150 greg 2.11 {
151     time_t tv;
152     time(&tv);
153     fputdate(tv, fp);
154     }
155    
156    
157 greg 2.29 void
158 schorsch 2.22 printargs( /* print arguments to a file */
159     int ac,
160     char **av,
161     FILE *fp
162     )
163 greg 1.1 {
164     while (ac-- > 0) {
165 greg 2.11 fputword(*av++, fp);
166     fputc(ac ? ' ' : '\n', fp);
167 greg 1.1 }
168     }
169    
170    
171 greg 2.29 int
172 schorsch 2.22 formatval( /* get format value (return true if format) */
173 greg 2.32 char fmt[MAXFMTLEN],
174 greg 2.31 const char *s
175 schorsch 2.22 )
176 greg 1.2 {
177 greg 2.25 const char *cp = FMTSTR;
178 greg 2.32 char *r = fmt;
179 greg 2.4
180     while (*cp) if (*cp++ != *s++) return(0);
181 greg 1.3 while (isspace(*s)) s++;
182 greg 2.4 if (!*s) return(0);
183     if (r == NULL) return(1);
184 greg 2.5 do
185     *r++ = *s++;
186 greg 2.32 while (*s && !isspace(*s) && r-fmt < MAXFMTLEN-1);
187 greg 1.2 *r = '\0';
188 greg 2.4 return(1);
189 greg 1.2 }
190    
191    
192 greg 2.29 void
193 schorsch 2.22 fputformat( /* put out a format value */
194 greg 2.31 const char *s,
195 schorsch 2.22 FILE *fp
196     )
197 greg 1.2 {
198     fputs(FMTSTR, fp);
199     fputs(s, fp);
200     putc('\n', fp);
201     }
202    
203    
204 greg 2.29 int
205 schorsch 2.22 getheader( /* get header from file */
206     FILE *fp,
207     gethfunc *f,
208     void *p
209     )
210 greg 1.1 {
211 greg 2.30 int rtotal = 0;
212 greg 1.1 char buf[MAXLINE];
213    
214     for ( ; ; ) {
215 greg 2.30 int rval = 0;
216 greg 1.1 buf[MAXLINE-2] = '\n';
217 greg 2.3 if (fgets(buf, MAXLINE, fp) == NULL)
218 greg 1.1 return(-1);
219 greg 2.28 if (buf[buf[0]=='\r'] == '\n')
220 greg 2.30 return(rtotal);
221 greg 1.1 if (buf[MAXLINE-2] != '\n') {
222     ungetc(buf[MAXLINE-2], fp); /* prevent false end */
223     buf[MAXLINE-2] = '\0';
224     }
225 greg 2.30 if (f != NULL && (rval = (*f)(buf, p)) < 0)
226 gwlarson 2.10 return(-1);
227 greg 2.30 rtotal += rval;
228 greg 1.1 }
229     }
230    
231    
232 greg 1.2 struct check {
233     FILE *fp;
234 greg 2.32 char fs[MAXFMTLEN];
235 greg 1.2 };
236 greg 1.1
237 greg 1.2
238 greg 2.11 static int
239 schorsch 2.22 mycheck( /* check a header line for format info. */
240     char *s,
241     void *cp
242     )
243     {
244     if (!formatval(((struct check*)cp)->fs, s)
245     && ((struct check*)cp)->fp != NULL) {
246     fputs(s, ((struct check*)cp)->fp);
247     }
248 greg 2.11 return(0);
249 greg 1.1 }
250    
251    
252 greg 2.29 int
253 schorsch 2.22 globmatch( /* check for match of s against pattern p */
254 greg 2.31 const char *p,
255     const char *s
256 schorsch 2.22 )
257 greg 1.3 {
258 greg 2.11 int setmatch;
259 greg 1.3
260     do {
261     switch (*p) {
262     case '?': /* match any character */
263     if (!*s++)
264     return(0);
265     break;
266     case '*': /* match any string */
267     while (p[1] == '*') p++;
268     do
269 greg 2.28 if ( (p[1]=='?') | (p[1]==*s) &&
270 greg 2.6 globmatch(p+1,s) )
271 greg 1.3 return(1);
272     while (*s++);
273     return(0);
274 greg 2.11 case '[': /* character set */
275     setmatch = *s == *++p;
276     if (!*p)
277     return(0);
278     while (*++p != ']') {
279     if (!*p)
280     return(0);
281     if (*p == '-') {
282 greg 2.28 setmatch += (p[-1] <= *s && *s <= p[1]);
283 greg 2.11 if (!*++p)
284     break;
285     } else
286 greg 2.28 setmatch += (*p == *s);
287 greg 2.11 }
288     if (!setmatch)
289     return(0);
290     s++;
291     break;
292 greg 1.3 case '\\': /* literal next */
293     p++;
294     /* fall through */
295     default: /* normal character */
296     if (*p != *s)
297     return(0);
298     s++;
299     break;
300     }
301     } while (*p++);
302     return(1);
303     }
304    
305    
306     /*
307     * Checkheader(fin,fmt,fout) returns a value of 1 if the input format
308     * matches the specification in fmt, 0 if no input format was found,
309     * and -1 if the input format does not match or there is an
310 greg 1.4 * error reading the header. If fmt is empty, then -1 is returned
311 greg 1.3 * if any input format is found (or there is an error), and 0 otherwise.
312     * If fmt contains any '*' or '?' characters, then checkheader
313     * does wildcard expansion and copies a matching result into fmt.
314 greg 2.7 * Be sure that fmt is big enough to hold the match in such cases,
315     * and that it is not a static, read-only string!
316 greg 1.3 * The input header (minus any format lines) is copied to fout
317     * if fout is not NULL.
318     */
319    
320 greg 2.29 int
321 schorsch 2.22 checkheader(
322     FILE *fin,
323 greg 2.32 char fmt[MAXFMTLEN],
324 schorsch 2.22 FILE *fout
325     )
326 greg 1.1 {
327 greg 1.2 struct check cdat;
328 greg 2.25 char *cp;
329 greg 1.2
330     cdat.fp = fout;
331     cdat.fs[0] = '\0';
332 schorsch 2.22 if (getheader(fin, mycheck, &cdat) < 0)
333 greg 1.3 return(-1);
334 greg 2.6 if (!cdat.fs[0])
335     return(0);
336 greg 2.7 for (cp = fmt; *cp; cp++) /* check for globbing */
337 schorsch 2.20 if ((*cp == '?') | (*cp == '*')) {
338 greg 2.7 if (globmatch(fmt, cdat.fs)) {
339     strcpy(fmt, cdat.fs);
340     return(1);
341     } else
342     return(-1);
343 schorsch 2.19 }
344 greg 2.7 return(strcmp(fmt, cdat.fs) ? -1 : 1); /* literal match */
345 greg 1.1 }