| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: header.c,v 2.47 2022/03/14 22:57:24 greg Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | *  header.c - routines for reading and writing information headers. | 
| 6 | * | 
| 7 | *  Externals declared in rtio.h | 
| 8 | * | 
| 9 | *  newheader(t,fp)     start new information header identified by string t | 
| 10 | *  headidval(r,s)      copy header identifier value in s to r | 
| 11 | *  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 | *  fputnow(fp)         put out the current date and time | 
| 15 | *  printargs(ac,av,fp) print an argument list to fp, followed by '\n' | 
| 16 | *  formatval(r,s)      copy the format value in s to r | 
| 17 | *  fputformat(s,fp)    write "FORMAT=%s" to fp | 
| 18 | *  nativebigendian()   are we native on big-endian machine? | 
| 19 | *  isbigendian(s)      header line matches "BigEndian=1" (-1 if irrelevant) | 
| 20 | *  fputendian(fp)      write native endianness in BigEndian= line | 
| 21 | *  getheader(fp,f,p)   read header from fp, calling f(s,p) on each line | 
| 22 | *  globmatch(pat, str) check for glob match of str against pat | 
| 23 | *  checkheader(i,p,o)  check header format from i against p and copy to o | 
| 24 | * | 
| 25 | *  To copy header from input to output, use getheader(fin, fputs, fout) | 
| 26 | */ | 
| 27 |  | 
| 28 | #include "copyright.h" | 
| 29 |  | 
| 30 | #include  <ctype.h> | 
| 31 |  | 
| 32 | #include  "tiff.h"      /* for int32 */ | 
| 33 | #include  "rtio.h" | 
| 34 | #include  "color.h" | 
| 35 | #include  "resolu.h" | 
| 36 |  | 
| 37 | #define  MAXLINE        2048 | 
| 38 |  | 
| 39 | extern time_t           timegm(struct tm *tm); | 
| 40 |  | 
| 41 | const char  HDRSTR[] = "#?";            /* information header magic number */ | 
| 42 |  | 
| 43 | const char  FMTSTR[] = "FORMAT=";       /* format identifier */ | 
| 44 |  | 
| 45 | const char  TMSTR[] = "CAPDATE=";       /* capture date identifier */ | 
| 46 | const char  GMTSTR[] = "GMT=";          /* GMT identifier */ | 
| 47 |  | 
| 48 | const char  BIGEND[] = "BigEndian=";    /* big-endian variable */ | 
| 49 |  | 
| 50 | static gethfunc mycheck; | 
| 51 |  | 
| 52 |  | 
| 53 | void | 
| 54 | newheader(              /* identifying line of information header */ | 
| 55 | const char  *s, | 
| 56 | FILE  *fp | 
| 57 | ) | 
| 58 | { | 
| 59 | fputs(HDRSTR, fp); | 
| 60 | fputs(s, fp); | 
| 61 | fputc('\n', fp); | 
| 62 | } | 
| 63 |  | 
| 64 |  | 
| 65 | int | 
| 66 | headidval(                      /* get header id (return true if is id) */ | 
| 67 | char  *r, | 
| 68 | const char      *s | 
| 69 | ) | 
| 70 | { | 
| 71 | const char  *cp = HDRSTR; | 
| 72 |  | 
| 73 | while (*cp) if (*cp++ != *s++) return(0); | 
| 74 | if (r == NULL) return(1); | 
| 75 | while (*s && !isspace(*s)) *r++ = *s++; | 
| 76 | *r = '\0'; | 
| 77 | return(1); | 
| 78 | } | 
| 79 |  | 
| 80 |  | 
| 81 | int | 
| 82 | dateval(                /* convert capture date line to UTC */ | 
| 83 | time_t  *tloc, | 
| 84 | const char      *s | 
| 85 | ) | 
| 86 | { | 
| 87 | struct tm       tms; | 
| 88 | const char      *cp = TMSTR; | 
| 89 |  | 
| 90 | while (*cp) if (*cp++ != *s++) return(0); | 
| 91 | while (isspace(*s)) s++; | 
| 92 | if (!*s) return(0); | 
| 93 | if (sscanf(s, "%d:%d:%d %d:%d:%d", | 
| 94 | &tms.tm_year, &tms.tm_mon, &tms.tm_mday, | 
| 95 | &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) | 
| 96 | return(0); | 
| 97 | if (tloc == NULL) | 
| 98 | return(1); | 
| 99 | tms.tm_mon--; | 
| 100 | tms.tm_year -= 1900; | 
| 101 | tms.tm_isdst = -1;      /* ask mktime() to figure out DST */ | 
| 102 | *tloc = mktime(&tms); | 
| 103 | return(1); | 
| 104 | } | 
| 105 |  | 
| 106 |  | 
| 107 | int | 
| 108 | gmtval(                 /* convert GMT date line to UTC */ | 
| 109 | time_t  *tloc, | 
| 110 | const char      *s | 
| 111 | ) | 
| 112 | { | 
| 113 | struct tm       tms; | 
| 114 | const char      *cp = GMTSTR; | 
| 115 |  | 
| 116 | while (*cp) if (*cp++ != *s++) return(0); | 
| 117 | while (isspace(*s)) s++; | 
| 118 | if (!*s) return(0); | 
| 119 | if (sscanf(s, "%d:%d:%d %d:%d:%d", | 
| 120 | &tms.tm_year, &tms.tm_mon, &tms.tm_mday, | 
| 121 | &tms.tm_hour, &tms.tm_min, &tms.tm_sec) != 6) | 
| 122 | return(0); | 
| 123 | if (tloc == NULL) | 
| 124 | return(1); | 
| 125 | tms.tm_mon--; | 
| 126 | tms.tm_year -= 1900; | 
| 127 | *tloc = timegm(&tms); | 
| 128 | return(1); | 
| 129 | } | 
| 130 |  | 
| 131 |  | 
| 132 | void | 
| 133 | fputdate(               /* write out the given time value (local & GMT) */ | 
| 134 | time_t  tv, | 
| 135 | FILE    *fp | 
| 136 | ) | 
| 137 | { | 
| 138 | struct tm       *tms; | 
| 139 |  | 
| 140 | tms = localtime(&tv); | 
| 141 | if (tms != NULL) | 
| 142 | fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", TMSTR, | 
| 143 | tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, | 
| 144 | tms->tm_hour, tms->tm_min, tms->tm_sec); | 
| 145 | tms = gmtime(&tv); | 
| 146 | if (tms != NULL) | 
| 147 | fprintf(fp, "%s %04d:%02d:%02d %02d:%02d:%02d\n", GMTSTR, | 
| 148 | tms->tm_year+1900, tms->tm_mon+1, tms->tm_mday, | 
| 149 | tms->tm_hour, tms->tm_min, tms->tm_sec); | 
| 150 | } | 
| 151 |  | 
| 152 |  | 
| 153 | void | 
| 154 | fputnow(                        /* write out the current time */ | 
| 155 | FILE    *fp | 
| 156 | ) | 
| 157 | { | 
| 158 | time_t  tv; | 
| 159 | time(&tv); | 
| 160 | fputdate(tv, fp); | 
| 161 | } | 
| 162 |  | 
| 163 |  | 
| 164 | void | 
| 165 | printargs(              /* print arguments to a file */ | 
| 166 | int  ac, | 
| 167 | char  **av, | 
| 168 | FILE  *fp | 
| 169 | ) | 
| 170 | { | 
| 171 | #if defined(_WIN32) || defined(_WIN64) | 
| 172 | extern char     *fixargv0(char *arg0); | 
| 173 | char            myav0[128]; | 
| 174 | /* clean up Windows executable path */ | 
| 175 | if (ac-- <= 0) return; | 
| 176 | fputs(fixargv0(strcpy(myav0, *av++)), fp); | 
| 177 | fputc(ac ? ' ' : '\n', fp); | 
| 178 | #endif | 
| 179 | while (ac-- > 0) { | 
| 180 | fputword(*av++, fp); | 
| 181 | fputc(ac ? ' ' : '\n', fp); | 
| 182 | } | 
| 183 | } | 
| 184 |  | 
| 185 |  | 
| 186 | int | 
| 187 | formatval(                      /* get format value (return true if format) */ | 
| 188 | char  fmt[MAXFMTLEN], | 
| 189 | const char  *s | 
| 190 | ) | 
| 191 | { | 
| 192 | const char  *cp = FMTSTR; | 
| 193 | char  *r = fmt; | 
| 194 | /* check against format string */ | 
| 195 | while (*cp) if (*cp++ != *s++) return(0); | 
| 196 | while (isspace(*s)) s++; | 
| 197 | if (!*s) return(0); | 
| 198 | if (r == NULL)          /* just checking if format? */ | 
| 199 | return(1); | 
| 200 | do                      /* copy format ID */ | 
| 201 | *r++ = *s++; | 
| 202 | while (*s && r-fmt < MAXFMTLEN-1); | 
| 203 |  | 
| 204 | do                      /* remove trailing white space */ | 
| 205 | *r-- = '\0'; | 
| 206 | while (r > fmt && isspace(*r)); | 
| 207 |  | 
| 208 | return(1); | 
| 209 | } | 
| 210 |  | 
| 211 |  | 
| 212 | void | 
| 213 | fputformat(             /* put out a format value */ | 
| 214 | const char  *s, | 
| 215 | FILE  *fp | 
| 216 | ) | 
| 217 | { | 
| 218 | int     align = 0; | 
| 219 |  | 
| 220 | fputs(FMTSTR, fp); | 
| 221 | fputs(s, fp); | 
| 222 | /* pad to align binary type for mmap() */ | 
| 223 | if (globmatch(PICFMT, s)) | 
| 224 | align = 0;      /* not needed for picture data */ | 
| 225 | else if (!strncmp("float", s, 5)) | 
| 226 | align = sizeof(float); | 
| 227 | else if (!strncmp("double", s, 6)) | 
| 228 | align = sizeof(double); | 
| 229 | else if (!strncmp("16-bit", s, 6)) | 
| 230 | align = 2; | 
| 231 | else if (!strncmp("32-bit", s, 6)) | 
| 232 | align = 4; | 
| 233 | else if (!strncmp("64-bit", s, 6)) | 
| 234 | align = 8; | 
| 235 | if (align) { | 
| 236 | long    pos = ftell(fp); | 
| 237 | if (pos >= 0) { | 
| 238 | pos = (pos + 2) % align; | 
| 239 | if (pos) align -= pos; | 
| 240 | else align = 0; | 
| 241 | } else | 
| 242 | align = 0; | 
| 243 | } | 
| 244 | while (align-- > 0) | 
| 245 | putc(' ', fp); | 
| 246 | fputc('\n', fp); | 
| 247 | } | 
| 248 |  | 
| 249 |  | 
| 250 | int | 
| 251 | nativebigendian()       /* are we native on a big-endian machine? */ | 
| 252 | { | 
| 253 | union { int32 i; char c[4]; } u; | 
| 254 |  | 
| 255 | u.i = 1; | 
| 256 |  | 
| 257 | return(u.c[0] == 0); | 
| 258 | } | 
| 259 |  | 
| 260 |  | 
| 261 | int | 
| 262 | isbigendian(            /* header line says "BigEndian=1" (-1 if irrelevant) */ | 
| 263 | const char *s | 
| 264 | ) | 
| 265 | { | 
| 266 | const char      *be = BIGEND; | 
| 267 |  | 
| 268 | while ((*s != '\0') & (*be != '=') && *s++ == *be) | 
| 269 | ++be; | 
| 270 | if (*be != '=') | 
| 271 | return(-1);     /* irrelevant */ | 
| 272 | while (isspace(*s)) | 
| 273 | s++; | 
| 274 | if (*s++ != '=') | 
| 275 | return(-1); | 
| 276 | while (isspace(*s)) | 
| 277 | s++; | 
| 278 | return(*s == '1'); | 
| 279 | } | 
| 280 |  | 
| 281 |  | 
| 282 | void | 
| 283 | fputendian(             /* write native endianness in BigEndian= line */ | 
| 284 | FILE *fp | 
| 285 | ) | 
| 286 | { | 
| 287 | fputs(BIGEND, fp); | 
| 288 | if (nativebigendian()) | 
| 289 | fputs("1\n", fp); | 
| 290 | else | 
| 291 | fputs("0\n", fp); | 
| 292 | } | 
| 293 |  | 
| 294 |  | 
| 295 | int | 
| 296 | getheader(              /* get header from file */ | 
| 297 | FILE  *fp, | 
| 298 | gethfunc *f, | 
| 299 | void  *p | 
| 300 | ) | 
| 301 | { | 
| 302 | int   rtotal = 0; | 
| 303 | char  buf[MAXLINE]; | 
| 304 | int   firstc = fgetc(fp); | 
| 305 |  | 
| 306 | if (!isprint(firstc)) | 
| 307 | return(-1);                             /* messed up */ | 
| 308 | ungetc(firstc, fp); | 
| 309 | for ( ; ; ) { | 
| 310 | int     rval = 0; | 
| 311 | buf[MAXLINE-2] = '\n'; | 
| 312 | if (fgets(buf, MAXLINE, fp) == NULL) | 
| 313 | return(-1); | 
| 314 | if (buf[buf[0]=='\r'] == '\n')          /* end of header? */ | 
| 315 | return(rtotal); | 
| 316 | if (buf[MAXLINE-2] != '\n') { | 
| 317 | ungetc(buf[MAXLINE-2], fp);     /* prevent false end */ | 
| 318 | buf[MAXLINE-2] = '\0'; | 
| 319 | } | 
| 320 | if (f != NULL && (rval = (*f)(buf, p)) < 0) | 
| 321 | return(-1); | 
| 322 | rtotal += rval; | 
| 323 | } | 
| 324 | } | 
| 325 |  | 
| 326 |  | 
| 327 | struct check { | 
| 328 | FILE    *fp; | 
| 329 | char    fs[MAXFMTLEN]; | 
| 330 | }; | 
| 331 |  | 
| 332 |  | 
| 333 | static int | 
| 334 | mycheck(                        /* check a header line for format info. */ | 
| 335 | char  *s, | 
| 336 | void  *cp | 
| 337 | ) | 
| 338 | { | 
| 339 | struct check    *scp = (struct check *)cp; | 
| 340 |  | 
| 341 | if (!formatval(scp->fs, s) && scp->fp != NULL) | 
| 342 | return(fputs(s, scp->fp)); | 
| 343 |  | 
| 344 | return(0); | 
| 345 | } | 
| 346 |  | 
| 347 |  | 
| 348 | int | 
| 349 | globmatch(                      /* check for match of s against pattern p */ | 
| 350 | const char      *p, | 
| 351 | const char      *s | 
| 352 | ) | 
| 353 | { | 
| 354 | int     setmatch; | 
| 355 |  | 
| 356 | do { | 
| 357 | switch (*p) { | 
| 358 | case '?':                       /* match any character */ | 
| 359 | if (!*s++) | 
| 360 | return(0); | 
| 361 | break; | 
| 362 | case '*':                       /* match any string */ | 
| 363 | while (p[1] == '*') p++; | 
| 364 | do | 
| 365 | if ( (p[1]=='?') | (p[1]==*s) && | 
| 366 | globmatch(p+1,s) ) | 
| 367 | return(1); | 
| 368 | while (*s++); | 
| 369 | return(0); | 
| 370 | case '[':                       /* character set */ | 
| 371 | setmatch = (*s == *++p); | 
| 372 | if (!*p) | 
| 373 | return(0); | 
| 374 | while (*++p != ']') { | 
| 375 | if (!*p) | 
| 376 | return(0); | 
| 377 | if (*p == '-') { | 
| 378 | setmatch += (p[-1] <= *s) & (*s <= p[1]); | 
| 379 | if (!*++p) | 
| 380 | break; | 
| 381 | } else | 
| 382 | setmatch += (*p == *s); | 
| 383 | } | 
| 384 | if (!setmatch) | 
| 385 | return(0); | 
| 386 | s++; | 
| 387 | break; | 
| 388 | case '\\':                      /* literal next */ | 
| 389 | if (!*++p) | 
| 390 | return(0); | 
| 391 | /* fall through */ | 
| 392 | default:                        /* normal character */ | 
| 393 | if (*p != *s) | 
| 394 | return(0); | 
| 395 | s++; | 
| 396 | break; | 
| 397 | } | 
| 398 | } while (*p++); | 
| 399 | return(1); | 
| 400 | } | 
| 401 |  | 
| 402 |  | 
| 403 | /* | 
| 404 | * Checkheader(fin,fmt,fout) returns a value of 1 if the input format | 
| 405 | * matches the specification in fmt, 0 if no input format was found, | 
| 406 | * and -1 if the input format does not match or there is an | 
| 407 | * error reading the header.  If fmt is empty, then -1 is returned | 
| 408 | * if any input format is found (or there is an error), and 0 otherwise. | 
| 409 | * If fmt contains any '*' or '?' characters, then checkheader | 
| 410 | * does wildcard expansion and copies a matching result into fmt. | 
| 411 | * Be sure that fmt is big enough to hold the match in such cases, | 
| 412 | * and that it is not a static, read-only string! | 
| 413 | * The input header (minus any format lines) is copied to fout | 
| 414 | * if fout is not NULL. | 
| 415 | */ | 
| 416 |  | 
| 417 | int | 
| 418 | checkheader( | 
| 419 | FILE  *fin, | 
| 420 | char  fmt[MAXFMTLEN], | 
| 421 | FILE  *fout | 
| 422 | ) | 
| 423 | { | 
| 424 | struct check    cdat; | 
| 425 | char    *cp; | 
| 426 |  | 
| 427 | cdat.fp = fout; | 
| 428 | cdat.fs[0] = '\0'; | 
| 429 | if (getheader(fin, mycheck, &cdat) < 0) | 
| 430 | return(-1); | 
| 431 | if (!cdat.fs[0]) | 
| 432 | return(0); | 
| 433 | for (cp = fmt; *cp; cp++)               /* check for globbing */ | 
| 434 | if ((*cp == '?') | (*cp == '*')) { | 
| 435 | if (globmatch(fmt, cdat.fs)) { | 
| 436 | strcpy(fmt, cdat.fs); | 
| 437 | return(1); | 
| 438 | } else | 
| 439 | return(-1); | 
| 440 | } | 
| 441 | return(strcmp(fmt, cdat.fs) ? -1 : 1);  /* literal match */ | 
| 442 | } |