-
Notifications
You must be signed in to change notification settings - Fork 1
/
compact.c
216 lines (206 loc) · 5.28 KB
/
compact.c
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
/* compact.c - function to convert hex/octal/decimal/raw string to raw
* ChangeLog since initial release in sendip 2.1.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <ctype.h>
#include <netinet/in.h>
#include "sendip_module.h"
int compact_string(char *data_out) {
char *data_in = data_out;
int i=0;
if(*data_in=='0') {
data_in++;
if(*data_in=='x' || *data_in=='X') {
/* Hex */
char c='\0';
data_in++;
while(*data_in) {
if(*data_in>='0' && *data_in<='9') {
c+=*data_in-'0';
} else if(*data_in>='A' && *data_in<='F') {
c+=*data_in-'A'+10;
} else if(*data_in>='a' && *data_in<='f') {
c+=*data_in-'a'+10;
} else {
fprintf(stderr,"Character %c invalid in hex data stream\n",
*data_in);
return 0;
}
if( i&1) {
*(data_out++)=c; // odd nibble - output it
c='\0';
} else {
c<<=4; // even nibble - shift to top of byte
}
data_in++; i++;
}
*data_out=c; // make sure last nibble is added
i++; i>>=1; // i was a nibble count...
return i;
} else {
/* Octal */
char c='\0';
while(*data_in) {
if(*data_in>='0' && *data_in<='7') {
c+=*data_in-'0';
} else {
fprintf(stderr,"Character %c invalid in octal data stream\n",
*data_in);
return 0;
}
if( (i&3) == 3 ) {
*(data_out++)=c; // output every 4th char
c='\0';
} else { // otherwise just shift it up
c<<=2;
}
data_in++; i++;
}
*data_out=c; // add partial last byte
i+=3; i>>=2;
return i;
}
} else {
/* String */
return strlen(data_in);
}
}
/* @@ Since I'm using the "string, rand or zero" business for filling
* out several header data areas, I decided to extract all of this
* into routines here.
*
* Note the handling of space is slightly screwy - compact_string
* above overwrites its argument in place, since it knows that
* no matter what, the string it produces can be no longer than
* its argument. randombytes and zerobytes, however, use a static
* areas, since the calling argument there (something like r32) will
* generally be much shorter than the string produced.
*
* In practice, in both cases the string returned will be immediately
* copied into an allocated area, so the differences in string handling
* don't matter. But this should be kept in mind if these routines
* are used elsewhere.
*/
/* @@ Return a pointer to a string of random bytes. Note this is a
* static area which is overwritten at each call.
*/
u_int8_t *
randombytes(int length)
{
static u_int8_t answer[MAXRAND];
int i;
/* Sanity check */
if (length > MAXRAND) {
usage_error("Random data too long to be sane\n");
return NULL;
}
for (i=0; i < length; ++i)
answer[i] = (u_int8_t)random();
/* Zero-pad out to 64-bit boundary */
for (; i&07; ++i)
answer[i] = 0;
return answer;
}
/* @@ Return a pointer to a string of zero bytes. Note this is a
* static area which should really be left alone ...
*/
u_int8_t *
zerobytes(int length)
{
static u_int8_t answer[MAXRAND];
/* Sanity check */
if (length > MAXRAND) {
usage_error("Zero data too long to be sane\n");
return NULL;
}
/* Paranoia */
(void) memset((void *)answer, 0, MAXRAND);
return answer;
}
/* @@ Yes, well, not the world's most brilliant name, but this
* does the standard string argument handling. The output
* may either be the transformed input or a static area.
* The return value is the length of the output.
*/
int
stringargument(char *input, char **output)
{
int length=0;
if (!input || !output) return 0;
/* Special case for rN, zN strings */
switch (*input) {
case 'r':
if (isdigit(*(input+1))) {
length = atoi(input+1);
*output = (char *)randombytes(length);
if (!*output) return 0;
return length;
}
break;
case 'z':
if (isdigit(*(input+1))) {
length = atoi(input+1);
*output = (char *)zerobytes(length);
if (!*output) return 0;
return length;
}
break;
default:
break;
}
length = compact_string(input);
*output = input;
return length;
}
/* @@ This is the integer (1, 2 or 4 byte) version of the above. It takes
* the input, which may be decimal, octal, hex, or the special strings
* rX (random bytes) or zX (zero bytes - kind of pointless) and converts
* it to an integer with the specified number of bytes in *network* byte
* order. The idea is you can just do:
*
* field = integerargument(input, sizeof(field));
*/
u_int32_t
integerargument(char *input, int length)
{
int inputlength;
u_int8_t *string;
if (!input || !length) return 0;
/* Special case for rN, zN strings */
switch (*input) {
case 'r':
if (isdigit(*(input+1))) {
inputlength = atoi(input+1);
if (inputlength > length)
inputlength = length;
string = randombytes(inputlength);
if (!string) return 0;
switch (length) {
case 1:
return (u_int8_t)*string;
case 2:
return *(u_int16_t *)string;
default:
return *(u_int32_t *)string;
}
}
break;
case 'z':
/* like I said, pointless ... */
return 0;
default:
break;
}
/* Everything else, just use strtoul, then cast and swap */
switch (length) {
case 1:
return (u_int8_t)strtoul(input, (char **)NULL, 0);
case 2:
return htons((u_int16_t)strtoul(input, (char **)NULL, 0));
default:
return htonl(strtoul(input, (char **)NULL, 0));
}
}