1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
26 #include <asn_application.h>
27 #include <asn_codecs_prim.h>
33 typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
35 extern asn_TYPE_descriptor_t asn_DEF_INTEGER;
36 extern asn_TYPE_operation_t asn_OP_INTEGER;
38 /* Map with <tag> to integer value association */
39 typedef struct asn_INTEGER_enum_map_s {
40 long nat_value; /* associated native integer value */
41 size_t enum_len; /* strlen("tag") */
42 const char *enum_name; /* "tag" */
43 } asn_INTEGER_enum_map_t;
45 /* This type describes an enumeration for INTEGER and ENUMERATED types */
46 typedef struct asn_INTEGER_specifics_s {
47 const asn_INTEGER_enum_map_t *value2enum; /* N -> "tag"; sorted by N */
48 const unsigned int *enum2value; /* "tag" => N; sorted by tag */
49 int map_count; /* Elements in either map */
50 int extension; /* This map is extensible */
51 int strict_enumeration; /* Enumeration set is fixed */
52 int field_width; /* Size of native integer */
53 int field_unsigned; /* Signed=0, unsigned=1 */
54 } asn_INTEGER_specifics_t;
56 #define INTEGER_free ASN__PRIMITIVE_TYPE_free
57 #define INTEGER_decode_ber ber_decode_primitive
58 #define INTEGER_constraint asn_generic_no_constraint
59 asn_struct_print_f INTEGER_print;
60 asn_struct_compare_f INTEGER_compare;
61 der_type_encoder_f INTEGER_encode_der;
62 xer_type_decoder_f INTEGER_decode_xer;
63 xer_type_encoder_f INTEGER_encode_xer;
64 oer_type_decoder_f INTEGER_decode_oer;
65 oer_type_encoder_f INTEGER_encode_oer;
66 per_type_decoder_f INTEGER_decode_uper;
67 per_type_encoder_f INTEGER_encode_uper;
68 per_type_decoder_f INTEGER_decode_aper;
69 per_type_encoder_f INTEGER_encode_aper;
70 asn_random_fill_f INTEGER_random_fill;
72 /***********************************
73 * Some handy conversion routines. *
74 ***********************************/
77 * Natiwe size-independent conversion of native integers to/from INTEGER.
78 * (l_size) is in bytes.
79 * Returns 0 if it was possible to convert, -1 otherwise.
80 * -1/EINVAL: Mandatory argument missing
81 * -1/ERANGE: Value encoded is out of range for long representation
82 * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()).
84 int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l);
85 int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l);
86 int asn_imax2INTEGER(INTEGER_t *i, intmax_t l);
87 int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l);
90 * Size-specific conversion helpers.
92 int asn_INTEGER2long(const INTEGER_t *i, long *l);
93 int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
94 int asn_long2INTEGER(INTEGER_t *i, long l);
95 int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
96 int asn_int642INTEGER(INTEGER_t *i, int64_t l);
97 int asn_uint642INTEGER(INTEGER_t *i, uint64_t l);
99 /* A version of strtol/strtoimax(3) with nicer error reporting. */
100 enum asn_strtox_result_e {
101 ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */
102 ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */
103 ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */
104 ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */
105 ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */
107 enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end,
109 enum asn_strtox_result_e asn_strtoul_lim(const char *str, const char **end,
111 enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end,
113 enum asn_strtox_result_e asn_strtoumax_lim(const char *str, const char **end,
117 * Convert the integer value into the corresponding enumeration map entry.
119 const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(
120 const asn_INTEGER_specifics_t *specs, long value);
126 #endif /* _INTEGER_H_ */