sync/gen_codec.ysl2
author buff <andreas@pep-project.org>
Fri, 28 Jun 2019 18:16:23 +0200
branchIOS-1576
changeset 3892 e7e371c1ad53
parent 3512 a5a45f8fa684
permissions -rw-r--r--
IOS-1576 increases timeout, 60 does not work involving UI
     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-2019, 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 "platform.h"
   107 
   108         #include "«yml:lcase(@name)»_codec.h"
   109         #include "../asn.1/«@name».h"
   110         #include "pEp_internal.h"
   111         #include "growing_buf.h"
   112 
   113         DYNAMIC_API PEP_STATUS decode_«@name»_message(
   114                 const char *data,
   115                 size_t size,
   116                 «@name»_t **msg
   117             )
   118         {
   119             assert(data && msg);
   120             if (!(data && msg))
   121                 return PEP_ILLEGAL_VALUE;
   122 
   123             *msg = NULL;
   124             «@name»_t *_msg = NULL;
   125             uper_decode_complete(NULL, &asn_DEF_«@name», (void **) &_msg, data, size);
   126             if (!_msg)
   127                 return PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   128 
   129             *msg = _msg;
   130             return PEP_STATUS_OK;
   131         }
   132 
   133         PEP_STATUS encode_«@name»_message(
   134                 «@name»_t *msg,
   135                 char **data,
   136                 size_t *size
   137             )
   138         {
   139             assert(data && msg);
   140             if (!(data && msg))
   141                 return PEP_ILLEGAL_VALUE;
   142 
   143             *data = NULL;
   144             *size = 0;
   145 
   146             char *_data = NULL;
   147             ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_«@name», NULL, msg,
   148                     (void **) &_data);
   149             if (_size == -1)
   150                 return PEP_CANNOT_ENCODE;
   151 
   152             *data = _data;
   153             *size = (size_t) _size;
   154 
   155             return PEP_STATUS_OK;
   156         }
   157 
   158         PEP_STATUS PER_to_XER_«@name»_msg(
   159                 const char *data,
   160                 size_t size,
   161                 char **text
   162             )
   163         {
   164             PEP_STATUS status = PEP_STATUS_OK;
   165             growing_buf_t *dst = NULL;
   166 
   167             assert(data && text);
   168             if (!(data && text))
   169                 return PEP_ILLEGAL_VALUE;
   170 
   171             *text = NULL;
   172 
   173             «@name»_t *msg = NULL;
   174             status = decode_«@name»_message(data, size, &msg);
   175             if (status)
   176                 goto the_end;
   177 
   178             dst = new_growing_buf();
   179             if (!dst) {
   180                 status = PEP_OUT_OF_MEMORY;
   181                 goto the_end;
   182             }
   183 
   184             asn_enc_rval_t er = xer_encode(&asn_DEF_«@name», msg, XER_F_BASIC,
   185                     (asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
   186             if (er.encoded == -1) {
   187                 status = PEP_CANNOT_ENCODE;
   188                 goto the_end;
   189             }
   190 
   191             *text = dst->data;
   192             dst->data = NULL;
   193 
   194         the_end:
   195             free_growing_buf(dst);
   196             ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   197             return status;
   198         }
   199 
   200         PEP_STATUS XER_to_PER_«@name»_msg(
   201                 const char *text,
   202                 char **data,
   203                 size_t *size
   204             )
   205         {
   206             PEP_STATUS status = PEP_STATUS_OK;
   207 
   208             assert(text && data && size);
   209             if (!(text && data && size))
   210                 return PEP_ILLEGAL_VALUE;
   211 
   212             *data = NULL;
   213             *size = 0;
   214 
   215             «@name»_t *msg = NULL;
   216             asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_«@name», (void **) &msg,
   217                     (const void *) text, strlen(text));
   218             if (dr.code != RC_OK) {
   219                 status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   220                 goto the_end;
   221             }
   222 
   223             char *_data = NULL;
   224             size_t _size = 0;
   225             status = encode_«@name»_message(msg, &_data, &_size);
   226             if (status)
   227                 goto the_end;
   228 
   229             *data = _data;
   230             *size = (size_t) _size;
   231 
   232         the_end:
   233             ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   234             return status;
   235         }
   236 
   237         ||
   238     }
   239 }
   240