sync/gen_codec.ysl2
author Volker Birk <vb@pep.foundation>
Wed, 22 Aug 2018 21:44:48 +0200
branchsync
changeset 2875 8e3169c2952a
parent 2844 6fce34991f7d
child 2899 63b619aef131
permissions -rw-r--r--
send beacon
     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             «@name»_t *_msg = NULL;
   121             uper_decode_complete(NULL, &asn_DEF_«@name», (void **) &_msg, data, size);
   122             if (!_msg)
   123                 return PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   124 
   125             *msg = _msg;
   126             return PEP_STATUS_OK;
   127         }
   128 
   129         DYNAMIC_API PEP_STATUS encode_«@name»_message(
   130                 «@name»_t *msg,
   131                 char **data,
   132                 size_t *size
   133             )
   134         {
   135             assert(data && msg);
   136             if (!(data && msg))
   137                 return PEP_ILLEGAL_VALUE;
   138 
   139             *data = NULL;
   140             *size = 0;
   141 
   142             char *_data = NULL;
   143             ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_«@name», NULL, msg,
   144                     (void **) &_data);
   145             if (_size == -1)
   146                 return PEP_CANNOT_ENCODE;
   147 
   148             *data = _data;
   149             *size = (size_t) _size;
   150 
   151             return PEP_STATUS_OK;
   152         }
   153 
   154         DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
   155                 const char *data,
   156                 size_t size,
   157                 char **text
   158             )
   159         {
   160             PEP_STATUS status = PEP_STATUS_OK;
   161             growing_buf_t *dst = NULL;
   162 
   163             assert(data && text);
   164             if (!(data && text))
   165                 return PEP_ILLEGAL_VALUE;
   166 
   167             *text = NULL;
   168 
   169             «@name»_t *msg = NULL;
   170             status = decode_«@name»_message(data, size, &msg);
   171             if (status)
   172                 goto the_end;
   173 
   174             dst = new_growing_buf();
   175             if (!dst) {
   176                 status = PEP_OUT_OF_MEMORY;
   177                 goto the_end;
   178             }
   179 
   180             asn_enc_rval_t er = xer_encode(&asn_DEF_«@name», msg, XER_F_BASIC,
   181                     (asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
   182             if (er.encoded == -1) {
   183                 status = PEP_CANNOT_ENCODE;
   184                 goto the_end;
   185             }
   186 
   187             *text = dst->data;
   188             dst->data = NULL;
   189 
   190         the_end:
   191             free_growing_buf(dst);
   192             ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   193             return status;
   194         }
   195 
   196         DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
   197                 const char *text,
   198                 char **data,
   199                 size_t *size
   200             )
   201         {
   202             PEP_STATUS status = PEP_STATUS_OK;
   203 
   204             assert(text && data && size);
   205             if (!(text && data && size))
   206                 return PEP_ILLEGAL_VALUE;
   207 
   208             *data = NULL;
   209             *size = 0;
   210 
   211             «@name»_t *msg = NULL;
   212             asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_«@name», (void **) &msg,
   213                     (const void *) text, strlen(text));
   214             if (dr.code != RC_OK) {
   215                 status = PEP_«yml:ucase(@name)»_ILLEGAL_MESSAGE;
   216                 goto the_end;
   217             }
   218 
   219             char *_data = NULL;
   220             size_t _size = 0;
   221             status = encode_«@name»_message(msg, &_data, &_size);
   222             if (status)
   223                 goto the_end;
   224 
   225             *data = _data;
   226             *size = (size_t) _size;
   227 
   228         the_end:
   229             ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   230             return status;
   231         }
   232 
   233         ||
   234     }
   235 }
   236