summaryrefslogtreecommitdiff
path: root/src/usr.bin/nc/data/data.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/nc/data/data.c')
-rw-r--r--src/usr.bin/nc/data/data.c276
1 files changed, 0 insertions, 276 deletions
diff --git a/src/usr.bin/nc/data/data.c b/src/usr.bin/nc/data/data.c
deleted file mode 100644
index e1936d538c..0000000000
--- a/src/usr.bin/nc/data/data.c
+++ /dev/null
@@ -1,276 +0,0 @@
1/* $OpenBSD: data.c,v 1.4 2001/11/19 19:02:15 mpech Exp $ */
2
3/* primitive arbitrary-data frontend for netcat. 0.9 960226
4 only handles one value per ascii line, but at least parses 0xNN too
5 an input line containing "%r" during "-g" generates a random byte
6
7 todo:
8 make work on msloss jus' for kicks [workin' on it...]
9
10 syntax: data -X [limit]
11 where X is one of
12 d: dump raw bytes to ascii format
13 g: generate raw bytes from ascii input
14 c: generate ??? of value -- NOTYET
15 r: generate all random bytes
16 and limit is how many bytes to generate or dump [unspecified = infinite]
17
18 *Hobbit*, started 951004 or so and randomly screwed around with since */
19
20#include <stdio.h>
21
22#ifdef MSDOS /* for MSC only at the moment... */
23#include <fcntl.h>
24#else /* MSDOS */
25#include <sys/file.h>
26#define HAVE_RANDOM /* XXX: might have to change */
27#endif /* MSDOS */
28
29static char buf_in [128];
30static char buf_raw [8192];
31static char surveysez[] = "survey sez... XXX\n";
32
33/* fgetss :
34 wrapper for fgets, that yanks trailing newlines. Doing the work ourselves
35 instead of calling strchr/strlen/whatever */
36char * fgetss (buf, len, from)
37 char * buf;
38 size_t len;
39 FILE * from;
40{
41 int x;
42 char * p, * q;
43 p = fgets (buf, len, from); /* returns ptr to buf */
44 if (! p)
45 return (NULL);
46 q = p;
47 for (x = 0; x < len; x++) {
48 *p = (*p & 0x7f); /* rip parity, just in case */
49 switch (*p) {
50 case '\n':
51 case '\r':
52 case '\0':
53 *p = '\0';
54 return (q);
55 } /* switch */
56 p++;
57 } /* for */
58} /* fgetss */
59
60/* randint:
61 swiped from rndb.c. Generates an INT, you have to mask down to char. */
62int randint()
63{
64 int q;
65 int x;
66
67#ifndef HAVE_RANDOM
68 q = rand();
69#else
70 q = random();
71#endif
72 x = ((q >> 8) & 0xff); /* perturb low byte using some higher bits */
73 x = q ^ x;
74 return (x);
75}
76
77main (argc, argv)
78 int argc;
79 char ** argv;
80{
81 unsigned char * p;
82 char * q;
83 int x;
84 int bc = 0;
85 int limit = 0; /* num to gen, or 0 = infinite */
86 int xlimit; /* running limit */
87 FILE * txt; /* line-by-line ascii file */
88 int raw; /* raw bytes fd */
89 int dumping = 0; /* cmd flags ... */
90 int genning = 0;
91 int randing = 0;
92
93 memset (buf_in, 0, sizeof (buf_in));
94 memset (buf_raw, 0, sizeof (buf_raw));
95
96 xlimit = 1; /* doubles as "exit flag" */
97 bc = 1; /* preload, assuming "dump" */
98 x = getpid() + 687319;
99/* if your library doesnt have srandom/random, use srand/rand. [from rnd.c] */
100#ifndef HAVE_RANDOM
101 srand (time(0) + x);
102#else
103 srandom (time(0) + x);
104#endif
105
106#ifdef O_BINARY
107/* DOS stupidity */
108/* Aha: *here's* where that setmode() lib call conflict in ?BSD came from */
109 x = setmode (0, O_BINARY); /* make stdin raw */
110 if (x < 0) {
111 fprintf (stderr, "stdin binary setmode oops: %d\n", x);
112 exit (1);
113 }
114 x = setmode (1, O_BINARY); /* make stdout raw */
115 if (x < 0) {
116 fprintf (stderr, "stdout binary setmode oops: %d\n", x);
117 exit (1);
118 }
119#endif /* O_BINARY */
120
121 if (argv[1]) {
122 p = argv[1]; /* shit-simple single arg parser... */
123 if (*p == '-') /* dash is optional, we'll deal */
124 p++;
125 if (*p == 'd')
126 dumping++;
127 if (*p == 'g')
128 genning++;
129 if (*p == 'r')
130 randing++;
131 } /* if argv 1 */
132
133/* optional second argument: limit # of bytes shoveled either way */
134 if (argv[2]) {
135 x = atoi (argv[2]);
136 if (x)
137 limit = x;
138 else
139 goto wrong;
140 xlimit = limit;
141 }
142
143/* Since this prog would likely best be written in assmbler, I'm gonna
144 write it *like* assembler. So there. */
145
146 if (randing)
147 goto do_rand;
148
149nextbuf: /* loop sleaze */
150
151 if (dumping) { /* switch off to wherever */
152 if (genning)
153 goto wrong;
154 goto do_dump;
155 }
156 if (genning)
157 goto do_gen;
158wrong:
159 fprintf (stderr, "%s", surveysez); /* if both or neither */
160 exit (1);
161
162do_gen:
163/* here if genning -- original functionality */
164 q = buf_raw;
165 bc = 0;
166/* suck up lines until eof or buf_raw is full */
167 while (1) {
168 p = fgetss (buf_in, 120, stdin);
169 if (! p)
170 break; /* EOF */
171/* super-primitive version first: one thingie per line */
172 if (*p == '#') /* comment */
173 continue;
174 if (*p == '\0') /* blank line */
175 continue;
176 if (*p == '%') { /* escape char? */
177 p++;
178 if (*p == 'r') { /* random byte */
179 x = randint();
180 goto stuff;
181 } /* %r */
182 } /* if "%" escape */
183 if (*p == '0')
184 if (*(p+1) == 'x') /* 0x?? */
185 goto hex;
186 x = atoi (p); /* reg'lar decimal number */
187 goto stuff;
188
189hex:
190/* A 65 a 97 */
191/* xxx: use a conversion table for this or something. Since we ripped the
192 parity bit, we only need a preset array of 128 with downconversion factors
193 loaded in *once*. maybe look at scanf... */
194 p++; p++; /* point at hex-chars */
195 x = 0;
196 if ((*p > 96) && (*p < 123)) /* a-z */
197 *p = (*p - 32); /* this is massively clumsy */
198 if ((*p > 64) && (*p < 71)) /* A-F */
199 x = (*p - 55);
200 if ((*p > 47) && (*p < 58)) /* digits */
201 x = (*p - 48);
202 p++;
203 if (*p) /* another digit? */
204 x = (x << 4); /* shift to hi half */
205 if ((*p > 96) && (*p < 123)) /* a-z */
206 *p = (*p - 32);
207 if ((*p > 64) && (*p < 71)) /* A-F */
208 x = (x | (*p - 55)); /* lo half */
209 if ((*p > 47) && (*p < 58)) /* digits */
210 x = (x | (*p - 48));
211
212/* fall thru */
213stuff: /* cvt to byte and add to buffer */
214 *q = (x & 0xff);
215 q++;
216 bc++;
217 if (limit) {
218 xlimit--;
219 if (xlimit == 0) /* max num reached */
220 break;
221 } /* limit */
222 if (bc >= sizeof (buf_raw)) /* buffer full */
223 break;
224 } /* while 1 */
225
226/* now in theory we have our buffer formed; shovel it out */
227 x = write (1, buf_raw, bc);
228 if (x <= 0) {
229 fprintf (stderr, "write oops: %d\n", x);
230 exit (1);
231 }
232 if (xlimit && p)
233 goto nextbuf; /* go get some more */
234 exit (0);
235
236do_dump:
237/* here if dumping raw stuff into an ascii file */
238/* gad, this is *so* much simpler! can we say "don't rewrite printf"? */
239 x = read (0, buf_raw, 8192);
240 if (x <= 0)
241 exit (0);
242 q = buf_raw;
243 for ( ; x > 0; x--) {
244 p = q;
245 printf ("%-3.3d # 0x%-2.2x # ", *p, *p);
246 if ((*p > 31) && (*p < 127))
247 printf ("%c %d\n", *p, bc);
248 else
249 printf (". %d\n", bc);
250 q++;
251 bc++;
252 if (limit) {
253 xlimit--;
254 if (xlimit == 0) {
255 fflush (stdout);
256 exit (0);
257 }
258 } /* limit */
259 } /* for */
260 goto nextbuf;
261
262do_rand:
263/* here if generating all-random bytes. Stays in this loop */
264 p = buf_raw;
265 while (1) {
266 *p = (randint() & 0xff);
267 write (1, p, 1); /* makes very slow! */
268 if (limit) {
269 xlimit--;
270 if (xlimit == 0)
271 break;
272 }
273 } /* while */
274 exit (0);
275
276} /* main */