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