per_support.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2005-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
  3. * Redistribution and modifications are permitted subject to BSD license.
  4. */
  5. #ifndef _PER_SUPPORT_H_
  6. #define _PER_SUPPORT_H_
  7. #include <asn_system.h> /* Platform-specific types */
  8. #include <asn_bit_data.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /*
  13. * Pre-computed PER constraints.
  14. */
  15. typedef struct asn_per_constraint_s {
  16. enum asn_per_constraint_flags {
  17. APC_UNCONSTRAINED = 0x0, /* No PER visible constraints */
  18. APC_SEMI_CONSTRAINED = 0x1, /* Constrained at "lb" */
  19. APC_CONSTRAINED = 0x2, /* Fully constrained */
  20. APC_EXTENSIBLE = 0x4 /* May have extension */
  21. } flags;
  22. int range_bits; /* Full number of bits in the range */
  23. int effective_bits; /* Effective bits */
  24. long lower_bound; /* "lb" value */
  25. long upper_bound; /* "ub" value */
  26. } asn_per_constraint_t;
  27. typedef struct asn_per_constraints_s {
  28. asn_per_constraint_t value;
  29. asn_per_constraint_t size;
  30. int (*value2code)(unsigned int value);
  31. int (*code2value)(unsigned int code);
  32. } asn_per_constraints_t;
  33. /* Temporary compatibility layer. Will get removed. */
  34. typedef struct asn_bit_data_s asn_per_data_t;
  35. #define per_get_few_bits(data, bits) asn_get_few_bits(data, bits)
  36. #define per_get_undo(data, bits) asn_get_undo(data, bits)
  37. #define per_get_many_bits(data, dst, align, bits) \
  38. asn_get_many_bits(data, dst, align, bits)
  39. /*
  40. * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
  41. * Get the length "n" from the Unaligned PER stream.
  42. */
  43. ssize_t uper_get_length(asn_per_data_t *pd, int effective_bound_bits,
  44. size_t lower_bound, int *repeat);
  45. /*
  46. * Get the normally small length "n".
  47. */
  48. ssize_t uper_get_nslength(asn_per_data_t *pd);
  49. /*
  50. * Get the normally small non-negative whole number.
  51. */
  52. ssize_t uper_get_nsnnwn(asn_per_data_t *pd);
  53. /* X.691-2008/11, #11.5.6 */
  54. int uper_get_constrained_whole_number(asn_per_data_t *pd, unsigned long *v, int nbits);
  55. /* Temporary compatibility layer. Will get removed. */
  56. typedef struct asn_bit_outp_s asn_per_outp_t;
  57. #define per_put_few_bits(out, bits, obits) asn_put_few_bits(out, bits, obits)
  58. #define per_put_many_bits(out, src, nbits) asn_put_many_bits(out, src, nbits)
  59. #define per_put_aligned_flush(out) asn_put_aligned_flush(out)
  60. /*
  61. * Rebase the given value as an offset into the range specified by the
  62. * lower bound (lb) and upper bound (ub).
  63. * RETURN VALUES:
  64. * -1: Conversion failed due to range problems.
  65. * 0: Conversion was successful.
  66. */
  67. int per_long_range_rebase(long v, long lb, long ub, unsigned long *output);
  68. /* The inverse operation: restores the value by the offset and its bounds. */
  69. int per_long_range_unrebase(unsigned long inp, long lb, long ub, long *outp);
  70. /* X.691-2008/11, #11.5 */
  71. int uper_put_constrained_whole_number_u(asn_per_outp_t *po, unsigned long v, int nbits);
  72. /*
  73. * X.691 (08/2015) #11.9 "General rules for encoding a length determinant"
  74. * Put the length "whole_length" to the Unaligned PER stream.
  75. * If (opt_need_eom) is given, it will be set to 1 if final 0-length is needed.
  76. * In that case, invoke uper_put_length(po, 0, 0) after encoding the last block.
  77. * This function returns the number of units which may be flushed
  78. * in the next units saving iteration.
  79. */
  80. ssize_t uper_put_length(asn_per_outp_t *po, size_t whole_length,
  81. int *opt_need_eom);
  82. /*
  83. * Put the normally small length "n" to the Unaligned PER stream.
  84. * Returns 0 or -1.
  85. */
  86. int uper_put_nslength(asn_per_outp_t *po, size_t length);
  87. /*
  88. * Put the normally small non-negative whole number.
  89. */
  90. int uper_put_nsnnwn(asn_per_outp_t *po, int n);
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif /* _PER_SUPPORT_H_ */