p≡p engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
7.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. // generate conditions and actions
  4. // Copyleft (c) 2018-2019, p≡p foundation
  5. // Written by Volker Birk
  6. include yslt.yml2
  7. tstylesheet {
  8. include standardlib.ysl2
  9. template "/protocol" {
  10. document "generated/{yml:lcase(@name)}_codec.h", "text"
  11. ||
  12. /**
  13. * @file «@name»_codec.h
  14. * @brief Definitions for «@name» encode and decode functions which transform message payloads to
  15. * and from PER-encoded data, and XER text to and from PER
  16. * @generated from ../sync/gen_codec.ysl2
  17. *
  18. * @see https://www.itu.int/en/ITU-T/asn1/Pages/introduction.aspx
  19. *
  20. * @license GNU General Public License 3.0 - see LICENSE.txt
  21. */
  22. #ifndef «yml:ucase(@name)»_CODEC_H
  23. #define «yml:ucase(@name)»_CODEC_H
  24. #include "pEpEngine.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. struct «@name»;
  29. /**
  30. * <!-- decode_«@name»_message() -->
  31. *
  32. * @brief decode PER encoded «@name» message
  33. *
  34. * @param[in] data PER encoded data
  35. * @param[in] size size of PER encoded data
  36. * @param[out] msg decoded «@name» message
  37. *
  38. * @retval status
  39. *
  40. * @ownership msg goes into the ownership of the caller
  41. */
  42. DYNAMIC_API PEP_STATUS decode_«@name»_message(
  43. const char *data,
  44. size_t size,
  45. struct «@name» **msg
  46. );
  47. /**
  48. * <!-- encode_«@name»_message() -->
  49. *
  50. * @brief decode PER encoded «@name» message
  51. *
  52. * @param[in] msg «@name» message to encode
  53. * @param[out] data PER encoded data
  54. * @param[out] size size of PER encoded data
  55. *
  56. * @retval status
  57. *
  58. * @ownership msg goes into the ownership of the caller
  59. */
  60. DYNAMIC_API PEP_STATUS encode_«@name»_message(
  61. struct «@name» *msg,
  62. char **data,
  63. size_t *size
  64. );
  65. /**
  66. * <!-- PER_to_XER_«@name»_msg() -->
  67. *
  68. * @brief decode «@name» message from PER into XER
  69. *
  70. * @param[in] data PER encoded data
  71. * @param[in] size size of PER encoded data
  72. * @param[out] text XER text of the same «@name» message
  73. *
  74. * @retval status
  75. */
  76. DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
  77. const char *data,
  78. size_t size,
  79. char **text
  80. );
  81. /**
  82. * <!-- XER_to_PER_«@name»_msg() -->
  83. *
  84. * @brief encode «@name» message from XER into PER
  85. *
  86. * @param[in] text string text with XER text of the «@name» message
  87. * @param[out] data PER encoded data
  88. * @param[out] size size of PER encoded data
  89. *
  90. * @retval status
  91. */
  92. DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
  93. const char *text,
  94. char **data,
  95. size_t *size
  96. );
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif
  101. ||
  102. document "generated/{yml:lcase(@name)}_codec.c", "text"
  103. ||
  104. /**
  105. * @file «@name»_codec.c
  106. * @brief Implementation for «@name» encode and decode functions which transform message payloads to
  107. * and from PER-encoded data, and XER text to and from PER
  108. * @generated from ../sync/gen_codec.ysl2
  109. *
  110. * @see https://www.itu.int/en/ITU-T/asn1/Pages/introduction.aspx
  111. *
  112. * @license GNU General Public License 3.0 - see LICENSE.txt
  113. */
  114. #include "platform.h"
  115. #include "«yml:lcase(@name)»_codec.h"
  116. #include "../asn.1/«@name».h"
  117. #include "pEp_internal.h"
  118. #include "growing_buf.h"
  119. DYNAMIC_API PEP_STATUS decode_«@name»_message(
  120. const char *data,
  121. size_t size,
  122. «@name»_t **msg
  123. )
  124. {
  125. assert(data && msg);
  126. if (!(data && msg))
  127. return PEP_ILLEGAL_VALUE;
  128. *msg = NULL;
  129. «@name»_t *_msg = NULL;
  130. uper_decode_complete(NULL, &asn_DEF_«@name», (void **) &_msg, data, size);
  131. if (!_msg)
  132. return PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
  133. *msg = _msg;
  134. return PEP_STATUS_OK;
  135. }
  136. PEP_STATUS encode_«@name»_message(
  137. «@name»_t *msg,
  138. char **data,
  139. size_t *size
  140. )
  141. {
  142. assert(data && msg);
  143. if (!(data && msg))
  144. return PEP_ILLEGAL_VALUE;
  145. *data = NULL;
  146. *size = 0;
  147. char *_data = NULL;
  148. ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_«@name», NULL, msg,
  149. (void **) &_data);
  150. if (_size == -1)
  151. return PEP_CANNOT_ENCODE;
  152. *data = _data;
  153. *size = (size_t) _size;
  154. return PEP_STATUS_OK;
  155. }
  156. PEP_STATUS PER_to_XER_«@name»_msg(
  157. const char *data,
  158. size_t size,
  159. char **text
  160. )
  161. {
  162. PEP_STATUS status = PEP_STATUS_OK;
  163. growing_buf_t *dst = NULL;
  164. assert(data && text);
  165. if (!(data && text))
  166. return PEP_ILLEGAL_VALUE;
  167. *text = NULL;
  168. «@name»_t *msg = NULL;
  169. status = decode_«@name»_message(data, size, &msg);
  170. if (status)
  171. goto the_end;
  172. dst = new_growing_buf();
  173. if (!dst) {
  174. status = PEP_OUT_OF_MEMORY;
  175. goto the_end;
  176. }
  177. asn_enc_rval_t er = xer_encode(&asn_DEF_«@name», msg, XER_F_BASIC,
  178. (asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
  179. if (er.encoded == -1) {
  180. status = PEP_CANNOT_ENCODE;
  181. goto the_end;
  182. }
  183. *text = dst->data;
  184. dst->data = NULL;
  185. the_end:
  186. free_growing_buf(dst);
  187. ASN_STRUCT_FREE(asn_DEF_«@name», msg);
  188. return status;
  189. }
  190. PEP_STATUS XER_to_PER_«@name»_msg(
  191. const char *text,
  192. char **data,
  193. size_t *size
  194. )
  195. {
  196. PEP_STATUS status = PEP_STATUS_OK;
  197. assert(text && data && size);
  198. if (!(text && data && size))
  199. return PEP_ILLEGAL_VALUE;
  200. *data = NULL;
  201. *size = 0;
  202. «@name»_t *msg = NULL;
  203. asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_«@name», (void **) &msg,
  204. (const void *) text, strlen(text));
  205. if (dr.code != RC_OK) {
  206. status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
  207. goto the_end;
  208. }
  209. char *_data = NULL;
  210. size_t _size = 0;
  211. status = encode_«@name»_message(msg, &_data, &_size);
  212. if (status)
  213. goto the_end;
  214. *data = _data;
  215. *size = (size_t) _size;
  216. the_end:
  217. ASN_STRUCT_FREE(asn_DEF_«@name», msg);
  218. return status;
  219. }
  220. ||
  221. }
  222. }