[RFC 7541] 5.1. Integer representation
| 141 | // [RFC 7541] 5.1. Integer representation |
| 142 | // |
| 143 | int64_t |
| 144 | xpack_encode_integer(uint8_t *buf_start, const uint8_t *buf_end, uint64_t value, uint8_t n) |
| 145 | { |
| 146 | if (buf_start >= buf_end) { |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | uint8_t *p = buf_start; |
| 151 | |
| 152 | // Preserve the first n bits |
| 153 | uint8_t prefix = *buf_start & (0xFF << n); |
| 154 | |
| 155 | if (value < (static_cast<uint64_t>(UINT64_C(1) << n) - 1)) { |
| 156 | *(p++) = value; |
| 157 | } else { |
| 158 | *(p++) = (1 << n) - 1; |
| 159 | value -= (1 << n) - 1; |
| 160 | while (value >= 128) { |
| 161 | if (p >= buf_end) { |
| 162 | return -1; |
| 163 | } |
| 164 | *(p++) = (value & 0x7F) | 0x80; |
| 165 | value = value >> 7; |
| 166 | } |
| 167 | if (p + 1 >= buf_end) { |
| 168 | return -1; |
| 169 | } |
| 170 | *(p++) = value; |
| 171 | } |
| 172 | |
| 173 | // Restore the prefix |
| 174 | *buf_start |= prefix; |
| 175 | |
| 176 | return p - buf_start; |
| 177 | } |
| 178 | |
| 179 | int64_t |
| 180 | xpack_encode_string(uint8_t *buf_start, const uint8_t *buf_end, const char *value, uint64_t value_len, uint8_t n) |
no outgoing calls