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 * ----------------------------------------------------------------------- */
46 #define numvalue(c) ((c)>='a' ? (c)-'a'+10 : (c)>='A' ? (c)-'A'+10 : (c)-'0')
48 char *nasm_quote(char *str
, size_t len
)
50 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 (c
> 077 || (c1
>= '0' && c1
<= '7'))
89 qlen
+= 4; /* Must use the full form */
103 if (sq_ok
|| dq_ok
) {
104 /* Use '...' or "..." */
105 nstr
= nasm_malloc(len
+3);
106 nstr
[0] = nstr
[len
+1] = sq_ok
? '\'' : '\"';
108 memcpy(nstr
+1, str
, len
);
110 /* Need to use `...` quoted syntax */
111 nstr
= nasm_malloc(qlen
+3);
114 for (p
= str
; p
< ep
; p
++) {
155 if (c
< ' ' || c
> '~') {
156 c1
= (p
+1 < ep
) ? p
[1] : 0;
157 if (c1
>= '0' && c1
<= '7')
158 q
+= sprintf(q
, "\\%03o", (unsigned char)c
);
160 q
+= sprintf(q
, "\\%o", (unsigned char)c
);
169 assert((size_t)(q
-nstr
) == qlen
+3);
174 static char *emit_utf8(char *q
, int32_t v
)
177 /* Impossible - do nothing */
178 } else if (v
<= 0x7f) {
180 } else if (v
<= 0x000007ff) {
181 *q
++ = 0xc0 | (v
>> 6);
182 *q
++ = 0x80 | (v
& 63);
183 } else if (v
<= 0x0000ffff) {
184 *q
++ = 0xe0 | (v
>> 12);
185 *q
++ = 0x80 | ((v
>> 6) & 63);
186 *q
++ = 0x80 | (v
& 63);
187 } else if (v
<= 0x001fffff) {
188 *q
++ = 0xf0 | (v
>> 18);
189 *q
++ = 0x80 | ((v
>> 12) & 63);
190 *q
++ = 0x80 | ((v
>> 6) & 63);
191 *q
++ = 0x80 | (v
& 63);
192 } else if (v
<= 0x03ffffff) {
193 *q
++ = 0xf8 | (v
>> 24);
194 *q
++ = 0x80 | ((v
>> 18) & 63);
195 *q
++ = 0x80 | ((v
>> 12) & 63);
196 *q
++ = 0x80 | ((v
>> 6) & 63);
197 *q
++ = 0x80 | (v
& 63);
199 *q
++ = 0xfc | (v
>> 30);
200 *q
++ = 0x80 | ((v
>> 24) & 63);
201 *q
++ = 0x80 | ((v
>> 18) & 63);
202 *q
++ = 0x80 | ((v
>> 12) & 63);
203 *q
++ = 0x80 | ((v
>> 6) & 63);
204 *q
++ = 0x80 | (v
& 63);
210 * Do an *in-place* dequoting of the specified string, returning the
211 * resulting length (which may be containing embedded nulls.)
213 * In-place replacement is possible since the unquoted length is always
214 * shorter than or equal to the quoted length.
216 * *ep points to the final quote, or to the null if improperly quoted.
218 size_t nasm_unquote(char *str
, char **ep
)
243 /* '...' or "..." string */
244 while ((c
= *p
) && c
!= bq
) {
261 state
= st_backslash
;
274 escp
= p
; /* Beginning of argument sequence */
323 ndig
= 2; /* Up to two more digits */
333 if (c
>= '0' && c
<= '7') {
334 nval
= (nval
<< 3) + (c
- '0');
340 p
--; /* Process this character again */
347 if ((c
>= '0' && c
<= '9') ||
348 (c
>= 'A' && c
<= 'F') ||
349 (c
>= 'a' && c
<= 'f')) {
350 nval
= (nval
<< 4) + numvalue(c
);
356 p
--; /* Process this character again */
357 *q
++ = (p
> escp
) ? nval
: escp
[-1];
363 if ((c
>= '0' && c
<= '9') ||
364 (c
>= 'A' && c
<= 'F') ||
365 (c
>= 'a' && c
<= 'f')) {
366 nval
= (nval
<< 4) + numvalue(c
);
368 q
= emit_utf8(q
, nval
);
372 p
--; /* Process this character again */
374 q
= emit_utf8(q
, nval
);
390 *q
++ = (p
> escp
) ? nval
: escp
[-1];
394 q
= emit_utf8(q
, nval
);
403 /* Not a quoted string, just return the input... */
404 p
= q
= strchr(str
, '\0');
414 * Find the end of a quoted string; returns the pointer to the terminating
415 * character (either the ending quote or the null character, if unterminated.)
417 char *nasm_skip_string(char *str
)
428 if (bq
== '\'' || bq
== '\"') {
429 /* '...' or "..." string */
430 for (p
= str
+1; *p
&& *p
!= bq
; p
++)
433 } else if (bq
== '`') {
443 state
= st_backslash
;
446 return p
-1; /* Found the end */
454 * Note: for the purpose of finding the end of the string,
455 * all successor states to st_backslash are functionally
456 * equivalent to st_start, since either a backslash or
457 * a backquote will force a return to the st_start state.
463 return p
; /* Unterminated string... */
465 return str
; /* Not a string... */