Subj : dosxnt To : BEN RITCHEY From : Paul Williams Date : Sat Sep 07 2002 04:43 am Hi BEN RITCHEY, hope you are having a nice day PW>> NP. It'd be nice if fixing the PKTFIX program Jesper Soerensen wrote PW>>to handle msgs over 16k were that easy. BR>I'd have to see the source. :) That's easy enough to solve. :> Also if you take fido f'attaches I can send you Jesper's full archive w/ the source and the exe's for cygwin, os2/emx, and w32, plus mine w/ the dos16 and a quick&dirty documentation. When I did the dos16 version I did change some of the error msgs and other text. Most people aren't going do understand what `Unable to malloc memory' means where `Unable to allocate memory' is a little clearer. ;> I did have to remove some tabs to get this to fit in 80col w/o a lot of wrapping but otherwise it's the original source. His comments were that it should be possible to get rid of the MAX_MSG_SIZE so that it would process msgs larger than 16k w/o needing to be defined to a larger number. I tried values up to 128k and compiled it w/ borland turbo c++ 3.0 trying each memory model from tiny to huge but it didn't seem to make any difference. Since c/c++ wasn't my thing I can't see what would be limiting things when using a larger value. ------pktfix.c---------- #include #include #include #include #define MAX_MSG_SIZE 16384 /* Read a null terminated string from file */ int getCString(char *p, int maxlen, FILE* in) { int len; char c; for (len = 1; (c = fgetc(in)) != 0; len++) { if (c == EOF) { printf("Unexpected end of file\n"); return -1; } if (len >= maxlen) { printf("String too long\n"); return -1; } *p++ = c; } *p = 0; return len; } /* Calculate UTC offset - this function is stolen from Tobias Ernst's MsgEd :) */ int tz_my_offset(void) { time_t now; struct tm *tm; int gm_minutes; long gm_days; int local_minutes; long local_days; tzset(); now = time(NULL); tm = localtime(&now); local_minutes = tm->tm_hour * 60 + tm->tm_min; local_days = (long)tm->tm_year * 366L + (long)tm->tm_yday; tm = gmtime(&now); gm_minutes = tm->tm_hour * 60 + tm->tm_min; gm_days = (long)tm->tm_year * 366L + (long)tm->tm_yday; if (gm_days < local_days) { local_minutes += 1440; } else if (gm_days > local_days) { gm_minutes += 1440; } return local_minutes - gm_minutes; } int main(int argc, char *args[]) { char *TEMPFILE = "pktfix.tmp"; char *USAGE = "Usage: pktfix [-address
] [-chrs ] [-msgid] [-tzutc] \n" " -address = only process messages from this address\n" " -chrs = add CHRS kludge with this value\n" " -msgid = add MSGID kludge (value is calculated automatically)\n" " -tzutc = add TZUTC kludge (value is calculated automatically)\n"; FILE *in, *out; int pktVer, msgVer; char buf[58], toName[37], fromName[37], subject[73]; char address[64], origin[32], intl[32], fmpt[32]; char *body, *p, *p2, *p3, *p4; char *chrs = NULL, *myAddress = NULL, *filename = NULL; int tzutc = 0, msgid = 0; int state, ignore, haveChrs, haveMsgid, haveTzutc; int utcOffset, serial, lastSerial = 0; int i; for (i = 1; i < argc; i++) { if (args[i][0] == '-') { if (!strcmp(args[i], "-msgid")) { msgid = 1; } else if (!strcmp(args[i], "-tzutc")) { tzutc = 1; } else if (!strcmp(args[i], "-chrs")) { if (++i < argc) { chrs = args[i]; } else { printf(USAGE); return -1; } } else if (!strcmp(args[i], "-address")) { if (++i < argc) { myAddress = args[i]; } else { printf(USAGE); return -1; } } else { printf(USAGE); return -1; } } else { if (filename == NULL) { filename = args[i]; } else { printf(USAGE); return -1; } } } if (filename == NULL) { printf(USAGE); return -1; } if ((body = malloc(MAX_MSG_SIZE)) == NULL) { printf("Cannot malloc memory\n"); return -1; } if ((out = fopen(TEMPFILE, "wb")) == NULL) { printf("Cannot create file\n"); return -1; } if ((in = fopen(filename, "rb")) == NULL) { printf("Cannot open file\n"); return -1; } utcOffset = tz_my_offset(); if (fread(buf, 1, 58, in) != 58) { printf("Cannot read packet header\n"); return -1; } pktVer = buf[18] | (buf[19] << 8); if (pktVer != 2) { /* Not a Type 2 packet */ printf("Invalid packet version\n"); return -1; } if (fwrite(buf, 1, 58, out) != 58) { printf("Cannot write packet header\n"); return -1; } while (1) { if (fread(buf, 1, 2, in) != 2) { printf("Cannot read message version\n"); return -1; } msgVer = buf[0] | (buf[1] << 8); if (msgVer == 0) { /* End of packet */ if (fwrite(buf, 1, 2, out) != 2) { printf("Cannot write packet termination\n"); return -1; } break; } else if (msgVer != 2) { printf("Invalid message version\n"); return -1; } if (fread(buf + 2, 1, 32, in) != 32) { printf("Cannot read message header\n"); return -1; } if (fwrite(buf, 1, 34, out) != 34) { printf("Cannot write message header\n"); return -1; } if ((getCString(toName, 37, in) < 0) || (getCString(fromName, 37, in) < 0) || (getCString(subject, 73, in) < 0) || (getCString(body, MAX_MSG_SIZE, in) < 0)) { printf("Error while reading message\n"); return -1; } fprintf(out, "%s%c", toName, 0); fprintf(out, "%s%c", fromName, 0); fprintf(out, "%s%c", subject, 0); /* We need to find the origin address first */ origin[0] = intl[0] = fmpt[0] = 0; p = body; while ((p2 = strchr(p, 13)) != NULL) { *p2 = 0; /* Temporarily terminate the string so we can parse it */ if (!strncmp(p, "\01INTL", 5)) { if ((p3 = strchr(p, ' ')) != NULL) { p3++; if ((p4 = strchr(p3, ' ')) != NULL) { p4++; i = strlen(p4); if (i >= 32) { i = 31; /* truncate to 31 bytes */ } strncpy(intl, p4, i); intl[i] = 0; } } } else if (!strncmp(p, "\01FMPT", 5)) { if ((p3 = strchr(p, ' ')) != NULL) { p3++; i = strlen(p3); if (i >= 32) { i = 31; /* truncate to 31 bytes */ } strncpy(fmpt, p3, i); fmpt[i] = 0; } } else if (!strncmp(p, " * Origin:", 10)) { if ((p3 = strrchr(p, '(')) != NULL) { if ((p4 = strchr(p3, ')')) != NULL) { i = p4 - p3 - 1; if (i >= 32) { i = 31; /* truncate to 31 bytes */ } strncpy(origin, p3 + 1, i); origin[i] = 0; } } } *p2 = 13; /* Restore CR */ p = p2 + 1; } /* Construct origin address */ if (origin[0]) { strcpy(address, origin); } else { if (intl[0]) { strcpy(address, intl); } else { sprintf(address, "%i/%i", buf[6] | (buf[7] << 8), buf[2] | (buf[3] << 8)); } if (fmpt[0]) { strcat(address, "."); strcat(address, fmpt); } } /* Process this message? */ if (myAddress != NULL) { ignore = strcmp(address, myAddress); } else { ignore = 0; } printf("Message from %s => %s\n", address, (ignore ? "Ignoring" : "Processing")); /* Now to the real work - fixing the kludges */ p = body; state = 0; haveChrs = haveMsgid = haveTzutc = 0; while ((p2 = strchr(p, 13)) != NULL) { *p2 = 0; if (!ignore) { if (state == 0) { if (!strncmp(p, "AREA:", 5)) { state = 1; } else { state = 2; } } else if (state == 1) { state = 2; } if (state == 2) { if (*p == 1) { if (!strncmp(p, "\01CHRS:", 6)) { haveChrs = 1; printf("Already have CHRS\n"); } else if (!strncmp(p, "\01MSGID:", 7)) { haveMsgid = 1; printf("Already have MSGID\n"); } else if (!strncmp(p, "\01TZUTC:", 7)) { haveTzutc = 1; printf("Already have TZUTC\n"); } } else { state = 3; } } if (state == 3) { if ((chrs != NULL) && !haveChrs) { fprintf(out, "\01CHRS: %s 2\r", chrs); printf("Added CHRS\n"); } if (msgid && !haveMsgid) { serial = time(NULL); if (serial <= lastSerial) { serial = lastSerial + 1; } lastSerial = serial; fprintf(out, "\01MSGID: %s %08x\r", address, serial); printf("Added MSGID\n"); } if (tzutc && !haveTzutc) { fprintf(out, "\01TZUTC: %.4i\r", ((utcOffset / 60) * 100) + (utcOffset % 60)); printf("Added TZUTC\n"); } state = 4; } } fprintf(out, "%s\r", p); p = p2 + 1; } fprintf(out, "%s%c", p, 0); } fclose(out); fclose(in); free(body); if (remove(filename)) { printf("Cannot remove file\n"); return -1; } if (rename(TEMPFILE, filename)) { printf("Cannot rename file\n"); return -1; } return 0; } -=> Yours sincerely, Paul Williams <=- .... "Tood? Tood! Heheheheheheheheheheheheee...--Amy Neal-Crandell --- Terminate 4.00/Pro * Origin: Hi ho, hi ho, it's hand grenades I throw! (1:387/710) .