summaryrefslogtreecommitdiff
path: root/include/ecbor.h
blob: a4aed39b5ec1c0658356ecb1f9b649346273d80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (c) 2018 Vasile Vilvoiu <vasi.vilvoiu@gmail.com>
 *
 * libecbor is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#ifndef _ECBOR_H_
#define _ECBOR_H_

#include <stdint.h>

/*
 * Error codes
 */
typedef enum {
  /* no error */
  ECBOR_OK                                  = 0,
  ECBOR_ERR_UNKNOWN                         = 1,    /* shouldn't happen, hopefully */
  
  /* internal (i.e. misuse) errors */
  ECBOR_ERR_NULL_CONTEXT                    = 10,
  ECBOR_ERR_NULL_INPUT_BUFFER               = 11,
  ECBOR_ERR_NULL_OUTPUT_BUFFER              = 12,
  ECBOR_ERR_NULL_ITEM_BUFFER                = 13,
  
  ECBOR_ERR_NULL_ITEM                       = 20,
  
  ECBOR_ERR_WRONG_MODE                      = 30,
  
  /* bounds errors */
  ECBOR_ERR_INVALID_END_OF_BUFFER           = 50,
  ECBOR_ERR_END_OF_NODE_BUFFER              = 51,
  
  /* syntax errors */
  ECBOR_ERR_CURRENTLY_NOT_SUPPORTED         = 100,
  ECBOR_ERR_INVALID_ADDITIONAL              = 101,
  ECBOR_ERR_INVALID_CHUNK_MAJOR_TYPE        = 102,
  ECBOR_ERR_NESTET_INDEFINITE_STRING        = 103,
  ECBOR_ERR_INVALID_KEY_VALUE_PAIR          = 104,
  ECBOR_ERR_INVALID_STOP_CODE               = 105,
  
  /* control codes */
  ECBOR_END_OF_BUFFER                       = 200,
  ECBOR_END_OF_INDEFINITE                   = 201,
} ecbor_error_t;

/*
 * Implementation limits
 */

/*
 * CBOR major types
 */
typedef enum {
  ECBOR_MT_UNDEFINED  = -1,
  ECBOR_MT_UINT       = 0,
  ECBOR_MT_NINT       = 1,
  ECBOR_MT_BSTR       = 2,
  ECBOR_MT_STR        = 3,
  ECBOR_MT_ARRAY      = 4,
  ECBOR_MT_MAP        = 5,
  ECBOR_MT_TAG        = 6,
  ECBOR_MT_SPECIAL    = 7,
} ecbor_major_type_t;

/*
 * CBOR Item
 */
typedef struct {
  /* major type of item */
  ecbor_major_type_t major_type;
  
  /* value */
  union {
    uint64_t uinteger;
    int64_t integer;
    const uint8_t *string;
    const uint8_t *items;
  } value;
  
  /* storage size of value, in bytes */
  uint64_t size;
  
  /* number of chunks in value, in case it is indefinite */
  uint64_t n_chunks;
  
  /* non-zero if size is indefinite (for strings, maps and arrays) */
  uint8_t is_indefinite;
} ecbor_item_t;

/*
 * CBOR tree node
 */
typedef struct ecbor_node ecbor_node_t;
struct ecbor_node {
  ecbor_item_t item;
  ecbor_node_t *parent;
  ecbor_node_t *next; /* next in array or map */
  uint64_t index; /* index in array or map */
};

/*
 * Mode
 */
typedef enum {
  ECBOR_MODE_DECODE           = 0,
  ECBOR_MODE_DECODE_STREAMED  = 1,
  ECBOR_MODE_DECODE_TREE      = 2,
  ECBOR_MODE_ENCODE           = 3
} ecbor_mode_t;

/*
 * CBOR parsing context
 */
typedef struct {
  /* mode of operation for this context */
  ecbor_mode_t mode;
  
  /* output buffer position */
  uint8_t *out_position;
  
  /* remaining bytes */
  unsigned int bytes_left;
} ecbor_encode_context_t;
 
typedef struct {
  /* mode of operation for this context */
  ecbor_mode_t mode;
  
  /* input buffer position */
  const uint8_t *in_position;
  
  /* remaining bytes */
  uint64_t bytes_left;

  /* node buffer */
  ecbor_node_t *nodes;
  
  /* capacity of item buffer (in items) */
  unsigned int node_capacity;
  
  /* number of used items so far */
  unsigned int n_nodes;
} ecbor_decode_context_t;


/*
 * Initialization routines
 */
extern ecbor_error_t
ecbor_initialize_decode (ecbor_decode_context_t *context,
                         const uint8_t *buffer,
                         uint64_t buffer_size);

extern ecbor_error_t
ecbor_initialize_decode_streamed (ecbor_decode_context_t *context,
                                  const uint8_t *buffer,
                                  uint64_t buffer_size);

extern ecbor_error_t
ecbor_initialize_decode_tree (ecbor_decode_context_t *context,
                              const uint8_t *buffer,
                              uint64_t buffer_size,
                              ecbor_node_t *node_buffer,
                              uint64_t node_capacity);


/*
 * Decoding routines
 */
extern ecbor_error_t
ecbor_decode (ecbor_decode_context_t *context, ecbor_item_t *item);

/*
 * Encoding routines
 */

#endif