xref: /csrg-svn/usr.bin/tftp/tftpsubs.c (revision 26093)
1*26093Sminshall /*
2*26093Sminshall  * Copyright (c) 1985 Regents of the University of California.
3*26093Sminshall  * All rights reserved.  The Berkeley software License Agreement
4*26093Sminshall  * specifies the terms and conditions for redistribution.
5*26093Sminshall  */
6*26093Sminshall 
7*26093Sminshall #ifndef lint
8*26093Sminshall static char sccsid[] = "@(#)tftpsubs.c	5.1 (Berkeley) 02/06/86";
9*26093Sminshall #endif not lint
10*26093Sminshall 
11*26093Sminshall /* Simple minded read-ahead/write-behind subroutines for tftp user and
12*26093Sminshall    server.  Written originally with multiple buffers in mind, but current
13*26093Sminshall    implementation has two buffer logic wired in.
14*26093Sminshall 
15*26093Sminshall    Todo:  add some sort of final error check so when the write-buffer
16*26093Sminshall    is finally flushed, the caller can detect if the disk filled up
17*26093Sminshall    (or had an i/o error) and return a nak to the other side.
18*26093Sminshall 
19*26093Sminshall 			Jim Guyton 10/85
20*26093Sminshall  */
21*26093Sminshall 
22*26093Sminshall #include <sys/types.h>
23*26093Sminshall #include <sys/socket.h>
24*26093Sminshall #include <netinet/in.h>
25*26093Sminshall #include <arpa/tftp.h>
26*26093Sminshall #include <stdio.h>
27*26093Sminshall 
28*26093Sminshall #define PKTSIZE SEGSIZE+4       /* should be moved to tftp.h */
29*26093Sminshall 
30*26093Sminshall struct bf {
31*26093Sminshall 	int counter;            /* size of data in buffer, or flag */
32*26093Sminshall 	char buf[PKTSIZE];      /* room for data packet */
33*26093Sminshall } bfs[2];
34*26093Sminshall 
35*26093Sminshall 				/* Values for bf.counter  */
36*26093Sminshall #define BF_ALLOC -3             /* alloc'd but not yet filled */
37*26093Sminshall #define BF_FREE  -2             /* free */
38*26093Sminshall /* [-1 .. SEGSIZE] = size of data in the data buffer */
39*26093Sminshall 
40*26093Sminshall static int nextone;     /* index of next buffer to use */
41*26093Sminshall static int current;     /* index of buffer in use */
42*26093Sminshall 
43*26093Sminshall 			/* control flags for crlf conversions */
44*26093Sminshall int newline = 0;        /* fillbuf: in middle of newline expansion */
45*26093Sminshall int prevchar = -1;      /* putbuf: previous char (cr check) */
46*26093Sminshall 
47*26093Sminshall struct tftphdr *rw_init();
48*26093Sminshall 
49*26093Sminshall struct tftphdr *w_init() { return rw_init(0); }         /* write-behind */
50*26093Sminshall struct tftphdr *r_init() { return rw_init(1); }         /* read-ahead */
51*26093Sminshall 
52*26093Sminshall struct tftphdr *
53*26093Sminshall rw_init(x)              /* init for either read-ahead or write-behind */
54*26093Sminshall int x;                  /* zero for write-behind, one for read-head */
55*26093Sminshall {
56*26093Sminshall 	newline = 0;            /* init crlf flag */
57*26093Sminshall 	prevchar = -1;
58*26093Sminshall 	bfs[0].counter =  BF_ALLOC;     /* pass out the first buffer */
59*26093Sminshall 	current = 0;
60*26093Sminshall 	bfs[1].counter = BF_FREE;
61*26093Sminshall 	nextone = x;                    /* ahead or behind? */
62*26093Sminshall 	return (struct tftphdr *)bfs[0].buf;
63*26093Sminshall }
64*26093Sminshall 
65*26093Sminshall 
66*26093Sminshall /* Have emptied current buffer by sending to net and getting ack.
67*26093Sminshall    Free it and return next buffer filled with data.
68*26093Sminshall  */
69*26093Sminshall readit(file, dpp, convert)
70*26093Sminshall 	FILE *file;                     /* file opened for read */
71*26093Sminshall 	struct tftphdr **dpp;
72*26093Sminshall 	int convert;                    /* if true, convert to ascii */
73*26093Sminshall {
74*26093Sminshall 	struct bf *b;
75*26093Sminshall 
76*26093Sminshall 	bfs[current].counter = BF_FREE; /* free old one */
77*26093Sminshall 	current = !current;             /* "incr" current */
78*26093Sminshall 
79*26093Sminshall 	b = &bfs[current];              /* look at new buffer */
80*26093Sminshall 	if (b->counter == BF_FREE)      /* if it's empty */
81*26093Sminshall 		read_ahead(file, convert);      /* fill it */
82*26093Sminshall /*      assert(b->counter != BF_FREE);  /* check */
83*26093Sminshall 	*dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
84*26093Sminshall 	return b->counter;
85*26093Sminshall }
86*26093Sminshall 
87*26093Sminshall /*
88*26093Sminshall  * fill the input buffer, doing ascii conversions if requested
89*26093Sminshall  * conversions are  lf -> cr,lf  and cr -> cr, nul
90*26093Sminshall  */
91*26093Sminshall read_ahead(file, convert)
92*26093Sminshall 	FILE *file;                     /* file opened for read */
93*26093Sminshall 	int convert;                    /* if true, convert to ascii */
94*26093Sminshall {
95*26093Sminshall 	register int i;
96*26093Sminshall 	register char *p;
97*26093Sminshall 	register int c;
98*26093Sminshall 	struct bf *b;
99*26093Sminshall 	struct tftphdr *dp;
100*26093Sminshall 
101*26093Sminshall 	b = &bfs[nextone];              /* look at "next" buffer */
102*26093Sminshall 	if (b->counter != BF_FREE)      /* nop if not free */
103*26093Sminshall 		return;
104*26093Sminshall 	nextone = !nextone;             /* "incr" next buffer ptr */
105*26093Sminshall 
106*26093Sminshall 	dp = (struct tftphdr *)b->buf;
107*26093Sminshall 
108*26093Sminshall 	if (convert == 0) {
109*26093Sminshall 		b->counter = read(fileno(file), dp->th_data, SEGSIZE);
110*26093Sminshall 		return;
111*26093Sminshall 	}
112*26093Sminshall 
113*26093Sminshall 	p = dp->th_data;
114*26093Sminshall 	for (i = 0 ; i < SEGSIZE; i++) {
115*26093Sminshall 		if (newline) {
116*26093Sminshall 			if (prevchar == '\n')
117*26093Sminshall 				c = '\n';       /* lf to cr,lf */
118*26093Sminshall 			else    c = '\0';       /* cr to cr,nul */
119*26093Sminshall 			newline = 0;
120*26093Sminshall 		}
121*26093Sminshall 		else {
122*26093Sminshall 			c = getc(file);
123*26093Sminshall 			if (c == EOF) break;
124*26093Sminshall 			if (c == '\n' || c == '\r') {
125*26093Sminshall 				prevchar = c;
126*26093Sminshall 				c = '\r';
127*26093Sminshall 				newline = 1;
128*26093Sminshall 			}
129*26093Sminshall 		}
130*26093Sminshall 	       *p++ = c;
131*26093Sminshall 	}
132*26093Sminshall 	b->counter = (int)(p - dp->th_data);
133*26093Sminshall }
134*26093Sminshall 
135*26093Sminshall /* Update count associated with the buffer, get new buffer
136*26093Sminshall    from the queue.  Calls write_behind only if next buffer not
137*26093Sminshall    available.
138*26093Sminshall  */
139*26093Sminshall writeit(file, dpp, ct, convert)
140*26093Sminshall 	FILE *file;
141*26093Sminshall 	struct tftphdr **dpp;
142*26093Sminshall 	int convert;
143*26093Sminshall {
144*26093Sminshall 	bfs[current].counter = ct;      /* set size of data to write */
145*26093Sminshall 	current = !current;             /* switch to other buffer */
146*26093Sminshall 	if (bfs[current].counter != BF_FREE)     /* if not free */
147*26093Sminshall 		write_behind(file, convert);     /* flush it */
148*26093Sminshall 	bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
149*26093Sminshall 	*dpp =  (struct tftphdr *)bfs[current].buf;
150*26093Sminshall 	return ct;                      /* this is a lie of course */
151*26093Sminshall }
152*26093Sminshall 
153*26093Sminshall /*
154*26093Sminshall  * Output a buffer to a file, converting from netascii if requested.
155*26093Sminshall  * CR,NUL -> CR  and CR,LF => LF.
156*26093Sminshall  * Note spec is undefined if we get CR as last byte of file or a
157*26093Sminshall  * CR followed by anything else.  In this case we leave it alone.
158*26093Sminshall  */
159*26093Sminshall write_behind(file, convert)
160*26093Sminshall 	FILE *file;
161*26093Sminshall 	int convert;
162*26093Sminshall {
163*26093Sminshall 	char *buf;
164*26093Sminshall 	int count;
165*26093Sminshall 	register int ct;
166*26093Sminshall 	register char *p;
167*26093Sminshall 	register int c;                 /* current character */
168*26093Sminshall 	struct bf *b;
169*26093Sminshall 	struct tftphdr *dp;
170*26093Sminshall 
171*26093Sminshall 	b = &bfs[nextone];
172*26093Sminshall 	if (b->counter < -1)            /* anything to flush? */
173*26093Sminshall 		return 0;               /* just nop if nothing to do */
174*26093Sminshall 
175*26093Sminshall 	count = b->counter;             /* remember byte count */
176*26093Sminshall 	b->counter = BF_FREE;           /* reset flag */
177*26093Sminshall 	dp = (struct tftphdr *)b->buf;
178*26093Sminshall 	nextone = !nextone;             /* incr for next time */
179*26093Sminshall 	buf = dp->th_data;
180*26093Sminshall 
181*26093Sminshall 	if (count <= 0) return -1;      /* nak logic? */
182*26093Sminshall 
183*26093Sminshall 	if (convert == 0)
184*26093Sminshall 		return write(fileno(file), buf, count);
185*26093Sminshall 
186*26093Sminshall 	p = buf;
187*26093Sminshall 	ct = count;
188*26093Sminshall 	while (ct--) {                  /* loop over the buffer */
189*26093Sminshall 	    c = *p++;                   /* pick up a character */
190*26093Sminshall 	    if (prevchar == '\r') {     /* if prev char was cr */
191*26093Sminshall 		if (c == '\n')          /* if have cr,lf then just */
192*26093Sminshall 		   fseek(file, -1, 1);  /* smash lf on top of the cr */
193*26093Sminshall 		else
194*26093Sminshall 		   if (c == '\0')       /* if have cr,nul then */
195*26093Sminshall 			goto skipit;    /* just skip over the putc */
196*26093Sminshall 		/* else just fall through and allow it */
197*26093Sminshall 	    }
198*26093Sminshall 	    putc(c, file);
199*26093Sminshall skipit:
200*26093Sminshall 	    prevchar = c;
201*26093Sminshall 	}
202*26093Sminshall 	return count;
203*26093Sminshall }
204*26093Sminshall 
205*26093Sminshall 
206