blob: cc760ffa9de5e3d502a185248a4fa05f8e8e70de [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin20696a42017-10-17 21:27:33 -07002 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#ifndef _INTEGER_H_
6#define _INTEGER_H_
7
Lev Walkin11c3e172004-09-24 21:00:50 +00008#include <asn_application.h>
Lev Walkind703ff42004-10-21 11:21:25 +00009#include <asn_codecs_prim.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000010
Lev Walkin21b41ac2005-07-24 09:03:44 +000011#ifdef __cplusplus
12extern "C" {
13#endif
14
Lev Walkin8e8078a2004-09-26 13:10:40 +000015typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
Lev Walkinf15320b2004-06-03 03:38:44 +000016
Lev Walkin5e033762004-09-29 13:26:15 +000017extern asn_TYPE_descriptor_t asn_DEF_INTEGER;
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080018extern asn_TYPE_operation_t asn_OP_INTEGER;
Lev Walkinf15320b2004-06-03 03:38:44 +000019
Lev Walkine0b56e02005-02-25 12:10:27 +000020/* 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 */
Lev Walkin36ddfc82017-08-22 09:22:08 +000028typedef struct asn_INTEGER_specifics_s {
Wim Lewisfb6344e2014-07-28 12:16:01 -070029 const asn_INTEGER_enum_map_t *value2enum; /* N -> "tag"; sorted by N */
30 const unsigned int *enum2value; /* "tag" => N; sorted by tag */
Lev Walkine0b56e02005-02-25 12:10:27 +000031 int map_count; /* Elements in either map */
Lev Walkin59b176e2005-11-26 11:25:14 +000032 int extension; /* This map is extensible */
Lev Walkine0b56e02005-02-25 12:10:27 +000033 int strict_enumeration; /* Enumeration set is fixed */
Lev Walkin8bb57a22007-12-03 13:41:36 +000034 int field_width; /* Size of native integer */
35 int field_unsigned; /* Signed=0, unsigned=1 */
Lev Walkine0b56e02005-02-25 12:10:27 +000036} asn_INTEGER_specifics_t;
37
Lev Walkin8077f932017-08-07 17:27:43 -070038#define INTEGER_free ASN__PRIMITIVE_TYPE_free
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080039#define INTEGER_decode_ber ber_decode_primitive
40#define INTEGER_constraint asn_generic_no_constraint
Lev Walkina9cc46e2004-09-22 16:06:28 +000041asn_struct_print_f INTEGER_print;
Lev Walkincd2f48e2017-08-10 02:14:59 -070042asn_struct_compare_f INTEGER_compare;
Lev Walkinf15320b2004-06-03 03:38:44 +000043der_type_encoder_f INTEGER_encode_der;
Lev Walkind703ff42004-10-21 11:21:25 +000044xer_type_decoder_f INTEGER_decode_xer;
Lev Walkina9cc46e2004-09-22 16:06:28 +000045xer_type_encoder_f INTEGER_encode_xer;
Lev Walkin602aacb2017-07-24 01:46:41 +040046oer_type_decoder_f INTEGER_decode_oer;
47oer_type_encoder_f INTEGER_encode_oer;
Lev Walkin59b176e2005-11-26 11:25:14 +000048per_type_decoder_f INTEGER_decode_uper;
Lev Walkin523de9e2006-08-18 01:34:18 +000049per_type_encoder_f INTEGER_encode_uper;
Lev Walkina5972be2017-09-29 23:15:58 -070050asn_random_fill_f INTEGER_random_fill;
Lev Walkinf15320b2004-06-03 03:38:44 +000051
52/***********************************
53 * Some handy conversion routines. *
54 ***********************************/
55
56/*
Lev Walkin72ec9092017-07-05 05:49:12 -070057 * Natiwe size-independent conversion of native integers to/from INTEGER.
58 * (l_size) is in bytes.
Lev Walkinf15320b2004-06-03 03:38:44 +000059 * Returns 0 if it was possible to convert, -1 otherwise.
60 * -1/EINVAL: Mandatory argument missing
61 * -1/ERANGE: Value encoded is out of range for long representation
Lev Walkin72ec9092017-07-05 05:49:12 -070062 * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()).
63 */
64int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l);
65int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l);
66int asn_imax2INTEGER(INTEGER_t *i, intmax_t l);
67int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l);
68
69/*
70 * Size-specific conversion helpers.
Lev Walkinf15320b2004-06-03 03:38:44 +000071 */
Lev Walkin5e033762004-09-29 13:26:15 +000072int asn_INTEGER2long(const INTEGER_t *i, long *l);
Lev Walkin5c879db2007-11-06 06:23:31 +000073int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
Lev Walkind703ff42004-10-21 11:21:25 +000074int asn_long2INTEGER(INTEGER_t *i, long l);
Lev Walkin5c879db2007-11-06 06:23:31 +000075int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
Lev Walkinf15320b2004-06-03 03:38:44 +000076
Lev Walkin72ec9092017-07-05 05:49:12 -070077/* A version of strtol/strtoimax(3) with nicer error reporting. */
78enum asn_strtox_result_e {
79 ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */
80 ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */
81 ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */
82 ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */
83 ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -070084};
Lev Walkin588bf0f2017-10-13 23:51:16 -070085enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end,
86 long *l);
87enum asn_strtox_result_e asn_strtoul_lim(const char *str, const char **end,
88 unsigned long *l);
89enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end,
90 intmax_t *l);
91enum asn_strtox_result_e asn_strtoumax_lim(const char *str, const char **end,
92 uintmax_t *l);
Lev Walkinb3751942012-09-02 19:36:47 -070093
Lev Walkinfc776432005-03-29 17:21:14 +000094/*
95 * Convert the integer value into the corresponding enumeration map entry.
96 */
Lev Walkind62a6622017-08-22 02:31:02 -070097const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(
98 const asn_INTEGER_specifics_t *specs, long value);
Lev Walkinfc776432005-03-29 17:21:14 +000099
Lev Walkin21b41ac2005-07-24 09:03:44 +0000100#ifdef __cplusplus
101}
102#endif
103
Lev Walkinf15320b2004-06-03 03:38:44 +0000104#endif /* _INTEGER_H_ */