Debian Codegraph @

src/bin/dwarfdump/dd_sanitized.c

raw
1 /*
2 Copyright 2016-2018 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 #include <config.h>
29 #include <stdio.h> /* FILE decl for dd_esb.h */
30
31 #include "dwarf.h"
32 #include "libdwarf.h"
33 #include "dd_defined_types.h"
34 #include "dd_checkutil.h"
35 #include "dd_glflags.h"
36 #include "dd_globals.h"
37 #include "dd_esb.h"
38 #ifndef TESTING
39 #include "dd_glflags.h"
40 #endif
41 #ifdef HAVE_UTF8
42 #include "dd_utf8.h"
43 #endif /* HAVE_UTF8 */
44 #include "dd_sanitized.h"
45
46 /* This does a uri-style conversion of control characters.
47 So SOH prints as %01 for example.
48 Which stops corrupted or crafted strings from
49 doing things to the terminal the string is routed to.
50
51 We do not translate an input % to %% (as in real uri)
52 as that would be a bit confusing for most readers.
53
54 The conversion makes it possble to print UTF-8 strings
55 reproducibly, sort of (not showing the
56 real glyph!).
57
58 Only call this in a printf or sprintf, and
59 only call it once in any single printf/sprintf.
60 Otherwise you will get bogus results and confusion. */
61
62 /* ASCII control codes:
63 We leave newline as is, NUL is end of string,
64 the others are translated.
65 NUL Null 0 00 Ctrl-@ ^@
66 SOH Start of heading 1 01 Alt-1 Ctrl-A ^A
67 STX Start of text 2 02 Alt-2 Ctrl-B ^B
68 ETX End of text 3 03 Alt-3 Ctrl-C ^C
69 EOT End of transmission 4 04 Alt-4 Ctrl-D ^D
70 ENQ Enquiry 5 05 Alt-5 Ctrl-E ^E
71 ACK Acknowledge 6 06 Alt-6 Ctrl-F ^F
72 BEL Bell 7 07 Alt-7 Ctrl-G ^G
73 BS Backspace 8 08 Alt-8 Ctrl-H ^H
74 HT Horizontal tab 9 09 Alt-9 Ctrl-I ^I
75 LF Line feed 10 0A Alt-10 Ctrl-J ^J
76 VT Vertical tab 11 0B Alt-11 Ctrl-K ^K
77 FF Form feed 12 0C Alt-12 Ctrl-L ^L
78 CR Carriage return 13 0D Alt-13 Ctrl-M ^M
79 SO Shift out 14 0E Alt-14 Ctrl-N ^N
80 SI Shift in 15 0F Alt-15 Ctrl-O ^O
81 DLE Data line escape 16 10 Alt-16 Ctrl-P ^P
82 DC1 Device control 1 17 11 Alt-17 Ctrl-Q ^Q
83 DC2 Device control 2 18 12 Alt-18 Ctrl-R ^R
84 DC3 Device control 3 19 13 Alt-19 Ctrl-S ^S
85 DC4 Device control 4 20 14 Alt-20 Ctrl-T ^T
86 NAK Negative acknowledge 21 15 Alt-21 Ctrl-U ^U
87 SYN Synchronous idle 22 16 Alt-22 Ctrl-V ^V
88 ETB End transmission block 23 17 Alt-23 Ctrl-W ^W
89 CAN Cancel 24 18 Alt-24 Ctrl-X ^X
90 EM End of medium 25 19 Alt-25 Ctrl-Y ^Y
91 SU Substitute 26 1A Alt-26 Ctrl-Z ^Z
92 ES Escape 27 1B Alt-27 Ctrl-[ ^[
93 FS File separator 28 1C Alt-28 Ctrl-\ ^\
94 GS Group separator 29 1D Alt-29 Ctrl-] ^]
95 RS Record separator 30 1E Alt-30 Ctrl-^ ^^
96 US Unit separator 31 1F Alt-31 Ctrl-_ ^_
97
98 In addition, characters decimal 141, 157, 127,128, 129
99 143,144,157
100 appear to be questionable too.
101 Not in iso-8859-1 nor in html character entities list.
102
103 We translate all strings with a % to do sanitizing.
104 we change a literal ASCII '%' char to %25 so readers
105 know any % is a sanitized char. We could double up
106 a % into %% on output, but switching to %25 is simpler
107 and for readers and prevents ambiguity.
108 If a % is found then utf8 is suppressed for the entire string
109 and uri-style %25 is used and.
110
111 If the dwarfdump runtime environment is UTF-8
112 and the necessary locale() and nl_langinfo()
113 functions are available and the string
114 has no unwanted bytes then utf8 names
115 are printed using utf8 so they look as
116 expected.
117
118 */
119 #define SANBUF_SIZE 400
120 /* Allocates as esb_constructor_fixed() would,
121 so for strings shorter than
122 SANBUF_SIZE bytes no malloc needed. */
123 static char sanbufa[SANBUF_SIZE];
124 static char sanbufb[SANBUF_SIZE];
125 static int usebufa = TRUE;
126 static struct esb_s localesba = {sanbufa,
127 SANBUF_SIZE,0,1,0};
128 static struct esb_s localesbb = {sanbufb,
129 SANBUF_SIZE,0,1,0};
130
131 /* dwarfdump-sanitize table
132 1 means printable ASCII character (byte)
133 3 means non-printable
134 5 means non-printable control character (byte) */
135 char dwarfdump_sanitize_table[256] = {
136 0 /*0*/ ,5 /*0x1*/,5 /*0x2*/,5 /*0x3*/,
137 5 /*0x4*/,5 /*0x5*/,5 /*0x6*/,5 /*0x7*/,
138 5 /*0x8*/,1 /*0x9*/,1 /*0xa*/,5 /*0xb*/,
139 5 /*0xc*/,
140 #ifdef _WIN32
141 1 /*0x0d*/,
142 #else
143 5 /*0x0d*/,
144 #endif
145 5 /*0x0e*/,5 /*0xf*/,
146 5 /*0x10*/,5 /*0x11*/,5 /*0x12*/,5 /*0x13*/,
147 5 /*0x14*/,5 /*0x15*/,5 /*0x16*/,5 /*0x17*/,
148 5 /*0x18*/,5 /*0x19*/,5 /*0x1a*/,5 /*0x1b*/,
149 5 /*0x1c*/,5 /*0x1d*/,5 /*0x1e*/,5 /*0x1f*/,
150 1 /* */,1 /* ! */,1 /* " */,1 /* # */,
151 1 /* $ */,5 /* % */,1 /* & */,1 /* ' */,
152 1 /* ( */,1 /* ) */,1 /* * */,1 /* + */,
153 1 /* , */,1 /* - */,1 /* . */,1 /* / */,
154 1 /* 0 */,1 /* 1 */,1 /* 2 */,1 /* 3 */,
155 1 /* 4 */,1 /* 5 */,1 /* 6 */,1 /* 7 */,
156 1 /* 8 */,1 /* 9 */,1 /* : */,1 /* ; */,
157 1 /* < */,1 /* = */,1 /* > */,1 /* ? */,
158 1 /* @ */,1 /* A */,1 /* B */,1 /* C */,
159 1 /* D */,1 /* E */,1 /* F */,1 /* G */,
160 1 /* H */,1 /* I */,1 /* J */,1 /* K */,
161 1 /* L */,1 /* M */,1 /* N */,1 /* O */,
162 1 /* P */,1 /* Q */,1 /* R */,1 /* S */,
163 1 /* T */,1 /* U */,1 /* V */,1 /* W */,
164 1 /* X */,1 /* Y */,1 /* Z */,1 /* [ */,
165 1 /* \ */,1 /* ] */,1 /* ^ */,1 /* _ */,
166 1 /* ` */,1 /* a */,1 /* b */,1 /* c */,
167 1 /* d */,1 /* e */,1 /* f */,1 /* g */,
168 1 /* h */,1 /* i */,1 /* j */,1 /* k */,
169 1 /* l */,1 /* m */,1 /* n */,1 /* o */,
170 1 /* p */,1 /* q */,1 /* r */,1 /* s */,
171 1 /* t */,1 /* u */,1 /* v */,1 /* w */,
172 1 /* x */,1 /* y */,1 /* z */,1 /* { */,
173 1 /* | */,1 /* } */,1 /* ~ */,5 /*0x7f*/,
174 3 /*0x80*/,3 /*0x81*/,3 /*0x82*/,3 /*0x83*/,
175 3 /*0x84*/,3 /*0x85*/,3 /*0x86*/,3 /*0x87*/,
176 3 /*0x88*/,3 /*0x89*/,3 /*0x8a*/,3 /*0x8b*/,
177 3 /*0x8c*/,3 /*0x8d*/,3 /*0x8e*/,3 /*0x8f*/,
178 3 /*0x90*/,3 /*0x91*/,3 /*0x92*/,3 /*0x93*/,
179 3 /*0x94*/,3 /*0x95*/,3 /*0x96*/,3 /*0x97*/,
180 3 /*0x98*/,3 /*0x99*/,3 /*0x9a*/,3 /*0x9b*/,
181 3 /*0x9c*/,3 /*0x9d*/,3 /*0x9e*/,3 /*0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/,
204 3 /*0xf8*/,3 /*0xf9*/,3 /*0xfa*/,3 /*0xfb*/,
205 3 /*0xfc*/,3 /*0xfd*/,3 /*0xfe*/,3 /*0xff*/,};
206
207 /* do_sanity_insert() and no_questionable_chars()
208 absolutely must have the same idea of
209 questionable characters. Be Careful.
210 Until 0.5.0 the two did NOT agree on
211 handling of newline or tab or carriage return
212 which was... a bug.
213 no_questionable_chars() said those three ok
214 so do_sanity_insert was never called, making
215 an inconsistency vs this code as it was
216 before 0.5.0.
217 */
218 static void
219 do_sanity_insert( const char *s,struct esb_s *mesb)
220 {
221 const char *cp = s;
222
223 for ( ; *cp; cp++) {
224 unsigned c = *cp & 0xff ;
225 int t = dwarfdump_sanitize_table[c];
226
227 if (t == 1) {
228 esb_appendn(mesb,cp,1);
229 continue;
230 }
231 esb_appendn(mesb, "%",1);
232 esb_append_printf_u(mesb, "%02x",c & 0xff);
233 }
234 }
235
236 /* This routine improves overall dwarfdump
237 run times a lot by separating strings
238 that might print badly from strings that
239 will print fine.
240 In one large test case it reduces run time
241 from 140 seconds to 13 seconds. */
242 static int
243 no_questionable_chars(const char *s) {
244 const char *cp = s;
245
246 for ( ; *cp; cp++) {
247 unsigned c = *cp & 0xff ;
248 int t = dwarfdump_sanitize_table[c];
249
250 if (t == 1) {
251 continue;
252 }
253 return FALSE;
254 }
255 return TRUE;
256 }
257
258 void
259 sanitized_string_destructor(void)
260 {
261 esb_destructor(&localesba);
262 esb_destructor(&localesbb);
263 }
264
265 /* Because we reuse static esb's this MUST NOT
266 be called a third time before printing
267 out the initial returns. It is rarely
268 a problem. But it is up to the caller to
269 behave correctly to avoid getting
270 incorect strings.
271 */
272 const char *
273 sanitized(const char *s)
274 {
275 const char *sout = 0;
276 struct esb_s *lsp = 0;
277
278 #ifndef TESTING
279 if (glflags.gf_no_sanitize_strings) {
280 return s;
281 }
282 #endif
283 if (no_questionable_chars(s)) {
284 /* The original string is safe ASCII as is. */
285 return s;
286 }
287 /* Using esb_destructor is quite expensive in cpu time
288 when we build the next sanitized string
289 so we just empty the localesb.
290 One reason it's expensive is that we do the appends
291 in such small batches in do_sanity-insert().
292 */
293
294 #ifndef TESTING
295 #ifdef HAVE_UTF8
296 if (glflags.gf_print_utf8_flag) {
297 if (DW_DLV_OK == dd_utf8_checkCodePoints(
298 (unsigned char *)s)) {
299 return s;
300 }
301 }
302 #endif /* HAVE_UTF8 */
303 #endif /* TESTING */
304 lsp = usebufa? &localesba: &localesbb;
305 usebufa = !usebufa;
306 esb_empty_string(lsp);
307 do_sanity_insert(s,lsp);
308 sout = esb_get_string(lsp);
309 return sout;
310 }