[RFC 7541] 5.2. String Literal Representation return content from String Data (Length octets) with huffman decoding if it is encoded
| 96 | // return content from String Data (Length octets) with huffman decoding if it is encoded |
| 97 | // |
| 98 | int64_t |
| 99 | xpack_decode_string(Arena &arena, char **str, uint64_t &str_length, const uint8_t *buf_start, const uint8_t *buf_end, uint8_t n) |
| 100 | { |
| 101 | if (buf_start >= buf_end) { |
| 102 | return XPACK_ERROR_COMPRESSION_ERROR; |
| 103 | } |
| 104 | |
| 105 | const uint8_t *p = buf_start; |
| 106 | bool isHuffman = *p & (0x01 << n); |
| 107 | uint64_t encoded_string_len = 0; |
| 108 | int64_t len = 0; |
| 109 | |
| 110 | len = xpack_decode_integer(encoded_string_len, p, buf_end, n); |
| 111 | if (len == XPACK_ERROR_COMPRESSION_ERROR) { |
| 112 | return XPACK_ERROR_COMPRESSION_ERROR; |
| 113 | } |
| 114 | p += len; |
| 115 | |
| 116 | if (buf_end < p || static_cast<uint64_t>(buf_end - p) < encoded_string_len) { |
| 117 | return XPACK_ERROR_COMPRESSION_ERROR; |
| 118 | } |
| 119 | |
| 120 | if (isHuffman) { |
| 121 | // Allocate temporary area twice the size of before decoded data |
| 122 | *str = arena.str_alloc(encoded_string_len * 2); |
| 123 | |
| 124 | len = huffman_decode(*str, p, encoded_string_len); |
| 125 | if (len < 0) { |
| 126 | return XPACK_ERROR_COMPRESSION_ERROR; |
| 127 | } |
| 128 | str_length = len; |
| 129 | } else { |
| 130 | *str = arena.str_alloc(encoded_string_len); |
| 131 | |
| 132 | memcpy(*str, reinterpret_cast<const char *>(p), encoded_string_len); |
| 133 | |
| 134 | str_length = encoded_string_len; |
| 135 | } |
| 136 | |
| 137 | return p + encoded_string_len - buf_start; |
| 138 | } |
| 139 | |
| 140 | // |
| 141 | // [RFC 7541] 5.1. Integer representation |
no test coverage detected