Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Author: Jeff Thompson |
| 3 | * |
| 4 | * BSD license, See the LICENSE file for more information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 7 | #include "BinaryXML.h" |
Jeff Thompson | c896365 | 2013-06-28 20:17:43 -0700 | [diff] [blame] | 8 | #include "BinaryXMLEncoder.h" |
Jeff Thompson | 590b869 | 2013-06-28 22:07:41 -0700 | [diff] [blame] | 9 | |
| 10 | enum { |
| 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 | */ |
| 19 | static 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 | } |