From e0c6872cf40896c7be36b11dcc744620f10adf1d Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 2 Sep 2019 13:46:59 +0900 Subject: Initial commit --- support/dktools/dk4userf.ctr | 480 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 480 insertions(+) create mode 100644 support/dktools/dk4userf.ctr (limited to 'support/dktools/dk4userf.ctr') diff --git a/support/dktools/dk4userf.ctr b/support/dktools/dk4userf.ctr new file mode 100644 index 0000000000..0166022078 --- /dev/null +++ b/support/dktools/dk4userf.ctr @@ -0,0 +1,480 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2016-xxxx +license = bsd + + + +%% header + +/** @file dk4userf.h Unserialize (read) data from binary file. +*/ + +#ifndef DK4CONF_H_INCLUDED +#include "dk4conf.h" +#endif + +#ifndef DK4TYPES_H_INCLUDED +#include "dk4types.h" +#endif + +#ifndef DK4ERROR_H_INCLUDED +#include "dk4error.h" +#endif + +#if DK4_HAVE_STDLIB_H +#ifndef DK4_STDLIB_H_INCLUDED +#include +#define DK4_STDLIB_H_INCLUDED 1 +#endif +#endif + +#if DK4_HAVE_SYS_TYPES_H +#ifndef DK4_SYS_TYPES_H_INCLUDED +#include +#define DK4_SYS_TYPES_H_INCLUDED 1 +#endif +#endif + +#if DK4_HAVE_STDINT_H +#ifndef DK4_STDINT_H_INCLUDED +#include +#define DK4_STDINT_H_INCLUDED 1 +#endif +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** Unserialize (read) a 16 bit unsigned number from binary file. + @param dptr Address of destination variable. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_uint16_t(uint16_t *dptr, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) a 16 bit signed number from binary file. + @param dptr Address of destination variable. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_int16_t(int16_t *dptr, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) a 32 bit unsigned number from binary file. + @param dptr Address of destination variable. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_uint32_t(uint32_t *dptr, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) a 32 bit signed number from binary file. + @param dptr Address of destination variable. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_int32_t(int32_t *dptr, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) an UTF-8 encoded text string from binary file. + On success the function dynamically allocates memory for the string + and sets the pointer to this memory. Use dk4mem_free() to release + the memory after use. + @param dptr Address of destination pointer variable, initialize + the pointer to NULL before calling this function. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_utf8_string(char **dptr, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) a text string in the system default encoding + from binary file. + On success the function dynamically allocates memory for the string + and sets the pointer to this memory. Use dk4mem_free() to release + the memory after use. + @param dptr Address of destination pointer variable, initialize + the pointer to NULL before calling this function. + @param de Encoding for dkChar. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_dk_string(dkChar **dptr, int de, FILE *fipo, dk4_er_t *erp); + + +/** Unserialize (read) a double number from binary file. + @param dptr Address of destination variable. + @param fipo Input file (opened for binary read access). + @param erp Error report, may be NULL. + @return 1 on success, 0 on error. +*/ + +int +dk4unserialize_file_double(double *dptr, FILE *fipo, dk4_er_t *erp); + + +#ifdef __cplusplus +} +#endif + + +%% module + +#include "dk4userf.h" + +#if DK4_HAVE_ERRNO_H +#ifndef ERRNO_H_INCLUDED +#include +#define ERRNO_H_INCLUDED 1 +#endif +#endif + +#if DK4_HAVE_MATH_H +#ifndef MATH_H_INCLUDED +#ifdef _WIN32 +#define _USE_MATH_DEFINES 1 +#endif +#include +#define MATH_H_INCLUDED 1 +#endif +#endif + +#if DK4_HAVE_FLOAT_H +#ifndef FLOAT_H_INCLUDED +#include +#define FLOAT_H_INCLUDED 1 +#endif +#endif + +#include "dk4enc.h" +#include "dk4mem.h" +#include "dk4rec04.h" +#include "dk4rec06.h" +#include "dk4rec11.h" +#include "dk4rec17.h" + + +$!trace-include + + + +int +dk4unserialize_file_uint16_t(uint16_t *dptr, FILE *fipo, dk4_er_t *erp) +{ + unsigned char uc[2]; + uint16_t ui16; + int back = 0; + + if ((NULL != dptr) && (NULL != fipo)) { + if (2 == fread(uc, 1, 2, fipo)) { + ui16 = (((uint16_t)(uc[0]) << 8) & 0xFF00U) + | (((uint16_t)(uc[1])) & 0x00FFU); + *dptr = ui16; + back = 1; + } else { + dk4error_set_simple_error_code(erp, DK4_E_READ_FAILED); + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + + +int +dk4unserialize_file_int16_t(int16_t *dptr, FILE *fipo, dk4_er_t *erp) +{ + uint16_t ui; + int back = 0; + + if ((NULL != dptr) && (NULL != fipo)) { + back = dk4unserialize_file_uint16_t(&ui, fipo, erp); + if (0 != back) { + *dptr = (int16_t)ui; + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + + +int +dk4unserialize_file_uint32_t(uint32_t *dptr, FILE *fipo, dk4_er_t *erp) +{ + unsigned char uc[4]; + uint32_t ui32; + int back = 0; + $? "+ dk4unserialize_file_uint32_t" + if ((NULL != dptr) && (NULL != fipo)) { $? ". args ok" + if (4 == fread(uc, 1, 4, fipo)) { $? ". fread" + ui32 = ((((uint32_t)(uc[0])) << 24) & 0xFF000000UL) + | ((((uint32_t)(uc[1])) << 16) & 0x00FF0000UL) + | ((((uint32_t)(uc[2])) << 8) & 0x0000FF00UL) + | (( (uint32_t)(uc[3])) & 0x000000FFUL); + *dptr = ui32; + back = 1; + } else { $? "! fread" + dk4error_set_simple_error_code(erp, DK4_E_READ_FAILED); + } + } else { $? "! args" + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + $? "- dk4unserialize_file_uint32_t %d", back + return back; +} + + + +int +dk4unserialize_file_int32_t(int32_t *dptr, FILE *fipo, dk4_er_t *erp) +{ + uint32_t ui32; + int back = 0; + if ((NULL != dptr) && (NULL != fipo)) { + back = dk4unserialize_file_uint32_t(&ui32, fipo, erp); + if (0 != back) { + *dptr = (int32_t)ui32; + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + + +int +dk4unserialize_file_utf8_string(char **dptr, FILE *fipo, dk4_er_t *erp) +{ + char *np = NULL; + size_t ls = 0; + int back = 0; + uint16_t lgt = 0; + if ((NULL != dptr) && (NULL != fipo)) { + if (0 != dk4unserialize_file_uint16_t(&lgt, fipo, erp)) { + if (0 < lgt) { + ls = (size_t)lgt; + np = dk4mem_new(char,ls,erp); + if (NULL != np) { + if (fread(np, 1, ls, fipo) == ls) { + if ('\0' == np[ls - 1]) { + back = 1; + *dptr = np; + } else { + dk4mem_free(np); + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + } + } else { + dk4mem_free(np); + dk4error_set_simple_error_code(erp, DK4_E_READ_FAILED); + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + + +int +dk4unserialize_file_double(double *dptr, FILE *fipo, dk4_er_t *erp) +{ + char *bptr = NULL; + char *tg = NULL; + double dv = 0.0; + int back = 0; + if ((NULL != dptr) && (NULL != fipo)) { + if (0 != dk4unserialize_file_utf8_string(&bptr, fipo, erp)) { + if (NULL != bptr) { + errno = 0; + dv = strtod(bptr, &tg); + if ((HUGE_VAL == dv) && (ERANGE == errno)) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } else { + if ((-HUGE_VAL == dv) && (ERANGE == errno)) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } else { + if ((0.0 == dv) && (ERANGE == errno)) { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } else { + back = 1; + if (NULL != tg) { + while ('\0' != *tg) { + if ( + (' ' != *tg) && ('\t' != *tg) + && ('\r' != *tg) && ('\n' != *tg) + ) + { + back = 0; + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + } + tg++; + } + } + } + } + } + dk4mem_free(bptr); + *dptr = dv; + } else { + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + +int +dk4unserialize_file_dk_string(dkChar **dptr, int de, FILE *fipo, dk4_er_t *erp) +{ + dkChar *resb = NULL; + char *putf8 = NULL; + size_t szdk = 0; + int back = 0; + if ((NULL != dptr) && (NULL != fipo)) { + if (0 != dk4unserialize_file_utf8_string(&putf8, fipo, erp)) { + if (NULL != putf8) { + switch (de) { +#if DK4_CHAR_SIZE > 1 +#if DK4_CHAR_SIZE > 2 + case DK4_ENCODING_32 : { + szdk = dk4recode_size_utf8_to_c32(putf8, erp); + if ((0 < szdk) && ((SIZE_MAX / sizeof(dk4_c32_t)) >= szdk)) { + resb = dk4mem_new(dk4_c32_t,szdk,erp); + if (NULL != resb) { + back = dk4recode_utf8_to_c32(resb, szdk, putf8, erp); + if (0 != back) { + if (dkT('\0') == resb[szdk - 1]) { + *dptr = resb; + } else { + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + dk4mem_free(resb); + } + } else { + dk4mem_free(resb); + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } + } break; +#else + case DK4_ENCODING_UTF16 : { + szdk = dk4recode_size_utf8_to_utf16(putf8, erp); + if ((0 < szdk) && ((SIZE_MAX / sizeof(dk4_c16_t)) >= szdk)) { + resb = dk4mem_new(dk4_c16_t,szdk,erp); + if (NULL != resb) { + back = dk4recode_utf8_to_utf16(resb, szdk, putf8, erp); + if (0 != back) { + if (dkT('\0') == resb[szdk - 1]) { + *dptr = resb; + } else { + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + dk4mem_free(resb); + } + } else { + dk4mem_free(resb); + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_MATH_OVERFLOW); + } + } break; +#endif +#else + case DK4_ENCODING_ASCII : { + szdk = dk4recode_size_utf8_to_ascii(putf8, erp); + if (0 < szdk) { + resb = dk4mem_new(char,szdk,erp); + if (NULL != resb) { + back = dk4recode_utf8_to_ascii(resb, szdk, putf8, erp); + if (0 != back) { + if ('\0' == resb[szdk - 1]) { + *dptr = resb; + } else { + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + dk4mem_free(resb); + } + } else { + dk4mem_free(resb); + } + } + } + } break; + case DK4_ENCODING_ANSI : { + szdk = dk4recode_size_utf8_to_ansi(putf8, erp); + if (0 < szdk) { + resb = dk4mem_new(char,szdk,erp); + if (NULL != resb) { + back = dk4recode_utf8_to_ansi(resb, szdk, putf8, erp); + if (0 != back) { + if ('\0' == resb[szdk - 1]) { + *dptr = resb; + } else { + dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); + dk4mem_free(resb); + } + } else { + dk4mem_free(resb); + } + } + } + } break; + case DK4_ENCODING_UTF8 : { + *dptr = putf8; + putf8 = NULL; + back = 1; + } break; +#endif + default : { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } break; + } + } + if (NULL != putf8) { + dk4mem_free(putf8); + } + } + } else { + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + return back; +} + + + -- cgit v1.2.3