sync/gen_codec.ysl2
branchsync
changeset 2840 b881c48c9e9d
child 2841 3b56e21206cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sync/gen_codec.ysl2	Mon Aug 13 15:33:42 2018 +0200
     1.3 @@ -0,0 +1,234 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +// generate conditions and actions
     1.8 +
     1.9 +// Copyleft (c) 2018, p≡p foundation
    1.10 +
    1.11 +// Written by Volker Birk
    1.12 +
    1.13 +include yslt.yml2
    1.14 +
    1.15 +tstylesheet {
    1.16 +    include standardlib.ysl2
    1.17 +
    1.18 +    template "/protocol" {
    1.19 +        document "generated/{@name}_codec.h", "text"
    1.20 +        ||
    1.21 +        // This file is under GNU General Public License 3.0
    1.22 +        // see LICENSE.txt
    1.23 +
    1.24 +        #pragma once
    1.25 +
    1.26 +
    1.27 +        #include "pEpEngine.h"
    1.28 +        #include "../asn.1/«@name».h"
    1.29 +
    1.30 +
    1.31 +        #ifdef __cplusplus
    1.32 +        extern "C" {
    1.33 +        #endif
    1.34 +
    1.35 +
    1.36 +        // decode_«@name»_msg() - decode PER encoded «@name» message
    1.37 +        //
    1.38 +        //  parameters:
    1.39 +        //      data (in)               PER encoded data
    1.40 +        //      size (in)               size of PER encoded data
    1.41 +        //      msg (out)               «@name» message
    1.42 +        //
    1.43 +        //  caveat:
    1.44 +        //      msg goes into the ownership of the caller
    1.45 +
    1.46 +        DYNAMIC_API PEP_STATUS decode_«@name»_msg(
    1.47 +                const char *data,
    1.48 +                size_t size,
    1.49 +                «@name»_t **msg
    1.50 +            );
    1.51 +
    1.52 +
    1.53 +        // encode_«@name»_msg() - encode «@name» message into PER encoded data
    1.54 +        //
    1.55 +        //  parameters:
    1.56 +        //      msg (in)                «@name» message
    1.57 +        //      data (out)              PER encoded data
    1.58 +        //      size (out)              size of PER encoded data
    1.59 +        //
    1.60 +        //  caveat:
    1.61 +        //      data goes to the ownership of the caller
    1.62 +
    1.63 +        DYNAMIC_API PEP_STATUS encode_«@name»_msg(
    1.64 +                «@name»_t *msg,
    1.65 +                char **data,
    1.66 +                size_t *size
    1.67 +            );
    1.68 +
    1.69 +
    1.70 +        // PER_to_XER_«@name»_msg() - decode «@name» message from PER into XER
    1.71 +        //
    1.72 +        //  parameters:
    1.73 +        //      data (in)               PER encoded data
    1.74 +        //      size (in)               size of PER encoded data
    1.75 +        //      text (out)              XER text of the same «@name» message
    1.76 +
    1.77 +        DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
    1.78 +                const char *data,
    1.79 +                size_t size,
    1.80 +                char **text
    1.81 +            );
    1.82 +
    1.83 +
    1.84 +        // XER_to_PER_«@name»_msg() - encode «@name» message from XER into PER
    1.85 +        //
    1.86 +        //  parameters:
    1.87 +        //      text (in)               string with XER text of the «@name» message
    1.88 +        //      data (out)              PER encoded data
    1.89 +        //      size (out)              size of PER encoded data
    1.90 +
    1.91 +        DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
    1.92 +                const char *text,
    1.93 +                char **data,
    1.94 +                size_t *size
    1.95 +            );
    1.96 +
    1.97 +
    1.98 +        #ifdef __cplusplus
    1.99 +        }
   1.100 +        #endif
   1.101 +
   1.102 +        ||
   1.103 +        document "generated/{@name}_codec.c", "text"
   1.104 +        ||
   1.105 +        // This file is under GNU General Public License 3.0
   1.106 +        // see LICENSE.txt
   1.107 +
   1.108 +        #include "«@name»_codec.h"
   1.109 +        #include "pEp_internal.h"
   1.110 +        #include "growing_buf.h"
   1.111 +
   1.112 +        DYNAMIC_API PEP_STATUS decode_«@name»_msg(
   1.113 +                const char *data,
   1.114 +                size_t size,
   1.115 +                «@name»_t **msg
   1.116 +            )
   1.117 +        {
   1.118 +            assert(data && msg);
   1.119 +            if (!(data && msg))
   1.120 +                return PEP_ILLEGAL_VALUE;
   1.121 +
   1.122 +            *msg = NULL;
   1.123 +            uper_decode_complete(NULL, &asn_DEF_«@name», (void **) &msg, data, size);
   1.124 +            if (!msg)
   1.125 +                return PEP_SYNC_ILLEGAL_MESSAGE;
   1.126 +
   1.127 +            return PEP_STATUS_OK;
   1.128 +        }
   1.129 +
   1.130 +        DYNAMIC_API PEP_STATUS encode_«@name»_msg(
   1.131 +                «@name»_t *msg,
   1.132 +                char **data,
   1.133 +                size_t *size
   1.134 +            )
   1.135 +        {
   1.136 +            assert(data && msg);
   1.137 +            if (!(data && msg))
   1.138 +                return PEP_ILLEGAL_VALUE;
   1.139 +
   1.140 +            *data = NULL;
   1.141 +            *size = 0;
   1.142 +
   1.143 +            char *_data = NULL;
   1.144 +            ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_«@name», NULL, msg,
   1.145 +                    (void **) &_data);
   1.146 +            if (_size == -1)
   1.147 +                return PEP_CANNOT_ENCODE;
   1.148 +
   1.149 +            *data = _data;
   1.150 +            *size = (size_t) _size;
   1.151 +
   1.152 +            return PEP_STATUS_OK;
   1.153 +        }
   1.154 +
   1.155 +        DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
   1.156 +                const char *data,
   1.157 +                size_t size,
   1.158 +                char **text
   1.159 +            )
   1.160 +        {
   1.161 +            PEP_STATUS status = PEP_STATUS_OK;
   1.162 +            growing_buf_t *dst = NULL;
   1.163 +
   1.164 +            assert(data && text);
   1.165 +            if (!(data && text))
   1.166 +                return PEP_ILLEGAL_VALUE;
   1.167 +
   1.168 +            *text = NULL;
   1.169 +
   1.170 +            «@name»_t *msg = NULL;
   1.171 +            status = decode_«@name»_msg(data, size, &msg);
   1.172 +            if (status)
   1.173 +                goto the_end;
   1.174 +
   1.175 +            dst = new_growing_buf();
   1.176 +            if (!dst) {
   1.177 +                status = PEP_OUT_OF_MEMORY;
   1.178 +                goto the_end;
   1.179 +            }
   1.180 +
   1.181 +            asn_enc_rval_t er = xer_encode(&asn_DEF_«@name», msg, XER_F_BASIC,
   1.182 +                    (asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
   1.183 +            if (er.encoded == -1) {
   1.184 +                status = PEP_CANNOT_ENCODE;
   1.185 +                goto the_end;
   1.186 +            }
   1.187 +
   1.188 +            *text = dst->data;
   1.189 +            dst->data = NULL;
   1.190 +
   1.191 +        the_end:
   1.192 +            free_growing_buf(dst);
   1.193 +            ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   1.194 +            return status;
   1.195 +        }
   1.196 +
   1.197 +        DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
   1.198 +                const char *text,
   1.199 +                char **data,
   1.200 +                size_t *size
   1.201 +            )
   1.202 +        {
   1.203 +            PEP_STATUS status = PEP_STATUS_OK;
   1.204 +
   1.205 +            assert(text && data && size);
   1.206 +            if (!(text && data && size))
   1.207 +                return PEP_ILLEGAL_VALUE;
   1.208 +
   1.209 +            *data = NULL;
   1.210 +            *size = 0;
   1.211 +
   1.212 +            «@name»_t *msg = NULL;
   1.213 +            asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_«@name», (void **) &msg,
   1.214 +                    (const void *) text, strlen(text));
   1.215 +            if (dr.code != RC_OK) {
   1.216 +                status = PEP_SYNC_ILLEGAL_MESSAGE;
   1.217 +                goto the_end;
   1.218 +            }
   1.219 +
   1.220 +            char *_data = NULL;
   1.221 +            size_t _size = 0;
   1.222 +            status = encode_«@name»_msg(msg, &_data, &_size);
   1.223 +            if (status)
   1.224 +                goto the_end;
   1.225 +
   1.226 +            *data = _data;
   1.227 +            *size = (size_t) _size;
   1.228 +
   1.229 +        the_end:
   1.230 +            ASN_STRUCT_FREE(asn_DEF_«@name», msg);
   1.231 +            return status;
   1.232 +        }
   1.233 +
   1.234 +        ||
   1.235 +    }
   1.236 +}
   1.237 +