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