diff options
Diffstat (limited to 'src/u8c/u8.h.d')
-rw-r--r-- | src/u8c/u8.h.d/u8alloc.c | 28 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8dec.c | 105 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8enc.c | 97 | ||||
-rw-r--r-- | src/u8c/u8.h.d/u8free.c | 24 |
4 files changed, 254 insertions, 0 deletions
diff --git a/src/u8c/u8.h.d/u8alloc.c b/src/u8c/u8.h.d/u8alloc.c new file mode 100644 index 0000000..ba02bc8 --- /dev/null +++ b/src/u8c/u8.h.d/u8alloc.c @@ -0,0 +1,28 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <stdbool.h> +# include <stdlib.h> +# include <u8c/err.h> +# include <u8c/u8.h> +bool u8c_u8alloc(unsigned char * * const _u8,size_t const _sz) { + unsigned char * arr = NULL; + if((arr = calloc(sizeof *arr,_sz)) == NULL) { + u8c_seterr(U"u8c_u8alloc: Unable to allocate resources (not enough memory?).",u8c_errtyp_badalloc); + return false; + } + *_u8 = arr; + return false; +} diff --git a/src/u8c/u8.h.d/u8dec.c b/src/u8c/u8.h.d/u8dec.c new file mode 100644 index 0000000..365c81a --- /dev/null +++ b/src/u8c/u8.h.d/u8dec.c @@ -0,0 +1,105 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +# include <u8c/err.h> +# include <u8c/u32.h> +# include <u8c/u8.h> +# 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 unsigned char const tmp = _in[n]; + if(tmp == UINT8_C(0x0)) { /* Null-terminator: end of string has been reached. */ + insz = n; + goto nottoobig; + } + if(tmp >= UINT8_C(0b11111000)) { /* Too big. */ + u8c_seterr(U"u8c_u8dec: Character out of range (too big).",u8c_errtyp_u8oor); + return true; + } + if(tmp >= UINT8_C(0b11110000)) { /* Four byte. */ + n += SIZE_C(0x4); + continue; + } + if(tmp >= UINT8_C(0b11100000)) { /* Three bytes. */ + n += SIZE_C(0x3); + continue; + } + if(tmp >= UINT8_C(0b11000000)) { /* Two bytes. */ + n += SIZE_C(0x2); + continue; + } + /* One byte. */ + n += SIZE_C(0x1); + } + /* Input is not null-terminated. */ + u8c_seterr(U"u8c_u8dec: Unterminated input.",u8c_errtyp_untermin); + return true; +nottoobig:; + if(_sz != NULL) { + *_sz = 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(0b11110000)) { /* Four bytes. */ + uint_least32_t codep = (_in[n] ^ UINT32_C(0b11110000)) << UINT32_C(0x12); + n += SIZE_C(0x1); + codep += (_in[n] ^ UINT32_C(0b10000000)) << UINT32_C(0xC); + n += SIZE_C(0x1); + codep += (_in[n] ^ UINT32_C(0b10000000)) << UINT32_C(0x6); + n += SIZE_C(0x1); + codep += (uint_least32_t)(_in[n]) ^ SIZE_C(0b10000000); + n += SIZE_C(0x1); + out[outn] = codep; + continue; + } + if(_in[n] >= UINT8_C(0b11100000)) { /* Three bytes. */ + uint_least32_t codep = (_in[n] ^ UINT32_C(0b11100000)) << UINT32_C(0xC); + n += SIZE_C(0x1); + codep += (_in[n] ^ UINT32_C(0b10000000)) << UINT32_C(0x6); + n += SIZE_C(0x1); + codep += _in[n] ^ UINT32_C(0b10000000); + n += SIZE_C(0x1); + out[outn] = codep; + continue; + } + if(_in[n] >= UINT8_C(0b11000000)) { /* Two bytes. */ + uint_least32_t codep = (_in[n] ^ UINT32_C(0b11000000)) << UINT32_C(0x6); + n += SIZE_C(0x1); + codep += _in[n] ^ UINT32_C(0b10000000); + n += SIZE_C(0x1); + out[outn] = codep; + continue; + } + /* One byte. */ + out[outn] = (uint_least32_t)(_in[n]); + n += SIZE_C(0x1); + continue; + } + u8c_u32free(_out); + *_out = out; + return false; +} diff --git a/src/u8c/u8.h.d/u8enc.c b/src/u8c/u8.h.d/u8enc.c new file mode 100644 index 0000000..f3f3570 --- /dev/null +++ b/src/u8c/u8.h.d/u8enc.c @@ -0,0 +1,97 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <assert.h> +# include <stdbool.h> +# include <stddef.h> +# include <stdint.h> +# include <u8c/SIZE_C.h> +# include <u8c/err.h> +# include <u8c/u32.h> +# include <u8c/u8.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 char32_t const tmp = _in[n]; + if(tmp > u8c_u32max) { /* Codepoint out of range. */ + u8c_seterr(U"u8c_u8enc: Codepoint out of range (too big).",u8c_errtyp_u32oor); + return true; + } + if(tmp >= UINT32_C(0x10000)) { /* 4 bytes. */ + outsz += SIZE_C(0x4); + continue; + } + if(tmp >= UINT32_C(0x800)) { /* 3 bytes. */ + outsz += SIZE_C(0x3); + continue; + } + if(tmp >= UINT32_C(0x80)) { /* 2 bytes. */ + outsz += SIZE_C(0x2); + continue; + } + /* 1 byte. */ + outsz += SIZE_C(0x1); + if(tmp == UINT32_C(0x0)) { + insz = n + SIZE_C(0x1); + goto nottoobig; + } + } + u8c_seterr(U"u8c_u8enc: Unterminated input.",u8c_errtyp_untermin); + return true; +nottoobig:; + if(_sz != NULL) { + *_sz = outsz; + } + unsigned char * out = NULL; + if(u8c_u8alloc(&out,outsz + SIZE_C(0x1))) { + 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 char32_t const tmp = _in[n]; + if(tmp >= UINT32_C(0x10000)) { // Four bytes. + out[outn] = UINT8_C(0b11110000) + (uint_least8_t)(tmp >> UINT32_C(0x12)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0b10000000) + (uint_least8_t)(tmp >> UINT32_C(0xC) & UINT8_C(0b00111111)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0b10000000) + (uint_least8_t)(tmp >> UINT32_C(0x6) & UINT8_C(0b00111111)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0b10000000) + (uint_least8_t)(tmp & UINT32_C(0b00111111)); + continue; + } + if(tmp >= UINT32_C(0x800)) { /* Three bytes. */ + out[outn] = UINT8_C(0xE0) + (uint_least8_t)(tmp >> UINT32_C(0xC)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0x80) + (uint_least8_t)(tmp >> UINT32_C(0x6) & UINT8_C(0b00111111)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0x80) + (uint_least8_t)(tmp & UINT32_C(0b00111111)); + continue; + } + if(tmp >= UINT32_C(0x80)) { /* Two bytes. */ + out[outn] = UINT8_C(0xC0) + (uint_least8_t)(tmp >> UINT8_C(0x6)); + outn += SIZE_C(0x1); + out[outn] = UINT8_C(0x80) + (uint_least8_t)(tmp & UINT8_C(0b00111111)); + continue; + } + /* One byte. */ + out[outn] = (uint_least8_t)tmp; + } + u8c_u8free(_out); + *_out = out; + return false; +} diff --git a/src/u8c/u8.h.d/u8free.c b/src/u8c/u8.h.d/u8free.c new file mode 100644 index 0000000..af5a6bd --- /dev/null +++ b/src/u8c/u8.h.d/u8free.c @@ -0,0 +1,24 @@ +/* + Copyright 2021 Gabriel Jensen + + This file is part of u8c. + + u8c is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + + u8c is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with u8c. + + If not, see <https://www.gnu.org/licenses/>. +*/ +# include <stdbool.h> +# include <stdint.h> +# include <stdlib.h> +# include <u8c/u8.h> +bool u8c_u8free(unsigned char const * * const _u8) { + free((unsigned char *)*_u8); + *_u8 = NULL; + return false; +} |