Guardtime KSI c SDK
tlv_template.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-2015 Guardtime, Inc.
3  *
4  * This file is part of the Guardtime client SDK.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License").
7  * You may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES, CONDITIONS, OR OTHER LICENSES OF ANY KIND, either
13  * express or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  * "Guardtime" and "KSI" are trademarks or registered trademarks of
16  * Guardtime, Inc., and no license to trademarks is granted; Guardtime
17  * reserves and retains all trademark rights.
18  */
19 
20 #include <stdlib.h>
21 #include "common.h"
22 #include "types.h"
23 
24 #ifndef KSI_TLV_TEMPLATE_H_
25 #define KSI_TLV_TEMPLATE_H_
26 
31 #define KSI_TLV_toTlv KSI_TLV_clone
32 #define KSI_TLV_fromTlv KSI_TLV_clone
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 
48  typedef int (*getter_t)(const void *, void **);
49 
53  typedef int (*setter_t)(void *, void *);
54 
58  typedef int (*cb_decode_t)(KSI_CTX *ctx, const KSI_TLV *, void *, const KSI_TlvTemplate *);
59 
63  typedef int (*cb_encode_t)(KSI_CTX *, KSI_TLV *, const void *, const KSI_TlvTemplate *);
64 
65  typedef int (*parse_t)(KSI_CTX *, unsigned char *, size_t, int, void *);
66 
74  int type;
75 
79  unsigned tag;
80 
81  unsigned flags;
82 
87 
92 
96  int (*construct)(KSI_CTX *, void **);
97 
101  void (*destruct)(void *);
102 
108 
112  int (*listAppend)(void *, void *);
113 
118  int multiple;
119 
123  int (*listNew)(void **);
124 
129  void (*listFree)(void *);
130 
134  int (*listLength)(const void *);
135 
139  int (*listElementAt)(const void *, int, void **);
140 
144  int (*fromTlv)(KSI_TLV *tlv, void **);
145 
149  int (*toTlv)(KSI_CTX *, void *, unsigned, int, int, KSI_TLV **tlv);
150 
154  const char *descr;
155 
156  /* Object parser. */
159 
160  int (*setRaw)(void *, KSI_OctetString *);
161  };
162 
163 
170  #define KSI_TLV_TEMPLATE(name) name##_template
171 
175  #define KSI_IMPORT_TLV_TEMPLATE(name) KSI_DEFINE_EXTERN(const KSI_TlvTemplate KSI_TLV_TEMPLATE(name)[])
176 
180  #define KSI_TLV_TEMPLATE_OBJECT 1
181 
185  #define KSI_TLV_TEMPLATE_COMPOSITE 2
186 
190  #define KSI_TLV_TEMPLATE_LIST 3
191 
195  #define KSI_TLV_TMPL_FLG_NONE 0x00
196 
200  #define KSI_TLV_TMPL_FLG_FORWARD 0x01
201 
205  #define KSI_TLV_TMPL_FLG_NONCRITICAL 0x02
206 
210  #define KSI_TLV_TMPL_FLG_MANDATORY 0x04
211 
215  #define KSI_TLV_TMPL_FLG_LEAST_ONE_G0 0x08
216 
220  #define KSI_TLV_TMPL_FLG_LEAST_ONE_G1 0x10
221 
226  #define KSI_TLV_TMPL_FLG_MORE_DEFS 0x20
227 
231  #define KSI_TLV_TMPL_FLG_NO_SERIALIZE 0x40
232 
236  #define KSI_TLV_TMPL_FLG_MOST_ONE_G0 0x80
237 
241  #define KSI_TLV_TMPL_FLG_MOST_ONE_G1 0x100
242 
247  #define KSI_TLV_TMPL_FLG_FIXED_ORDER 0x200
248 
252  #define KSI_TLV_TMPL_FLG_FIRST 0x400
253 
257  #define KSI_TLV_TMPL_FLG_LAST 0x800
258 
262  #define KSI_TLV_TMPL_FLG_NO_VALUE 0x1000
263 
267  #define KSI_TLV_TMPL_FLG_MANTATORY_MOST_ONE_G0 (KSI_TLV_TMPL_FLG_LEAST_ONE_G0 | KSI_TLV_TMPL_FLG_MOST_ONE_G0)
268 
272  #define KSI_TLV_TMPL_FLG_MANTATORY_MOST_ONE_G1 (KSI_TLV_TMPL_FLG_LEAST_ONE_G1 | KSI_TLV_TMPL_FLG_MOST_ONE_G1)
273 
297  #define KSI_TLV_FULL_TEMPLATE_DEF(typ, tg, flg, gttr, sttr, constr, destr, subTmpl, list_append, mul, list_new, list_free, list_len, list_elAt, fromTlv, toTlv, descr, parser, p_opt, setRaw) \
298  { typ, tg, flg , (getter_t)gttr, (setter_t)sttr, (int (*)(KSI_CTX *, void **)) constr, (void (*)(void *)) destr, subTmpl, \
299  (int (*)(void *, void *))list_append, mul, (int (*)(void **)) list_new, (void (*)(void *)) list_free, (int (*)(const void *)) list_len, (int (*)(const void *, int, void **))list_elAt, \
300  (int (*)(KSI_TLV *, void **)) fromTlv, (int (*)(KSI_CTX *, void *, unsigned, int, int, KSI_TLV **))toTlv, (descr), (parse_t)(parser), (p_opt), (int (*)(void *, KSI_OctetString *))(setRaw)}, \
301 
302 
311  #define KSI_TLV_PRIMITIVE_TEMPLATE_DEF(typ, tg, flg, gttr, sttr, descr) KSI_TLV_FULL_TEMPLATE_DEF(typ, tg, flg, gttr, sttr, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, descr, NULL, 0, NULL)
312 
317  #define KSI_DEFINE_TLV_TEMPLATE(name) const KSI_TlvTemplate name##_template[] = {
318 
330  #define KSI_TLV_OBJECT(tg, flg, gttr, sttr, fromTlv, toTlv, destr, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_OBJECT, tg, flg, gttr, sttr, NULL, destr, NULL, NULL, 0, NULL, NULL, NULL, NULL, fromTlv, toTlv, descr, NULL, 0, NULL)
331  #define KSI_TLV_WRAP_OBJECT(tg, flg, gttr, sttr, parser, toTlv, destr, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_OBJECT, tg, flg, gttr, sttr, NULL, destr, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, toTlv, descr, (parser), 0, NULL)
332  #define KSI_TLV_COMPOSITE_OBJECT(tg, flg, gttr, sttr, fromTlv, toTlv, destr, tmpl, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_OBJECT, tg, flg, gttr, sttr, NULL, destr, (tmpl), NULL, 0, NULL, NULL, NULL, NULL, fromTlv, toTlv, descr, NULL, 0, NULL)
333 
334 
343  #define KSI_TLV_UTF8_STRING(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT(tg, flg, gttr, sttr, KSI_Utf8String_fromTlv, KSI_Utf8String_toTlv, KSI_Utf8String_free, descr)
344 
353  #define KSI_TLV_INTEGER(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT(tg, flg, gttr, sttr, KSI_Integer_fromTlv, KSI_Integer_toTlv, KSI_Integer_free, descr)
354 
363  #define KSI_TLV_OCTET_STRING(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT(tg, flg, gttr, sttr, KSI_OctetString_fromTlv, KSI_OctetString_toTlv, KSI_OctetString_free, descr)
364 
373  #define KSI_TLV_IMPRINT(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT(tg, flg, gttr, sttr, KSI_DataHash_fromTlv, KSI_DataHash_toTlv, KSI_DataHash_free, descr)
374  #define KSI_TLV_WRAP_IMPRINT(tg, flg, gttr, sttr, descr) KSI_TLV_WRAP_OBJECT(tg, flg, gttr, sttr, KSI_DataHash_parse, KSI_DataHash_toTlv, KSI_DataHash_free, descr)
375 
379  #define KSI_TLV_TIME_S KSI_TLV_INTEGER
380  #define KSI_TLV_TIME_US KSI_TLV_INTEGER
381 
392  #define KSI_TLV_OBJECT_LIST(tg, flg, gttr, sttr, obj, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_OBJECT, tg, flg, gttr, sttr, NULL, obj##_free, NULL, KSI_List_append, 1, obj##List_new, obj##List_free, KSI_List_length, KSI_List_elementAt, obj##_fromTlv, obj##_toTlv, descr, NULL, 0, NULL)
393 
402  #define KSI_TLV_OCTET_STRING_LIST(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT_LIST(tg, flg, gttr, sttr, KSI_OctetString, descr)
403 
412  #define KSI_TLV_UTF8_STRING_LIST(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT_LIST(tg, flg, gttr, sttr, KSI_Utf8String, descr)
413 
422  #define KSI_TLV_INTEGER_LIST(tg, flg, gttr, sttr, descr) KSI_TLV_OBJECT_LIST(tg, flg, gttr, sttr, KSI_Integer, descr)
423 
433  #define KSI_TLV_COMPOSITE(tg, flg, gttr, sttr, sub, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_COMPOSITE, tg, flg, gttr, sttr, sub##_new, sub##_free, sub##_template, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, descr, NULL, 0, NULL)
434 
444  #define KSI_TLV_COMPOSITE_LIST(tg, flg, gttr, sttr, sub, descr) KSI_TLV_FULL_TEMPLATE_DEF(KSI_TLV_TEMPLATE_COMPOSITE, tg, flg, gttr, sttr, sub##_new, sub##_free, sub##_template, KSI_List_append, 1, sub##List_new, sub##List_free, KSI_List_length, KSI_List_elementAt, NULL, NULL, descr, NULL, 0, NULL)
445 
449  #define KSI_END_TLV_TEMPLATE { -1, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL}};
450 
460  int KSI_TlvTemplate_extract(KSI_CTX *ctx, void *payload, KSI_TLV *tlv, const KSI_TlvTemplate *tmpl);
461 
471  int KSI_TlvTemplate_parse(KSI_CTX *ctx, const unsigned char *raw, size_t raw_len, const KSI_TlvTemplate *tmpl, void *payload);
472 
485  int KSI_TlvTemplate_extractGenerator(KSI_CTX *ctx, void *payload, void *generatorCtx, const KSI_TlvTemplate *tmpl, int (*generator)(void *, KSI_TLV **));
486 
498  int KSI_TlvTemplate_construct(KSI_CTX *ctx, KSI_TLV *tlv, const void *payload, const KSI_TlvTemplate *tmpl);
499 
514  int KSI_TlvTemplate_serializeObject(KSI_CTX *ctx, const void *obj, unsigned tag, int isNc, int isFwd, const KSI_TlvTemplate *tmpl, unsigned char **raw, size_t *raw_len);
515 
530  int KSI_TlvTemplate_writeBytes(KSI_CTX *ctx, const void *obj, unsigned tag, int isNc, int isFwd, const KSI_TlvTemplate *tmpl, unsigned char *raw, size_t raw_size, size_t *raw_len, int opt);
531 
537  #define KSI_IMPLEMENT_OBJECT_PARSE(type, tag) \
538  int type##_parse(KSI_CTX *ctx, const unsigned char *raw, size_t len, type **t) { \
539  int res = KSI_UNKNOWN_ERROR; \
540  KSI_TLV *tlv = NULL; \
541  type *tmp = NULL; \
542  if (ctx == NULL || t == NULL) { \
543  res = KSI_INVALID_ARGUMENT; \
544  goto cleanup; \
545  } \
546  res = KSI_TLV_parseBlob2(ctx, (unsigned char *)raw, len, 0, &tlv); \
547  if (res != KSI_OK) goto cleanup; \
548  if (KSI_TLV_getTag(tlv) != (tag)) { \
549  res = KSI_INVALID_FORMAT; \
550  goto cleanup; \
551  } \
552  res = type##_new(ctx, &tmp); \
553  if (res != KSI_OK) goto cleanup; \
554  res = KSI_TlvTemplate_parse(ctx, raw, len, KSI_TLV_TEMPLATE(type), tmp); \
555  if (res != KSI_OK) goto cleanup; \
556  *t = tmp; \
557  tmp = NULL; \
558  res = KSI_OK; \
559  cleanup: \
560  KSI_TLV_free(tlv); \
561  type##_free(tmp); \
562  return res; \
563  } \
564 
565 
572  #define KSI_IMPLEMENT_OBJECT_SERIALIZE(type, tag, nc, fwd) \
573  int type##_serialize(const type *t, unsigned char **raw, size_t *len) { \
574  int res = KSI_UNKNOWN_ERROR; \
575  if (t == NULL || raw == NULL || len == NULL) { \
576  res = KSI_INVALID_ARGUMENT; \
577  goto cleanup; \
578  } \
579  res = KSI_TlvTemplate_serializeObject(t->ctx, t, (tag), (nc), (fwd), KSI_TLV_TEMPLATE(type), raw, len); \
580  if (res != KSI_OK) goto cleanup; \
581  res = KSI_OK; \
582  cleanup: \
583  return res; \
584  } \
585 
586  #define KSI_IMPLEMENT_WRITE_BYTES(typ, tag, nc, fwd) \
587  KSI_DEFINE_WRITE_BYTES(typ) { \
588  if (o == NULL) return KSI_INVALID_ARGUMENT; \
589  return KSI_TlvTemplate_writeBytes(o->ctx, (void *)o, tag, nc, fwd, KSI_TLV_TEMPLATE(typ), buf, buf_size, buf_len, opt); \
590  } \
591 
592 
596 #ifdef __cplusplus
597 }
598 #endif
599 
600 #endif /* KSI_TLV_TEMPLATE_H_ */
int KSI_TlvTemplate_parse(KSI_CTX *ctx, const unsigned char *raw, size_t raw_len, const KSI_TlvTemplate *tmpl, void *payload)
int(* cb_encode_t)(KSI_CTX *, KSI_TLV *, const void *, const KSI_TlvTemplate *)
Definition: tlv_template.h:63
int(* fromTlv)(KSI_TLV *tlv, void **)
Definition: tlv_template.h:144
int(* cb_decode_t)(KSI_CTX *ctx, const KSI_TLV *, void *, const KSI_TlvTemplate *)
Definition: tlv_template.h:58
int KSI_TlvTemplate_writeBytes(KSI_CTX *ctx, const void *obj, unsigned tag, int isNc, int isFwd, const KSI_TlvTemplate *tmpl, unsigned char *raw, size_t raw_size, size_t *raw_len, int opt)
unsigned flags
Definition: tlv_template.h:81
int KSI_TlvTemplate_extract(KSI_CTX *ctx, void *payload, KSI_TLV *tlv, const KSI_TlvTemplate *tmpl)
int parser_opt
Definition: tlv_template.h:158
int(* listLength)(const void *)
Definition: tlv_template.h:134
int(* construct)(KSI_CTX *, void **)
Definition: tlv_template.h:96
int(* listElementAt)(const void *, int, void **)
Definition: tlv_template.h:139
int(* parse_t)(KSI_CTX *, unsigned char *, size_t, int, void *)
Definition: tlv_template.h:65
getter_t getValue
Definition: tlv_template.h:86
int KSI_TlvTemplate_construct(KSI_CTX *ctx, KSI_TLV *tlv, const void *payload, const KSI_TlvTemplate *tmpl)
unsigned tag
Definition: tlv_template.h:79
int(* setRaw)(void *, KSI_OctetString *)
Definition: tlv_template.h:160
const KSI_TlvTemplate * subTemplate
Definition: tlv_template.h:107
struct KSI_OctetString_st KSI_OctetString
Definition: types_base.h:145
int(* getter_t)(const void *, void **)
Definition: tlv_template.h:48
parse_t parser
Definition: tlv_template.h:157
int KSI_TlvTemplate_extractGenerator(KSI_CTX *ctx, void *payload, void *generatorCtx, const KSI_TlvTemplate *tmpl, int(*generator)(void *, KSI_TLV **))
void(* listFree)(void *)
Definition: tlv_template.h:129
void(* destruct)(void *)
Definition: tlv_template.h:101
int(* listNew)(void **)
Definition: tlv_template.h:123
const char * descr
Definition: tlv_template.h:154
struct KSI_CTX_st KSI_CTX
Definition: types_base.h:124
int KSI_TlvTemplate_serializeObject(KSI_CTX *ctx, const void *obj, unsigned tag, int isNc, int isFwd, const KSI_TlvTemplate *tmpl, unsigned char **raw, size_t *raw_len)
struct KSI_TLV_st KSI_TLV
Definition: types_base.h:129
int(* toTlv)(KSI_CTX *, void *, unsigned, int, int, KSI_TLV **tlv)
Definition: tlv_template.h:149
Definition: tlv_template.h:70
int type
Definition: tlv_template.h:74
int multiple
Definition: tlv_template.h:118
setter_t setValue
Definition: tlv_template.h:91
int(* listAppend)(void *, void *)
Definition: tlv_template.h:112
int(* setter_t)(void *, void *)
Definition: tlv_template.h:53