1 /*
2  * nghttp3
3  *
4  * Copyright (c) 2019 nghttp3 contributors
5  * Copyright (c) 2017 ngtcp2 contributors
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #include "nghttp3_buf.h"
27 
nghttp3_buf_init(nghttp3_buf * buf)28 void nghttp3_buf_init(nghttp3_buf *buf) {
29   buf->begin = buf->end = buf->pos = buf->last = NULL;
30 }
31 
nghttp3_buf_wrap_init(nghttp3_buf * buf,uint8_t * src,size_t len)32 void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) {
33   buf->begin = buf->pos = buf->last = src;
34   buf->end = buf->begin + len;
35 }
36 
nghttp3_buf_free(nghttp3_buf * buf,const nghttp3_mem * mem)37 void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem) {
38   nghttp3_mem_free(mem, buf->begin);
39 }
40 
nghttp3_buf_left(const nghttp3_buf * buf)41 size_t nghttp3_buf_left(const nghttp3_buf *buf) {
42   return (size_t)(buf->end - buf->last);
43 }
44 
nghttp3_buf_len(const nghttp3_buf * buf)45 size_t nghttp3_buf_len(const nghttp3_buf *buf) {
46   return (size_t)(buf->last - buf->pos);
47 }
48 
nghttp3_buf_cap(const nghttp3_buf * buf)49 size_t nghttp3_buf_cap(const nghttp3_buf *buf) {
50   return (size_t)(buf->end - buf->begin);
51 }
52 
nghttp3_buf_reset(nghttp3_buf * buf)53 void nghttp3_buf_reset(nghttp3_buf *buf) { buf->pos = buf->last = buf->begin; }
54 
nghttp3_buf_reserve(nghttp3_buf * buf,size_t size,const nghttp3_mem * mem)55 int nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem) {
56   uint8_t *p;
57   nghttp3_ssize pos_offset, last_offset;
58 
59   if ((size_t)(buf->end - buf->begin) >= size) {
60     return 0;
61   }
62 
63   pos_offset = buf->pos - buf->begin;
64   last_offset = buf->last - buf->begin;
65 
66   p = nghttp3_mem_realloc(mem, buf->begin, size);
67   if (p == NULL) {
68     return NGHTTP3_ERR_NOMEM;
69   }
70 
71   buf->begin = p;
72   buf->end = p + size;
73   buf->pos = p + pos_offset;
74   buf->last = p + last_offset;
75 
76   return 0;
77 }
78 
nghttp3_buf_swap(nghttp3_buf * a,nghttp3_buf * b)79 void nghttp3_buf_swap(nghttp3_buf *a, nghttp3_buf *b) {
80   nghttp3_buf c = *a;
81 
82   *a = *b;
83   *b = c;
84 }
85 
nghttp3_typed_buf_init(nghttp3_typed_buf * tbuf,const nghttp3_buf * buf,nghttp3_buf_type type)86 void nghttp3_typed_buf_init(nghttp3_typed_buf *tbuf, const nghttp3_buf *buf,
87                             nghttp3_buf_type type) {
88   tbuf->buf = *buf;
89   tbuf->type = type;
90 }
91