2009-06-29 08:13:04 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2019-04-23 05:29:29 +08:00
|
|
|
*
|
2019-06-07 07:21:01 +08:00
|
|
|
* Copyright 1996-2019 The NASM Authors - All Rights Reserved
|
2009-06-29 08:13:04 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
2008-06-02 08:23:51 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2019-04-23 05:29:29 +08:00
|
|
|
*
|
2009-06-29 08:13:04 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* quote.c
|
2008-06-02 08:23:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "nasmlib.h"
|
|
|
|
#include "quote.h"
|
2019-04-23 05:29:29 +08:00
|
|
|
#include "nctype.h"
|
|
|
|
#include "error.h"
|
2008-06-02 08:23:51 +08:00
|
|
|
|
2019-04-26 09:00:32 +08:00
|
|
|
/*
|
|
|
|
* Create a NASM quoted string in newly allocated memory. Update the
|
|
|
|
* *lenp parameter with the output length (sans final NUL).
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *nasm_quote(const char *str, size_t *lenp)
|
2008-06-02 08:23:51 +08:00
|
|
|
{
|
2016-05-10 17:54:15 +08:00
|
|
|
const char *p, *ep;
|
|
|
|
char c, c1, *q, *nstr;
|
2009-07-16 04:22:47 +08:00
|
|
|
unsigned char uc;
|
2008-06-02 08:23:51 +08:00
|
|
|
bool sq_ok, dq_ok;
|
|
|
|
size_t qlen;
|
2019-04-26 09:00:32 +08:00
|
|
|
size_t len = *lenp;
|
2008-06-02 08:23:51 +08:00
|
|
|
|
|
|
|
sq_ok = dq_ok = true;
|
|
|
|
ep = str+len;
|
|
|
|
qlen = 0; /* Length if we need `...` quotes */
|
|
|
|
for (p = str; p < ep; p++) {
|
|
|
|
c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '\'':
|
|
|
|
sq_ok = false;
|
|
|
|
qlen++;
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
dq_ok = false;
|
|
|
|
qlen++;
|
|
|
|
break;
|
|
|
|
case '`':
|
|
|
|
case '\\':
|
|
|
|
qlen += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (c < ' ' || c > '~') {
|
|
|
|
sq_ok = dq_ok = false;
|
|
|
|
switch (c) {
|
|
|
|
case '\a':
|
|
|
|
case '\b':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
|
|
|
case '\r':
|
|
|
|
case 27:
|
|
|
|
qlen += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
c1 = (p+1 < ep) ? p[1] : 0;
|
2009-07-15 02:48:26 +08:00
|
|
|
if (c1 >= '0' && c1 <= '7')
|
2009-07-16 04:22:47 +08:00
|
|
|
uc = 0377; /* Must use the full form */
|
2008-06-02 08:23:51 +08:00
|
|
|
else
|
2009-07-16 04:22:47 +08:00
|
|
|
uc = c;
|
|
|
|
if (uc > 077)
|
2009-07-15 02:48:26 +08:00
|
|
|
qlen++;
|
2009-07-16 04:22:47 +08:00
|
|
|
if (uc > 07)
|
2009-07-15 02:48:26 +08:00
|
|
|
qlen++;
|
|
|
|
qlen += 2;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qlen++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sq_ok || dq_ok) {
|
|
|
|
/* Use '...' or "..." */
|
|
|
|
nstr = nasm_malloc(len+3);
|
|
|
|
nstr[0] = nstr[len+1] = sq_ok ? '\'' : '\"';
|
2019-04-26 09:00:32 +08:00
|
|
|
q = &nstr[len+2];
|
2010-09-07 13:52:47 +08:00
|
|
|
if (len > 0)
|
|
|
|
memcpy(nstr+1, str, len);
|
2008-06-02 08:23:51 +08:00
|
|
|
} else {
|
|
|
|
/* Need to use `...` quoted syntax */
|
|
|
|
nstr = nasm_malloc(qlen+3);
|
|
|
|
q = nstr;
|
|
|
|
*q++ = '`';
|
|
|
|
for (p = str; p < ep; p++) {
|
|
|
|
c = *p;
|
|
|
|
switch (c) {
|
|
|
|
case '`':
|
|
|
|
case '\\':
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = c;
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 7:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'a';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 8:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'b';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 9:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 't';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 10:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'n';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 11:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'v';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 12:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'f';
|
|
|
|
break;
|
2008-06-03 01:38:54 +08:00
|
|
|
case 13:
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'r';
|
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = 'e';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (c < ' ' || c > '~') {
|
|
|
|
c1 = (p+1 < ep) ? p[1] : 0;
|
|
|
|
if (c1 >= '0' && c1 <= '7')
|
2009-07-16 04:22:47 +08:00
|
|
|
uc = 0377; /* Must use the full form */
|
2008-06-02 08:23:51 +08:00
|
|
|
else
|
2009-07-16 04:22:47 +08:00
|
|
|
uc = c;
|
2009-07-15 02:48:26 +08:00
|
|
|
*q++ = '\\';
|
2009-07-16 04:22:47 +08:00
|
|
|
if (uc > 077)
|
|
|
|
*q++ = ((unsigned char)c >> 6) + '0';
|
|
|
|
if (uc > 07)
|
|
|
|
*q++ = (((unsigned char)c >> 3) & 7) + '0';
|
|
|
|
*q++ = ((unsigned char)c & 7) + '0';
|
2009-07-15 02:48:26 +08:00
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
} else {
|
|
|
|
*q++ = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*q++ = '`';
|
2019-04-26 09:00:32 +08:00
|
|
|
nasm_assert((size_t)(q-nstr) == qlen+2);
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
2019-04-26 09:00:32 +08:00
|
|
|
*q = '\0';
|
|
|
|
*lenp = q - nstr;
|
2008-06-02 08:23:51 +08:00
|
|
|
return nstr;
|
|
|
|
}
|
|
|
|
|
2019-04-23 05:29:29 +08:00
|
|
|
static unsigned char *emit_utf8(unsigned char *q, uint32_t v)
|
2008-06-02 08:23:51 +08:00
|
|
|
{
|
2019-04-23 05:29:29 +08:00
|
|
|
uint32_t vb1, vb2, vb3, vb4, vb5;
|
|
|
|
|
|
|
|
if (v <= 0x7f) {
|
2008-06-02 08:23:51 +08:00
|
|
|
*q++ = v;
|
2019-04-23 05:29:29 +08:00
|
|
|
goto out0;
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
2019-04-23 05:29:29 +08:00
|
|
|
|
|
|
|
vb1 = v >> 6;
|
2019-06-07 08:15:16 +08:00
|
|
|
if (vb1 <= 0x1f) {
|
2019-04-23 05:29:29 +08:00
|
|
|
*q++ = 0xc0 + vb1;
|
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb2 = vb1 >> 6;
|
2019-06-07 08:15:16 +08:00
|
|
|
if (vb2 <= 0x0f) {
|
2019-04-23 05:29:29 +08:00
|
|
|
*q++ = 0xe0 + vb2;
|
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb3 = vb2 >> 6;
|
2019-06-07 08:15:16 +08:00
|
|
|
if (vb3 <= 0x07) {
|
2019-04-23 05:29:29 +08:00
|
|
|
*q++ = 0xf0 + vb3;
|
|
|
|
goto out3;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb4 = vb3 >> 6;
|
2019-06-07 08:15:16 +08:00
|
|
|
if (vb4 <= 0x03) {
|
2019-04-23 05:29:29 +08:00
|
|
|
*q++ = 0xf8 + vb4;
|
|
|
|
goto out4;
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:26:28 +08:00
|
|
|
/*
|
|
|
|
* Note: this is invalid even for "classic" (pre-UTF16) 31-bit
|
|
|
|
* UTF-8 if the value is >= 0x8000000. This at least tries to do
|
|
|
|
* something vaguely sensible with it. Caveat programmer.
|
|
|
|
* The __utf*__ string transform functions do reject these
|
|
|
|
* as invalid input.
|
2019-06-07 08:32:44 +08:00
|
|
|
*
|
|
|
|
* vb5 cannot be more than 3, as a 32-bit value has been shifted
|
|
|
|
* right by 5*6 = 30 bits already.
|
2019-06-07 08:26:28 +08:00
|
|
|
*/
|
2019-04-23 05:29:29 +08:00
|
|
|
vb5 = vb4 >> 6;
|
2019-06-07 08:26:28 +08:00
|
|
|
*q++ = 0xfc + vb5;
|
|
|
|
goto out5;
|
2019-04-23 05:29:29 +08:00
|
|
|
|
|
|
|
/* Emit extension bytes as appropriate */
|
|
|
|
out5: *q++ = 0x80 + (vb4 & 63);
|
|
|
|
out4: *q++ = 0x80 + (vb3 & 63);
|
|
|
|
out3: *q++ = 0x80 + (vb2 & 63);
|
|
|
|
out2: *q++ = 0x80 + (vb1 & 63);
|
|
|
|
out1: *q++ = 0x80 + (v & 63);
|
|
|
|
out0: return q;
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
|
|
|
|
2019-06-07 07:21:01 +08:00
|
|
|
static inline uint32_t ctlbit(uint32_t v)
|
|
|
|
{
|
|
|
|
return unlikely(v < 32) ? UINT32_C(1) << v : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CTL_ERR(c) \
|
|
|
|
(badctl & (ctlmask |= ctlbit(c)))
|
|
|
|
|
|
|
|
#define EMIT_UTF8(c) \
|
|
|
|
do { \
|
|
|
|
uint32_t ec = (c); \
|
|
|
|
if (!CTL_ERR(ec)) \
|
|
|
|
q = emit_utf8(q, ec); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define EMIT(c) \
|
|
|
|
do { \
|
|
|
|
unsigned char ec = (c); \
|
|
|
|
if (!CTL_ERR(ec)) \
|
|
|
|
*q++ = ec; \
|
|
|
|
} while (0)
|
|
|
|
|
preproc: add %i... variants, evaluated macro parameters, cleanups
All directives which create single-line macros now have %i... variants
to define case-insensitive versions. Case insensitive rather sucks,
but at least this way it is consistent.
Single-line macro parameters can now be evaluated as a number, as done
by %assign. To do so, declare a parameter starting with =, for
example:
%define foo(x,=y) mov [x],macro_array_y
... would evaluate y as a number but leave x as a string.
NOTE: it would arguably be better to have this as a per-instance
basis, but it is easily handled by having a secondary macro called
with the same argument twice.
Finally, add a more consistent method for defining "magic" macros,
which need to be evaluated at runtime. For now, it is only used by the
special macros __FILE__, __LINE__, __BITS__, __PTR__, and __PASS__.
__PTR__ is a new macro which evaluates to word, dword or qword
matching the value of __BITS__.
The magic macro framework, however, provides a natural hook for a
future plug-in infrastructure to hook into a scripting language.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-02-27 06:00:54 +08:00
|
|
|
/*
|
2019-04-26 09:00:32 +08:00
|
|
|
* Same as nasm_quote, but take the length of a C string;
|
|
|
|
* the lenp argument is optional.
|
preproc: add %i... variants, evaluated macro parameters, cleanups
All directives which create single-line macros now have %i... variants
to define case-insensitive versions. Case insensitive rather sucks,
but at least this way it is consistent.
Single-line macro parameters can now be evaluated as a number, as done
by %assign. To do so, declare a parameter starting with =, for
example:
%define foo(x,=y) mov [x],macro_array_y
... would evaluate y as a number but leave x as a string.
NOTE: it would arguably be better to have this as a per-instance
basis, but it is easily handled by having a secondary macro called
with the same argument twice.
Finally, add a more consistent method for defining "magic" macros,
which need to be evaluated at runtime. For now, it is only used by the
special macros __FILE__, __LINE__, __BITS__, __PTR__, and __PASS__.
__PTR__ is a new macro which evaluates to word, dword or qword
matching the value of __BITS__.
The magic macro framework, however, provides a natural hook for a
future plug-in infrastructure to hook into a scripting language.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-02-27 06:00:54 +08:00
|
|
|
*/
|
2019-04-26 09:00:32 +08:00
|
|
|
char *nasm_quote_cstr(const char *str, size_t *lenp)
|
preproc: add %i... variants, evaluated macro parameters, cleanups
All directives which create single-line macros now have %i... variants
to define case-insensitive versions. Case insensitive rather sucks,
but at least this way it is consistent.
Single-line macro parameters can now be evaluated as a number, as done
by %assign. To do so, declare a parameter starting with =, for
example:
%define foo(x,=y) mov [x],macro_array_y
... would evaluate y as a number but leave x as a string.
NOTE: it would arguably be better to have this as a per-instance
basis, but it is easily handled by having a secondary macro called
with the same argument twice.
Finally, add a more consistent method for defining "magic" macros,
which need to be evaluated at runtime. For now, it is only used by the
special macros __FILE__, __LINE__, __BITS__, __PTR__, and __PASS__.
__PTR__ is a new macro which evaluates to word, dword or qword
matching the value of __BITS__.
The magic macro framework, however, provides a natural hook for a
future plug-in infrastructure to hook into a scripting language.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-02-27 06:00:54 +08:00
|
|
|
{
|
2019-04-26 09:00:32 +08:00
|
|
|
size_t len = strlen(str);
|
|
|
|
char *qstr = nasm_quote(str, &len);
|
|
|
|
if (lenp)
|
|
|
|
*lenp = len;
|
|
|
|
return qstr;
|
preproc: add %i... variants, evaluated macro parameters, cleanups
All directives which create single-line macros now have %i... variants
to define case-insensitive versions. Case insensitive rather sucks,
but at least this way it is consistent.
Single-line macro parameters can now be evaluated as a number, as done
by %assign. To do so, declare a parameter starting with =, for
example:
%define foo(x,=y) mov [x],macro_array_y
... would evaluate y as a number but leave x as a string.
NOTE: it would arguably be better to have this as a per-instance
basis, but it is easily handled by having a secondary macro called
with the same argument twice.
Finally, add a more consistent method for defining "magic" macros,
which need to be evaluated at runtime. For now, it is only used by the
special macros __FILE__, __LINE__, __BITS__, __PTR__, and __PASS__.
__PTR__ is a new macro which evaluates to word, dword or qword
matching the value of __BITS__.
The magic macro framework, however, provides a natural hook for a
future plug-in infrastructure to hook into a scripting language.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
2019-02-27 06:00:54 +08:00
|
|
|
}
|
|
|
|
|
2008-06-02 08:23:51 +08:00
|
|
|
/*
|
|
|
|
* Do an *in-place* dequoting of the specified string, returning the
|
|
|
|
* resulting length (which may be containing embedded nulls.)
|
|
|
|
*
|
|
|
|
* In-place replacement is possible since the unquoted length is always
|
|
|
|
* shorter than or equal to the quoted length.
|
2008-06-05 02:26:59 +08:00
|
|
|
*
|
|
|
|
* *ep points to the final quote, or to the null if improperly quoted.
|
2019-04-23 05:29:29 +08:00
|
|
|
*
|
2019-06-07 07:21:01 +08:00
|
|
|
* Issue an error if the string contains control characters
|
|
|
|
* corresponding to bits set in badctl; in that case, the output
|
|
|
|
* string, but not *ep, is truncated before the first invalid
|
|
|
|
* character.
|
2008-06-02 08:23:51 +08:00
|
|
|
*/
|
2019-04-23 05:29:29 +08:00
|
|
|
|
|
|
|
static size_t nasm_unquote_common(char *str, char **ep,
|
2019-06-07 07:21:01 +08:00
|
|
|
const uint32_t badctl)
|
2008-06-02 08:23:51 +08:00
|
|
|
{
|
2019-06-07 07:21:01 +08:00
|
|
|
unsigned char bq;
|
|
|
|
const unsigned char *p;
|
|
|
|
const unsigned char *escp = NULL;
|
|
|
|
unsigned char *q;
|
2019-04-23 05:29:29 +08:00
|
|
|
unsigned char c;
|
2019-06-07 07:21:01 +08:00
|
|
|
uint32_t ctlmask = 0; /* Mask of control characters seen */
|
2008-06-02 08:23:51 +08:00
|
|
|
enum unq_state {
|
|
|
|
st_start,
|
|
|
|
st_backslash,
|
|
|
|
st_hex,
|
|
|
|
st_oct,
|
2019-06-07 07:21:01 +08:00
|
|
|
st_ucs,
|
|
|
|
st_done
|
2008-06-02 08:23:51 +08:00
|
|
|
} state;
|
|
|
|
int ndig = 0;
|
2019-04-23 05:29:29 +08:00
|
|
|
uint32_t nval = 0;
|
|
|
|
|
|
|
|
p = q = (unsigned char *)str;
|
2008-06-02 08:23:51 +08:00
|
|
|
|
2008-06-05 02:26:59 +08:00
|
|
|
bq = *p++;
|
2008-06-02 08:23:51 +08:00
|
|
|
if (!bq)
|
|
|
|
return 0;
|
|
|
|
|
2008-06-05 02:26:59 +08:00
|
|
|
switch (bq) {
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
2008-06-02 08:23:51 +08:00
|
|
|
/* '...' or "..." string */
|
2019-06-07 07:21:01 +08:00
|
|
|
while ((c = *p++) && (c != bq))
|
2019-04-23 05:29:29 +08:00
|
|
|
EMIT(c);
|
2008-06-05 02:26:59 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
2008-06-02 08:23:51 +08:00
|
|
|
/* `...` string */
|
|
|
|
state = st_start;
|
|
|
|
|
2019-06-07 07:21:01 +08:00
|
|
|
while (state != st_done) {
|
|
|
|
c = *p++;
|
2008-06-02 08:23:51 +08:00
|
|
|
switch (state) {
|
|
|
|
case st_start:
|
2008-06-05 02:26:59 +08:00
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
2008-06-02 08:23:51 +08:00
|
|
|
state = st_backslash;
|
2008-06-05 02:26:59 +08:00
|
|
|
break;
|
|
|
|
case '`':
|
2019-06-07 07:21:01 +08:00
|
|
|
case '\0':
|
|
|
|
state = st_done;
|
|
|
|
break;
|
2008-06-05 02:26:59 +08:00
|
|
|
default:
|
2019-04-23 05:29:29 +08:00
|
|
|
EMIT(c);
|
2008-06-05 02:26:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case st_backslash:
|
|
|
|
state = st_start;
|
2008-06-03 04:59:09 +08:00
|
|
|
escp = p; /* Beginning of argument sequence */
|
2008-06-03 01:02:36 +08:00
|
|
|
nval = 0;
|
2008-06-02 08:23:51 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'a':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 7;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'b':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 8;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'e':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 27;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'f':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 12;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 10;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'r':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 13;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 't':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 9;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
state = st_ucs;
|
|
|
|
ndig = 4;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
state = st_ucs;
|
|
|
|
ndig = 8;
|
|
|
|
break;
|
|
|
|
case 'v':
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = 11;
|
2008-06-03 01:02:36 +08:00
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
state = st_hex;
|
2008-06-03 01:02:36 +08:00
|
|
|
ndig = 2;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
state = st_oct;
|
2008-06-03 01:02:36 +08:00
|
|
|
ndig = 2; /* Up to two more digits */
|
2008-06-02 08:23:51 +08:00
|
|
|
nval = c - '0';
|
|
|
|
break;
|
2019-06-07 07:21:01 +08:00
|
|
|
case '\0':
|
|
|
|
nval = '\\';
|
|
|
|
p--; /* Reprocess; terminates string */
|
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
default:
|
2019-04-23 05:29:29 +08:00
|
|
|
nval = c;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2019-04-23 05:29:29 +08:00
|
|
|
if (state == st_start)
|
|
|
|
EMIT(nval);
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case st_oct:
|
|
|
|
if (c >= '0' && c <= '7') {
|
|
|
|
nval = (nval << 3) + (c - '0');
|
2019-06-07 07:21:01 +08:00
|
|
|
if (--ndig)
|
|
|
|
break; /* Might have more digits */
|
|
|
|
} else {
|
2008-06-02 08:23:51 +08:00
|
|
|
p--; /* Process this character again */
|
2019-06-07 07:21:01 +08:00
|
|
|
}
|
|
|
|
EMIT(nval);
|
|
|
|
state = st_start;
|
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
|
|
|
|
case st_hex:
|
2019-06-07 07:21:01 +08:00
|
|
|
case st_ucs:
|
2019-04-23 05:29:29 +08:00
|
|
|
if (nasm_isxdigit(c)) {
|
2008-06-02 08:23:51 +08:00
|
|
|
nval = (nval << 4) + numvalue(c);
|
2019-06-07 07:21:01 +08:00
|
|
|
if (--ndig)
|
|
|
|
break; /* Might have more digits */
|
|
|
|
} else {
|
2008-06-02 08:23:51 +08:00
|
|
|
p--; /* Process this character again */
|
2019-06-07 07:21:01 +08:00
|
|
|
}
|
2008-06-02 08:23:51 +08:00
|
|
|
|
2019-06-07 07:21:01 +08:00
|
|
|
if (unlikely(p <= escp))
|
|
|
|
EMIT(escp[-1]);
|
|
|
|
else if (state == st_ucs)
|
|
|
|
EMIT_UTF8(nval);
|
|
|
|
else
|
|
|
|
EMIT(nval);
|
|
|
|
|
|
|
|
state = st_start;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
2019-06-07 07:21:01 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic();
|
2019-04-23 05:29:29 +08:00
|
|
|
}
|
2019-06-07 07:21:01 +08:00
|
|
|
}
|
|
|
|
break;
|
2008-06-05 02:26:59 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Not a quoted string, just return the input... */
|
2019-06-07 07:21:01 +08:00
|
|
|
while ((c = *p++))
|
2019-04-23 05:29:29 +08:00
|
|
|
EMIT(c);
|
2008-06-05 02:26:59 +08:00
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
|
|
|
|
2019-06-07 07:21:01 +08:00
|
|
|
/* Zero-terminate the output */
|
2019-04-23 05:29:29 +08:00
|
|
|
*q = '\0';
|
|
|
|
|
2019-06-07 07:21:01 +08:00
|
|
|
if (ctlmask & badctl)
|
2019-04-23 05:29:29 +08:00
|
|
|
nasm_nonfatal("control character in string not allowed here");
|
|
|
|
|
2008-06-05 02:26:59 +08:00
|
|
|
if (ep)
|
2019-06-07 07:21:01 +08:00
|
|
|
*ep = (char *)p - 1;
|
2019-04-23 05:29:29 +08:00
|
|
|
return (char *)q - str;
|
|
|
|
}
|
|
|
|
#undef EMIT
|
|
|
|
|
|
|
|
size_t nasm_unquote(char *str, char **ep)
|
|
|
|
{
|
|
|
|
return nasm_unquote_common(str, ep, 0);
|
|
|
|
}
|
|
|
|
size_t nasm_unquote_cstr(char *str, char **ep)
|
|
|
|
{
|
2019-06-07 07:21:01 +08:00
|
|
|
/*
|
|
|
|
* These are the only control characters permitted: BEL BS TAB ESC
|
|
|
|
*/
|
|
|
|
const uint32_t okctl = (1 << '\a') | (1 << '\b') | (1 << '\t') | (1 << 27);
|
|
|
|
|
|
|
|
return nasm_unquote_common(str, ep, ~okctl);
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the end of a quoted string; returns the pointer to the terminating
|
|
|
|
* character (either the ending quote or the null character, if unterminated.)
|
2019-06-07 07:29:52 +08:00
|
|
|
* If the input is not a quoted string, return NULL.
|
2008-06-02 08:23:51 +08:00
|
|
|
*/
|
2019-06-07 07:21:01 +08:00
|
|
|
char *nasm_skip_string(const char *str)
|
2008-06-02 08:23:51 +08:00
|
|
|
{
|
|
|
|
char bq;
|
2019-06-07 07:21:01 +08:00
|
|
|
const char *p;
|
2008-06-02 08:23:51 +08:00
|
|
|
char c;
|
|
|
|
enum unq_state {
|
|
|
|
st_start,
|
2019-06-07 07:21:01 +08:00
|
|
|
st_backslash,
|
|
|
|
st_done
|
2008-06-02 08:23:51 +08:00
|
|
|
} state;
|
|
|
|
|
|
|
|
bq = str[0];
|
2019-06-07 07:21:01 +08:00
|
|
|
p = str+1;
|
2019-06-07 07:29:52 +08:00
|
|
|
switch (bq) {
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
2008-06-02 08:23:51 +08:00
|
|
|
/* '...' or "..." string */
|
2019-06-07 07:21:01 +08:00
|
|
|
while ((c = *p++) && (c != bq))
|
|
|
|
;
|
2019-06-07 07:29:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
2008-06-02 08:23:51 +08:00
|
|
|
/* `...` string */
|
|
|
|
state = st_start;
|
2019-06-07 07:21:01 +08:00
|
|
|
while (state != st_done) {
|
|
|
|
c = *p++;
|
2008-06-02 08:23:51 +08:00
|
|
|
switch (state) {
|
|
|
|
case st_start:
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
state = st_backslash;
|
|
|
|
break;
|
|
|
|
case '`':
|
2019-06-07 07:21:01 +08:00
|
|
|
case '\0':
|
|
|
|
state = st_done;
|
|
|
|
break;
|
2008-06-02 08:23:51 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_backslash:
|
2008-06-03 00:57:46 +08:00
|
|
|
/*
|
|
|
|
* Note: for the purpose of finding the end of the string,
|
|
|
|
* all successor states to st_backslash are functionally
|
|
|
|
* equivalent to st_start, since either a backslash or
|
|
|
|
* a backquote will force a return to the st_start state.
|
|
|
|
*/
|
2019-06-07 07:21:01 +08:00
|
|
|
state = c ? st_start : st_done;
|
2008-06-02 08:23:51 +08:00
|
|
|
break;
|
2019-06-07 07:21:01 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic();
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 07:29:52 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Not a string at all... */
|
|
|
|
return NULL;
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|
2019-06-07 07:21:01 +08:00
|
|
|
return (char *)p - 1;
|
2008-06-02 08:23:51 +08:00
|
|
|
}
|