ctm_input.c revision 2886
1#include "ctm.h"
2
3/*---------------------------------------------------------------------------*/
4char *
5String(char *s)
6{
7    char *p = malloc(strlen(s) + 1);
8    strcpy(p,s);
9    return p;
10}
11/*---------------------------------------------------------------------------*/
12void
13Fatal_(int ln, char *fn, char *kind)
14{
15    if(Verbose > 2)
16	fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
17    fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
18}
19#define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
20#define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
21
22/*---------------------------------------------------------------------------*/
23/* get next field, check that the terminating whitespace is what we expect */
24u_char *
25Ffield(FILE *fd, MD5_CTX *ctx,u_char term)
26{
27    static u_char buf[BUFSIZ];
28    int i,l;
29
30    for(l=0;;) {
31	if((i=getc(fd)) == EOF) {
32	    Fatal("Truncated patch.");
33	    return 0;
34	}
35	buf[l++] = i;
36	if(isspace(i))
37	    break;
38	if(l >= sizeof buf) {
39	    Fatal("Corrupt patch.");
40	    printf("Token is too long.\n");
41	    return 0;
42	}
43    }
44    buf[l] = '\0';
45    MD5Update(ctx,buf,l);
46    if(buf[l-1] != term) {
47        Fatal("Corrupt patch.");
48	fprintf(stderr,"Expected \"%s\" but didn't find it.\n",
49	    term == '\n' ? "\\n" : " ");
50	return 0;
51    }
52    buf[--l] = '\0';
53    return buf;
54}
55
56int
57Fbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
58{
59    u_char *p,*q;
60    int u_chars;
61
62    p = Ffield(fd,ctx,term);
63    if(!p) return -1;
64    for(q=p;*q;q++)
65	if(!isdigit(*q)) {
66	    Fatal("Bytecount contains non-digit.");
67	    return -1;
68	}
69    u_chars=atoi(p);
70    if(u_chars > MAXSIZE) {
71	Fatal("Bytecount too large.");
72	return -1;
73    }
74    return u_chars;
75}
76
77u_char *
78Fdata(FILE *fd, int u_chars, MD5_CTX *ctx)
79{
80    u_char *p = Malloc(u_chars+1);
81
82    if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
83	Fatal("Truncated patch.");
84	return 0;
85    }
86    MD5Update(ctx,p,u_chars+1);
87    if(p[u_chars] != '\n') {
88	if(Verbose > 3)
89	    printf("FileData wasn't followed by a newline.\n");
90        Fatal("Corrupt patch.");
91	return 0;
92    }
93    p[u_chars] = '\0';
94    return p;
95}
96