| 1 |
/* |
| 2 |
Copyright 2011 David Anderson. All rights reserved. |
| 3 |
|
| 4 |
This program is free software; you can redistribute |
| 5 |
it and/or modify it under the terms of version 2 of |
| 6 |
the GNU General Public License as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is distributed in the hope that it would |
| 10 |
be useful, but WITHOUT ANY WARRANTY; without even the |
| 11 |
implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 |
PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
Further, this software is distributed without any |
| 15 |
warranty that it is free of the rightful claim of |
| 16 |
any third person regarding infringement or the like. |
| 17 |
Any license provided herein, whether implied or |
| 18 |
otherwise, applies only to this software file. |
| 19 |
Patent licenses, if any, provided herein do not apply |
| 20 |
to combinations of this program with other software, |
| 21 |
or any other product whatsoever. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General |
| 24 |
Public License along with this program; if not, write |
| 25 |
the Free Software Foundation, Inc., 51 Franklin Street |
| 26 |
- Fifth Floor, Boston MA 02110-1301, USA. |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
#include <ctype.h> /* isalnum() isspace() */ |
| 31 |
#include <stdio.h> /* printf() */ |
| 32 |
|
| 33 |
/* Generates a table which identifies a few dangerous characters. |
| 34 |
Ones one does not want to appear in output. |
| 35 |
|
| 36 |
It's a bit arbitrary in that we allow lots of shell-interpreted |
| 37 |
characters through, and most characters generally. |
| 38 |
|
| 39 |
But not control characters or single or double quotes. |
| 40 |
The quotes would be particularly problematic for post-processing |
| 41 |
dwarfdump output sensibly. |
| 42 |
|
| 43 |
This was used once to create the table in uri.c and |
| 44 |
not is unlikely to be used again. |
| 45 |
|
| 46 |
*/ |
| 47 |
static void |
| 48 |
print_ctype_entry(int c) |
| 49 |
{ |
| 50 |
char v[2]; |
| 51 |
v[0] = c; |
| 52 |
v[1] = 0; |
| 53 |
if (c == 0) { |
| 54 |
printf("0, /* NUL 0x%02x */\n",c); |
| 55 |
return; |
| 56 |
} |
| 57 |
if (isalnum(c) || c == ' ' ) { |
| 58 |
/* We let the space character print as space since |
| 59 |
lots of files are named that way in Mac and Windows. |
| 60 |
*/ |
| 61 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 62 |
return; |
| 63 |
} |
| 64 |
if (c == 0x21 || c == 0x23 || c == 0x26) { |
| 65 |
/* We let the space character print as space since |
| 66 |
lots of files are named that way in Mac and Windows. |
| 67 |
*/ |
| 68 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 69 |
return; |
| 70 |
} |
| 71 |
if (isspace(c)) { |
| 72 |
/* Other white space translated. */ |
| 73 |
printf("0, /* whitespace 0x%02x */\n",c); |
| 74 |
return; |
| 75 |
} |
| 76 |
if (c == 0x7f) { |
| 77 |
printf("0, /* DEL 0x%02x */\n",c); |
| 78 |
return; |
| 79 |
} |
| 80 |
if (c >= 0x01 && c <= 0x20 ) { |
| 81 |
/* ASCII control characters. */ |
| 82 |
printf("0, /* control 0x%02x */\n",c); |
| 83 |
return; |
| 84 |
} |
| 85 |
if (c == '\'' || c == '\"' || c == '%' || c == ';' ) { |
| 86 |
printf("0, /* \'%s\' 0x%02x */\n",v,c); |
| 87 |
return; |
| 88 |
} |
| 89 |
if (c >= 0x3a && c <= 0x40 ) { |
| 90 |
/* ASCII */ |
| 91 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 92 |
return; |
| 93 |
} |
| 94 |
if (c == 0xa0 || c == 0xff ) { |
| 95 |
printf("0, /* other: 0x%02x */\n",c); |
| 96 |
return; |
| 97 |
} |
| 98 |
if (c >= 0x27 && c <= 0x2f ) { |
| 99 |
/* ASCII */ |
| 100 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 101 |
return; |
| 102 |
} |
| 103 |
if (c >= 0x5b && c <= 0x5f ) { |
| 104 |
/* ASCII */ |
| 105 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 106 |
return; |
| 107 |
} |
| 108 |
if (c >= 0x60 && c <= 0x60 ) { |
| 109 |
/* ASCII */ |
| 110 |
printf("0, /* \'%s\' 0x%02x */\n",v,c); |
| 111 |
return; |
| 112 |
} |
| 113 |
if (c >= 0x7b && c <= 0x7e ) { |
| 114 |
/* ASCII */ |
| 115 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 116 |
return; |
| 117 |
} |
| 118 |
if (c < 0x7f) { |
| 119 |
/* ASCII */ |
| 120 |
printf("1, /* \'%s\' 0x%02x */\n",v,c); |
| 121 |
return; |
| 122 |
} |
| 123 |
/* We are allowing other iso 8859 characters through unchanged. */ |
| 124 |
printf("1, /* 0x%02x */\n",c); |
| 125 |
} |
| 126 |
|
| 127 |
/* This mirrors what dwarfdump has long done via code |
| 128 |
in do_sanity_insert() in dd_sanitized.c. |
| 129 |
We are dropping special treatment of \r here |
| 130 |
as sometimes a pass through (with _WIN32) |
| 131 |
as that was a bug in sanity_insert() given |
| 132 |
we never passed through \t or \n characters. |
| 133 |
The table output here makes dd_sanitized simpler and |
| 134 |
faster. |
| 135 |
|
| 136 |
The code in dd_sanitized.c was inconsistent on |
| 137 |
treatment of newline carriage-return and tab, but |
| 138 |
now we are consistent. */ |
| 139 |
static void |
| 140 |
print_sanitize_entry(int c_in) |
| 141 |
{ |
| 142 |
unsigned char c = c_in & 0xff; |
| 143 |
|
| 144 |
if (!c_in) { |
| 145 |
printf("0 /*%u*/,",c); |
| 146 |
return; |
| 147 |
} |
| 148 |
if (!(c_in % 4)) { |
| 149 |
printf("\n"); |
| 150 |
} |
| 151 |
if (c == '%') { |
| 152 |
printf("3 /* %c */,",c); |
| 153 |
return; |
| 154 |
} |
| 155 |
if (c >= 0x20 && c <=0x7e) { |
| 156 |
printf("1 /* %c */,",c); |
| 157 |
return; |
| 158 |
} |
| 159 |
if (c == 0x0D) { |
| 160 |
printf("\n#ifdef _WIN32\n"); |
| 161 |
printf("1 /*0x0d*/,\n"); |
| 162 |
printf("#else\n"); |
| 163 |
printf("3 /*0x0d*/,\n"); |
| 164 |
printf("#endif\n"); |
| 165 |
return; |
| 166 |
} |
| 167 |
if (c == 0x0A || c == 0x09 ) { |
| 168 |
printf("1 /*0x%x*/,",c); |
| 169 |
return; |
| 170 |
} |
| 171 |
if (c < 0x20) { |
| 172 |
printf("3 /*0x%x*/,",c); |
| 173 |
return; |
| 174 |
} |
| 175 |
/* This notices iso-8859 and UTF-8 |
| 176 |
data as we don't deal with them |
| 177 |
properly in dwarfdump or allow in uri output. */ |
| 178 |
printf("3 /*0x%x*/,",c); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
int |
| 183 |
main(void) |
| 184 |
{ |
| 185 |
int i = 0; |
| 186 |
printf("/* dwarfdump-ctype table */\n"); |
| 187 |
printf("char dwarfdump_ctype_table[256] = { \n"); |
| 188 |
for ( i = 0 ; i <= 255; ++i) { |
| 189 |
print_ctype_entry(i); |
| 190 |
} |
| 191 |
printf("};\n"); |
| 192 |
|
| 193 |
printf("\n"); |
| 194 |
printf("/* dwarfdump-sanitize table */\n"); |
| 195 |
printf("char dwarfdump_sanitize_table[256] = { \n"); |
| 196 |
for ( i = 0 ; i <= 255; ++i) { |
| 197 |
print_sanitize_entry(i); |
| 198 |
} |
| 199 |
printf("};\n"); |
| 200 |
return 0; |
| 201 |
} |