src/http/v2/ngx_http_v2_encode.c - nginx source code

Functions defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. * Copyright (C) Valentin V. Bartenev
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. static u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
  9.     ngx_uint_t value);


  10. u_char *
  11. ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
  12.     ngx_uint_t lower)
  13. {
  14.     size_t  hlen;

  15.     hlen = ngx_http_huff_encode(src, len, tmp, lower);

  16.     if (hlen > 0) {
  17.         *dst = NGX_HTTP_V2_ENCODE_HUFF;
  18.         dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), hlen);
  19.         return ngx_cpymem(dst, tmp, hlen);
  20.     }

  21.     *dst = NGX_HTTP_V2_ENCODE_RAW;
  22.     dst = ngx_http_v2_write_int(dst, ngx_http_v2_prefix(7), len);

  23.     if (lower) {
  24.         ngx_strlow(dst, src, len);
  25.         return dst + len;
  26.     }

  27.     return ngx_cpymem(dst, src, len);
  28. }


  29. static u_char *
  30. ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix, ngx_uint_t value)
  31. {
  32.     if (value < prefix) {
  33.         *pos++ |= value;
  34.         return pos;
  35.     }

  36.     *pos++ |= prefix;
  37.     value -= prefix;

  38.     while (value >= 128) {
  39.         *pos++ = value % 128 + 128;
  40.         value /= 128;
  41.     }

  42.     *pos++ = (u_char) value;

  43.     return pos;
  44. }