blob: cfecc4dce1be5912f2baf3a953c5bde92d14d9b8 [file] [log] [blame]
Jeff Thompsonc8963652013-06-28 20:17:43 -07001/*
2 * Author: Jeff Thompson
3 *
4 * BSD license, See the LICENSE file for more information.
5 */
6
Jeff Thompson590b8692013-06-28 22:07:41 -07007#include "BinaryXML.h"
Jeff Thompsonc8963652013-06-28 20:17:43 -07008#include "BinaryXMLEncoder.h"
Jeff Thompson590b8692013-06-28 22:07:41 -07009
10enum {
11 ENCODING_LIMIT_1_BYTE = ((1 << ndn_BinaryXML_TT_VALUE_BITS) - 1),
12 ENCODING_LIMIT_2_BYTES = ((1 << (ndn_BinaryXML_TT_VALUE_BITS + ndn_BinaryXML_REGULAR_VALUE_BITS)) - 1),
13 ENCODING_LIMIT_3_BYTES = ((1 << (ndn_BinaryXML_TT_VALUE_BITS + 2 * ndn_BinaryXML_REGULAR_VALUE_BITS)) - 1)
14};
15
16/**
17 * Return the number of bytes to encode a header of value x.
18 */
19static unsigned int getNEncodingBytes(unsigned int x)
20{
21 // Do a quick check for pre-compiled results.
22 if (x <= ENCODING_LIMIT_1_BYTE)
23 return 1;
24 if (x <= ENCODING_LIMIT_2_BYTES)
25 return 2;
26 if (x <= ENCODING_LIMIT_3_BYTES)
27 return 3;
28
29 unsigned int nBytes = 1;
30
31 // Last byte gives you TT_VALUE_BITS.
32 // Remainder each gives you REGULAR_VALUE_BITS.
33 x >>= ndn_BinaryXML_TT_VALUE_BITS;
34 while (x != 0) {
35 ++nBytes;
36 x >>= ndn_BinaryXML_REGULAR_VALUE_BITS;
37 }
38
39 return nBytes;
40}