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_buf.h" 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_civoid nghttp3_buf_init(nghttp3_buf *buf) { 291cb0ef41Sopenharmony_ci buf->begin = buf->end = buf->pos = buf->last = NULL; 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_civoid nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) { 331cb0ef41Sopenharmony_ci buf->begin = buf->pos = buf->last = src; 341cb0ef41Sopenharmony_ci buf->end = buf->begin + len; 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_civoid nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem) { 381cb0ef41Sopenharmony_ci nghttp3_mem_free(mem, buf->begin); 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cisize_t nghttp3_buf_left(const nghttp3_buf *buf) { 421cb0ef41Sopenharmony_ci return (size_t)(buf->end - buf->last); 431cb0ef41Sopenharmony_ci} 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_cisize_t nghttp3_buf_len(const nghttp3_buf *buf) { 461cb0ef41Sopenharmony_ci return (size_t)(buf->last - buf->pos); 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_cisize_t nghttp3_buf_cap(const nghttp3_buf *buf) { 501cb0ef41Sopenharmony_ci return (size_t)(buf->end - buf->begin); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_civoid nghttp3_buf_reset(nghttp3_buf *buf) { buf->pos = buf->last = buf->begin; } 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciint nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem) { 561cb0ef41Sopenharmony_ci uint8_t *p; 571cb0ef41Sopenharmony_ci nghttp3_ssize pos_offset, last_offset; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci if ((size_t)(buf->end - buf->begin) >= size) { 601cb0ef41Sopenharmony_ci return 0; 611cb0ef41Sopenharmony_ci } 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci pos_offset = buf->pos - buf->begin; 641cb0ef41Sopenharmony_ci last_offset = buf->last - buf->begin; 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci p = nghttp3_mem_realloc(mem, buf->begin, size); 671cb0ef41Sopenharmony_ci if (p == NULL) { 681cb0ef41Sopenharmony_ci return NGHTTP3_ERR_NOMEM; 691cb0ef41Sopenharmony_ci } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci buf->begin = p; 721cb0ef41Sopenharmony_ci buf->end = p + size; 731cb0ef41Sopenharmony_ci buf->pos = p + pos_offset; 741cb0ef41Sopenharmony_ci buf->last = p + last_offset; 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci return 0; 771cb0ef41Sopenharmony_ci} 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_civoid nghttp3_buf_swap(nghttp3_buf *a, nghttp3_buf *b) { 801cb0ef41Sopenharmony_ci nghttp3_buf c = *a; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci *a = *b; 831cb0ef41Sopenharmony_ci *b = c; 841cb0ef41Sopenharmony_ci} 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_civoid nghttp3_typed_buf_init(nghttp3_typed_buf *tbuf, const nghttp3_buf *buf, 871cb0ef41Sopenharmony_ci nghttp3_buf_type type) { 881cb0ef41Sopenharmony_ci tbuf->buf = *buf; 891cb0ef41Sopenharmony_ci tbuf->type = type; 901cb0ef41Sopenharmony_ci} 91