diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk3maih8.ctr |
Initial commit
Diffstat (limited to 'support/dktools/dk3maih8.ctr')
-rw-r--r-- | support/dktools/dk3maih8.ctr | 819 |
1 files changed, 819 insertions, 0 deletions
diff --git a/support/dktools/dk3maih8.ctr b/support/dktools/dk3maih8.ctr new file mode 100644 index 0000000000..29ecec837a --- /dev/null +++ b/support/dktools/dk3maih8.ctr @@ -0,0 +1,819 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2014-xxxx +license = bsd + + + +%% header + +/** @file dk3maih8.h Retrieve numbers from hexadecimal char strings. +*/ + +#include <dk3conf.h> + +#include <stdio.h> +#if DK3_HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#if DK3_HAVE_STDINT +#include <stdint.h> +#endif +#if DK3_HAVE_INTTYPES_H +#include <inttypes.h> +#endif +#if DK3_HAVE_LIMITS_H +#include <limits.h> +#endif +#if DK3_HAVE_MATH_H +#include <math.h> +#endif +#if DK3_HAVE_FLOAT_H +#include <float.h> +#endif + +#include <dk3types.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** Convert string to dk3_um_t. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_um_from_c8_hex_string(dk3_um_t *rp, char const *src, int *ec); + +/** Convert string to dk3_im_t. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_im_from_c8_hex_string(dk3_im_t *rp, char const *src, int *ec); + +/** Convert string to short. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_s_from_c8_hex_string(short *rp, char const *src, int *ec); + +/** Convert string to unsigned short. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_us_from_c8_hex_string(unsigned short *rp, char const *src, int *ec); + +/** Convert string to int. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_i_from_c8_hex_string(int *rp, char const *src, int *ec); + +/** Convert string to unsigned. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_ui_from_c8_hex_string(unsigned *rp, char const *src, int *ec); + +/** Convert string to long. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_l_from_c8_hex_string(long *rp, char const *src, int *ec); + +/** Convert string to unsigned long. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_ul_from_c8_hex_string(unsigned long *rp, char const *src, int *ec); + +#if DK3_HAVE_LONG_LONG + +/** Convert string to long long. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_ll_from_c8_hex_string(long long *rp, char const *src, int *ec); + +/** Convert string to unsigned long long. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_ull_from_c8_hex_string(unsigned long long *rp, char const *src, int *ec); + +#endif + +#if DK3_HAVE_INTMAX_T + +/** Convert string to intmax_t. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_intmax_t_from_c8_hex_string(intmax_t *rp, char const *src, int *ec); + +/** Convert string to uintmax_t. + @param rp Pointer to result variable. + @param src Source text to convert. + @param ec Pointer to error code variable, may be NULL. + Set to DK3_ERROR_INVALID_ARGS if rp and/or src is NULL, + DK3_ERROR_SYNTAX if unacceptable characters found, + DK3_ERROR_MATH_OVERFLOW on numeric overflow. + @return 1 on success, 0 on error (see ec). + If trailing garbage is found after the number, the function + returns 1 and sets the error code variable to DK3_ERROR_SYNTAX. +*/ +int +dk3ma_uintmax_t_from_c8_hex_string(uintmax_t *rp, char const *src, int *ec); + +#endif + +#ifdef __cplusplus +} +#endif + +%% module + +#include "dk3ma.h" + + + +$!trace-include + + + +/** Constant numeric values. +*/ +static dk3_um_t const dk3maih8_um_array[] = { +#if DK3_HAVE_LONG_LONG + /* 0 */ (dk3_um_t)10ULL, + /* 1 */ (dk3_um_t)1ULL, + /* 2 */ (dk3_um_t)2ULL, + /* 3 */ (dk3_um_t)3ULL, + /* 4 */ (dk3_um_t)4ULL, + /* 5 */ (dk3_um_t)5ULL, + /* 6 */ (dk3_um_t)6ULL, + /* 7 */ (dk3_um_t)7ULL, + /* 8 */ (dk3_um_t)8ULL, + /* 9 */ (dk3_um_t)9ULL, + /* 10 */ (dk3_um_t)10ULL, + /* 11 */ (dk3_um_t)11ULL, + /* 12 */ (dk3_um_t)12ULL, + /* 13 */ (dk3_um_t)13ULL, + /* 14 */ (dk3_um_t)14ULL, + /* 15 */ (dk3_um_t)15ULL, + /* 16 */ (dk3_um_t)16ULL +#else + /* 0 */ (dk3_um_t)10UL, + /* 1 */ (dk3_um_t)1UL, + /* 2 */ (dk3_um_t)2UL, + /* 3 */ (dk3_um_t)3UL, + /* 4 */ (dk3_um_t)4UL, + /* 5 */ (dk3_um_t)5UL, + /* 6 */ (dk3_um_t)6UL, + /* 7 */ (dk3_um_t)7UL, + /* 8 */ (dk3_um_t)8UL, + /* 9 */ (dk3_um_t)9UL, + /* 10 */ (dk3_um_t)10UL, + /* 11 */ (dk3_um_t)11UL, + /* 12 */ (dk3_um_t)12UL, + /* 13 */ (dk3_um_t)13UL, + /* 14 */ (dk3_um_t)14UL, + /* 15 */ (dk3_um_t)15UL, + /* 16 */ (dk3_um_t)16UL +#endif +}; + + + +/** Find start of text in string. + @param str String to search for text. + @return Pointer to start of text on success, NULL on error. +*/ +static +char const * +dk3maih8_str_c8_start(char const *str) +{ + char const *back = NULL; + if (NULL != str) { + back = str; + while((' ' == *back) || ('\t' == *back)) { back++; } + switch(*back) { + case '\0': case '\n': case '\r': { + back = NULL; + } break; + } + } + return back; +} + + + +int +dk3ma_um_from_c8_hex_string(dk3_um_t *rp, char const *src, int *ec) +{ + char const *ptr = NULL; /* Current char to process */ + dk3_um_t val = DK3_UM_0; /* Value found */ + dk3_um_t op = DK3_UM_0; /* Operand to add */ + int mec = 0; /* Math error code */ + int back = 0; /* Function result */ + int found = 0; /* Flag: Digits found */ + int cc = 1; /* Flag: Can continue */ + int action = 0; /* Action to take */ + $? "+ dk3ma_um_from_c8_hex_string \"%!8s\"", TR_8STR(src) + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('0' == *ptr) { if(('x' == ptr[1])||('X' == ptr[1])) {ptr++; ptr++;} } + while (0 != cc) { + action = 0; + switch(*(ptr++)) { + case '\0': { /* End of string */ + cc = 0; + if (0 != found) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } break; + case '0': { + found = 1; action = 1; op = DK3_UM_0; + } break; + case '1': { + found = 1; action = 1; op = dk3maih8_um_array[1]; + } break; + case '2': { + found = 1; action = 1; op = dk3maih8_um_array[2]; + } break; + case '3': { + found = 1; action = 1; op = dk3maih8_um_array[3]; + } break; + case '4': { + found = 1; action = 1; op = dk3maih8_um_array[4]; + } break; + case '5': { + found = 1; action = 1; op = dk3maih8_um_array[5]; + } break; + case '6': { + found = 1; action = 1; op = dk3maih8_um_array[6]; + } break; + case '7': { + found = 1; action = 1; op = dk3maih8_um_array[7]; + } break; + case '8': { + found = 1; action = 1; op = dk3maih8_um_array[8]; + } break; + case '9': { + found = 1; action = 1; op = dk3maih8_um_array[9]; + } break; + case 'a': case 'A': { + found = 1; action = 1; op = dk3maih8_um_array[10]; + } break; + case 'b': case 'B': { + found = 1; action = 1; op = dk3maih8_um_array[11]; + } break; + case 'c': case 'C': { + found = 1; action = 1; op = dk3maih8_um_array[12]; + } break; + case 'd': case 'D': { + found = 1; action = 1; op = dk3maih8_um_array[13]; + } break; + case 'e': case 'E': { + found = 1; action = 1; op = dk3maih8_um_array[14]; + } break; + case 'f': case 'F': { + found = 1; action = 1; op = dk3maih8_um_array[15]; + } break; + default: { /* Non-digit */ + cc = 0; + if (0 != found) { + back = 1; + } + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } break; + } + if (0 != cc) { + switch(action) { + case 1: { + val = dk3ma_um_add_ok( + dk3ma_um_mul_ok(val, dk3maih8_um_array[16], &mec), op, &mec + ); $? ". mec = %d", mec + if (0 != mec) { $? "! mec != 0" + cc = 0; + val = DK3_UM_MAX; + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } break; + } + } $? ". mec = %d", mec + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + *rp = val; +#if DK3_ON_WINDOWS + $? ". value = %I64u", val +#else +#if DK3_HAVE_INTMAX_T + $? ". value = %ju", val +#else +#if DK3_HAVE_LONG_LONG + $? ". value = %llu", val +#else + $? ". value = %lu", val +#endif +#endif +#endif + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } $? "- dk3ma_um_from_c8_hex_string %d", back + return back; +} + + + +int +dk3ma_im_from_c8_hex_string(dk3_im_t *rp, char const *src, int *ec) +{ + char const *ptr = NULL; /* Start of text */ + dk3_um_t umv = DK3_UM_0; /* Temporary value */ + dk3_im_t val = DK3_IM_0; /* Conversion result */ + int back = 0; /* Function result */ + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + ptr++; + if (0 != dk3ma_um_from_c8_hex_string(&umv, ptr, ec)) { + if ((dk3_um_t)DK3_IM_MAX >= umv) { + val = (dk3_im_t)umv; + val = DK3_IM_0 - val; + back = 1; + } else { + val = DK3_IM_MIN; +#if 0 + if ((dk3_um_t)(DK3_IM_MAX + (dk3_im_t)1L) == umv) +#else + if (((dk3_um_t)DK3_IM_MAX + (dk3_um_t)1UL) == umv) +#endif + { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + val = (dk3_im_t)umv; + } + } else { + if (0 != dk3ma_um_from_c8_hex_string(&umv, ptr, ec)) { + val = (dk3_im_t)umv; + back = 1; + } else { + val = (dk3_im_t)umv; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + *rp = val; + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_us_from_c8_hex_string(unsigned short *rp, char const *src, int *ec) +{ + dk3_um_t val = DK3_UM_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + if (0 != dk3ma_um_from_c8_hex_string(&val, src, ec)) { + if ((dk3_um_t)DK3_US_MAX >= val) { + *rp = (unsigned short)val; + back = 1; + } else { + *rp = DK3_US_MAX; + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } else { + *rp = (unsigned short)val; + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_s_from_c8_hex_string(short *rp, char const *src, int *ec) +{ + const char *ptr; + unsigned short val = 0U; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + if (0 != dk3ma_us_from_c8_hex_string(&val, &(ptr[1]), ec)) { + if ((unsigned short)DK3_S_MAX >= val) { + *rp = (short)(0 - (short)val); + back = 1; + } else { + *rp = DK3_S_MIN; + if (((unsigned short)DK3_S_MAX + (unsigned short)1U) == val) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + *rp = (short)val; + } + } else { + if (0 != dk3ma_us_from_c8_hex_string(&val, ptr, ec)) { + *rp = (short)val; + back = 1; + } else { + *rp = (short)val; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_ui_from_c8_hex_string(unsigned *rp, char const *src, int *ec) +{ + dk3_um_t val = DK3_UM_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + if (0 != dk3ma_um_from_c8_hex_string(&val, src, ec)) { + if ((dk3_um_t)DK3_U_MAX >= val) { + *rp = (unsigned)val; + back = 1; + } else { + *rp = DK3_U_MAX; + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } else { + *rp = (unsigned)val; + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_i_from_c8_hex_string(int *rp, char const *src, int *ec) +{ + const char *ptr; + unsigned val = 0U; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + if (0 != dk3ma_ui_from_c8_hex_string(&val, &(ptr[1]), ec)) { + if ((unsigned)DK3_I_MAX >= val) { + *rp = 0 - (int)val; + } else { + *rp = DK3_I_MIN; + if (((unsigned)DK3_I_MAX + 1U) == val) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + *rp = (int)val; + } + } else { + if (0 != dk3ma_ui_from_c8_hex_string(&val, ptr, ec)) { + *rp = (int)val; + back = 1; + } else { + *rp = (int)val; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_ul_from_c8_hex_string(unsigned long *rp, char const *src, int *ec) +{ + dk3_um_t val = DK3_UM_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + if (0 != dk3ma_um_from_c8_hex_string(&val, src, ec)) { + if ((dk3_um_t)DK3_UL_MAX >= val) { + *rp = (unsigned long)val; + back = 1; + } else { + *rp = DK3_UL_MAX; + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } else { + *rp = (unsigned long)val; + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_l_from_c8_hex_string(long *rp, char const *src, int *ec) +{ + const char *ptr; + unsigned long val = 0UL; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + if (0 != dk3ma_ul_from_c8_hex_string(&val, &(ptr[1]), ec)) { + if ((unsigned long)DK3_L_MAX >= val) { + *rp = (long)(0L - (long)val); + back = 1; + } else { + *rp = DK3_L_MIN; + if (((unsigned long)DK3_L_MAX + 1UL) == val) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + *rp = (long)val; + } + } else { + if (0 != dk3ma_ul_from_c8_hex_string(&val, ptr, ec)) { + *rp = (long)val; + back = 1; + } else { + *rp = (long)val; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +#if DK3_HAVE_LONG_LONG + +int +dk3ma_ull_from_c8_hex_string(unsigned long long *rp, char const *src, int *ec) +{ + dk3_um_t val = DK3_UM_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + if (0 != dk3ma_um_from_c8_hex_string(&val, src, ec)) { + if ((dk3_um_t)DK3_ULL_MAX >= val) { + *rp = (unsigned long long)val; + back = 1; + } else { + *rp = DK3_ULL_MAX; + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } else { + *rp = (unsigned long long)val; + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_ll_from_c8_hex_string(long long *rp, char const *src, int *ec) +{ + char const *ptr; + unsigned long long val = 0ULL; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + if (0 != dk3ma_ull_from_c8_hex_string(&val, &(ptr[1]), ec)) { + if ((unsigned long long)DK3_LL_MAX >= val) { + *rp = (long long)(0LL - (long long)val); + back = 1; + } else { + *rp = DK3_LL_MIN; + if (((unsigned long long)DK3_LL_MAX + 1ULL) == val) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + *rp = (long long)val; + } + } else { + if (0 != dk3ma_ull_from_c8_hex_string(&val, ptr, ec)) { + *rp = (long long)val; + back = 1; + } else { + *rp = (long long)val; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + +#endif + + + +#if DK3_HAVE_INTMAX_T + +int +dk3ma_uintmax_t_from_c8_hex_string(uintmax_t *rp, char const *src, int *ec) +{ + dk3_um_t val = DK3_UM_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + if (0 != dk3ma_um_from_c8_hex_string(&val, src, ec)) { + *rp = (uintmax_t)val; + back = 1; + } else { + *rp = (uintmax_t)val; + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + + + +int +dk3ma_intmax_t_from_c8_hex_string(intmax_t *rp, char const *src, int *ec) +{ + const char *ptr; + uintmax_t val = DK3_UINTMAX_T_0; + int back = 0; + if ((NULL != rp) && (NULL != src)) { + ptr = dk3maih8_str_c8_start(src); + if (NULL != ptr) { + if ('-' == *ptr) { + if (0 != dk3ma_uintmax_t_from_c8_hex_string(&val, &(ptr[1]), ec)) { + if ((uintmax_t)DK3_INTMAX_T_MAX >= val) { + *rp = (intmax_t)(DK3_INTMAX_T_0 - (intmax_t)val); + back = 1; + } else { + *rp = DK3_INTMAX_T_MIN; + if (((uintmax_t)DK3_INTMAX_T_MAX + DK3_UINTMAX_T_1) == val) { + back = 1; + } else { + if (NULL != ec) { *ec = DK3_ERROR_MATH_OVERFLOW; } + } + } + } else { + *rp = (intmax_t)val; + } + } else { + if (0 != dk3ma_uintmax_t_from_c8_hex_string(&val, ptr, ec)) { + *rp = (intmax_t)val; + back = 1; + } else { + *rp = (intmax_t)val; + } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_SYNTAX; } + } + } else { + if (NULL != ec) { *ec = DK3_ERROR_INVALID_ARGS; } + } + return back; +} + +#endif + |