1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
45 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
47 char *nasm_quote(char *str, size_t len)
49 char c, c1, *p, *q, *nstr, *ep;
56 qlen = 0; /* Length if we need `...` quotes */
57 for (p = str; p < ep; p++) {
73 if (c < ' ' || c > '~') {
74 sq_ok = dq_ok = false;
87 c1 = (p+1 < ep) ? p[1] : 0;
88 if (c1 >= '0' && c1 <= '7')
89 uc = 0377; /* Must use the full form */
106 if (sq_ok || dq_ok) {
107 /* Use '...' or "..." */
108 nstr = nasm_malloc(len+3);
109 nstr[0] = nstr[len+1] = sq_ok ? '\'' : '\"';
112 memcpy(nstr+1, str, len);
114 /* Need to use `...` quoted syntax */
115 nstr = nasm_malloc(qlen+3);
118 for (p = str; p < ep; p++) {
159 if (c < ' ' || c > '~') {
160 c1 = (p+1 < ep) ? p[1] : 0;
161 if (c1 >= '0' && c1 <= '7')
162 uc = 0377; /* Must use the full form */
167 *q++ = ((unsigned char)c >> 6) + '0';
169 *q++ = (((unsigned char)c >> 3) & 7) + '0';
170 *q++ = ((unsigned char)c & 7) + '0';
180 nasm_assert((size_t)(q-nstr) == qlen+3);
185 static char *emit_utf8(char *q, int32_t v)
188 /* Impossible - do nothing */
189 } else if (v <= 0x7f) {
191 } else if (v <= 0x000007ff) {
192 *q++ = 0xc0 | (v >> 6);
193 *q++ = 0x80 | (v & 63);
194 } else if (v <= 0x0000ffff) {
195 *q++ = 0xe0 | (v >> 12);
196 *q++ = 0x80 | ((v >> 6) & 63);
197 *q++ = 0x80 | (v & 63);
198 } else if (v <= 0x001fffff) {
199 *q++ = 0xf0 | (v >> 18);
200 *q++ = 0x80 | ((v >> 12) & 63);
201 *q++ = 0x80 | ((v >> 6) & 63);
202 *q++ = 0x80 | (v & 63);
203 } else if (v <= 0x03ffffff) {
204 *q++ = 0xf8 | (v >> 24);
205 *q++ = 0x80 | ((v >> 18) & 63);
206 *q++ = 0x80 | ((v >> 12) & 63);
207 *q++ = 0x80 | ((v >> 6) & 63);
208 *q++ = 0x80 | (v & 63);
210 *q++ = 0xfc | (v >> 30);
211 *q++ = 0x80 | ((v >> 24) & 63);
212 *q++ = 0x80 | ((v >> 18) & 63);
213 *q++ = 0x80 | ((v >> 12) & 63);
214 *q++ = 0x80 | ((v >> 6) & 63);
215 *q++ = 0x80 | (v & 63);
221 * Do an *in-place* dequoting of the specified string, returning the
222 * resulting length (which may be containing embedded nulls.)
224 * In-place replacement is possible since the unquoted length is always
225 * shorter than or equal to the quoted length.
227 * *ep points to the final quote, or to the null if improperly quoted.
229 size_t nasm_unquote(char *str, char **ep)
254 /* '...' or "..." string */
255 while ((c = *p) && c != bq) {
272 state = st_backslash;
285 escp = p; /* Beginning of argument sequence */
334 ndig = 2; /* Up to two more digits */
344 if (c >= '0' && c <= '7') {
345 nval = (nval << 3) + (c - '0');
351 p--; /* Process this character again */
358 if ((c >= '0' && c <= '9') ||
359 (c >= 'A' && c <= 'F') ||
360 (c >= 'a' && c <= 'f')) {
361 nval = (nval << 4) + numvalue(c);
367 p--; /* Process this character again */
368 *q++ = (p > escp) ? nval : escp[-1];
374 if ((c >= '0' && c <= '9') ||
375 (c >= 'A' && c <= 'F') ||
376 (c >= 'a' && c <= 'f')) {
377 nval = (nval << 4) + numvalue(c);
379 q = emit_utf8(q, nval);
383 p--; /* Process this character again */
385 q = emit_utf8(q, nval);
401 *q++ = (p > escp) ? nval : escp[-1];
405 q = emit_utf8(q, nval);
414 /* Not a quoted string, just return the input... */
415 p = q = strchr(str, '\0');
425 * Find the end of a quoted string; returns the pointer to the terminating
426 * character (either the ending quote or the null character, if unterminated.)
428 char *nasm_skip_string(char *str)
439 if (bq == '\'' || bq == '\"') {
440 /* '...' or "..." string */
441 for (p = str+1; *p && *p != bq; p++)
444 } else if (bq == '`') {
454 state = st_backslash;
457 return p-1; /* Found the end */
465 * Note: for the purpose of finding the end of the string,
466 * all successor states to st_backslash are functionally
467 * equivalent to st_start, since either a backslash or
468 * a backquote will force a return to the st_start state.
474 return p; /* Unterminated string... */
476 return str; /* Not a string... */