Browse Source

some encoding

sync_and_key_reset
Volker Birk 4 years ago
parent
commit
570984adb5
12 changed files with 373 additions and 179 deletions
  1. +2
    -1
      .hgignore
  2. +0
    -124
      src/Sync_helper.c
  3. +0
    -45
      src/Sync_helper.h
  4. +39
    -0
      src/growing_buf.c
  5. +56
    -0
      src/growing_buf.h
  6. +3
    -3
      src/sync.c
  7. +1
    -1
      src/sync.h
  8. +16
    -0
      src/sync_impl.c
  9. +4
    -3
      sync/Makefile
  10. +234
    -0
      sync/gen_codec.ysl2
  11. +17
    -1
      sync/gen_statemachine.ysl2
  12. +1
    -1
      sync/sync.fsm

+ 2
- 1
.hgignore View File

@ -69,6 +69,8 @@ src/Sync_func.c
src/Sync_func.h
src/Sync_event.c
src/Sync_event.h
src/Sync_codec.c
src/Sync_codec.h
asn.1/keysync.asn1
asn.1/sync.asn1
sync/.codecs
@ -76,4 +78,3 @@ sync/.messages
sync/.actions
sync/.codegen
sync/.statemachines

+ 0
- 124
src/Sync_helper.c View File

@ -1,124 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "Sync_helper.h"
#include "pEp_internal.h"
typedef struct growing_buf {
char *data;
size_t size;
} growing_buf_t;
growing_buf_t *new_growing_buf(void)
{
growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
assert(result);
return result;
}
void free_growing_buf(growing_buf_t *buf)
{
if (buf) {
free(buf->data);
free(buf);
}
}
int consume_bytes(const void *src, size_t size, growing_buf_t *dst)
{
assert(src && dst);
if (!(src && dst))
return -1;
char *new_data = realloc(dst->data, dst->size + size + 1);
assert(new_data);
if (!new_data)
return -1;
dst->data = new_data;
memcpy(dst->data + dst->size, src, size);
dst->size += size;
dst->data[dst->size] = 0; // safeguard
return 1;
}
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(data && text);
if (!(data && text))
return PEP_ILLEGAL_VALUE;
*text = NULL;
Sync_t *msg = NULL;
uper_decode_complete(NULL, &asn_DEF_Sync, (void **) &msg, data, size);
if (!msg)
return PEP_SYNC_ILLEGAL_MESSAGE;
growing_buf_t *dst = new_growing_buf();
if (!dst) {
status = PEP_OUT_OF_MEMORY;
goto the_end;
}
asn_enc_rval_t er = xer_encode(&asn_DEF_Sync, msg, XER_F_BASIC,
(asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
if (er.encoded == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*text = dst->data;
dst->data = NULL;
the_end:
free_growing_buf(dst);
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(text && data && size);
if (!(text && data && size))
return PEP_ILLEGAL_VALUE;
*data = NULL;
*size = 0;
Sync_t *msg = NULL;
asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_Sync, (void **) &msg,
(const void *) text, strlen(text));
if (dr.code != RC_OK) {
status = PEP_SYNC_ILLEGAL_MESSAGE;
goto the_end;
}
char *payload = NULL;
ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_Sync, NULL, msg,
(void **) &payload);
if (_size == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*data = payload;
*size = (size_t) _size;
the_end:
ASN_STRUCT_FREE(asn_DEF_Sync, msg);
return status;
}

+ 0
- 45
src/Sync_helper.h View File

@ -1,45 +0,0 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include "pEpEngine.h"
#ifdef __cplusplus
extern "C" {
#endif
// decode_sync_msg() - decode sync message from PER into XER
//
// parameters:
// data (in) PER encoded data
// size (in) size of PER encoded data
// text (out) XER text of the same sync message
DYNAMIC_API PEP_STATUS decode_sync_msg(
const char *data,
size_t size,
char **text
);
// encode_sync_msg() - encode sync message from XER into PER
//
// parameters:
// text (in) string with XER text of the sync message
// data (out) PER encoded data
// size (out) size of PER encoded data
DYNAMIC_API PEP_STATUS encode_sync_msg(
const char *text,
char **data,
size_t *size
);
#ifdef __cplusplus
}
#endif

+ 39
- 0
src/growing_buf.c View File

@ -0,0 +1,39 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "pEp_internal.h"
#include "growing_buf.h"
growing_buf_t *new_growing_buf(void)
{
growing_buf_t *result = calloc(1, sizeof(growing_buf_t));
assert(result);
return result;
}
void free_growing_buf(growing_buf_t *buf)
{
if (buf) {
free(buf->data);
free(buf);
}
}
int growing_buf_consume(const void *src, size_t size, growing_buf_t *dst)
{
assert(src && dst);
if (!(src && dst))
return -1;
char *new_data = realloc(dst->data, dst->size + size + 1);
assert(new_data);
if (!new_data)
return -1;
dst->data = new_data;
memcpy(dst->data + dst->size, src, size);
dst->size += size;
dst->data[dst->size] = 0; // safeguard
return 1;
}

+ 56
- 0
src/growing_buf.h View File

@ -0,0 +1,56 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include "pEpEngine.h"
#ifdef __cplusplus
extern "C" {
#endif
// this is a growing buffer, which is needed by the ASN.1 implementation
// i.e. for encoding to XER
typedef struct growing_buf {
char *data;
size_t size;
} growing_buf_t;
// new_growing_buf() - allocate a new growing buffer
//
// return value:
// new buffer or NULL if out of memory
growing_buf_t *new_growing_buf(void);
// free_growing_buf() - free growing buffer
//
// parameters:
// buf (in) buffer to free
void free_growing_buf(growing_buf_t *buf);
// growing_buf_consume() - append new data to growing buffer
//
// parameters:
// src (in) new data
// size (in) size of new data
// dst (in) growing buffer where new data will be appended
//
// return value:
// 1 on succes, -1 on failure
int growing_buf_consume(const void *src, size_t size, growing_buf_t *dst);
#ifdef __cplusplus
}
#endif

+ 3
- 3
src/sync.c View File

@ -88,7 +88,7 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
void *obj
)
{
Sync_t *msg = NULL;
Sync_event_t *event= NULL;
PEP_STATUS status = PEP_STATUS_OK;
assert(session && session->retrieve_next_sync_event);
@ -101,10 +101,10 @@ DYNAMIC_API PEP_STATUS do_sync_protocol(
while (true)
{
event = session->retrieve_next_sync_event(session->sync_management);
if (msg == NULL)
if (!event)
break;
status = recv_Sync_event(session, msg);
status = recv_Sync_event(session, event);
if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
char buffer[MAX_LINELENGTH];
memset(buffer, 0, MAX_LINELENGTH);


+ 1
- 1
src/sync.h View File

@ -95,7 +95,7 @@ typedef int (*inject_sync_event_t)(Sync_event_t *ev, void *management);
// return value:
// next event
typedef struct Sync_event_t *(*retrieve_next_sync_event_t)(void *management);
typedef Sync_event_t *(*retrieve_next_sync_event_t)(void *management);
// register_sync_callbacks() - register adapter's callbacks


+ 16
- 0
src/sync_impl.c View File

@ -131,6 +131,22 @@ the_end:
return status;
}
PEP_STATUS send_Sync_message(
PEP_SESSION session,
Sync_PR fsm,
int event
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(session && fsm > None && event > None);
if (!(session && fsm > None && event > None))
return PEP_ILLEGAL_VALUE;
return status;
}
PEP_STATUS recv_Sync_event(
PEP_SESSION session,
Sync_event_t *ev


+ 4
- 3
sync/Makefile View File

@ -15,11 +15,12 @@ all: .codegen
.statemachines: sync.fsm gen_statemachine.ysl2 fsm.yml2 functions.ysl2
$(YML2_PROC) -y gen_statemachine.ysl2 $< -o $@
.codecs: sync.fsm gen_message_func.ysl2 fsm.yml2 functions.ysl2
$(YML2_PROC) -y gen_message_func.ysl2 $< -o $@
.codecs: sync.fsm gen_codec.ysl2 fsm.yml2 functions.ysl2
$(YML2_PROC) -y gen_codec.ysl2 $< -o $@
.messages: sync.fsm gen_messages.ysl2 fsm.yml2 functions.ysl2
.messages: sync.fsm gen_messages.ysl2 gen_message_func.ysl2 fsm.yml2 functions.ysl2
$(YML2_PROC) -y gen_messages.ysl2 $< -o $@
$(YML2_PROC) -y gen_message_func.ysl2 $< -o $@
.PHONY: clean copy


+ 234
- 0
sync/gen_codec.ysl2 View File

@ -0,0 +1,234 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
// generate conditions and actions
// Copyleft (c) 2018, p≡p foundation
// Written by Volker Birk
include yslt.yml2
tstylesheet {
include standardlib.ysl2
template "/protocol" {
document "generated/{@name}_codec.h", "text"
||
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include "pEpEngine.h"
#include "../asn.1/«@name».h"
#ifdef __cplusplus
extern "C" {
#endif
// decode_«@name»_msg() - decode PER encoded «@name» message
//
// parameters:
// data (in) PER encoded data
// size (in) size of PER encoded data
// msg (out) «@name» message
//
// caveat:
// msg goes into the ownership of the caller
DYNAMIC_API PEP_STATUS decode_«@name»_msg(
const char *data,
size_t size,
«@name»_t **msg
);
// encode_«@name»_msg() - encode «@name» message into PER encoded data
//
// parameters:
// msg (in) «@name» message
// data (out) PER encoded data
// size (out) size of PER encoded data
//
// caveat:
// data goes to the ownership of the caller
DYNAMIC_API PEP_STATUS encode_«@name»_msg(
«@name»_t *msg,
char **data,
size_t *size
);
// PER_to_XER_«@name»_msg() - decode «@name» message from PER into XER
//
// parameters:
// data (in) PER encoded data
// size (in) size of PER encoded data
// text (out) XER text of the same «@name» message
DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
const char *data,
size_t size,
char **text
);
// XER_to_PER_«@name»_msg() - encode «@name» message from XER into PER
//
// parameters:
// text (in) string with XER text of the «@name» message
// data (out) PER encoded data
// size (out) size of PER encoded data
DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
const char *text,
char **data,
size_t *size
);
#ifdef __cplusplus
}
#endif
||
document "generated/{@name}_codec.c", "text"
||
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "«@name»_codec.h"
#include "pEp_internal.h"
#include "growing_buf.h"
DYNAMIC_API PEP_STATUS decode_«@name»_msg(
const char *data,
size_t size,
«@name»_t **msg
)
{
assert(data && msg);
if (!(data && msg))
return PEP_ILLEGAL_VALUE;
*msg = NULL;
uper_decode_complete(NULL, &asn_DEF_«@name», (void **) &msg, data, size);
if (!msg)
return PEP_SYNC_ILLEGAL_MESSAGE;
return PEP_STATUS_OK;
}
DYNAMIC_API PEP_STATUS encode_«@name»_msg(
«@name»_t *msg,
char **data,
size_t *size
)
{
assert(data && msg);
if (!(data && msg))
return PEP_ILLEGAL_VALUE;
*data = NULL;
*size = 0;
char *_data = NULL;
ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_«@name», NULL, msg,
(void **) &_data);
if (_size == -1)
return PEP_CANNOT_ENCODE;
*data = _data;
*size = (size_t) _size;
return PEP_STATUS_OK;
}
DYNAMIC_API PEP_STATUS PER_to_XER_«@name»_msg(
const char *data,
size_t size,
char **text
)
{
PEP_STATUS status = PEP_STATUS_OK;
growing_buf_t *dst = NULL;
assert(data && text);
if (!(data && text))
return PEP_ILLEGAL_VALUE;
*text = NULL;
«@name»_t *msg = NULL;
status = decode_«@name»_msg(data, size, &msg);
if (status)
goto the_end;
dst = new_growing_buf();
if (!dst) {
status = PEP_OUT_OF_MEMORY;
goto the_end;
}
asn_enc_rval_t er = xer_encode(&asn_DEF_«@name», msg, XER_F_BASIC,
(asn_app_consume_bytes_f *) growing_buf_consume, (void *) dst);
if (er.encoded == -1) {
status = PEP_CANNOT_ENCODE;
goto the_end;
}
*text = dst->data;
dst->data = NULL;
the_end:
free_growing_buf(dst);
ASN_STRUCT_FREE(asn_DEF_«@name», msg);
return status;
}
DYNAMIC_API PEP_STATUS XER_to_PER_«@name»_msg(
const char *text,
char **data,
size_t *size
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(text && data && size);
if (!(text && data && size))
return PEP_ILLEGAL_VALUE;
*data = NULL;
*size = 0;
«@name»_t *msg = NULL;
asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_«@name», (void **) &msg,
(const void *) text, strlen(text));
if (dr.code != RC_OK) {
status = PEP_SYNC_ILLEGAL_MESSAGE;
goto the_end;
}
char *_data = NULL;
size_t _size = 0;
status = encode_«@name»_msg(msg, &_data, &_size);
if (status)
goto the_end;
*data = _data;
*size = (size_t) _size;
the_end:
ASN_STRUCT_FREE(asn_DEF_«@name», msg);
return status;
}
||
}
}

+ 17
- 1
sync/gen_statemachine.ysl2 View File

@ -3,7 +3,7 @@
// generate state machine code
// Copyleft (c) 2016, 2017, p≡p foundation
// Copyleft (c) 2016 - 2018, p≡p foundation
// Written by Volker Birk
@ -316,6 +316,22 @@ tstylesheet {
return status;
}
PEP_STATUS send_«@name»_message(
PEP_SESSION session,
«@name»_PR fsm,
int event
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(session && fsm > None && event > None);
if (!(session && fsm > None && event > None))
return PEP_ILLEGAL_VALUE;
return status;
}
PEP_STATUS recv_«@name»_event(
PEP_SESSION session,
«@name»_event_t *ev


+ 1
- 1
sync/sync.fsm View File

@ -1,7 +1,7 @@
// This file is under BSD License 2.0
// Sync protocol for p≡p
// Copyright (c) 2016, 2017 p≡p foundation
// Copyright (c) 2016 - 2018, p≡p foundation
// Written by Volker Birk


Loading…
Cancel
Save