ctm_input.c revision 2948
12926Sphk/*
22926Sphk * ----------------------------------------------------------------------------
32926Sphk * "THE BEER-WARE LICENSE" (Revision 42):
42926Sphk * <phk@login.dknet.dk> wrote this file.  As long as you retain this notice you
52926Sphk * can do whatever you want with this stuff. If we meet some day, and you think
62926Sphk * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
72926Sphk * ----------------------------------------------------------------------------
82926Sphk *
92926Sphk * $Id$
102926Sphk *
112926Sphk */
122926Sphk
132886Sphk#include "ctm.h"
142886Sphk
152886Sphk/*---------------------------------------------------------------------------*/
162886Sphkchar *
172886SphkString(char *s)
182886Sphk{
192886Sphk    char *p = malloc(strlen(s) + 1);
202886Sphk    strcpy(p,s);
212886Sphk    return p;
222886Sphk}
232886Sphk/*---------------------------------------------------------------------------*/
242886Sphkvoid
252886SphkFatal_(int ln, char *fn, char *kind)
262886Sphk{
272886Sphk    if(Verbose > 2)
282886Sphk	fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
292886Sphk    fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
302886Sphk}
312886Sphk#define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
322886Sphk#define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
332886Sphk
342886Sphk/*---------------------------------------------------------------------------*/
352886Sphk/* get next field, check that the terminating whitespace is what we expect */
362886Sphku_char *
372886SphkFfield(FILE *fd, MD5_CTX *ctx,u_char term)
382886Sphk{
392886Sphk    static u_char buf[BUFSIZ];
402886Sphk    int i,l;
412886Sphk
422886Sphk    for(l=0;;) {
432886Sphk	if((i=getc(fd)) == EOF) {
442886Sphk	    Fatal("Truncated patch.");
452886Sphk	    return 0;
462886Sphk	}
472886Sphk	buf[l++] = i;
482886Sphk	if(isspace(i))
492886Sphk	    break;
502886Sphk	if(l >= sizeof buf) {
512886Sphk	    Fatal("Corrupt patch.");
522886Sphk	    printf("Token is too long.\n");
532886Sphk	    return 0;
542886Sphk	}
552886Sphk    }
562886Sphk    buf[l] = '\0';
572886Sphk    MD5Update(ctx,buf,l);
582886Sphk    if(buf[l-1] != term) {
592886Sphk        Fatal("Corrupt patch.");
602948Sphk	fprintf(stderr,"Expected \"%s\" but didn't find it {%02x}.\n",
612948Sphk	    term == '\n' ? "\\n" : " ",buf[l-1]);
622948Sphk	if(Verbose > 4)
632948Sphk	    fprintf(stderr,"{%s}\n",buf);
642886Sphk	return 0;
652886Sphk    }
662886Sphk    buf[--l] = '\0';
672948Sphk    if(Verbose > 4)
682948Sphk        fprintf(stderr,"<%s>\n",buf);
692886Sphk    return buf;
702886Sphk}
712886Sphk
722886Sphkint
732886SphkFbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
742886Sphk{
752886Sphk    u_char *p,*q;
762948Sphk    int u_chars=0;
772886Sphk
782886Sphk    p = Ffield(fd,ctx,term);
792886Sphk    if(!p) return -1;
802948Sphk    for(q=p;*q;q++) {
812886Sphk	if(!isdigit(*q)) {
822886Sphk	    Fatal("Bytecount contains non-digit.");
832886Sphk	    return -1;
842886Sphk	}
852948Sphk	u_chars *= 10;
862948Sphk	u_chars += (*q - '0');
872948Sphk    }
882886Sphk    if(u_chars > MAXSIZE) {
892886Sphk	Fatal("Bytecount too large.");
902886Sphk	return -1;
912886Sphk    }
922886Sphk    return u_chars;
932886Sphk}
942886Sphk
952886Sphku_char *
962886SphkFdata(FILE *fd, int u_chars, MD5_CTX *ctx)
972886Sphk{
982886Sphk    u_char *p = Malloc(u_chars+1);
992886Sphk
1002886Sphk    if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
1012886Sphk	Fatal("Truncated patch.");
1022886Sphk	return 0;
1032886Sphk    }
1042886Sphk    MD5Update(ctx,p,u_chars+1);
1052886Sphk    if(p[u_chars] != '\n') {
1062886Sphk	if(Verbose > 3)
1072886Sphk	    printf("FileData wasn't followed by a newline.\n");
1082886Sphk        Fatal("Corrupt patch.");
1092886Sphk	return 0;
1102886Sphk    }
1112886Sphk    p[u_chars] = '\0';
1122886Sphk    return p;
1132886Sphk}
114