decode_sync_msg() for debugging keysync
authorVolker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 22:47:08 +0200
branchkeysync
changeset 1116c9a03f2423c1
parent 1114 5e1507afb216
child 1117 afcee607d04d
decode_sync_msg() for debugging
src/asn1_helper.c
src/asn1_helper.h
src/sync.c
src/sync.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/asn1_helper.c	Thu Sep 01 22:47:08 2016 +0200
     1.3 @@ -0,0 +1,36 @@
     1.4 +#include "asn1_helper.h"
     1.5 +#include <assert.h>
     1.6 +#include <stdlib.h>
     1.7 +
     1.8 +growing_buf_t *new_growing_buf(void)
     1.9 +{
    1.10 +    growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
    1.11 +    assert(result);
    1.12 +    return result;
    1.13 +}
    1.14 +
    1.15 +void free_growing_buf(growing_buf_t *buf)
    1.16 +{
    1.17 +    if (buf) {
    1.18 +        free(buf->data);
    1.19 +        free(buf);
    1.20 +    }
    1.21 +}
    1.22 +
    1.23 +int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
    1.24 +{
    1.25 +    assert(src && dst);
    1.26 +    if (!(src && dst))
    1.27 +        return -1;
    1.28 +
    1.29 +    char *new_data = realloc(dst->data, dst->size + size + 1);
    1.30 +    if (!new_data)
    1.31 +        return -1;
    1.32 +    dst->data = new_data;
    1.33 +    memcpy(dst->data + dst->size, src, size);
    1.34 +    dst->size += size;
    1.35 +    dst->data[dst->size] = 0; // safeguard
    1.36 +
    1.37 +    return 1;
    1.38 +}
    1.39 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/asn1_helper.h	Thu Sep 01 22:47:08 2016 +0200
     2.3 @@ -0,0 +1,13 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include <string.h>
     2.7 +
     2.8 +typedef struct growing_buf {
     2.9 +    char *data;
    2.10 +    size_t size;
    2.11 +} growing_buf_t;
    2.12 +
    2.13 +growing_buf_t *new_growing_buf(void);
    2.14 +void free_growing_buf(growing_buf_t *buf);
    2.15 +int consume_bytes(const void *src, size_t size, growing_buf_t *dst);
    2.16 +
     3.1 --- a/src/sync.c	Thu Sep 01 18:32:21 2016 +0200
     3.2 +++ b/src/sync.c	Thu Sep 01 22:47:08 2016 +0200
     3.3 @@ -3,6 +3,7 @@
     3.4  #include <memory.h>
     3.5  #include <assert.h>
     3.6  
     3.7 +#include "asn1_helper.h"
     3.8  #include "../asn.1/DeviceGroup-Protocol.h"
     3.9  
    3.10  // receive_sync_msg is defined in the sync_actions
    3.11 @@ -110,3 +111,45 @@
    3.12      return PEP_STATUS_OK;
    3.13  }
    3.14  
    3.15 +DYNAMIC_API PEP_STATUS decode_sync_msg(
    3.16 +        const char *data,
    3.17 +        size_t size,
    3.18 +        char **text
    3.19 +    )
    3.20 +{
    3.21 +    PEP_STATUS status = PEP_STATUS_OK;
    3.22 +
    3.23 +    assert(data && text);
    3.24 +    if (!(data && text))
    3.25 +        return PEP_ILLEGAL_VALUE;
    3.26 +
    3.27 +    *text = NULL;
    3.28 +
    3.29 +    DeviceGroup_Protocol_t *msg = NULL;
    3.30 +    uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **) &msg,
    3.31 +            data, size);
    3.32 +    if (!msg)
    3.33 +        return PEP_SYNC_ILLEGAL_MESSAGE;
    3.34 +
    3.35 +    growing_buf_t *dst = new_growing_buf();
    3.36 +    if (!dst) {
    3.37 +        status = PEP_OUT_OF_MEMORY;
    3.38 +        goto the_end;
    3.39 +    }
    3.40 +
    3.41 +    asn_enc_rval_t er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg,
    3.42 +            XER_F_BASIC, (asn_app_consume_bytes_f *) consume_bytes, (void *) &dst);
    3.43 +    if (er.encoded == -1) {
    3.44 +        status = PEP_SYNC_ILLEGAL_MESSAGE;
    3.45 +        goto the_end;
    3.46 +    }
    3.47 +
    3.48 +    *text = dst->data;
    3.49 +    dst->data = NULL;
    3.50 +
    3.51 +the_end:
    3.52 +    free_growing_buf(dst);
    3.53 +    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    3.54 +    return status;
    3.55 +}
    3.56 +
     4.1 --- a/src/sync.h	Thu Sep 01 18:32:21 2016 +0200
     4.2 +++ b/src/sync.h	Thu Sep 01 22:47:08 2016 +0200
     4.3 @@ -121,7 +121,7 @@
     4.4  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
     4.5  
     4.6  
     4.7 -// do_sync_protocol - function to be run on an extra thread
     4.8 +// do_sync_protocol() - function to be run on an extra thread
     4.9  //
    4.10  //  parameters:
    4.11  //      session                 pEp session to use
    4.12 @@ -146,6 +146,20 @@
    4.13      );
    4.14  
    4.15  
    4.16 +// decode_sync_msg() - decode sync message from PER into XER
    4.17 +//
    4.18 +//  parameters:
    4.19 +//      data (in)               PER encoded data
    4.20 +//      size (in)               size of PER encoded data
    4.21 +//      text (out)              XER text of the same sync message
    4.22 +
    4.23 +DYNAMIC_API PEP_STATUS decode_sync_msg(
    4.24 +        const char *data,
    4.25 +        size_t size,
    4.26 +        char **text
    4.27 +    );
    4.28 +
    4.29 +
    4.30  #ifdef __cplusplus
    4.31  }
    4.32  #endif