asn_bit_data.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 ASN_BIT_DATA
  6. #define ASN_BIT_DATA
  7. #include <asn_system.h> /* Platform-specific types */
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /*
  12. * This structure describes a position inside an incoming PER bit stream.
  13. */
  14. typedef struct asn_bit_data_s {
  15. const uint8_t *buffer; /* Pointer to the octet stream */
  16. size_t nboff; /* Bit offset to the meaningful bit */
  17. size_t nbits; /* Number of bits in the stream */
  18. size_t moved; /* Number of bits moved through this bit stream */
  19. int (*refill)(struct asn_bit_data_s *);
  20. void *refill_key;
  21. } asn_bit_data_t;
  22. /*
  23. * Create a contiguous non-refillable bit data structure.
  24. * Can be freed by FREEMEM().
  25. */
  26. asn_bit_data_t *asn_bit_data_new_contiguous(const void *data, size_t size_bits);
  27. /*
  28. * Extract a small number of bits (<= 31) from the specified PER data pointer.
  29. * This function returns -1 if the specified number of bits could not be
  30. * extracted due to EOD or other conditions.
  31. */
  32. int32_t asn_get_few_bits(asn_bit_data_t *, int get_nbits);
  33. /* Undo the immediately preceeding "get_few_bits" operation */
  34. void asn_get_undo(asn_bit_data_t *, int get_nbits);
  35. /*
  36. * Extract a large number of bits from the specified PER data pointer.
  37. * This function returns -1 if the specified number of bits could not be
  38. * extracted due to EOD or other conditions.
  39. */
  40. int asn_get_many_bits(asn_bit_data_t *, uint8_t *dst, int right_align,
  41. int get_nbits);
  42. /* Non-thread-safe debugging function, don't use it */
  43. char *asn_bit_data_string(asn_bit_data_t *);
  44. /*
  45. * This structure supports forming bit output.
  46. */
  47. typedef struct asn_bit_outp_s {
  48. uint8_t *buffer; /* Pointer into the (tmpspace) */
  49. size_t nboff; /* Bit offset to the meaningful bit */
  50. size_t nbits; /* Number of bits left in (tmpspace) */
  51. uint8_t tmpspace[32]; /* Preliminary storage to hold data */
  52. int (*output)(const void *data, size_t size, void *op_key);
  53. void *op_key; /* Key for (output) data callback */
  54. size_t flushed_bytes; /* Bytes already flushed through (output) */
  55. } asn_bit_outp_t;
  56. /* Output a small number of bits (<= 31) */
  57. int asn_put_few_bits(asn_bit_outp_t *, uint32_t bits, int obits);
  58. /* Output a large number of bits */
  59. int asn_put_many_bits(asn_bit_outp_t *, const uint8_t *src, int put_nbits);
  60. /*
  61. * Flush whole bytes (0 or more) through (outper) member.
  62. * The least significant bits which are not used are guaranteed to be set to 0.
  63. * Returns -1 if callback returns -1. Otherwise, 0.
  64. */
  65. int asn_put_aligned_flush(asn_bit_outp_t *);
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. #endif /* ASN_BIT_DATA */