aboutsummaryrefslogtreecommitdiffstats
path: root/src/slibc-human/unescape.c
blob: 6224c15150bcafac9c856f76470a19b6d3ac79e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
 * slibc — Yet another C library
 * Copyright © 2015  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <slibc-human.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>
#include "escapes.h"



/**
 * Parse an escaped string.
 * 
 * Supported escapes:
 *   \' \" \$ \& \? \\ \/ \### \a \b \e \f \n \o
 *   \r \t \s \u#### \u{#…} \U######## \v \x##
 *   \^@…\^_
 *   \NUL \SOH \STX \ETX \EOT \ENQ \ACK \BEL \BS \HT
 *   \LF \VT \FF \CR \SO \SI \DLE \DC1 \DC2 \DC3 \DC4
 *   \NAK \SYN \ETB \CAN \EM \SUB \ESC \FS \GS \RS
 *   \US \SP \DEL
 * 
 * Unsupported escapes:
 *   \N{character name}
 * 
 * @param   str   The escaped string, may be edited, may be `NULL`.
 *                Must not be reused on error.
 * @param   mode  How unrecognised escapes should be handled,
 *                and other configurations, 0 for default.
 * @return        The new end of `str` is returned. `NULL` is returned
 *                on error or if `str` is `NULL`.
 * 
 * @throws  0       `str` is `NULL`.
 * @throws  EINVAL  If `mode` is invalid.
 * @throws  EINVAL  If `str` is invalid and `mode & UNESCAPE_EINVAL`.
 */
char* unescape(char* str, enum unescape_mode mode)
{
#define RANGE(a, c, z)  (((a) <= (c)) && ((c) <= (z)))
#define CxC0(s, m)  (*w++ = (char)((m) | (v >> (s))))
#define Cx80(s)     (*w++ = (char)(0x80 | ((v >> (s)) & 0x3F)))
#define PARSE_HEX(v, C)				      \
  do {						      \
    char c = (C);				      \
    if      (RANGE('0', c, '9'))  c -= '0';	      \
    else if (RANGE('a', c, 'f'))  c -= 'a', c += 10;  \
    else if (RANGE('A', c, 'F'))  c -= 'A', c += 10;  \
    else					      \
      goto fail_u;				      \
    v = (v << 4) | (unsigned long int)c;	      \
    if (v > 0x10FFFFUL)				      \
      goto fail_u;				      \
  } while (0)
#define NEXT_OCTAL(v)  if (RANGE('0', r[1], '7'))  v = (v << 3) | (r[1] - '0'), r++;
#define UNRECOGNISED(c, action)                                     \
  if      (        mode & UNESCAPE_EINVAL)     goto invalid;        \
  else if ((c) && (mode & UNESCAPE_VERBATIM))  action;              \
  else if ((c) && (mode & UNESCAPE_IGNORE))    *w++ = '\\', action
#define ASCII()  \
  ((v == 0) && (mode & UNESCAPE_MOD_UTF8)) ? (*w++ = (char)0xC0, *w++ = (char)0x80) : \
  (v < 0x80)                               ? (*w++ = (char)v, 1)                    : 0
  
  int i, n;
  unsigned long int v;
  char* w;
  char* r;
  
  if (str == NULL)  return errno = 0, NULL;
  if (mode & ~31)   goto invalid;
  if (mod == 0)     mode |= UNESCAPE_EINVAL | UNESCAPE_MOD_UTF8;
  switch (mode & 7)
    {
    case 0:
      mode |= UNESCAPE_EINVAL;
    case 1:
    case 2:
    case 4:
      break;
    default:
      goto invalid;
    }
  
  for (w = r = str; *r; r++)
    if (*r != '/')
      *w++ = *r;
    else
      switch (*++r)
	{
	case '\0':
	  UNRECOGNISED(1, (void)0);
	  break;
	  
	case '&':
	  if   (mode & UNESCAPE_AMPERSAND)  *w++ = (char)255;
	  else UNRECOGNISED(*r, *w++ = '&');
	  break;
	  
#define X(e, c)  case e:  *w++ = c;  break;
	LIST_BIJECTIVE_ESCAPES
#undef X
	case 's':  *w++ = ' ';  break;
	  
	case '^':
	  if (RANGE('@', r[1], '_'))  *w++ = *++r - '@';
	  else
	    {
	      UNRECOGNISED(r[1], *w++ = '^');
	      if (r[1])
		*w++ = *++r;
	    }
	  break;
	  
	case 'u':
	case 'U':
	case 'x':
	  v = 0;
	  if ((r[0] == 'u') && (r[1] == '{'))
	    for (i = 2; r[i] != '}'; i++)
	      PARSE_HEX(v, r[i]);
	  else
	    {
	      switch (*r)
		{
		case 'U':  n = 8;  break;
		case 'u':  n = 4;  break;
		case 'x':  n = 2;  break;
		}
	      for (i = 1; i <= n; i++)
		PARSE_HEX(v, r[i]);
	    }
	  goto done_u;
	fail_u:
	  UNRECOGNISED(r--);
	done_u:
	  if (ASCII());
	  else if (v < (1L << 11))  CxC0(6,  0xC0), Cx80(0);
	  else if (v < (1L << 16))  CxC0(12, 0xE0), Cx80(6),  Cx80(0);
	  else                      CxC0(18, 0xF0), Cx80(12), Cx80(0), Cx80(0);
	  break;
	  
	default:
	  if (RANGE('0', *r, '7'))
	    {
	      int v = *r - '0';
	      NEXT_OCTAL(v);
	      NEXT_OCTAL(v);
	      if   (ASCII());
	      else CxC0(6, 0xC0), Cx80(0);
	    }
	  else if (strchr("'\"$?\\/", *r))  *w++ = *r;
#define X(e, i)  else if (strstarts(r, e) ? (*w++ = i, r += sizeof(e) / sizeof(char) - 2, 1) : 0);
	  LIST_ASCII_NAMES
#undef X
	  else  UNRECOGNISED(r--);
	  break;
	}
  
  return *w = 0, w;
 invalid:
  return errno = EINVAL, NULL;
}