summaryrefslogtreecommitdiff
path: root/src/u8c/u8enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/u8c/u8enc.c')
-rw-r--r--src/u8c/u8enc.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/src/u8c/u8enc.c b/src/u8c/u8enc.c
index dda62d3..60bc724 100644
--- a/src/u8c/u8enc.c
+++ b/src/u8c/u8enc.c
@@ -14,20 +14,26 @@
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/u8alloc.h>
# include <u8c/u8enc.h>
-# include <u8c/SIZE_C.h>
-uint_least8_t u8c_u8enc(size_t * const _sz,uint_least8_t const * * const _out,uint_least32_t const * const _in) {
+# include <u8c/u8free.h>
+# include <u8c/unimax.h>
+# include <uchar.h>
+bool u8c_u8enc(size_t * const _sz,unsigned char const * * const _out,char32_t const * const _in) {
+ assert(_out != NULL);
assert(_in != NULL);
size_t insz = SIZE_C(0x0); /* Size of input array (bytes). */
size_t outsz = SIZE_C(0x0); /* Size of output array /bytes). */
for(register size_t n = SIZE_C(0x0);n <= SIZE_MAX;n += SIZE_C(0x1)) { /* First pass: get size of input array, and determine size of output array. */
- register uint_least32_t const tmp = _in[n];
- if(tmp >= UINT32_C(0x110000)) { /* Codepoint out of range. */
- 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(0x65),UINT32_C(0x6E),UINT32_C(0x63),UINT32_C(0x3A),UINT32_C(0x20),UINT32_C(0x43),UINT32_C(0x6F),UINT32_C(0x64),UINT32_C(0x65),UINT32_C(0x70),UINT32_C(0x6F),UINT32_C(0x69),UINT32_C(0x6E),UINT32_C(0x74),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_u8enc: Codepoint out of range (too big). */
- return UINT8_C(0x1);
+ register char32_t const tmp = _in[n];
+ if(tmp > u8c_unimax) { /* Codepoint out of range. */
+ u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big).");
+ return true;
}
if(tmp >= UINT32_C(0x10000)) { /* 4 bytes. */
outsz += SIZE_C(0x4);
@@ -48,15 +54,19 @@ uint_least8_t u8c_u8enc(size_t * const _sz,uint_least8_t const * * const _out,ui
goto nottoobig;
}
}
- 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(0x65),UINT32_C(0x6E),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_u8enc: Unterminated input. */
- return UINT8_C(0x1);
+ u8c_seterr(U"u8c_u8enc: Unterminated input.");
+ return true;
nottoobig:;
if(_sz != NULL) {
*_sz = outsz;
}
- uint_least8_t * out = calloc(sizeof(uint_least8_t),outsz); /* Allocate space for output array. */
+ unsigned char * out = NULL;
+ if(u8c_u8alloc(&out,outsz + SIZE_C(0x1))) {
+ u8c_seterr(U"u8c_u32enc: Unable to allocate resources (not enough memory?).");
+ return true;
+ }
for(register size_t n = SIZE_C(0x0), outn = SIZE_C(0x0);n < insz;n += SIZE_C(0x1),outn += SIZE_C(0x1)) { /* Second pass: encode each codepoint into UTF-8. */
- register uint_least32_t const tmp = _in[n];
+ register char32_t const tmp = _in[n];
if(tmp >= UINT32_C(0x10000)) { // Four bytes.
out[outn] = UINT8_C(0xF0) + (uint_least8_t)(tmp >> UINT32_C(0x12));
outn += SIZE_C(0x1);
@@ -84,6 +94,7 @@ nottoobig:;
/* One byte. */
out[outn] = (uint_least8_t)tmp;
}
+ u8c_u8free(_out);
*_out = out;
- return UINT8_C(0x0);
+ return false;
}