nghttp2/lib/nghttp2_buffer.h

171 lines
5.7 KiB
C
Raw Normal View History

/*
2013-07-12 17:19:03 +02:00
* nghttp2 - HTTP/2.0 C Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-07-12 17:19:03 +02:00
#ifndef NGHTTP2_BUFFER_H
#define NGHTTP2_BUFFER_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
2013-07-12 17:19:03 +02:00
#include <nghttp2/nghttp2.h>
2013-07-12 17:19:03 +02:00
typedef struct nghttp2_buffer_chunk {
2012-02-16 14:01:34 +01:00
uint8_t *data;
2013-07-12 17:19:03 +02:00
struct nghttp2_buffer_chunk *next;
} nghttp2_buffer_chunk;
2012-02-16 14:01:34 +01:00
/*
2012-02-16 14:01:34 +01:00
* List of fixed sized chunks
*/
typedef struct {
/* Capacity of each chunk buffer */
size_t capacity;
2012-02-16 14:01:34 +01:00
/* Root of list of chunk buffers. The root is dummy and its data
member is always NULL. */
2013-07-12 17:19:03 +02:00
nghttp2_buffer_chunk root;
2012-02-16 14:01:34 +01:00
/* Points to the current chunk to write */
2013-07-12 17:19:03 +02:00
nghttp2_buffer_chunk *current;
/* Total length of this buffer */
size_t len;
/* Offset of last chunk buffer */
size_t last_offset;
2013-07-12 17:19:03 +02:00
} nghttp2_buffer;
/*
* Initializes buffer with fixed chunk size chunk_capacity.
*/
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_init(nghttp2_buffer *buffer, size_t chunk_capacity);
/* Releases allocated memory for buffer */
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_free(nghttp2_buffer *buffer);
/* Returns buffer pointer */
2013-07-12 17:19:03 +02:00
uint8_t* nghttp2_buffer_get(nghttp2_buffer *buffer);
/* Returns available buffer length */
2013-07-12 17:19:03 +02:00
size_t nghttp2_buffer_avail(nghttp2_buffer *buffer);
/* Advances buffer pointer by amount. This reduces available buffer
length. */
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_advance(nghttp2_buffer *buffer, size_t amount);
/*
* Writes the |data| with the |len| bytes starting at the current
* position of the |buffer|. The new chunk buffer will be allocated on
* the course of the write and the current position is updated. If
* this function succeeds, the total length of the |buffer| will be
* increased by |len|.
*
* This function returns 0 if it succeeds, or one of the following
* negative error codes:
*
2013-07-12 17:19:03 +02:00
* NGHTTP2_ERR_NOMEM
* Out of memory.
*/
2013-07-12 17:19:03 +02:00
int nghttp2_buffer_write(nghttp2_buffer *buffer, const uint8_t *data,
size_t len);
2012-02-21 15:24:16 +01:00
/*
* Allocate new chunk buffer. This will increase total length of
2013-07-12 17:19:03 +02:00
* buffer (returned by nghttp2_buffer_length) by capacity-last_offset.
2012-02-21 15:24:16 +01:00
* It means untouched buffer is assumued to be written.
*
* This function returns 0 if it succeeds, or one of the following
* negative eror codes:
*
2013-07-12 17:19:03 +02:00
* NGHTTP2_ERR_NOMEM
2012-02-21 15:24:16 +01:00
* Out of memory.
*/
2013-07-12 17:19:03 +02:00
int nghttp2_buffer_alloc(nghttp2_buffer *buffer);
/* Returns total length of buffer */
2013-07-12 17:19:03 +02:00
size_t nghttp2_buffer_length(nghttp2_buffer *buffer);
/* Returns capacity of each fixed chunk buffer */
2013-07-12 17:19:03 +02:00
size_t nghttp2_buffer_capacity(nghttp2_buffer *buffer);
2012-02-16 14:01:34 +01:00
/* Stores the contents of buffer into |buf|. |buf| must be at least
2013-07-12 17:19:03 +02:00
nghttp2_buffer_length(buffer) bytes long. */
void nghttp2_buffer_serialize(nghttp2_buffer *buffer, uint8_t *buf);
2012-02-16 14:01:34 +01:00
/* Reset |buffer| for reuse. Set the total length of buffer to 0.
2013-07-12 17:19:03 +02:00
Next nghttp2_buffer_avail() returns 0. This function does not free
2012-02-16 14:01:34 +01:00
allocated memory space; they are reused. */
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_reset(nghttp2_buffer *buffer);
2012-02-16 14:01:34 +01:00
/*
2013-07-12 17:19:03 +02:00
* Reader interface to read data from nghttp2_buffer sequentially.
*/
typedef struct {
/* The buffer to read */
2013-07-12 17:19:03 +02:00
nghttp2_buffer *buffer;
/* Pointer to the current chunk to read. */
2013-07-12 17:19:03 +02:00
nghttp2_buffer_chunk *current;
/* Offset to the current chunk data to read. */
size_t offset;
2013-07-12 17:19:03 +02:00
} nghttp2_buffer_reader;
/*
* Initializes the |reader| with the |buffer|.
*/
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_reader_init(nghttp2_buffer_reader *reader,
nghttp2_buffer *buffer);
/*
* Reads 1 byte and return it. This function will advance the current
* position by 1.
*/
2013-07-12 17:19:03 +02:00
uint8_t nghttp2_buffer_reader_uint8(nghttp2_buffer_reader *reader);
/*
* Reads 2 bytes integer in network byte order and returns it in host
* byte order. This function will advance the current position by 2.
*/
2013-07-12 17:19:03 +02:00
uint16_t nghttp2_buffer_reader_uint16(nghttp2_buffer_reader *reader);
/*
* Reads 4 bytes integer in network byte order and returns it in host
* byte order. This function will advance the current position by 4.
*/
2013-07-12 17:19:03 +02:00
uint32_t nghttp2_buffer_reader_uint32(nghttp2_buffer_reader *reader);
/*
* Reads |len| bytes and store them in the |out|. This function will
* advance the current position by |len|.
*/
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_reader_data(nghttp2_buffer_reader *reader,
uint8_t *out, size_t len);
/**
* Reads |len| bytes and count the occurrence of |c| there and return
* it. This function will advance the current position by |len|.
*/
2013-07-12 17:19:03 +02:00
int nghttp2_buffer_reader_count(nghttp2_buffer_reader *reader,
size_t len, uint8_t c);
/*
* Advances the current position by |amount|.
*/
2013-07-12 17:19:03 +02:00
void nghttp2_buffer_reader_advance(nghttp2_buffer_reader *reader,
size_t amount);
2013-07-12 17:19:03 +02:00
#endif /* NGHTTP2_BUFFER_H */