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