NativeInteger_oer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
  3. * All rights reserved.
  4. * Redistribution and modifications are permitted subject to BSD license.
  5. */
  6. #ifndef ASN_DISABLE_OER_SUPPORT
  7. #include <asn_internal.h>
  8. #include <NativeInteger.h>
  9. #include <errno.h>
  10. asn_dec_rval_t
  11. NativeInteger_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
  12. const asn_TYPE_descriptor_t *td,
  13. const asn_oer_constraints_t *constraints,
  14. void **nint_ptr, const void *ptr, size_t size) {
  15. const asn_INTEGER_specifics_t *specs =
  16. (const asn_INTEGER_specifics_t *)td->specifics;
  17. asn_dec_rval_t rval = {RC_OK, 0};
  18. long *native = (long *)*nint_ptr;
  19. INTEGER_t tmpint;
  20. INTEGER_t *tmpintptr = &tmpint;
  21. memset(&tmpint, 0, sizeof(tmpint));
  22. if(!native) {
  23. native = (long *)(*nint_ptr = CALLOC(1, sizeof(*native)));
  24. if(!native) ASN__DECODE_FAILED;
  25. }
  26. /*
  27. * OPTIMIZATION: Encode directly rather than passing through INTEGER.
  28. * Saves a memory allocation.
  29. */
  30. rval = INTEGER_decode_oer(opt_codec_ctx, td, constraints,
  31. (void **)&tmpintptr, ptr, size);
  32. if(rval.code != RC_OK) {
  33. ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  34. return rval;
  35. }
  36. if(specs && specs->field_unsigned) {
  37. unsigned long ul;
  38. int ok = asn_INTEGER2ulong(&tmpint, &ul) == 0;
  39. ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  40. if(ok) {
  41. *native = ul;
  42. } else {
  43. rval.code = RC_FAIL;
  44. return rval;
  45. }
  46. } else {
  47. long l;
  48. int ok = asn_INTEGER2long(&tmpint, &l) == 0;
  49. ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  50. if(ok) {
  51. *native = l;
  52. } else {
  53. rval.code = RC_FAIL;
  54. return rval;
  55. }
  56. }
  57. return rval;
  58. }
  59. /*
  60. * Encode as Canonical OER.
  61. */
  62. asn_enc_rval_t
  63. NativeInteger_encode_oer(const asn_TYPE_descriptor_t *td,
  64. const asn_oer_constraints_t *constraints,
  65. const void *sptr, asn_app_consume_bytes_f *cb,
  66. void *app_key) {
  67. const asn_INTEGER_specifics_t *specs =
  68. (const asn_INTEGER_specifics_t *)td->specifics;
  69. INTEGER_t tmpint;
  70. long native;
  71. if(!sptr) ASN__ENCODE_FAILED;
  72. native = *(const long *)sptr;
  73. memset(&tmpint, 0, sizeof(tmpint));
  74. ASN_DEBUG("Encoding %s %ld as NativeInteger", td ? td->name : "", native);
  75. if((specs && specs->field_unsigned) ? asn_ulong2INTEGER(&tmpint, native)
  76. : asn_long2INTEGER(&tmpint, native)) {
  77. ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  78. ASN__ENCODE_FAILED;
  79. } else {
  80. asn_enc_rval_t er =
  81. INTEGER_encode_oer(td, constraints, &tmpint, cb, app_key);
  82. ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_INTEGER, &tmpint);
  83. return er;
  84. }
  85. }
  86. #endif /* ASN_DISABLE_OER_SUPPORT */