aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/codec/ccnxCodec_TlvDecoder.h
blob: 39cdcb946ce71dc424ae246e238e9596e6653866 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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:
 *
 *     http://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.
 */

/**
 * @file ccnxCodec_TlvDecoder.h
 * @ingroup networking
 * @brief TLV codec for messages
 *
 * TLV decoder
 *
 * Terminology:
 *   type     = a field that labels a value
 *   length   = the byte lenth of the value
 *   value    = the data
 *   header   = type + length
 *   container= a value that contains TLVs
 *
 * For example, in this structure, the "type 1" TLV is a container that holds a second TLV
 * The second TLV is a terminal, and holds an opaque value.
 *
 *  { .type = 1, .length = 20, .value = { .type = 2, .length = 16, .value ="It was a dark a " } }
 *
 * To decode the above example, we would use the decoder like this:
 *
 * @code
 * {
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(container);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      if (type == 3) {
 *          size_t end = ccnxCodecTlvDecoder_GetPosition(decoder) + length;
 *          while ( ccnxCodecTlvDecoder_GetPosition(decoder) < end ) {
 *              type = ccnxCodecTlvDecoder_GetType(decoder);
 *              length = ccnxCodecTlvDecoder_GetLength(decoder);
 *              if (type == 1) {
 *                  PARCBuffer *name = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use name, then release it
 *              } else if (type == 2) {
 *                  PARCBuffer *address = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use address, then release it
 *              }
 *          }
 *      }
 *      PARCReadOnlyBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 * }
 * @endcode
 *
 * Another way to do the same parsing without having to use `ccnxCodecTlvDecoder_GetPosition' is to
 * recursively parse each value:
 *
 * @code
 * {
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(container);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      PARCBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *      if (type == 3) {
 *          CCNxCodecTlvDecoder *innerDecoder = ccnxCodecTlvDecoder_Create(value);
 *          while ( ! ccnxCodecTlvDecoder_IsEmpty(innerDecoder) ) {
 *              type = ccnxCodecTlvDecoder_GetType(decoder);
 *              length = ccnxCodecTlvDecoder_GetLength(decoder);
 *              if (type == 1) {
 *                  PARCBuffer *name = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use name, then release it
 *              } else if (type == 2) {
 *                  PARCBuffer *address = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use address, then release it
 *              }
 *          }
 *          ccnxCodecTlvDecoder_Destroy(&innerDecoder);
 *      }
 *      PARCReadOnlyBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 * }
 * @endcode
 *
 * And an even cleaner way is to use ccnxCodecTlvDecoder_GetContainer to pull out sub-decoders
 *
 * @code
 * {
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(container);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      if (type == 3) {
 *          CCNxCodecTlvDecoder *innerDecoder = ccnxCodecTlvDecoder_GetContainer(decoder, length);
 *          while ( ! ccnxCodecTlvDecoder_IsEmpty(innerDecoder) ) {
 *              type = ccnxCodecTlvDecoder_GetType(decoder);
 *              length = ccnxCodecTlvDecoder_GetLength(decoder);
 *              if (type == 1) {
 *                  PARCBuffer *name = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use name, then release it
 *              } else if (type == 2) {
 *                  PARCBuffer *address = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *                  // use address, then release it
 *              }
 *          }
 *          ccnxCodecTlvDecoder_Destroy(&innerDecoder);
 *      }
 *      PARCReadOnlyBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 * }
 * @endcode
 *
 */

#ifndef libccnx_ccnx_TlvDecoder_h
#define libccnx_ccnx_TlvDecoder_h

#include <parc/algol/parc_Buffer.h>
#include <ccnx/common/codec/ccnxCodec_NetworkBuffer.h>
#include <parc/security/parc_Signer.h>
#include <parc/security/parc_Signature.h>

#include <ccnx/common/codec/ccnxCodec_Error.h>


struct ccnx_codec_tlv_decoder;
typedef struct ccnx_codec_tlv_decoder CCNxCodecTlvDecoder;

/**
 * Decodes a TLV-encoded buffer to individual buffers for each Value
 *
 * Walks through a TLV-encoded buffer returning buffer slices of the
 * original.  These are 0-copy operations.
 *
 * The decoder should be based on the CCNxCodecNetworkBufferIoVec, see case 1009
 *
 * @param [in] buffer The buffer to parse, must be ready to read.
 *
 * @return non-null A TLV decoder
 * @return null An error
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      ccnxCodecTlvDecoder_Destroy(&decoder);
 * }
 * @endcode
 */
CCNxCodecTlvDecoder *ccnxCodecTlvDecoder_Create(PARCBuffer *buffer);

/**
 * Releases the tlv decoder.
 *
 * Buffers that have been previously returned remain acquired.  The internal
 * reference to the input buffer will be released.
 *
 * @param [in] decoderPtr Pointer to the decoder to destroy
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      ccnxCodecTlvDecoder_Destroy(&decoder);
 * }
 * @endcode
 */
void ccnxCodecTlvDecoder_Destroy(CCNxCodecTlvDecoder **decoderPtr);

/**
 * Tests if there is anything left to decode
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return true There are more bytes available
 * @return false At the end of the buffer
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool ccnxCodecTlvDecoder_IsEmpty(CCNxCodecTlvDecoder *decoder);

/**
 * Checks if there are at least `bytes' bytes remaining in the buffer
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return true There are at least `bytes' bytes left
 * @return false Buffer underrun
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool ccnxCodecTlvDecoder_EnsureRemaining(CCNxCodecTlvDecoder *decoder, size_t bytes);

/**
 * Returns the bytes remaining in the decoder
 *
 * The remaining bytes to be decoded
 *
 * @param [in] decoder An allocated decoder
 *
 * @retval number The bytes left to decode
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecTlvDecoder_Remaining(const CCNxCodecTlvDecoder *decoder);

/**
 * Gets the next bytes as the TLV type
 *
 * The buffer is advanced the width of the type
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * {
 *      // GetType                                       |--------|
 *      // GetLength                                                 |--------|
 *      // GetValue                                                              |--------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      PARCBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *      // value = 0x01020304
 * }
 * @endcode
 */
uint16_t ccnxCodecTlvDecoder_GetType(CCNxCodecTlvDecoder *decoder);

/**
 * Returns the TLV Type but does not advance the decoder
 *
 * At the current position, decode the next bytes as the TLV type
 *
 * @param [in] decoder The Decoder object
 *
 * @return number The TLV type
 *
 * Example:
 * @code
 * {
 *      // PeekType                                      |--------|
 *      // GetType                                       |--------|
 *      // GetLength                                                 |--------|
 *      // GetValue                                                              |--------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      unsigned type   = ccnxCodecTlvDecoder_PeekType(decoder);
 *      if( type == 0xAABB ) {
 *         (void) ccnxCodecTlvDecoder_GetType(decoder);
 *         unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *          PARCBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *          // value = 0x01020304
 *      }
 * }
 * @endcode
 */
uint16_t ccnxCodecTlvDecoder_PeekType(CCNxCodecTlvDecoder *decoder);

/**
 * Gets the next bytes as the TLV length
 *
 * The buffer is advanced the width of the length
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * {
 *      // GetType                                       |--------|
 *      // GetLength                                                 |--------|
 *      // GetValue                                                              |--------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      PARCBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *      // value = 0x01020304
 * }
 * @endcode
 */
uint16_t ccnxCodecTlvDecoder_GetLength(CCNxCodecTlvDecoder *decoder);

/**
 * Returns the next `length' bytes as a value
 *
 * The buffer is advanced `length' bytes.  The returned value is ready for reading.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * {
 *      // GetType                                       |--------|
 *      // GetLength                                                 |--------|
 *      // GetValue                                                              |--------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *      unsigned type   = ccnxCodecTlvDecoder_GetType(decoder);
 *      unsigned length = ccnxCodecTlvDecoder_GetLength(decoder);
 *      PARCBuffer * value = ccnxCodecTlvDecoder_GetValue(decoder, length);
 *      // value = 0x01020304
 * }
 * @endcode
 */
PARCBuffer *ccnxCodecTlvDecoder_GetValue(CCNxCodecTlvDecoder *decoder, uint16_t length);

/**
 * Ensure the current position is of type `type', then return a buffer of the value
 *
 * If the buffer points to a type of `type', the function will create a buffer of
 * the specified length and return the value in a buffer.
 *
 * The function will return NULL if the types don't match or if there is a
 * a decoder underrun (its not as long as the type specifies), or if the length would
 * take the end of the input buffer.
 *
 * @param [in] decoder The TLV decoder object
 * @param [in] type The type type to validate
 *
 * @return non-null A conforming buffer
 * @return null An error
 *
 * Example:
 * @code
 * {
 *      // GetBuffer                                     |--------------------------------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 8, 0, 8);
 *
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *
 *      PARCBuffer *buffer = ccnxCodecTlvDecoder_GetBuffer(decoder, 0xAABB);
 *      // buffer contains { 0x01, 0x02, 0x03, 0x04 }
 * }
 * @endcode
 */
PARCBuffer *ccnxCodecTlvDecoder_GetBuffer(CCNxCodecTlvDecoder *decoder, uint16_t type);

/**
 * The current location is a TLV container (a value that is TLVs)
 *
 * Returns a TLV decoder that represents the "slice" of the input buffer from
 * the current position up to the current position plus `length'.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-null A new sub-decoder
 * @return null An error, such as input underrun
 *
 * Example:
 * @code
 * {
 *      // GetType                                       |--------|
 *      // GetLength                                                 |--------|
 *      // GetContainer                                                          |--------------------------------------------|
 *      // GetBuffer                                                             |--------------------------------------------|
 *      PARCBuffer *input = parcBuffer_Wrap((uint8_t[]) {0x00, 0x01, 0x00, 0x08, 0xAA, 0xBB, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04}, 12, 0, 12);
 *
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(input);
 *
 *      uint16_t containerType = ccnxCodecTlvDecoder_GetType(decoder);
 *      size_t containerLength = ccnxCodecTlvDecoder_GetLength(decoder);
 *      CCNxCodecTlvDecoder *innerDecoder = ccnxCodecTlvDecoder_GetContainer(decoder, containerLength);
 *      PARCBuffer *buffer = ccnxCodecTlvDecoder_GetBuffer(decoder, 0xAABB);
 *
 *      ccnxCodecTlvDecoder_Destroy(&innerDecoder);
 *      ccnxCodecTlvDecoder_Destroy(&decoder);
 *
 *      // buffer contains { 0x01, 0x02, 0x03, 0x04 }
 * }
 * @endcode
 */
CCNxCodecTlvDecoder *ccnxCodecTlvDecoder_GetContainer(CCNxCodecTlvDecoder *decoder, uint16_t length);

/**
 * Decodes the current location as a type, length, and uint8_t value.
 *
 * Ensures the type is `type' and returns the value as a uint8_t.  If the type
 * does not match or there is buffer underflow, the function will return false and
 * the output will be unchanged.  If the TLV length is not "1", it will also return false.
 * Otherwise, it returns true and the decoded value.
 *
 * On success, the decoder is advanced, on failure the decoder will remain at the
 * current position.
 *
 * @param [in] decoder The decoder object
 * @param [in] type The TLV type to validate
 * @param [out] output The output value
 *
 * @return true output parameter is valid
 * @return false on error (type did not match or buffer underflow)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x20, 0x00, 0x01, 0xFF }, 5, 0, 5 );
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(buffer);
 *      uint8_t value;
 *      bool success = ccnxCodecTlvDecoder_GetUint8(decoder, 0x1020, &value);
 *      assert(success && value == 0xFF);
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_GetUint8(CCNxCodecTlvDecoder *decoder, uint16_t type, uint8_t *output);

/**
 * Decodes the current location as a type, length, and uint16_t value.
 *
 * Ensures the type is `type' and returns the value as a uint16_t.  If the type
 * does not match or there is buffer underflow, the function will return false and
 * the output will be unchanged.  If the TLV length is not "2", it will also return false.
 * Otherwise, it returns true and the decoded value.
 *
 * On success, the decoder is advanced, on failure the decoder will remain at the
 * current position.
 *
 * @param [in] decoder The decoder object
 * @param [in] type The TLV type to validate
 * @param [out] output The output value
 *
 * @return true output parameter is valid
 * @return false on error (type did not match or buffer underflow)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x21, 0x00, 0x02, 0xFF, 0xAA }, 6, 0, 6 );
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(buffer);
 *      uint16_t value;
 *      bool success = ccnxCodecTlvDecoder_GetUint16(decoder, 0x1021, &value);
 *      assert(success && value == 0xFFAA);
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_GetUint16(CCNxCodecTlvDecoder *decoder, uint16_t type, uint16_t *output);

/**
 * Decodes the current location as a type, length, and uint32_t value.
 *
 * Ensures the type is `type' and returns the value as a uint32_t.  If the type
 * does not match or there is buffer underflow, the function will return false and
 * the output will be unchanged.  If the TLV length is not "4", it will also return false.
 * Otherwise, it returns true and the decoded value.
 *
 * On success, the decoder is advanced, on failure the decoder will remain at the
 * current position.
 *
 * @param [in] decoder The decoder object
 * @param [in] type The TLV type to validate
 * @param [out] output The output value
 *
 * @return true output parameter is valid
 * @return false on error (type did not match or buffer underflow)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x22, 0x00, 0x04, 0xFF, 0xAA, 0xBB, 0xCC }, 8, 0, 8 );
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(buffer);
 *      uint32_t value;
 *      bool success = ccnxCodecTlvDecoder_GetUint32(decoder, 0x1022, &value);
 *      assert(success && value == 0xFFAABBCC);
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_GetUint32(CCNxCodecTlvDecoder *decoder, uint16_t type, uint32_t *output);

/**
 * Decodes the current location as a type, length, and uint64_t value.
 *
 * Ensures the type is `type' and returns the value as a uint64_t.  If the type
 * does not match or there is buffer underflow, the function will return false and
 * the output will be unchanged.  If the TLV length is not "8", it will also return false.
 * Otherwise, it returns true and the decoded value.
 *
 * On success, the decoder is advanced, on failure the decoder will remain at the
 * current position.
 *
 * @param [in] decoder The decoder object
 * @param [in] type The TLV type to validate
 * @param [out] output The output value
 *
 * @return true output parameter is valid
 * @return false on error (type did not match or buffer underflow)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x23, 0x00, 0x08, 0xFF, 0xAA, 0xBB, 0xCC, 0x00, 0x00, 0x00, 0x00 }, 12, 0, 12 );
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(buffer);
 *      uint64_t value;
 *      bool success = ccnxCodecTlvDecoder_GetUint64(decoder, 0x1023, &value);
 *      assert(success && value == 0xFFAABBCC00000000ULL);
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_GetUint64(CCNxCodecTlvDecoder *decoder, uint16_t type, uint64_t *output);

/**
 * Returns the current byte position of the buffer
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
size_t ccnxCodecTlvDecoder_Position(CCNxCodecTlvDecoder *decoder);

/**
 * Advance the decoder a given number of bytes
 *
 * Advance the decoder, throwing away a given number of bytes.
 * If there are not enough bytes left in the decoder, no action is taken
 *
 * @param [in] decoder The decoder to advance
 * @param [in] length The number of bytes to skip
 *
 * @return true Advanced the buffer
 * @return false Error, likely a buffer underrun (not enough bytes)
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool ccnxCodecTlvDecoder_Advance(CCNxCodecTlvDecoder *decoder, uint16_t length);

/**
 * Decode the current position as a VarInt
 *
 * A VarInt may be 1 to 8 bytes long.  It is interpreted as an unsigned
 * integer in network byte order.
 *
 * @param [in] decoder The TLV decoder
 * @param [in] length The number of bytes in the varint
 * @param [out] output The value of the varint
 *
 * @return true Successful decode
 * @return fale Error (length too long, too short, or not enough bytes in decoder)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x23, 0x00 }, 3, 0, 3 );
 *      CCNxCodecTlvDecoder *decoder = ccnxCodecTlvDecoder_Create(buffer);
 *      uint64_t value;
 *      ccnxCodecTlvDecoder_GetVarInt(decoder, 3, &value);
 *      // value = 0x0000000000102300
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_GetVarInt(CCNxCodecTlvDecoder *decoder, uint16_t length, uint64_t *output);


/**
 * Decode the current position of the Buffer as a VarInt out to 'length' bytes
 *
 * A VarInt may be 1 to 8 bytes long.  It is interpreted as an unsigned
 * integer in network byte order.  The buffer must have at least 'length' bytes remaining.
 * The buffer is advanced.
 *
 * @param [in] decoder The TLV decoder
 * @param [in] length The number of bytes in the varint
 * @param [out] output The value of the varint
 *
 * @return true Successful decode
 * @return fale Error (length too long, too short, or not enough bytes in decoder)
 *
 * Example:
 * @code
 * {
 *      PARCBuffer *buffer = parcBuffer_Wrap((uint8_t[]) { 0x10, 0x23, 0x00 }, 3, 0, 3 );
 *      uint64_t value;
 *      ccnxCodecTlvDecoder_BufferToVarInt(buffer, 3, &value);
 *      // value = 0x0000000000102300
 * }
 * @endcode
 */
bool ccnxCodecTlvDecoder_BufferToVarInt(PARCBuffer *buffer, uint16_t length, uint64_t *output);

/**
 * Determines if the TLV Encoder has an error condition set
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool ccnxCodecTlvDecoder_HasError(const CCNxCodecTlvDecoder *decoder);

/**
 * Sets an error condition.  Only one error condition may be set.
 *
 * Stores a reference counted copy of the CCNxCodecError.  If an error is already set,
 * this function returns false and does not store a reference count.  The previous error
 * stays as the current error.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return true Error condition set
 * @return false Error already set, you must clear it first
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
bool ccnxCodecTlvDecoder_SetError(CCNxCodecTlvDecoder *decoder, CCNxCodecError *error);

/**
 * Clears the error condition, if any
 *
 * <#Paragraphs Of Explanation#>
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return <#value#> <#explanation#>
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
void ccnxCodecTlvDecoder_ClearError(CCNxCodecTlvDecoder *decoder);

/**
 * Retrieves the error message
 *
 * Retrieves the error condition, if any.  If no error is set, will return NULL.
 *
 * @param [<#in out in,out#>] <#name#> <#description#>
 *
 * @return non-null The error condition set
 * @return null No error condition is set
 *
 * Example:
 * @code
 * <#example#>
 * @endcode
 */
CCNxCodecError *ccnxCodecTlvDecoder_GetError(const CCNxCodecTlvDecoder *encoder);
#endif // libccnx_ccnx_TlvDecoder_h