Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 4ca03ac | 2005-02-28 15:20:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #ifndef _INTEGER_H_ |
| 6 | #define _INTEGER_H_ |
| 7 | |
Lev Walkin | 11c3e17 | 2004-09-24 21:00:50 +0000 | [diff] [blame] | 8 | #include <asn_application.h> |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 9 | #include <asn_codecs_prim.h> |
Bi-Ruei, Chiu | e1bf219 | 2017-08-19 00:55:25 +0800 | [diff] [blame] | 10 | #include <NativeInteger.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 16 | typedef ASN__PRIMITIVE_TYPE_t INTEGER_t; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 17 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 18 | extern asn_TYPE_descriptor_t asn_DEF_INTEGER; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 19 | extern asn_TYPE_operation_t asn_OP_INTEGER; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 21 | /* Map with <tag> to integer value association */ |
| 22 | typedef struct asn_INTEGER_enum_map_s { |
| 23 | long nat_value; /* associated native integer value */ |
| 24 | size_t enum_len; /* strlen("tag") */ |
| 25 | const char *enum_name; /* "tag" */ |
| 26 | } asn_INTEGER_enum_map_t; |
| 27 | |
| 28 | /* This type describes an enumeration for INTEGER and ENUMERATED types */ |
Lev Walkin | 36ddfc8 | 2017-08-22 09:22:08 +0000 | [diff] [blame] | 29 | typedef struct asn_INTEGER_specifics_s { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 30 | const asn_INTEGER_enum_map_t *value2enum; /* N -> "tag"; sorted by N */ |
| 31 | const unsigned int *enum2value; /* "tag" => N; sorted by tag */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 32 | int map_count; /* Elements in either map */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 33 | int extension; /* This map is extensible */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 34 | int strict_enumeration; /* Enumeration set is fixed */ |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 35 | int field_width; /* Size of native integer */ |
| 36 | int field_unsigned; /* Signed=0, unsigned=1 */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 37 | } asn_INTEGER_specifics_t; |
| 38 | |
Lev Walkin | 8077f93 | 2017-08-07 17:27:43 -0700 | [diff] [blame] | 39 | #define INTEGER_free ASN__PRIMITIVE_TYPE_free |
Bi-Ruei, Chiu | e1bf219 | 2017-08-19 00:55:25 +0800 | [diff] [blame] | 40 | #define INTEGER_decode_ber ber_decode_primitive |
| 41 | #define INTEGER_constraint asn_generic_no_constraint |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 42 | asn_struct_print_f INTEGER_print; |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 43 | asn_struct_compare_f INTEGER_compare; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | der_type_encoder_f INTEGER_encode_der; |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 45 | xer_type_decoder_f INTEGER_decode_xer; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 46 | xer_type_encoder_f INTEGER_encode_xer; |
Lev Walkin | 602aacb | 2017-07-24 01:46:41 +0400 | [diff] [blame] | 47 | oer_type_decoder_f INTEGER_decode_oer; |
| 48 | oer_type_encoder_f INTEGER_encode_oer; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 49 | per_type_decoder_f INTEGER_decode_uper; |
Lev Walkin | 523de9e | 2006-08-18 01:34:18 +0000 | [diff] [blame] | 50 | per_type_encoder_f INTEGER_encode_uper; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | |
| 52 | /*********************************** |
| 53 | * Some handy conversion routines. * |
| 54 | ***********************************/ |
| 55 | |
| 56 | /* |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 57 | * Natiwe size-independent conversion of native integers to/from INTEGER. |
| 58 | * (l_size) is in bytes. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | * Returns 0 if it was possible to convert, -1 otherwise. |
| 60 | * -1/EINVAL: Mandatory argument missing |
| 61 | * -1/ERANGE: Value encoded is out of range for long representation |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 62 | * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()). |
| 63 | */ |
| 64 | int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l); |
| 65 | int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l); |
| 66 | int asn_imax2INTEGER(INTEGER_t *i, intmax_t l); |
| 67 | int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l); |
| 68 | |
| 69 | /* |
| 70 | * Size-specific conversion helpers. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 72 | int asn_INTEGER2long(const INTEGER_t *i, long *l); |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 73 | int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l); |
Lev Walkin | d703ff4 | 2004-10-21 11:21:25 +0000 | [diff] [blame] | 74 | int asn_long2INTEGER(INTEGER_t *i, long l); |
Lev Walkin | 5c879db | 2007-11-06 06:23:31 +0000 | [diff] [blame] | 75 | int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 77 | /* A version of strtol/strtoimax(3) with nicer error reporting. */ |
| 78 | enum asn_strtox_result_e { |
| 79 | ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */ |
| 80 | ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */ |
| 81 | ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */ |
| 82 | ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */ |
| 83 | ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */ |
Lev Walkin | e09f9f1 | 2012-09-02 23:06:35 -0700 | [diff] [blame] | 84 | }; |
Lev Walkin | 72ec909 | 2017-07-05 05:49:12 -0700 | [diff] [blame] | 85 | enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end, long *l); |
| 86 | enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end, intmax_t *l); |
Lev Walkin | b375194 | 2012-09-02 19:36:47 -0700 | [diff] [blame] | 87 | |
Lev Walkin | fc77643 | 2005-03-29 17:21:14 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Convert the integer value into the corresponding enumeration map entry. |
| 90 | */ |
Lev Walkin | d62a662 | 2017-08-22 02:31:02 -0700 | [diff] [blame] | 91 | const asn_INTEGER_enum_map_t *INTEGER_map_value2enum( |
| 92 | const asn_INTEGER_specifics_t *specs, long value); |
Lev Walkin | fc77643 | 2005-03-29 17:21:14 +0000 | [diff] [blame] | 93 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 94 | #ifdef __cplusplus |
| 95 | } |
| 96 | #endif |
| 97 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | #endif /* _INTEGER_H_ */ |