INTEGER.h 3.9 KB

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