eat_header(char *str) { /* This attempts to determine if we are reading headers. It is not foolproof -- if something looks like a header but really isn't, it won't get into the delivered message. As long as the message is in the standard format (headers, blank line, text) it'll be ok. */ static int lastheader = HEADER_NONE; int header, is_cont; char *nl, *htext; if (str[0] == '\n' || str[0] == '\0') return 0; /* Chop newline */ nl = strchr(str, '\n'); if (nl != NULL) *nl = '\0'; if (str[0] == ' ' || str[0] == '\t') { header = lastheader; for (htext = str; *htext == ' ' || *htext == '\t'; htext++) ; is_cont = 1; } else { htext = identify_header(str, &header); is_cont = 0; } lastheader = header; switch (header) { case HEADER_NONE: return 0; case HEADER_OTHER: break; case HEADER_SUBJECT: if (!is_cont) strncpy(subject, htext, sizeof subject - 1); break; case HEADER_FROM: case HEADER_REPLYTO: if (!is_cont) split_sender(header, htext); break; } return 1; }
int main(int argc, char *argv[]) { int retval = EXIT_FAILURE, ok = 0, fail = 1, n = 0, i = 0, offset = 0; int offsets[MAX_HEAD_SIZE] = { 0 }; char *buf = NULL, *ptr = NULL, *fname = NULL, *log = NULL; size_t size = 0, nsize = 0; /* Check usage */ if(argc < 2 || argv[1][0] == '-') { fprintf(stderr, USAGE, argv[0]); goto end; } else { fname = argv[1]; if(argc == 3) { log = argv[2]; } } /* Read in target file */ buf = file_read(fname, &size); if(buf && size > MIN_FILE_SIZE) { /* Parse in the log file, if any */ n = parse_log(log, offsets); fprintf(stderr, "Processing %d header(s) from %s...\n", n, fname); /* Loop through each offset in the integer array */ for(i=0; i<n; i++) { ok = 0; offset = offsets[i]; nsize = size - offset; ptr = (buf + offset); fprintf(stderr, "Processing header at offset %d...", offset); /* Identify and patch the header at each offset */ switch(identify_header(ptr)) { case TRX: ok = patch_trx(ptr, nsize); break; case UIMAGE: ok = patch_uimage(ptr, nsize); break; default: fprintf(stderr, "sorry, this file type is not supported.\n"); break; } if(ok) { fail = 0; fprintf(stderr, "checksum(s) updated OK.\n"); } else { fprintf(stderr, "checksum update(s) failed!\n"); } } } if(!fail) { if(!file_write(fname, buf, size)) { fprintf(stderr, "Failed to save data to file '%s'\n", fname); } else { fprintf(stderr, "CRC(s) updated successfully.\n"); retval = EXIT_SUCCESS; } } else { fprintf(stderr, "CRC update failed.\n"); } end: if(buf) free(buf); return retval; }