blob: 1cadbd373e5e4ed09a2ec78ad514d88844ee78a7 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00006 * and Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000013#define WANT_HEX_ESCAPES 1
14
15/* Usual "this only works for ascii compatible encodings" disclaimer. */
16#undef _tolower
17#define _tolower(X) ((X)|((char) 0x20))
18
Manuel Novoa III cad53642003-03-19 09:13:01 +000019char bb_process_escape_sequence(const char **ptr)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000021 static const char charmap[] ALIGN1 = {
Eric Andersene5dfced2001-04-09 22:48:12 +000022 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
23 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenaad1a882001-03-16 22:47:14 +000024
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000025 const char *p;
26 const char *q;
27 unsigned int num_digits;
28 unsigned int r;
29 unsigned int n;
30 unsigned int d;
31 unsigned int base;
32
33 num_digits = n = 0;
34 base = 8;
Eric Andersene5dfced2001-04-09 22:48:12 +000035 q = *ptr;
Eric Andersenaad1a882001-03-16 22:47:14 +000036
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000037#ifdef WANT_HEX_ESCAPES
Eric Andersenb2a30052004-07-26 12:11:32 +000038 if (*q == 'x') {
Eric Andersenb2a30052004-07-26 12:11:32 +000039 ++q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000040 base = 16;
41 ++num_digits;
Eric Andersenb2a30052004-07-26 12:11:32 +000042 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000043#endif
Eric Andersenb2a30052004-07-26 12:11:32 +000044
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 do {
Denis Vlasenko7e754f12007-04-09 13:04:50 +000046 d = (unsigned char)(*q) - '0';
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000047#ifdef WANT_HEX_ESCAPES
48 if (d >= 10) {
Denis Vlasenko7e754f12007-04-09 13:04:50 +000049 d = (unsigned char)(_tolower(*q)) - 'a' + 10;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000050 }
51#endif
52
53 if (d >= base) {
54#ifdef WANT_HEX_ESCAPES
55 if ((base == 16) && (!--num_digits)) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056/* return '\\'; */
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000057 --q;
58 }
59#endif
Eric Andersenccfc4482004-07-27 16:45:46 +000060 break;
Eric Andersenb2a30052004-07-26 12:11:32 +000061 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000062
63 r = n * base + d;
64 if (r > UCHAR_MAX) {
65 break;
Eric Andersene5dfced2001-04-09 22:48:12 +000066 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000067
68 n = r;
69 ++q;
70 } while (++num_digits < 3);
Eric Andersenaad1a882001-03-16 22:47:14 +000071
Eric Andersene5dfced2001-04-09 22:48:12 +000072 if (num_digits == 0) { /* mnemonic escape sequence? */
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 p = charmap;
74 do {
Eric Andersene5dfced2001-04-09 22:48:12 +000075 if (*p == *q) {
76 q++;
77 break;
78 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 } while (*++p);
Denis Vlasenko7e754f12007-04-09 13:04:50 +000080 n = *(p + (sizeof(charmap)/2));
Eric Andersene5dfced2001-04-09 22:48:12 +000081 }
Eric Andersenaad1a882001-03-16 22:47:14 +000082
Eric Andersene5dfced2001-04-09 22:48:12 +000083 *ptr = q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000084
Eric Andersene5dfced2001-04-09 22:48:12 +000085 return (char) n;
Eric Andersenaad1a882001-03-16 22:47:14 +000086}