per_encoder.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include <asn_application.h>
  2. #include <asn_internal.h>
  3. #include <per_encoder.h>
  4. static int _uper_encode_flush_outp(asn_per_outp_t *po);
  5. static int
  6. ignore_output(const void *data, size_t size, void *app_key) {
  7. (void)data;
  8. (void)size;
  9. (void)app_key;
  10. return 0;
  11. }
  12. asn_enc_rval_t
  13. uper_encode(const asn_TYPE_descriptor_t *td,
  14. const asn_per_constraints_t *constraints, const void *sptr,
  15. asn_app_consume_bytes_f *cb, void *app_key) {
  16. asn_per_outp_t po;
  17. asn_enc_rval_t er;
  18. /*
  19. * Invoke type-specific encoder.
  20. */
  21. if(!td || !td->op->uper_encoder)
  22. ASN__ENCODE_FAILED; /* PER is not compiled in */
  23. po.buffer = po.tmpspace;
  24. po.nboff = 0;
  25. po.nbits = 8 * sizeof(po.tmpspace);
  26. po.output = cb ? cb : ignore_output;
  27. po.op_key = app_key;
  28. po.flushed_bytes = 0;
  29. er = td->op->uper_encoder(td, constraints, sptr, &po);
  30. if(er.encoded != -1) {
  31. size_t bits_to_flush;
  32. bits_to_flush = ((po.buffer - po.tmpspace) << 3) + po.nboff;
  33. /* Set number of bits encoded to a firm value */
  34. er.encoded = (po.flushed_bytes << 3) + bits_to_flush;
  35. if(_uper_encode_flush_outp(&po)) ASN__ENCODE_FAILED;
  36. }
  37. return er;
  38. }
  39. /*
  40. * Argument type and callback necessary for uper_encode_to_buffer().
  41. */
  42. typedef struct enc_to_buf_arg {
  43. void *buffer;
  44. size_t left;
  45. } enc_to_buf_arg;
  46. static int encode_to_buffer_cb(const void *buffer, size_t size, void *key) {
  47. enc_to_buf_arg *arg = (enc_to_buf_arg *)key;
  48. if(arg->left < size)
  49. return -1; /* Data exceeds the available buffer size */
  50. memcpy(arg->buffer, buffer, size);
  51. arg->buffer = ((char *)arg->buffer) + size;
  52. arg->left -= size;
  53. return 0;
  54. }
  55. asn_enc_rval_t
  56. uper_encode_to_buffer(const asn_TYPE_descriptor_t *td,
  57. const asn_per_constraints_t *constraints,
  58. const void *sptr, void *buffer, size_t buffer_size) {
  59. enc_to_buf_arg key;
  60. key.buffer = buffer;
  61. key.left = buffer_size;
  62. if(td) ASN_DEBUG("Encoding \"%s\" using UNALIGNED PER", td->name);
  63. return uper_encode(td, constraints, sptr, encode_to_buffer_cb, &key);
  64. }
  65. typedef struct enc_dyn_arg {
  66. void *buffer;
  67. size_t length;
  68. size_t allocated;
  69. } enc_dyn_arg;
  70. static int
  71. encode_dyn_cb(const void *buffer, size_t size, void *key) {
  72. enc_dyn_arg *arg = key;
  73. if(arg->length + size >= arg->allocated) {
  74. size_t new_size = arg->allocated ? arg->allocated : 8;
  75. void *p;
  76. do {
  77. new_size <<= 2;
  78. } while(arg->length + size >= new_size);
  79. p = REALLOC(arg->buffer, new_size);
  80. if(!p) {
  81. FREEMEM(arg->buffer);
  82. memset(arg, 0, sizeof(*arg));
  83. return -1;
  84. }
  85. arg->buffer = p;
  86. arg->allocated = new_size;
  87. }
  88. memcpy(((char *)arg->buffer) + arg->length, buffer, size);
  89. arg->length += size;
  90. return 0;
  91. }
  92. ssize_t
  93. uper_encode_to_new_buffer(const asn_TYPE_descriptor_t *td,
  94. const asn_per_constraints_t *constraints,
  95. const void *sptr, void **buffer_r) {
  96. asn_enc_rval_t er;
  97. enc_dyn_arg key;
  98. memset(&key, 0, sizeof(key));
  99. er = uper_encode(td, constraints, sptr, encode_dyn_cb, &key);
  100. switch(er.encoded) {
  101. case -1:
  102. FREEMEM(key.buffer);
  103. return -1;
  104. case 0:
  105. FREEMEM(key.buffer);
  106. key.buffer = MALLOC(1);
  107. if(key.buffer) {
  108. *(char *)key.buffer = '\0';
  109. *buffer_r = key.buffer;
  110. return 1;
  111. } else {
  112. return -1;
  113. }
  114. default:
  115. *buffer_r = key.buffer;
  116. ASN_DEBUG("Complete encoded in %ld bits", (long)er.encoded);
  117. return ((er.encoded + 7) >> 3);
  118. }
  119. }
  120. /*
  121. * Internally useful functions.
  122. */
  123. /* Flush partially filled buffer */
  124. static int
  125. _uper_encode_flush_outp(asn_per_outp_t *po) {
  126. uint8_t *buf;
  127. if(po->nboff == 0 && po->buffer == po->tmpspace)
  128. return 0;
  129. buf = po->buffer + (po->nboff >> 3);
  130. /* Make sure we account for the last, partially filled */
  131. if(po->nboff & 0x07) {
  132. buf[0] &= 0xff << (8 - (po->nboff & 0x07));
  133. buf++;
  134. }
  135. return po->output(po->tmpspace, buf - po->tmpspace, po->op_key);
  136. }