Debian Codegraph @

src/bin/dwarfdump/dd_uri.c

raw
1 /*
2 Copyright 2011-2012 David Anderson. All rights reserved.
3 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of version 2 of the GNU General
7 Public License as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write the Free
24 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 Boston MA 02110-1301, USA.
26
27 */
28
29 #include <config.h>
30 #include <stdio.h> /* FILE decl for dd_esb.h */
31
32 #include "dwarf.h"
33 #include "libdwarf.h"
34 #include "dd_defined_types.h"
35 #include "dd_checkutil.h"
36 #include "dd_glflags.h"
37 #include "dd_globals.h"
38 #include "dd_esb.h"
39 #include "dd_uri.h"
40
41 /* dwarfdump_ctype table. See uritablebuild.c */
42 static char dwarfdump_ctype_table[256] = {
43 0, /* NUL 0x00 */
44 0, /* control 0x01 */
45 0, /* control 0x02 */
46 0, /* control 0x03 */
47 0, /* control 0x04 */
48 0, /* control 0x05 */
49 0, /* control 0x06 */
50 0, /* control 0x07 */
51 0, /* control 0x08 */
52 0, /* whitespace 0x09 */
53 0, /* whitespace 0x0a */
54 0, /* whitespace 0x0b */
55 0, /* whitespace 0x0c */
56 0, /* whitespace 0x0d */
57 0, /* control 0x0e */
58 0, /* control 0x0f */
59 0, /* control 0x10 */
60 0, /* control 0x11 */
61 0, /* control 0x12 */
62 0, /* control 0x13 */
63 0, /* control 0x14 */
64 0, /* control 0x15 */
65 0, /* control 0x16 */
66 0, /* control 0x17 */
67 0, /* control 0x18 */
68 0, /* control 0x19 */
69 0, /* control 0x1a */
70 0, /* control 0x1b */
71 0, /* control 0x1c */
72 0, /* control 0x1d */
73 0, /* control 0x1e */
74 0, /* control 0x1f */
75 1, /* ' ' 0x20 */
76 1, /* '!' 0x21 */
77 0, /* '"' 0x22 */
78 1, /* '#' 0x23 */
79 1, /* '$' 0x24 */
80 0, /* '%' 0x25 */
81 1, /* '&' 0x26 */
82 0, /* ''' 0x27 */
83 1, /* '(' 0x28 */
84 1, /* ')' 0x29 */
85 1, /* '*' 0x2a */
86 1, /* '+' 0x2b */
87 1, /* ',' 0x2c */
88 1, /* '-' 0x2d */
89 1, /* '.' 0x2e */
90 1, /* '/' 0x2f */
91 1, /* '0' 0x30 */
92 1, /* '1' 0x31 */
93 1, /* '2' 0x32 */
94 1, /* '3' 0x33 */
95 1, /* '4' 0x34 */
96 1, /* '5' 0x35 */
97 1, /* '6' 0x36 */
98 1, /* '7' 0x37 */
99 1, /* '8' 0x38 */
100 1, /* '9' 0x39 */
101 1, /* ':' 0x3a */
102 0, /* ';' 0x3b */
103 1, /* '<' 0x3c */
104 1, /* '=' 0x3d */
105 1, /* '>' 0x3e */
106 1, /* '?' 0x3f */
107 1, /* '@' 0x40 */
108 1, /* 'A' 0x41 */
109 1, /* 'B' 0x42 */
110 1, /* 'C' 0x43 */
111 1, /* 'D' 0x44 */
112 1, /* 'E' 0x45 */
113 1, /* 'F' 0x46 */
114 1, /* 'G' 0x47 */
115 1, /* 'H' 0x48 */
116 1, /* 'I' 0x49 */
117 1, /* 'J' 0x4a */
118 1, /* 'K' 0x4b */
119 1, /* 'L' 0x4c */
120 1, /* 'M' 0x4d */
121 1, /* 'N' 0x4e */
122 1, /* 'O' 0x4f */
123 1, /* 'P' 0x50 */
124 1, /* 'Q' 0x51 */
125 1, /* 'R' 0x52 */
126 1, /* 'S' 0x53 */
127 1, /* 'T' 0x54 */
128 1, /* 'U' 0x55 */
129 1, /* 'V' 0x56 */
130 1, /* 'W' 0x57 */
131 1, /* 'X' 0x58 */
132 1, /* 'Y' 0x59 */
133 1, /* 'Z' 0x5a */
134 1, /* '[' 0x5b */
135 1, /* '\' 0x5c */
136 1, /* ']' 0x5d */
137 1, /* '^' 0x5e */
138 1, /* '_' 0x5f */
139 0, /* '`' 0x60 */
140 1, /* 'a' 0x61 */
141 1, /* 'b' 0x62 */
142 1, /* 'c' 0x63 */
143 1, /* 'd' 0x64 */
144 1, /* 'e' 0x65 */
145 1, /* 'f' 0x66 */
146 1, /* 'g' 0x67 */
147 1, /* 'h' 0x68 */
148 1, /* 'i' 0x69 */
149 1, /* 'j' 0x6a */
150 1, /* 'k' 0x6b */
151 1, /* 'l' 0x6c */
152 1, /* 'm' 0x6d */
153 1, /* 'n' 0x6e */
154 1, /* 'o' 0x6f */
155 1, /* 'p' 0x70 */
156 1, /* 'q' 0x71 */
157 1, /* 'r' 0x72 */
158 1, /* 's' 0x73 */
159 1, /* 't' 0x74 */
160 1, /* 'u' 0x75 */
161 1, /* 'v' 0x76 */
162 1, /* 'w' 0x77 */
163 1, /* 'x' 0x78 */
164 1, /* 'y' 0x79 */
165 1, /* 'z' 0x7a */
166 1, /* '{' 0x7b */
167 1, /* '|' 0x7c */
168 1, /* '}' 0x7d */
169 1, /* '~' 0x7e */
170 0, /* DEL 0x7f */
171 1, /* 0x80 */
172 1, /* 0x81 */
173 1, /* 0x82 */
174 1, /* 0x83 */
175 1, /* 0x84 */
176 1, /* 0x85 */
177 1, /* 0x86 */
178 1, /* 0x87 */
179 1, /* 0x88 */
180 1, /* 0x89 */
181 1, /* 0x8a */
182 1, /* 0x8b */
183 1, /* 0x8c */
184 1, /* 0x8d */
185 1, /* 0x8e */
186 1, /* 0x8f */
187 1, /* 0x90 */
188 1, /* 0x91 */
189 1, /* 0x92 */
190 1, /* 0x93 */
191 1, /* 0x94 */
192 1, /* 0x95 */
193 1, /* 0x96 */
194 1, /* 0x97 */
195 1, /* 0x98 */
196 1, /* 0x99 */
197 1, /* 0x9a */
198 1, /* 0x9b */
199 1, /* 0x9c */
200 1, /* 0x9d */
201 1, /* 0x9e */
202 1, /* 0x9f */
203 0, /* other: 0xa0 */
204 1, /* 0xa1 */
205 1, /* 0xa2 */
206 1, /* 0xa3 */
207 1, /* 0xa4 */
208 1, /* 0xa5 */
209 1, /* 0xa6 */
210 1, /* 0xa7 */
211 1, /* 0xa8 */
212 1, /* 0xa9 */
213 1, /* 0xaa */
214 1, /* 0xab */
215 1, /* 0xac */
216 1, /* 0xad */
217 1, /* 0xae */
218 1, /* 0xaf */
219 1, /* 0xb0 */
220 1, /* 0xb1 */
221 1, /* 0xb2 */
222 1, /* 0xb3 */
223 1, /* 0xb4 */
224 1, /* 0xb5 */
225 1, /* 0xb6 */
226 1, /* 0xb7 */
227 1, /* 0xb8 */
228 1, /* 0xb9 */
229 1, /* 0xba */
230 1, /* 0xbb */
231 1, /* 0xbc */
232 1, /* 0xbd */
233 1, /* 0xbe */
234 1, /* 0xbf */
235 1, /* 0xc0 */
236 1, /* 0xc1 */
237 1, /* 0xc2 */
238 1, /* 0xc3 */
239 1, /* 0xc4 */
240 1, /* 0xc5 */
241 1, /* 0xc6 */
242 1, /* 0xc7 */
243 1, /* 0xc8 */
244 1, /* 0xc9 */
245 1, /* 0xca */
246 1, /* 0xcb */
247 1, /* 0xcc */
248 1, /* 0xcd */
249 1, /* 0xce */
250 1, /* 0xcf */
251 1, /* 0xd0 */
252 1, /* 0xd1 */
253 1, /* 0xd2 */
254 1, /* 0xd3 */
255 1, /* 0xd4 */
256 1, /* 0xd5 */
257 1, /* 0xd6 */
258 1, /* 0xd7 */
259 1, /* 0xd8 */
260 1, /* 0xd9 */
261 1, /* 0xda */
262 1, /* 0xdb */
263 1, /* 0xdc */
264 1, /* 0xdd */
265 1, /* 0xde */
266 1, /* 0xdf */
267 1, /* 0xe0 */
268 1, /* 0xe1 */
269 1, /* 0xe2 */
270 1, /* 0xe3 */
271 1, /* 0xe4 */
272 1, /* 0xe5 */
273 1, /* 0xe6 */
274 1, /* 0xe7 */
275 1, /* 0xe8 */
276 1, /* 0xe9 */
277 1, /* 0xea */
278 1, /* 0xeb */
279 1, /* 0xec */
280 1, /* 0xed */
281 1, /* 0xee */
282 1, /* 0xef */
283 1, /* 0xf0 */
284 1, /* 0xf1 */
285 1, /* 0xf2 */
286 1, /* 0xf3 */
287 1, /* 0xf4 */
288 1, /* 0xf5 */
289 1, /* 0xf6 */
290 1, /* 0xf7 */
291 1, /* 0xf8 */
292 1, /* 0xf9 */
293 1, /* 0xfa */
294 1, /* 0xfb */
295 1, /* 0xfc */
296 1, /* 0xfd */
297 1, /* 0xfe */
298 0, /* other: 0xff */
299 };
300
301 /* Translate dangerous and some other characters to safe
302 %xx form.
303 */
304 void
305 translate_to_uri(const char * filename, struct esb_s *out)
306 {
307 const char *cp = 0;
308
309 for (cp = filename ; *cp; ++cp) {
310 char v[2];
311 int c = 0xff & (unsigned char)*cp;
312 if (dwarfdump_ctype_table[c]) {
313 v[0] = (char)c;
314 v[1] = 0;
315 esb_append(out,v);
316 } else {
317 esb_append(out,"%");
318 esb_append_printf_u(out, "%02x",c&0xff);
319 }
320 }
321 }
322
323 /* This is not very efficient, but it is seldom called. */
324 static char
325 hexdig(char c)
326 {
327 char ochar = 0;
328 if (c >= '0' && c <= '9') {
329 ochar = (c - '0');
330 return ochar;
331 }
332 if (c >= 'a' && c <= 'f') {
333 ochar = (c - 'a')+10;
334 return ochar;
335 }
336 if (c >= 'A' && c <= 'F') {
337 ochar = (c - 'A')+10;
338 return ochar;
339 }
340 /* We have an input botch here. */
341 printf("ERROR Translating from uri: "
342 "A supposed hexadecimal input character is "
343 "not 0-9 or a-f or A-F, it is (shown as hex here): %x\n",c);
344 glflags.gf_count_major_errors++;
345
346 return ochar;
347 }
348
349 static char tohex(char c1, char c2)
350 {
351 char out = (hexdig(c1) << 4) | hexdig(c2);
352 return out;
353 }
354 static int
355 hexpairtochar(const char *cp, char*myochar)
356 {
357 char ochar = 0;
358 int olen = 0;
359 char c = cp[0];
360 if (c) {
361 char c2 = cp[1];
362 if (c2) {
363 ochar = tohex(c,c2);
364 olen = 2;
365 } else {
366 printf("ERROR Translating from uri: "
367 "A supposed hexadecimal input character pair "
368 "runs off the end of the input after 1 hex digit.\n");
369 glflags.gf_count_major_errors++;
370 /* botched input. */
371 ochar = c;
372 olen = 1;
373 }
374 } else {
375 /* botched input. */
376 printf("ERROR Translating from uri: "
377 "A supposed hexadecimal input character pair "
378 "runs off the end of the input.\n");
379 glflags.gf_count_major_errors++;
380 ochar = '%';
381 olen = 0;
382 }
383 *myochar = ochar;
384 return olen;
385 }
386
387 void
388 translate_from_uri(const char * input, struct esb_s* out)
389 {
390 const char *cp = input;
391 char tempstr[2];
392 for (; *cp; ++cp) {
393 char c = *cp;
394 if (c == '%') {
395 int increment = 0;
396 char c2 = cp[1];
397 /* hexpairtochar deals with c2 being NUL. */
398 if (c2 == '%') {
399 tempstr[0] = c;
400 tempstr[1] = 0;
401 esb_append(out,tempstr);
402 ++cp;
403 continue;
404 }
405
406 increment = hexpairtochar(cp+1,&c);
407 tempstr[0] = c;
408 tempstr[1] = 0;
409 esb_append(out,tempstr);
410 cp +=increment;
411 continue;
412 }
413 tempstr[0] = c;
414 tempstr[1] = 0;
415 esb_append(out,tempstr);
416 }
417 }
418
419 #ifdef TEST
420
421 unsigned int errcnt = 0;
422
423 static void
424 mytestfrom(const char * in,const char *expected,int testnum)
425 {
426 struct esb_s out;
427 esb_constructor(&out);
428 translate_from_uri(in, &out);
429 if (strcmp(expected, esb_get_string(&out))) {
430 printf(" Fail test %d expected \"%s\" got \"%s\"\n",
431 testnum,expected,esb_get_string(&out));
432 ++errcnt;
433 }
434 esb_destructor(&out);
435 }
436
437 static void
438 mytest(char *in,char *expected,int testnum)
439 {
440 struct esb_s out;
441 esb_constructor(&out);
442 translate_to_uri(in, &out);
443 if (strcmp(expected, esb_get_string(&out))) {
444 printf(" Fail test %d expected %s got %s\n",
445 testnum,expected,esb_get_string(&out));
446 ++errcnt;
447 }
448 esb_destructor(&out);
449 }
450
451 int
452 main()
453 {
454 /* We no longer translate space to %20, that
455 turns out not to help all that much. */
456 mytest("aaa","aaa",1);
457 mytest(" bc"," bc",2);
458 mytest(";bc","%3bbc",3);
459 mytest(" bc\n"," bc%0a",4);
460 mytest(";bc\n","%3bbc%0a",5);
461 mytest(" bc\r"," bc%0d",6);
462 mytest(";bc\r","%3bbc%0d",7);
463 mytest(" \x01"," %01",8);
464 mytest(";\x01","%3b%01",9);
465 mytestfrom("abc","abc",10);
466 mytestfrom("a%20bc","a bc",11);
467 mytestfrom("a%%20bc","a%20bc",12);
468 mytestfrom("a%%%20bc","a% bc",13);
469 mytestfrom("a%%%%20bc","a%%20bc",14);
470 mytestfrom("a%20","a ",15);
471 /* The following is mistaken input. */
472 mytestfrom("a%2","a2",16);
473 mytestfrom("a%","a%",17);
474 mytest("%bc","%25bc",18);
475
476 if (errcnt) {
477 printf("uri errcount ",errcnt);
478 }
479 return errcnt? 1:0;
480 }
481 #endif