File: rfc1924-encode.c

package info (click to toggle)
ipv6calc 0.46-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,364 kB
  • ctags: 853
  • sloc: ansic: 33,344; perl: 977; sh: 405; makefile: 249
file content (371 lines) | stat: -rw-r--r-- 5,443 bytes parent folder | download | duplicates (2)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Base 85 (rfc1924) encodings of IPv6 addresses
 */

#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>

extern char *strchr();

typedef struct {
	unsigned char b[128];		/* never anything but 0 or 1 */
} bitvec;

char ChSet[86] = {
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
	'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
	'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
	'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
	'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
	'y', 'z', '!', '#', '$', '%', '&', '(', ')', '*',
	'+', '-', ';', '<', '=', '>', '?', '@', '^', '_',
	'`', '{', '|', '}', '~',
	0
};

bitvec
lsl(bv, n)
	bitvec bv;
	int n;
{
	register i;
	bitvec r;

	for (i = 128; --i >= 128-n; )
		r.b[i] = 0;

	while (i >= 0) {
		r.b[i] = bv.b[i + n];
		i--;
	}

	return r;
}

bitvec
lsr(bv, n)
	bitvec bv;
	int n;
{
	register i;
	bitvec r;

	for (i = 0; i < n; i++)
		r.b[i] = 0;

	while (i < 128) {
		r.b[i] = bv.b[i - n];
		i++;
	}

	return r;
}

bitvec
asr(bv, n)
	bitvec bv;
	int n;
{
	register i;
	bitvec r;

	for (i = 0; i < n; i++)
		r.b[i] = bv.b[0];

	while (i < 128) {
		r.b[i] = bv.b[i - n];
		i++;
	}

	return r;
}

int
topbit(bv)
	bitvec bv;
{
	return bv.b[0];
}

int
botbit(bv)
	bitvec bv;
{
	return bv.b[127];
}

void
settop(bv, v)
	bitvec *bv;
	int v;
{
	bv->b[0] = v;
}

void
setbot(bv, v)
	bitvec *bv;
	int v;
{
	bv->b[127] = v;
}

bitvec
zbv()
{
	bitvec r;
	register i;

	for (i = 0; i < 128; i++)
		r.b[i] = 0;
	return r;
}

bitvec
addbv(a, b)
	bitvec a, b;
{
	register i, j;
	bitvec r;

	j = 0;
	for (i = 128; --i >= 0; ) {
		j += a.b[i] + b.b[i];
		r.b[i] = j & 1;
		j >>= 1;
	}

	return r;
}

bitvec
subbv(a, b)
	bitvec a, b;
{
	register i;
	register unsigned j;
	register unsigned k;
	bitvec r;

	j = 0;
	for (i = 128; --i >= 0; ) {
		k = (a.b[i] ^ b.b[i] ^ j) & 1;
		j = ~j & ~a.b[i] & b.b[i] | j & ~a.b[i] | j & b.b[i];
		r.b[i] = k;
	}
	return r;
}

int
cmpbv(a, b)
	bitvec a, b;
{
	register i, j;

	for (i = 0; i < 128; i++) {
		j = (int)a.b[i] - (int)b.b[i];
		if (j != 0)
			break;
	}
	return j;
}

bitvec
bvx85(bv)
	bitvec bv;
{
	/* 85 == 64 + 16 + 4 + 1 */
	return addbv(lsl(bv, 6), addbv(lsl(bv, 4), addbv(lsl(bv, 2), bv)));
}

bitvec
bvdiv85(bv, rem)
	bitvec bv;
	int *rem;
{
	bitvec e5;
	bitvec div;
	bitvec r;
	register i, j;

	r = zbv();
	setbot(&r, 1);
	e5 = addbv(lsl(r, 6), addbv(lsl(r, 4), addbv(lsl(r, 2), r)));


	div = lsl(e5, 121);		/* e5 is 7 bits, 121 + 7 == 128 */


	r = zbv();

	for (i = 0; i < 122; i++) {
		if (cmpbv(bv, div) < 0) {
			div = lsr(div, 1);
			r = lsl(r, 1);
			continue;
		}
		bv = subbv(bv, div);
		div = lsr(div, 1);
		r = lsl(r, 1);
		setbot(&r, 1);
	}

	j = 0;
	bv = lsl(bv, 121);

	for (i = 0; i < 7; i++) {
		j <<= 1;
		j |= topbit(bv);
		bv = lsl(bv, 1);
	}
	if (rem)
		*rem = j;

	return r;
}

bitvec
smallbv(n)
	register int n;
{
	bitvec r, t;
	register i;

	r = zbv();

	i = 0;
	while (n != 0) {
		t = lsl(t, 1);
		setbot(&t, n & 1);
		n >>= 1;
		i++;
	}

	while (--i >= 0) {
		r = lsl(r, 1);
		setbot(&r, botbit(t));
		t = lsr(t, 1);
	}
	return r;
}

bitvec
unpk85(str)
	register char *str;
{
	bitvec r;
	register char c;
	register int i, j;
	register char *p;

	r = zbv();
	for (i = 0; i < 20; i++) {
		c = *str++;
		if (c == 0 || (p = strchr(ChSet, c)) == 0)
			return zbv();
		r = addbv(bvx85(r), smallbv(p - ChSet));
	}

	return (r);
}

char *
pk85(bv)
	bitvec bv;
{
	int vals[20];
	register i;
	static char str[21];
	register char *p;

	for (i = 0; i < 20; i++)
		bv = bvdiv85(bv, &vals[i]);

	p = str;
	while (--i >= 0)
		*p++ = ChSet[vals[i]];
	*p = '\0';

	return str;
}

bitvec
hex2bv(str)
	char *str;
{
	register char c;
	register v;
	bitvec r;

	r = zbv();
	while (c = *str++) {
		if (!isascii(c) || !isxdigit(c))
			continue;
		if (c <= '9')
			v = c - '0';
		else if (c <= 'Z')
			v = c - 'A' + 10;
		else
			v = c - 'a' + 10;

		r = addbv(lsl(r, 4), smallbv(v));
	}
	return r;
}

char *
bv2hex(bv)
	bitvec bv;
{
	register i, j, k;
	static char str[128/4 + 1];
	char *p = str;

	for (i = 0; i < 128; i += 4) {
		k = 0;
		for (j = 0; j < 4; j++) {
			k <<= 1;
			k |= topbit(bv);
			bv = lsl(bv, 1);
		}
		if (k < 10)
			*p++ = '0' + k;
		else
			*p++ = 'A' + k - 10;
	}

	return str;
}

main()
{
	char buf[128];
	bitvec bv;

	/*
	 * reads (using gets() so absurdly bug prone)
	 * either a [20-rfc1924-character] string, or
	 * a 32 hex (06E5222FDB4C8E8DB1AFF3152F9CC60C) string, with
	 * or without punctuation (it is ignored) - but no IPv6
	 * encoding shortcuts (no :: nor elided 0's), and prints
	 * the value read, and its conversion to the other form.
	 *
	 * Non-hex digits are simply ignored when reading hex,
	 * Anything but a 20 char string of rfc1924 chars will
	 * cause 00000000000000000000000000000000 to be returned
	 * as the result (as would [00000000000000000000])
	 */

	while (gets(buf)) {
		if (buf[0] == '[') {
			bv = unpk85(buf + 1);
			printf("%s = %s\n", buf, bv2hex(bv));
		} else {
			bv = hex2bv(buf);
			printf("%s = [%s]\n", buf, pk85(bv));
		}
	}
	exit (0);
}