summaryrefslogtreecommitdiff
path: root/src/u8c/u8dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/u8c/u8dec.c')
-rw-r--r--src/u8c/u8dec.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/u8c/u8dec.c b/src/u8c/u8dec.c
index dfbfeaf..bbc6e94 100644
--- a/src/u8c/u8dec.c
+++ b/src/u8c/u8dec.c
@@ -14,24 +14,29 @@
If not, see <https://www.gnu.org/licenses/>.
*/
# include <assert.h>
+# include <stdbool.h>
+# include <stddef.h>
# include <stdint.h>
-# include <stdlib.h>
+# include <u8c/SIZE_C.h>
# include <u8c/seterr.h>
+# include <u8c/u32alloc.h>
+# include <u8c/u32free.h>
# include <u8c/u8dec.h>
-# include <u8c/SIZE_C.h>
-uint_least8_t u8c_u8dec(size_t * const _sz,uint_least32_t const * * const _out,uint_least8_t const * const _in) {
+# include <uchar.h>
+bool u8c_u8dec(size_t * const _sz,char32_t const * * const _out,unsigned char const * const _in) {
+ assert(_out != NULL);
assert(_in != NULL);
register size_t insz = SIZE_C(0x0);
register size_t outsz = SIZE_C(0x1);
for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;outsz += SIZE_C(0x1)) { /* First pass: get size of input array and determine size of output array. */
- register uint_least8_t const tmp = _in[n];
+ register unsigned char const tmp = _in[n];
if(tmp == UINT8_C(0x0)) { /* Null-terminator: end of string has been reached. */
- insz = n + SIZE_C(0x1);
+ insz = n;
goto nottoobig;
}
if(tmp >= UINT8_C(0xF8)) { /* Too big. */
- u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x43),UINT32_C(0x68),UINT32_C(0x61),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x63),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x20),UINT32_C(0x6F),UINT32_C(0x66),UINT32_C(0x20),UINT32_C(0x72),UINT32_C(0x61),UINT32_C(0x6E),UINT32_C(0x67),UINT32_C(0x65),UINT32_C(0x20),UINT32_C(0x28),UINT32_C(0x74),UINT32_C(0x6F),UINT32_C(0x6F),UINT32_C(0x20),UINT32_C(0x62),UINT32_C(0x69),UINT32_C(0x67),UINT32_C(0x29),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8dec: Character out of range (too big). */
- return UINT8_C(0x1);
+ u8c_seterr(U"u8c_u8dec: Character out of range (too big).");
+ return true;
}
if(tmp >= UINT8_C(0xF0)) { /* Four byte. */
n += SIZE_C(0x4);
@@ -49,13 +54,16 @@ uint_least8_t u8c_u8dec(size_t * const _sz,uint_least32_t const * * const _out,u
n += SIZE_C(0x1);
}
/* Input is not null-terminated. */
- u8c_seterr((uint_least32_t[]){UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x63),UINT32_C(0x5F),UINT32_C(0x75),UINT32_C(0x38),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x55),UINT32_C(0x6E),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x72),UINT32_C(0x6D),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x61),UINT32_C(0x74),UINT32_C(0x65),UINT32_C(0x64),UINT32_C(0x20),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x70),UINT32_C(0x75),UINT32_C(0x74),UINT32_C(0x2E),UINT32_C(0x0),}); /* u8c_u8dec: Unterminated input. */
- return UINT8_C(0x1);
+ u8c_seterr(U"u8c_u8dec: Unterminated input.");
+ return true;
nottoobig:;
if(_sz != NULL) {
*_sz = outsz;
}
- uint_least32_t * out = calloc(sizeof(uint_least32_t),outsz);
+ uint_least32_t * out = NULL;
+ if(u8c_u32alloc(&out,outsz + SIZE_C(0x1))) {
+ return false;
+ }
for(register size_t n = SIZE_C(0x0),outn = SIZE_C(0x0);n < insz;outn += SIZE_C(0x1)) { /* Second pass: decode UTF-8. */
if(_in[n] >= UINT8_C(0xF0)) { /* Four byte. */
uint_least32_t codep = (_in[n] ^ UINT32_C(0xF0)) << UINT32_C(0x12);
@@ -92,6 +100,7 @@ nottoobig:;
n += SIZE_C(0x1);
continue;
}
+ u8c_u32free(_out);
*_out = out;
- return UINT8_C(0x0);
+ return false;
}