11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * nghttp3
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2019 nghttp3 contributors
51cb0ef41Sopenharmony_ci * Copyright (c) 2017 ngtcp2 contributors
61cb0ef41Sopenharmony_ci *
71cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
81cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the
91cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
101cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
111cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
121cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
131cb0ef41Sopenharmony_ci * the following conditions:
141cb0ef41Sopenharmony_ci *
151cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be
161cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software.
171cb0ef41Sopenharmony_ci *
181cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
191cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
201cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
211cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
221cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
231cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
241cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci#include "nghttp3_conv.h"
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci#include <string.h>
291cb0ef41Sopenharmony_ci#include <assert.h>
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#include "nghttp3_str.h"
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciint64_t nghttp3_get_varint(size_t *plen, const uint8_t *p) {
341cb0ef41Sopenharmony_ci  union {
351cb0ef41Sopenharmony_ci    char b[8];
361cb0ef41Sopenharmony_ci    uint16_t n16;
371cb0ef41Sopenharmony_ci    uint32_t n32;
381cb0ef41Sopenharmony_ci    uint64_t n64;
391cb0ef41Sopenharmony_ci  } n;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  *plen = 1u << (*p >> 6);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  switch (*plen) {
441cb0ef41Sopenharmony_ci  case 1:
451cb0ef41Sopenharmony_ci    return (int64_t)*p;
461cb0ef41Sopenharmony_ci  case 2:
471cb0ef41Sopenharmony_ci    memcpy(&n, p, 2);
481cb0ef41Sopenharmony_ci    n.b[0] &= 0x3f;
491cb0ef41Sopenharmony_ci    return (int64_t)ntohs(n.n16);
501cb0ef41Sopenharmony_ci  case 4:
511cb0ef41Sopenharmony_ci    memcpy(&n, p, 4);
521cb0ef41Sopenharmony_ci    n.b[0] &= 0x3f;
531cb0ef41Sopenharmony_ci    return (int64_t)ntohl(n.n32);
541cb0ef41Sopenharmony_ci  case 8:
551cb0ef41Sopenharmony_ci    memcpy(&n, p, 8);
561cb0ef41Sopenharmony_ci    n.b[0] &= 0x3f;
571cb0ef41Sopenharmony_ci    return (int64_t)nghttp3_ntohl64(n.n64);
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  assert(0);
611cb0ef41Sopenharmony_ci  abort();
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciint64_t nghttp3_get_varint_fb(const uint8_t *p) { return *p & 0x3f; }
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cisize_t nghttp3_get_varint_len(const uint8_t *p) { return 1u << (*p >> 6); }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n) {
691cb0ef41Sopenharmony_ci  n = nghttp3_htonl64(n);
701cb0ef41Sopenharmony_ci  return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_uint48be(uint8_t *p, uint64_t n) {
741cb0ef41Sopenharmony_ci  n = nghttp3_htonl64(n);
751cb0ef41Sopenharmony_ci  return nghttp3_cpymem(p, ((const uint8_t *)&n) + 2, 6);
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_uint32be(uint8_t *p, uint32_t n) {
791cb0ef41Sopenharmony_ci  n = htonl(n);
801cb0ef41Sopenharmony_ci  return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_uint24be(uint8_t *p, uint32_t n) {
841cb0ef41Sopenharmony_ci  n = htonl(n);
851cb0ef41Sopenharmony_ci  return nghttp3_cpymem(p, ((const uint8_t *)&n) + 1, 3);
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_uint16be(uint8_t *p, uint16_t n) {
891cb0ef41Sopenharmony_ci  n = htons(n);
901cb0ef41Sopenharmony_ci  return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
911cb0ef41Sopenharmony_ci}
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ciuint8_t *nghttp3_put_varint(uint8_t *p, int64_t n) {
941cb0ef41Sopenharmony_ci  uint8_t *rv;
951cb0ef41Sopenharmony_ci  if (n < 64) {
961cb0ef41Sopenharmony_ci    *p++ = (uint8_t)n;
971cb0ef41Sopenharmony_ci    return p;
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci  if (n < 16384) {
1001cb0ef41Sopenharmony_ci    rv = nghttp3_put_uint16be(p, (uint16_t)n);
1011cb0ef41Sopenharmony_ci    *p |= 0x40;
1021cb0ef41Sopenharmony_ci    return rv;
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci  if (n < 1073741824) {
1051cb0ef41Sopenharmony_ci    rv = nghttp3_put_uint32be(p, (uint32_t)n);
1061cb0ef41Sopenharmony_ci    *p |= 0x80;
1071cb0ef41Sopenharmony_ci    return rv;
1081cb0ef41Sopenharmony_ci  }
1091cb0ef41Sopenharmony_ci  assert(n < 4611686018427387904LL);
1101cb0ef41Sopenharmony_ci  rv = nghttp3_put_uint64be(p, (uint64_t)n);
1111cb0ef41Sopenharmony_ci  *p |= 0xc0;
1121cb0ef41Sopenharmony_ci  return rv;
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_cisize_t nghttp3_put_varint_len(int64_t n) {
1161cb0ef41Sopenharmony_ci  if (n < 64) {
1171cb0ef41Sopenharmony_ci    return 1;
1181cb0ef41Sopenharmony_ci  }
1191cb0ef41Sopenharmony_ci  if (n < 16384) {
1201cb0ef41Sopenharmony_ci    return 2;
1211cb0ef41Sopenharmony_ci  }
1221cb0ef41Sopenharmony_ci  if (n < 1073741824) {
1231cb0ef41Sopenharmony_ci    return 4;
1241cb0ef41Sopenharmony_ci  }
1251cb0ef41Sopenharmony_ci  assert(n < 4611686018427387904LL);
1261cb0ef41Sopenharmony_ci  return 8;
1271cb0ef41Sopenharmony_ci}
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ciuint64_t nghttp3_ord_stream_id(int64_t stream_id) {
1301cb0ef41Sopenharmony_ci  return (uint64_t)(stream_id >> 2) + 1;
1311cb0ef41Sopenharmony_ci}
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ciuint8_t nghttp3_pri_to_uint8(const nghttp3_pri *pri) {
1341cb0ef41Sopenharmony_ci  return (uint8_t)((uint32_t)pri->inc << 7 | pri->urgency);
1351cb0ef41Sopenharmony_ci}
136