blob: a8b1658a260ecebe9699f4613ca37b94bc0875fd [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin4ca03ac2005-02-28 15:20:27 +00002 * Copyright (c) 2003, 2005 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>
Bi-Ruei, Chiue1bf2192017-08-19 00:55:25 +080010#include <NativeInteger.h>
Lev Walkinf15320b2004-06-03 03:38:44 +000011
Lev Walkin21b41ac2005-07-24 09:03:44 +000012#ifdef __cplusplus
13extern "C" {
14#endif
15
Lev Walkin8e8078a2004-09-26 13:10:40 +000016typedef ASN__PRIMITIVE_TYPE_t INTEGER_t;
Lev Walkinf15320b2004-06-03 03:38:44 +000017
Lev Walkin5e033762004-09-29 13:26:15 +000018extern asn_TYPE_descriptor_t asn_DEF_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 Walkinf15320b2004-06-03 03:38:44 +000050
51/***********************************
52 * Some handy conversion routines. *
53 ***********************************/
54
55/*
Lev Walkin72ec9092017-07-05 05:49:12 -070056 * Natiwe size-independent conversion of native integers to/from INTEGER.
57 * (l_size) is in bytes.
Lev Walkinf15320b2004-06-03 03:38:44 +000058 * Returns 0 if it was possible to convert, -1 otherwise.
59 * -1/EINVAL: Mandatory argument missing
60 * -1/ERANGE: Value encoded is out of range for long representation
Lev Walkin72ec9092017-07-05 05:49:12 -070061 * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()).
62 */
63int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l);
64int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l);
65int asn_imax2INTEGER(INTEGER_t *i, intmax_t l);
66int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l);
67
68/*
69 * Size-specific conversion helpers.
Lev Walkinf15320b2004-06-03 03:38:44 +000070 */
Lev Walkin5e033762004-09-29 13:26:15 +000071int asn_INTEGER2long(const INTEGER_t *i, long *l);
Lev Walkin5c879db2007-11-06 06:23:31 +000072int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
Lev Walkind703ff42004-10-21 11:21:25 +000073int asn_long2INTEGER(INTEGER_t *i, long l);
Lev Walkin5c879db2007-11-06 06:23:31 +000074int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
Lev Walkinf15320b2004-06-03 03:38:44 +000075
Lev Walkin72ec9092017-07-05 05:49:12 -070076/* A version of strtol/strtoimax(3) with nicer error reporting. */
77enum asn_strtox_result_e {
78 ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */
79 ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */
80 ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */
81 ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */
82 ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -070083};
Lev Walkin72ec9092017-07-05 05:49:12 -070084enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end, long *l);
85enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end, intmax_t *l);
Lev Walkinb3751942012-09-02 19:36:47 -070086
Lev Walkinfc776432005-03-29 17:21:14 +000087/*
88 * Convert the integer value into the corresponding enumeration map entry.
89 */
Lev Walkind62a6622017-08-22 02:31:02 -070090const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(
91 const asn_INTEGER_specifics_t *specs, long value);
Lev Walkinfc776432005-03-29 17:21:14 +000092
Lev Walkin21b41ac2005-07-24 09:03:44 +000093#ifdef __cplusplus
94}
95#endif
96
Lev Walkinf15320b2004-06-03 03:38:44 +000097#endif /* _INTEGER_H_ */