summaryrefslogtreecommitdiff
path: root/usr.bin/nc/data/data.c
blob: 206945eed2e88bc32774aca43270290875947e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*	$OpenBSD: data.c,v 1.3 2001/01/29 01:58:11 niklas Exp $	*/

/* primitive arbitrary-data frontend for netcat.  0.9 960226
   only handles one value per ascii line, but at least parses 0xNN too
   an input line containing "%r" during "-g" generates a random byte

   todo:
	make work on msloss jus' for kicks [workin' on it...]

   syntax: data -X [limit]
   where X is one of
	d: dump raw bytes to ascii format
	g: generate raw bytes from ascii input
	c: generate ??? of value -- NOTYET
	r: generate all random bytes
   and limit is how many bytes to generate or dump [unspecified = infinite]

   *Hobbit*, started 951004 or so and randomly screwed around with since */

#include <stdio.h>

#ifdef MSDOS				/* for MSC only at the moment... */
#include <fcntl.h>
#else /* MSDOS */
#include <sys/file.h>
#define HAVE_RANDOM			/* XXX: might have to change */
#endif /* MSDOS */

static char buf_in [128];
static char buf_raw [8192];
static char surveysez[] = "survey sez... XXX\n";

/* fgetss :
   wrapper for fgets, that yanks trailing newlines.  Doing the work ourselves
   instead of calling strchr/strlen/whatever */
char * fgetss (buf, len, from)
  char * buf;
  size_t len;
  FILE * from;
{
  register int x;
  register char * p, * q;
  p = fgets (buf, len, from);		/* returns ptr to buf */
  if (! p)
    return (NULL);
  q = p;
  for (x = 0; x < len; x++) {
    *p = (*p & 0x7f);			/* rip parity, just in case */
    switch (*p) {
      case '\n':
      case '\r':
      case '\0':
	*p = '\0';
	return (q);
    } /* switch */
    p++;
  } /* for */
} /* fgetss */

/* randint:
   swiped from rndb.c.  Generates an INT, you have to mask down to char. */
int randint()
{
  register int q;
  register int x;

#ifndef HAVE_RANDOM
  q = rand();
#else
  q = random();
#endif
  x = ((q >> 8) & 0xff);	/* perturb low byte using some higher bits */
  x = q ^ x;
  return (x);
}

main (argc, argv)
  int argc;
  char ** argv;
{
  register unsigned char * p;
  register char * q;
  register int x;
  int bc = 0;
  int limit = 0;		/* num to gen, or 0 = infinite */
  register int xlimit;		/* running limit */
  FILE * txt;			/* line-by-line ascii file */
  int raw;			/* raw bytes fd */
  int dumping = 0;		/* cmd flags ... */
  int genning = 0;
  int randing = 0;

  memset (buf_in, 0, sizeof (buf_in));
  memset (buf_raw, 0, sizeof (buf_raw));

  xlimit = 1;				/* doubles as "exit flag" */
  bc = 1;				/* preload, assuming "dump" */
  x = getpid() + 687319;
/* if your library doesnt have srandom/random, use srand/rand. [from rnd.c] */
#ifndef HAVE_RANDOM
  srand (time(0) + x);
#else
  srandom (time(0) + x);
#endif

#ifdef O_BINARY
/* DOS stupidity */
/* Aha: *here's* where that setmode() lib call conflict in ?BSD came from */
  x = setmode (0, O_BINARY);		/* make stdin raw */
  if (x < 0) {
    fprintf (stderr, "stdin binary setmode oops: %d\n", x);
    exit (1);
  }
  x = setmode (1, O_BINARY);		/* make stdout raw */
  if (x < 0) {
    fprintf (stderr, "stdout binary setmode oops: %d\n", x);
    exit (1);
  }
#endif /* O_BINARY */

  if (argv[1]) {
    p = argv[1];		/* shit-simple single arg parser... */
    if (*p == '-')		/* dash is optional, we'll deal */
      p++;
    if (*p == 'd')
      dumping++;
    if (*p == 'g')
      genning++;
    if (*p == 'r')
      randing++;
  } /* if argv 1 */

/* optional second argument: limit # of bytes shoveled either way */
  if (argv[2]) {
    x = atoi (argv[2]);
    if (x)
      limit = x;
    else
      goto wrong;
    xlimit = limit;
  }

/* Since this prog would likely best be written in assmbler, I'm gonna
   write it *like* assembler.  So there. */

  if (randing)
    goto do_rand;

nextbuf:				/* loop sleaze */

  if (dumping) {			/* switch off to wherever */
    if (genning)
      goto wrong;
    goto do_dump;
  }
  if (genning)
    goto do_gen;
wrong:
  fprintf (stderr, "%s", surveysez);		/* if both or neither */
  exit (1);

do_gen:
/* here if genning -- original functionality */
  q = buf_raw;
  bc = 0;
/* suck up lines until eof or buf_raw is full */
  while (1) {
    p = fgetss (buf_in, 120, stdin);
    if (! p)
      break;				/* EOF */
/* super-primitive version first: one thingie per line */
    if (*p == '#')			/* comment */
      continue;
    if (*p == '\0')			/* blank line */
      continue;
    if (*p == '%') {			/* escape char? */
      p++;
      if (*p == 'r') {			/* random byte */
	x = randint();
	goto stuff;
      } /* %r */
    } /* if "%" escape */
    if (*p == '0')
      if (*(p+1) == 'x')		/* 0x?? */
	goto hex;
    x = atoi (p);			/* reg'lar decimal number */
    goto stuff;

hex:
/* A 65   a 97 */
/* xxx: use a conversion table for this or something.  Since we ripped the
   parity bit, we only need a preset array of 128 with downconversion factors
   loaded in *once*.   maybe look at scanf... */
    p++; p++;				/* point at hex-chars */
    x = 0;
    if ((*p > 96) && (*p < 123))	/* a-z */
      *p = (*p - 32);			/* this is massively clumsy */
    if ((*p > 64) && (*p < 71))		/* A-F */
      x = (*p - 55);
    if ((*p > 47) && (*p < 58))		/* digits */
      x = (*p - 48);
    p++;
    if (*p)				/* another digit? */
      x = (x << 4);			/* shift to hi half */
    if ((*p > 96) && (*p < 123))	/* a-z */
      *p = (*p - 32);
    if ((*p > 64) && (*p < 71))		/* A-F */
      x = (x | (*p - 55));		/* lo half */
    if ((*p > 47) && (*p < 58))		/* digits */
      x = (x | (*p - 48));

/* fall thru */
stuff:					/* cvt to byte and add to buffer */
    *q = (x & 0xff);
    q++;
    bc++;
    if (limit) {
      xlimit--;
      if (xlimit == 0)			/* max num reached */
	break;
    } /* limit */
    if (bc >= sizeof (buf_raw))		/* buffer full */
      break;
  } /* while 1 */

/* now in theory we have our buffer formed; shovel it out */
  x = write (1, buf_raw, bc);
  if (x <= 0) {
    fprintf (stderr, "write oops: %d\n", x);
    exit (1);
  }
  if (xlimit && p)
    goto nextbuf;			/* go get some more */
  exit (0);

do_dump:
/* here if dumping raw stuff into an ascii file */
/* gad, this is *so* much simpler!  can we say "don't rewrite printf"? */
  x = read (0, buf_raw, 8192);
  if (x <= 0)
    exit (0);
  q = buf_raw;
  for ( ; x > 0; x--) {
    p = q;
    printf ("%-3.3d # 0x%-2.2x # ", *p, *p);
    if ((*p > 31) && (*p < 127))
      printf ("%c %d\n", *p, bc);
    else
      printf (". %d\n", bc);
    q++;
    bc++;
    if (limit) {
      xlimit--;
      if (xlimit == 0) {
	fflush (stdout);
	exit (0);
      }
    } /* limit */
  } /* for */
  goto nextbuf;

do_rand:
/* here if generating all-random bytes.  Stays in this loop */
  p = buf_raw;
  while (1) {
    *p = (randint() & 0xff);
    write (1, p, 1);			/* makes very slow! */
    if (limit) {
      xlimit--;
      if (xlimit == 0)
	break;
    }
  } /* while */
  exit (0);

} /* main */