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