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 * ----------------------------------------------------------------------- */
37 * String transformation functions
44 * Convert a string in UTF-8 format to UTF-16LE
46 static size_t utf8_to_16le(uint8_t *str
, size_t len
, char *op
)
48 #define EMIT(x) do { if (op) { WRITESHORT(op,x); } outlen++; } while(0)
53 uint32_t v
= 0, vmin
= 0;
59 if ((c
& 0xc0) != 0x80) {
63 v
= (v
<< 6) | (c
& 0x3f);
65 if (v
< vmin
|| v
> 0x10ffff ||
66 (v
>= 0xd800 && v
<= 0xdfff)) {
68 } else if (v
> 0xffff) {
70 EMIT(0xd800 | (v
>> 10));
71 EMIT(0xdc00 | (v
& 0x3ff));
82 } else if (c
< 0xc0 || c
>= 0xfe) {
85 } else if (c
< 0xe0) {
89 } else if (c
< 0xf0) {
93 } else if (c
< 0xf8) {
97 } else if (c
< 0xfc) {
108 return expect
? (size_t)-1 : outlen
<< 1;
114 * Convert a string in UTF-8 format to UTF-32LE
116 static size_t utf8_to_32le(uint8_t *str
, size_t len
, char *op
)
118 #define EMIT(x) do { if (op) { WRITELONG(op,x); } outlen++; } while(0)
123 uint32_t v
= 0, vmin
= 0;
129 if ((c
& 0xc0) != 0x80) {
132 v
= (v
<< 6) | (c
& 0x3f);
134 if (v
< vmin
|| (v
>= 0xd800 && v
<= 0xdfff)) {
146 } else if (c
< 0xc0 || c
>= 0xfe) {
149 } else if (c
< 0xe0) {
153 } else if (c
< 0xf0) {
157 } else if (c
< 0xf8) {
161 } else if (c
< 0xfc) {
172 return expect
? (size_t)-1 : outlen
<< 2;
177 typedef size_t (*transform_func
)(uint8_t *, size_t, char *);
180 * Apply a specific string transform and return it in a nasm_malloc'd
181 * buffer, returning the length. On error, returns (size_t)-1 and no
182 * buffer is allocated.
184 size_t string_transform(char *str
, size_t len
, char **out
, enum strfunc func
)
186 /* This should match enum strfunc in nasm.h */
187 static const transform_func str_transforms
[] = {
191 transform_func transform
= str_transforms
[func
];
193 uint8_t *s
= (uint8_t *)str
;
196 outlen
= transform(s
, len
, NULL
);
197 if (outlen
== (size_t)-1)
200 *out
= buf
= nasm_malloc(outlen
+1);
201 buf
[outlen
] = '\0'; /* Forcibly null-terminate the buffer */
202 return transform(s
, len
, buf
);