diff options
-rw-r--r-- | libbb/process_escape_sequence.c | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c index f5ac500fa..e6b5fc995 100644 --- a/libbb/process_escape_sequence.c +++ b/libbb/process_escape_sequence.c | |||
@@ -22,35 +22,42 @@ | |||
22 | * | 22 | * |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include <string.h> | ||
25 | #include <stdio.h> | 26 | #include <stdio.h> |
26 | #include <limits.h> | 27 | #include <limits.h> |
28 | #include <ctype.h> | ||
27 | #include "libbb.h" | 29 | #include "libbb.h" |
28 | 30 | ||
31 | #define isodigit(c) ((c) >= '0' && (c) <= '7') | ||
32 | #define hextobin(c) ((c)>='a'&&(c)<='f' ? (c)-'a'+10 : (c)>='A'&&(c)<='F' ? (c)-'A'+10 : (c)-'0') | ||
33 | #define octtobin(c) ((c) - '0') | ||
29 | char bb_process_escape_sequence(const char **ptr) | 34 | char bb_process_escape_sequence(const char **ptr) |
30 | { | 35 | { |
36 | const char *p, *q; | ||
37 | unsigned int num_digits, r, n, hexescape; | ||
31 | static const char charmap[] = { | 38 | static const char charmap[] = { |
32 | 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, | 39 | 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, |
33 | '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; | 40 | '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; |
34 | 41 | ||
35 | const char *p; | 42 | n = r = hexescape = num_digits = 0; |
36 | const char *q; | ||
37 | unsigned int num_digits; | ||
38 | unsigned int r; | ||
39 | unsigned int n; | ||
40 | |||
41 | n = 0; | ||
42 | q = *ptr; | 43 | q = *ptr; |
43 | 44 | ||
44 | num_digits = 0; | 45 | if (*q == 'x') { |
46 | hexescape++; | ||
47 | ++q; | ||
48 | } | ||
49 | |||
45 | do { | 50 | do { |
46 | if (((unsigned int)(*q - '0')) <= 7) { | 51 | if (hexescape && isxdigit(*q)) { |
47 | r = n * 8 + (*q - '0'); | 52 | r = n * 16 + hextobin(*q); |
48 | if (r <= UCHAR_MAX) { | 53 | } else if (isodigit(*q)) { |
49 | n = r; | 54 | r = n * 8 + octtobin(*q); |
50 | ++q; | 55 | } |
51 | if (++num_digits < 3) { | 56 | if (r <= UCHAR_MAX) { |
52 | continue; | 57 | n = r; |
53 | } | 58 | ++q; |
59 | if (++num_digits < 3) { | ||
60 | continue; | ||
54 | } | 61 | } |
55 | } | 62 | } |
56 | break; | 63 | break; |