aboutsummaryrefslogtreecommitdiff
path: root/src/cbor_reader.c
blob: 035a0bcf99cc9c21bdb4ac947ee87b08ec8e219b (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "dice/cbor_reader.h"

enum CborType {
  CBOR_TYPE_UINT = 0,
  CBOR_TYPE_NINT = 1,
  CBOR_TYPE_BSTR = 2,
  CBOR_TYPE_TSTR = 3,
  CBOR_TYPE_ARRAY = 4,
  CBOR_TYPE_MAP = 5,
  CBOR_TYPE_TAG = 6,
  CBOR_TYPE_SIMPLE = 7,
};

static bool CborReadWouldOverflow(size_t size, struct CborIn* in) {
  return size > SIZE_MAX - in->cursor || in->cursor + size > in->buffer_size;
}

static enum CborReadResult CborPeekIntialValueAndArgument(struct CborIn* in,
                                                          uint8_t* size,
                                                          enum CborType* type,
                                                          uint64_t* val) {
  uint8_t initial_byte;
  uint8_t additional_information;
  uint64_t value;
  uint8_t bytes = 1;
  if (CborInAtEnd(in)) {
    return CBOR_READ_RESULT_END;
  }
  initial_byte = in->buffer[in->cursor];
  *type = initial_byte >> 5;
  additional_information = initial_byte & 0x1f;
  if (additional_information <= 23) {
    value = additional_information;
  } else if (additional_information <= 27) {
    bytes += 1 << (additional_information - 24);
    if (CborReadWouldOverflow(bytes, in)) {
      return CBOR_READ_RESULT_END;
    }
    value = 0;
    if (bytes == 2) {
      value |= in->buffer[in->cursor + 1];
    } else if (bytes == 3) {
      value |= (uint16_t)in->buffer[in->cursor + 1] << 8;
      value |= (uint16_t)in->buffer[in->cursor + 2];
    } else if (bytes == 5) {
      value |= (uint32_t)in->buffer[in->cursor + 1] << 24;
      value |= (uint32_t)in->buffer[in->cursor + 2] << 16;
      value |= (uint32_t)in->buffer[in->cursor + 3] << 8;
      value |= (uint32_t)in->buffer[in->cursor + 4];
    } else if (bytes == 9) {
      value |= (uint64_t)in->buffer[in->cursor + 1] << 56;
      value |= (uint64_t)in->buffer[in->cursor + 2] << 48;
      value |= (uint64_t)in->buffer[in->cursor + 3] << 40;
      value |= (uint64_t)in->buffer[in->cursor + 4] << 32;
      value |= (uint64_t)in->buffer[in->cursor + 5] << 24;
      value |= (uint64_t)in->buffer[in->cursor + 6] << 16;
      value |= (uint64_t)in->buffer[in->cursor + 7] << 8;
      value |= (uint64_t)in->buffer[in->cursor + 8];
    }
  } else {
    // Indefinite lengths and reserved values are not supported.
    return CBOR_READ_RESULT_MALFORMED;
  }
  *val = value;
  *size = bytes;
  return CBOR_READ_RESULT_OK;
}

static enum CborReadResult CborReadSize(struct CborIn* in, enum CborType type,
                                        size_t* size) {
  uint8_t bytes;
  enum CborType in_type;
  uint64_t raw;
  enum CborReadResult res =
      CborPeekIntialValueAndArgument(in, &bytes, &in_type, &raw);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (in_type != type) {
    return CBOR_READ_RESULT_NOT_FOUND;
  }
  if (raw > SIZE_MAX) {
    return CBOR_READ_RESULT_MALFORMED;
  }
  *size = raw;
  in->cursor += bytes;
  return CBOR_READ_RESULT_OK;
}

static enum CborReadResult CborReadStr(struct CborIn* in, enum CborType type,
                                       size_t* data_size,
                                       const uint8_t** data) {
  size_t size;
  struct CborIn peeker = *in;
  enum CborReadResult res = CborReadSize(&peeker, type, &size);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (CborReadWouldOverflow(size, &peeker)) {
    return CBOR_READ_RESULT_END;
  }
  *data_size = size;
  *data = &in->buffer[peeker.cursor];
  in->cursor = peeker.cursor + size;
  return CBOR_READ_RESULT_OK;
}

static enum CborReadResult CborReadSimple(struct CborIn* in, uint8_t val) {
  uint8_t bytes;
  enum CborType type;
  uint64_t raw;
  enum CborReadResult res =
      CborPeekIntialValueAndArgument(in, &bytes, &type, &raw);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (type != CBOR_TYPE_SIMPLE || raw != val) {
    return CBOR_READ_RESULT_NOT_FOUND;
  }
  in->cursor += bytes;
  return CBOR_READ_RESULT_OK;
}

enum CborReadResult CborReadInt(struct CborIn* in, int64_t* val) {
  uint8_t bytes;
  enum CborType type;
  uint64_t raw;
  enum CborReadResult res =
      CborPeekIntialValueAndArgument(in, &bytes, &type, &raw);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (type != CBOR_TYPE_UINT && type != CBOR_TYPE_NINT) {
    return CBOR_READ_RESULT_NOT_FOUND;
  }
  if (raw > INT64_MAX) {
    return CBOR_READ_RESULT_MALFORMED;
  }
  *val = (type == CBOR_TYPE_NINT) ? (-1 - (int64_t)raw) : (int64_t)raw;
  in->cursor += bytes;
  return CBOR_READ_RESULT_OK;
}

enum CborReadResult CborReadUint(struct CborIn* in, uint64_t* val) {
  uint8_t bytes;
  enum CborType type;
  enum CborReadResult res =
      CborPeekIntialValueAndArgument(in, &bytes, &type, val);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (type != CBOR_TYPE_UINT) {
    return CBOR_READ_RESULT_NOT_FOUND;
  }
  in->cursor += bytes;
  return CBOR_READ_RESULT_OK;
}

enum CborReadResult CborReadBstr(struct CborIn* in, size_t* data_size,
                                 const uint8_t** data) {
  return CborReadStr(in, CBOR_TYPE_BSTR, data_size, data);
}

enum CborReadResult CborReadTstr(struct CborIn* in, size_t* size,
                                 const char** str) {
  return CborReadStr(in, CBOR_TYPE_TSTR, size, (const uint8_t**)str);
}

enum CborReadResult CborReadArray(struct CborIn* in, size_t* num_elements) {
  return CborReadSize(in, CBOR_TYPE_ARRAY, num_elements);
}

enum CborReadResult CborReadMap(struct CborIn* in, size_t* num_pairs) {
  return CborReadSize(in, CBOR_TYPE_MAP, num_pairs);
}

enum CborReadResult CborReadTag(struct CborIn* in, uint64_t* tag) {
  uint8_t bytes;
  enum CborType type;
  enum CborReadResult res =
      CborPeekIntialValueAndArgument(in, &bytes, &type, tag);
  if (res != CBOR_READ_RESULT_OK) {
    return res;
  }
  if (type != CBOR_TYPE_TAG) {
    return CBOR_READ_RESULT_NOT_FOUND;
  }
  in->cursor += bytes;
  return CBOR_READ_RESULT_OK;
}

enum CborReadResult CborReadFalse(struct CborIn* in) {
  return CborReadSimple(in, /*val=*/20);
}

enum CborReadResult CborReadTrue(struct CborIn* in) {
  return CborReadSimple(in, /*val=*/21);
}

enum CborReadResult CborReadNull(struct CborIn* in) {
  return CborReadSimple(in, /*val=*/22);
}

enum CborReadResult CborReadSkip(struct CborIn* in) {
  struct CborIn peeker = *in;
  size_t size_stack[CBOR_READ_SKIP_STACK_SIZE];
  size_t stack_size = 0;

  size_stack[stack_size++] = 1;

  while (stack_size > 0) {
    // Get the type
    uint8_t bytes;
    enum CborType type;
    uint64_t val;
    enum CborReadResult res;

    res = CborPeekIntialValueAndArgument(&peeker, &bytes, &type, &val);
    if (res != CBOR_READ_RESULT_OK) {
      return res;
    }

    if (CborReadWouldOverflow(bytes, &peeker)) {
      return CBOR_READ_RESULT_END;
    }
    peeker.cursor += bytes;

    if (--size_stack[stack_size - 1] == 0) {
      --stack_size;
    }

    switch (type) {
      case CBOR_TYPE_UINT:
      case CBOR_TYPE_NINT:
      case CBOR_TYPE_SIMPLE:
        continue;
      case CBOR_TYPE_BSTR:
      case CBOR_TYPE_TSTR:
        if (CborReadWouldOverflow(val, &peeker)) {
          return CBOR_READ_RESULT_END;
        }
        peeker.cursor += val;
        continue;
      case CBOR_TYPE_MAP:
        if (val > UINT64_MAX / 2) {
          return CBOR_READ_RESULT_END;
        }
        val *= 2;
        break;
      case CBOR_TYPE_TAG:
        val = 1;
        break;
      case CBOR_TYPE_ARRAY:
        break;
      default:
        return CBOR_READ_RESULT_MALFORMED;
    }

    // Push a new level of nesting to the stack.
    if (val == 0) {
      continue;
    }
    if (stack_size == CBOR_READ_SKIP_STACK_SIZE) {
      return CBOR_READ_RESULT_MALFORMED;
    }
    size_stack[stack_size++] = val;
  }

  in->cursor = peeker.cursor;
  return CBOR_READ_RESULT_OK;
}