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