4 * String transformation functions
11 * Convert a string in UTF-8 format to UTF-16LE
13 static size_t utf8_to_16le(uint8_t *str
, size_t len
, char *op
)
15 #define EMIT(x) do { if (op) { WRITESHORT(op,x); } outlen++; } while(0)
20 uint32_t v
= 0, vmin
= 0;
26 if ((c
& 0xc0) != 0x80) {
30 v
= (v
<< 6) | (c
& 0x3f);
32 if (v
< vmin
|| v
> 0x10ffff ||
33 (v
>= 0xd800 && v
<= 0xdfff)) {
35 } else if (v
> 0xffff) {
37 EMIT(0xd800 | (v
>> 10));
38 EMIT(0xdc00 | (v
& 0x3ff));
49 } else if (c
< 0xc0 || c
>= 0xfe) {
52 } else if (c
< 0xe0) {
56 } else if (c
< 0xf0) {
60 } else if (c
< 0xf8) {
64 } else if (c
< 0xfc) {
75 return expect
? (size_t)-1 : outlen
<< 1;
81 * Convert a string in UTF-8 format to UTF-32LE
83 static size_t utf8_to_32le(uint8_t *str
, size_t len
, char *op
)
85 #define EMIT(x) do { if (op) { WRITELONG(op,x); } outlen++; } while(0)
90 uint32_t v
= 0, vmin
= 0;
96 if ((c
& 0xc0) != 0x80) {
99 v
= (v
<< 6) | (c
& 0x3f);
101 if (v
< vmin
|| (v
>= 0xd800 && v
<= 0xdfff)) {
113 } else if (c
< 0xc0 || c
>= 0xfe) {
116 } else if (c
< 0xe0) {
120 } else if (c
< 0xf0) {
124 } else if (c
< 0xf8) {
128 } else if (c
< 0xfc) {
139 return expect
? (size_t)-1 : outlen
<< 2;
144 typedef size_t (*transform_func
)(uint8_t *, size_t, char *);
147 * Apply a specific string transform and return it in a nasm_malloc'd
148 * buffer, returning the length. On error, returns (size_t)-1 and no
149 * buffer is allocated.
151 size_t string_transform(char *str
, size_t len
, char **out
, enum strfunc func
)
153 /* This should match enum strfunc in nasm.h */
154 static const transform_func str_transforms
[] = {
158 transform_func transform
= str_transforms
[func
];
160 uint8_t *s
= (uint8_t *)str
;
163 outlen
= transform(s
, len
, NULL
);
164 if (outlen
== (size_t)-1)
167 *out
= buf
= nasm_malloc(outlen
+1);
168 buf
[outlen
] = '\0'; /* Forcibly null-terminate the buffer */
169 return transform(s
, len
, buf
);