]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ctm/ctm/ctm_input.c
Fix bugs in plugable CC algorithm and siftr sysctls.
[FreeBSD/FreeBSD.git] / usr.sbin / ctm / ctm / ctm_input.c
1 /*-
2  * SPDX-License-Identifier: Beerware
3  *
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  *
11  * $FreeBSD$
12  *
13  */
14
15 #include "ctm.h"
16
17 /*---------------------------------------------------------------------------*/
18 void
19 Fatal_(int ln, char *fn, char *kind)
20 {
21     if(Verbose > 2)
22         fprintf(stderr,"Fatal error. (%s:%d)\n",fn,ln);
23     fprintf(stderr,"%s Fatal error: %s\n",FileName, kind);
24 }
25 #define Fatal(foo) Fatal_(__LINE__,__FILE__,foo)
26 #define Assert() Fatal_(__LINE__,__FILE__,"Assert failed.")
27
28 /*---------------------------------------------------------------------------*/
29 /* get next field, check that the terminating whitespace is what we expect */
30 u_char *
31 Ffield(FILE *fd, MD5_CTX *ctx,u_char term)
32 {
33     static u_char buf[BUFSIZ];
34     int i,l;
35
36     for(l=0;;) {
37         if((i=getc(fd)) == EOF) {
38             Fatal("Truncated patch.");
39             return 0;
40         }
41         buf[l++] = i;
42         if(isspace(i))
43             break;
44         if(l >= sizeof buf) {
45             Fatal("Corrupt patch.");
46             printf("Token is too long.\n");
47             return 0;
48         }
49     }
50     buf[l] = '\0';
51     MD5Update(ctx,buf,l);
52     if(buf[l-1] != term) {
53         Fatal("Corrupt patch.");
54         fprintf(stderr,"Expected \"%s\" but didn't find it {%02x}.\n",
55             term == '\n' ? "\\n" : " ",buf[l-1]);
56         if(Verbose > 4)
57             fprintf(stderr,"{%s}\n",buf);
58         return 0;
59     }
60     buf[--l] = '\0';
61     if(Verbose > 4)
62         fprintf(stderr,"<%s>\n",buf);
63     return buf;
64 }
65
66 int
67 Fbytecnt(FILE *fd, MD5_CTX *ctx, u_char term)
68 {
69     u_char *p,*q;
70     int u_chars=0;
71
72     p = Ffield(fd,ctx,term);
73     if(!p) return -1;
74     for(q=p;*q;q++) {
75         if(!isdigit(*q)) {
76             Fatal("Bytecount contains non-digit.");
77             return -1;
78         }
79         u_chars *= 10;
80         u_chars += (*q - '0');
81     }
82     return u_chars;
83 }
84
85 u_char *
86 Fdata(FILE *fd, int u_chars, MD5_CTX *ctx)
87 {
88     u_char *p = Malloc(u_chars+1);
89
90     if(u_chars+1 != fread(p,1,u_chars+1,fd)) {
91         Fatal("Truncated patch.");
92         return 0;
93     }
94     MD5Update(ctx,p,u_chars+1);
95     if(p[u_chars] != '\n') {
96         if(Verbose > 3)
97             printf("FileData wasn't followed by a newline.\n");
98         Fatal("Corrupt patch.");
99         return 0;
100     }
101     p[u_chars] = '\0';
102     return p;
103 }
104
105 /*---------------------------------------------------------------------------*/
106 /* get the filename in the next field, prepend BaseDir and give back the result
107    strings. The sustitute filename is return (the one with the suffix SUBSUFF) 
108    if it exists and the qualifier contains CTM_Q_Name_Subst
109    NOTA: Buffer is already initialize with BaseDir, CatPtr is the insertion
110    point on this buffer + the length test in Ffield() is enough for Fname() */
111
112 u_char *
113 Fname(FILE *fd, MD5_CTX *ctx,u_char term,int qual, int verbose)
114 {
115     u_char * p;
116     struct stat st;
117
118     if ((p = Ffield(fd,ctx,term)) == NULL) return(NULL);
119
120     strcpy(CatPtr, p);
121
122     if (!(qual & CTM_Q_Name_Subst)) return(Buffer);
123
124     p = Buffer + strlen(Buffer);
125
126     strcat(Buffer, SUBSUFF);
127
128     if ( -1 == stat(Buffer, &st) ) {
129         *p = '\0';
130     } else {
131         if(verbose > 2)
132             fprintf(stderr,"Using %s as substitute file\n", Buffer);
133     }
134
135     return (Buffer);
136 }