blob: b2a1eac815fb52fcb5aad149aec9bc714307489b [file] [log] [blame]
Harry Tran1f1098a2020-03-10 10:40:10 -04001/*****************************************************************************
2# *
3# Copyright 2019 AT&T Intellectual Property *
4# *
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 *
8# *
9# http://www.apache.org/licenses/LICENSE-2.0 *
10# *
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. *
16# *
17******************************************************************************/
18
19/*-
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
22 */
23#ifndef _INTEGER_H_
24#define _INTEGER_H_
25
26#include <asn_application.h>
27#include <asn_codecs_prim.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
34
35extern asn_TYPE_descriptor_t asn_DEF_INTEGER;
36extern asn_TYPE_operation_t asn_OP_INTEGER;
37
38/* Map with <tag> to integer value association */
39typedef 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;
44
45/* This type describes an enumeration for INTEGER and ENUMERATED types */
46typedef 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;
55
56#define INTEGER_free ASN__PRIMITIVE_TYPE_free
57#define INTEGER_decode_ber ber_decode_primitive
58#define INTEGER_constraint asn_generic_no_constraint
59asn_struct_print_f INTEGER_print;
60asn_struct_compare_f INTEGER_compare;
61der_type_encoder_f INTEGER_encode_der;
62xer_type_decoder_f INTEGER_decode_xer;
63xer_type_encoder_f INTEGER_encode_xer;
64oer_type_decoder_f INTEGER_decode_oer;
65oer_type_encoder_f INTEGER_encode_oer;
66per_type_decoder_f INTEGER_decode_uper;
67per_type_encoder_f INTEGER_encode_uper;
68per_type_decoder_f INTEGER_decode_aper;
69per_type_encoder_f INTEGER_encode_aper;
70asn_random_fill_f INTEGER_random_fill;
71
72/***********************************
73 * Some handy conversion routines. *
74 ***********************************/
75
76/*
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()).
83 */
84int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l);
85int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l);
86int asn_imax2INTEGER(INTEGER_t *i, intmax_t l);
87int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l);
88
89/*
90 * Size-specific conversion helpers.
91 */
92int asn_INTEGER2long(const INTEGER_t *i, long *l);
93int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
94int asn_long2INTEGER(INTEGER_t *i, long l);
95int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
96int asn_int642INTEGER(INTEGER_t *i, int64_t l);
97int asn_uint642INTEGER(INTEGER_t *i, uint64_t l);
98
99/* A version of strtol/strtoimax(3) with nicer error reporting. */
100enum 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 */
106};
107enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end,
108 long *l);
109enum asn_strtox_result_e asn_strtoul_lim(const char *str, const char **end,
110 unsigned long *l);
111enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end,
112 intmax_t *l);
113enum asn_strtox_result_e asn_strtoumax_lim(const char *str, const char **end,
114 uintmax_t *l);
115
116/*
117 * Convert the integer value into the corresponding enumeration map entry.
118 */
119const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(
120 const asn_INTEGER_specifics_t *specs, long value);
121
122#ifdef __cplusplus
123}
124#endif
125
126#endif /* _INTEGER_H_ */