blob: 86b603bbbee55587e4fa7c809aa6f06cc1b042ee [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>
Lev Walkin76780762017-07-07 10:07:30 -070010#include <INTEGER_oer.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 */
Wim Lewisfb6344e2014-07-28 12:16:01 -070028typedef const 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 */
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 Walkina9cc46e2004-09-22 16:06:28 +000038asn_struct_print_f INTEGER_print;
Lev Walkinf15320b2004-06-03 03:38:44 +000039ber_type_decoder_f INTEGER_decode_ber;
40der_type_encoder_f INTEGER_encode_der;
Lev Walkind703ff42004-10-21 11:21:25 +000041xer_type_decoder_f INTEGER_decode_xer;
Lev Walkina9cc46e2004-09-22 16:06:28 +000042xer_type_encoder_f INTEGER_encode_xer;
Lev Walkin59b176e2005-11-26 11:25:14 +000043per_type_decoder_f INTEGER_decode_uper;
Lev Walkin523de9e2006-08-18 01:34:18 +000044per_type_encoder_f INTEGER_encode_uper;
Lev Walkinf15320b2004-06-03 03:38:44 +000045
46/***********************************
47 * Some handy conversion routines. *
48 ***********************************/
49
50/*
Lev Walkin72ec9092017-07-05 05:49:12 -070051 * Natiwe size-independent conversion of native integers to/from INTEGER.
52 * (l_size) is in bytes.
Lev Walkinf15320b2004-06-03 03:38:44 +000053 * Returns 0 if it was possible to convert, -1 otherwise.
54 * -1/EINVAL: Mandatory argument missing
55 * -1/ERANGE: Value encoded is out of range for long representation
Lev Walkin72ec9092017-07-05 05:49:12 -070056 * -1/ENOMEM: Memory allocation failed (in asn_*2INTEGER()).
57 */
58int asn_INTEGER2imax(const INTEGER_t *i, intmax_t *l);
59int asn_INTEGER2umax(const INTEGER_t *i, uintmax_t *l);
60int asn_imax2INTEGER(INTEGER_t *i, intmax_t l);
61int asn_umax2INTEGER(INTEGER_t *i, uintmax_t l);
62
63/*
64 * Size-specific conversion helpers.
Lev Walkinf15320b2004-06-03 03:38:44 +000065 */
Lev Walkin5e033762004-09-29 13:26:15 +000066int asn_INTEGER2long(const INTEGER_t *i, long *l);
Lev Walkin5c879db2007-11-06 06:23:31 +000067int asn_INTEGER2ulong(const INTEGER_t *i, unsigned long *l);
Lev Walkind703ff42004-10-21 11:21:25 +000068int asn_long2INTEGER(INTEGER_t *i, long l);
Lev Walkin5c879db2007-11-06 06:23:31 +000069int asn_ulong2INTEGER(INTEGER_t *i, unsigned long l);
Lev Walkinf15320b2004-06-03 03:38:44 +000070
Lev Walkin72ec9092017-07-05 05:49:12 -070071/* A version of strtol/strtoimax(3) with nicer error reporting. */
72enum asn_strtox_result_e {
73 ASN_STRTOX_ERROR_RANGE = -3, /* Input outside of supported numeric range */
74 ASN_STRTOX_ERROR_INVAL = -2, /* Invalid data encountered (e.g., "+-") */
75 ASN_STRTOX_EXPECT_MORE = -1, /* More data expected (e.g. "+") */
76 ASN_STRTOX_OK = 0, /* Conversion succeded, number ends at (*end) */
77 ASN_STRTOX_EXTRA_DATA = 1 /* Conversion succeded, but the string has extra stuff */
Lev Walkine09f9f12012-09-02 23:06:35 -070078};
Lev Walkin72ec9092017-07-05 05:49:12 -070079enum asn_strtox_result_e asn_strtol_lim(const char *str, const char **end, long *l);
80enum asn_strtox_result_e asn_strtoimax_lim(const char *str, const char **end, intmax_t *l);
Lev Walkinb3751942012-09-02 19:36:47 -070081
Lev Walkinfc776432005-03-29 17:21:14 +000082/*
83 * Convert the integer value into the corresponding enumeration map entry.
84 */
85const asn_INTEGER_enum_map_t *INTEGER_map_value2enum(asn_INTEGER_specifics_t *specs, long value);
86
Lev Walkin21b41ac2005-07-24 09:03:44 +000087#ifdef __cplusplus
88}
89#endif
90
Lev Walkinf15320b2004-06-03 03:38:44 +000091#endif /* _INTEGER_H_ */