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