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.c | 388 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 support/dktools/dk4userf.c (limited to 'support/dktools/dk4userf.c') diff --git a/support/dktools/dk4userf.c b/support/dktools/dk4userf.c new file mode 100644 index 0000000000..29b7a6b07a --- /dev/null +++ b/support/dktools/dk4userf.c @@ -0,0 +1,388 @@ +/* + WARNING: This file was generated by dkct. + Changes you make here will be lost if dkct is run again! + You should modify the original source and run dkct on it. + Original source: dk4userf.ctr +*/ + +/* +Copyright (C) 2016-2017, Dirk Krause + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above opyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +* Neither the name of the author nor the names of contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/** @file dk4userf.c The dk4userf module. +*/ + + +#line 147 "dk4userf.ctr" + +#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" + + + + +#line 183 "dk4userf.ctr" + + + +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; + + +#line 236 "dk4userf.ctr" + if ((NULL != dptr) && (NULL != fipo)) { + +#line 237 "dk4userf.ctr" + if (4 == fread(uc, 1, 4, fipo)) { + +#line 238 "dk4userf.ctr" + 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 { + +#line 245 "dk4userf.ctr" + dk4error_set_simple_error_code(erp, DK4_E_READ_FAILED); + } + } else { + +#line 248 "dk4userf.ctr" + dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); + } + + +#line 251 "dk4userf.ctr" + 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