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