| 1 |
/* |
| 2 |
Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]> |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person |
| 5 |
obtaining a copy of this software and associated documentation |
| 6 |
files (the "Software"), to deal in the Software without |
| 7 |
restriction, including without limitation the rights to use, |
| 8 |
copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
sell copies of the Software, and to permit persons to whom |
| 10 |
the Software is furnished to do so, subject to the following |
| 11 |
conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be |
| 14 |
included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 17 |
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 |
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
| 19 |
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
| 23 |
OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. |
| 26 |
*/ |
| 27 |
|
| 28 |
/* The code has been reformatted to match libdwarf |
| 29 |
codingstyle */ |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
#include <stdio.h> |
| 33 |
#include "dwarf.h" |
| 34 |
#include "libdwarf.h" |
| 35 |
#include "dd_sanitized.h" |
| 36 |
#include "dd_utf8.h" |
| 37 |
|
| 38 |
#ifdef HAVE_UTF8 |
| 39 |
|
| 40 |
#define UTF8_ACCEPT 0 |
| 41 |
#define UTF8_REJECT 1 |
| 42 |
#define TRUE 1 |
| 43 |
#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1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 61 |
}; |
| 62 |
|
| 63 |
/* codep (codepoints) count the number of characters |
| 64 |
(as opposed to the number of bytes) |
| 65 |
*/ |
| 66 |
Dwarf_Unsigned |
| 67 |
dd_utf8_decode(Dwarf_Unsigned* state, Dwarf_Unsigned* codep, |
| 68 |
unsigned char byte) { |
| 69 |
unsigned char type = utf8d[byte]; |
| 70 |
|
| 71 |
if (*state == UTF8_ACCEPT) { |
| 72 |
int t = dwarfdump_sanitize_table[byte]; |
| 73 |
if (t == 5) { |
| 74 |
/* Certain control chars are legal utf8 |
| 75 |
since legal ASCII, but we |
| 76 |
reject them here so printf won't emit them |
| 77 |
and possibly screw up a live terminal session. |
| 78 |
We do not really care about a string |
| 79 |
with both utf8 *and* bogus control |
| 80 |
characters: let such print uri-style. */ |
| 81 |
*state = UTF8_REJECT; |
| 82 |
return *state; |
| 83 |
} |
| 84 |
*codep = (0xff >> type) & (byte); |
| 85 |
} else { |
| 86 |
*codep = (byte & 0x3fu) | (*codep << 6) ; |
| 87 |
} |
| 88 |
#if 0 |
| 89 |
/* Documenting: from the original version */ |
| 90 |
*codep = (*state != UTF8_ACCEPT) ? |
| 91 |
(byte & 0x3fu) | (*codep << 6) : |
| 92 |
(0xff >> type) & (byte); |
| 93 |
#endif |
| 94 |
|
| 95 |
/* ASSERT: values cannot exceed 400 */ |
| 96 |
*state = utf8d[256 + *state*16 + type]; |
| 97 |
return *state; |
| 98 |
} |
| 99 |
|
| 100 |
/* It is necessary to remember the codepoint |
| 101 |
for the algorithm to work, though |
| 102 |
we ignore the codpoints. */ |
| 103 |
int |
| 104 |
dd_utf8_checkCodePoints(unsigned char* s) { |
| 105 |
Dwarf_Unsigned codepoint; |
| 106 |
Dwarf_Unsigned state = 0; |
| 107 |
|
| 108 |
for ( ; *s; ++s) { |
| 109 |
dd_utf8_decode(&state, &codepoint, *s); |
| 110 |
if (state == UTF8_REJECT) { |
| 111 |
return DW_DLV_ERROR; |
| 112 |
} |
| 113 |
} |
| 114 |
if ( state == UTF8_ACCEPT) { |
| 115 |
return DW_DLV_OK; |
| 116 |
} |
| 117 |
return DW_DLV_ERROR; |
| 118 |
} |
| 119 |
#endif /* HAVE_UTF8 */ |