Packages
hackney
2.0.0
4.7.2
4.7.1
4.7.0
4.6.1
4.6.0
4.5.2
4.5.1
4.5.0
4.4.5
4.4.3
4.4.2
4.4.1
4.4.0
4.3.0
4.2.3
4.2.2
4.2.1
4.2.0
4.1.0
4.0.3
4.0.2
4.0.1
4.0.0
3.2.1
3.2.0
3.1.2
3.1.1
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0
retired
2.0.1
2.0.0
2.0.0-beta.1
1.25.0
1.24.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.1
1.20.0
1.19.1
1.19.0
1.18.2
1.18.1
1.18.0
1.17.4
1.17.3
1.17.2
1.17.1
1.17.0
1.16.0
1.15.2
1.15.1
1.15.0
1.14.3
1.14.2
1.14.0
1.13.0
1.12.1
1.12.0
1.11.0
1.10.1
1.10.0
1.9.0
1.8.6
1.8.5
1.8.4
1.8.3
1.8.2
1.8.0
1.7.1
1.7.0
1.6.6
retired
1.6.5
1.6.4
retired
1.6.3
1.6.2
1.6.1
1.6.0
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.10
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.0
1.1.0
1.0.6
1.0.5
1.0.2
1.0.1
0.15.2
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.1
Simple HTTP client with HTTP/1.1, HTTP/2, and HTTP/3 support
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
c_src/lsquic/src/liblsquic/lsquic_frab_list.c
/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */
/*
* lsquic_frab_list.c -- List of buffer for simple reading and writing
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include "lsquic_frab_list.h"
static void *
fral_alloc (void *ctx, size_t size)
{
return malloc(size);
}
static void
fral_free (void *ctx, void *obj)
{
free(obj);
}
void
lsquic_frab_list_init (struct frab_list *fral, unsigned short buf_size,
void * (*alloc)(void *alloc_ctx, size_t size),
void (*free)(void *alloc_ctx, void *obj), void *alloc_ctx)
{
TAILQ_INIT(&fral->fl_frabs);
fral->fl_alloc_ctx = alloc_ctx;
fral->fl_alloc = alloc ? alloc : fral_alloc;
fral->fl_free = free ? free : fral_free;
fral->fl_buf_size = buf_size;
fral->fl_size = 0;
}
void
lsquic_frab_list_cleanup (struct frab_list *fral)
{
struct frame_buf *frab, *next;
for (frab = TAILQ_FIRST(&fral->fl_frabs); frab; frab = next)
{
next = TAILQ_NEXT(frab, frab_next);
fral->fl_free(fral->fl_alloc_ctx, frab);
}
}
static struct frame_buf *
fral_get_frab (struct frab_list *fral)
{
struct frame_buf *frab;
frab = fral->fl_alloc(fral->fl_alloc_ctx, fral->fl_buf_size);
if (frab)
{
memset(frab, 0, sizeof(*frab));
frab->frab_buf_size = fral->fl_buf_size;
}
return frab;
}
int
lsquic_frab_list_write (struct frab_list *fral, const void *buf, size_t bufsz)
{
const unsigned char *p = buf;
const unsigned char *const end = p + bufsz;
struct frame_buf *frab;
unsigned ntowrite;
while (p < end)
{
frab = TAILQ_LAST(&fral->fl_frabs, frame_buf_head);
if (!(frab && (ntowrite = frab_left_to_write(frab)) > 0))
{
frab = fral_get_frab(fral);
if (!frab)
return -1;
TAILQ_INSERT_TAIL(&fral->fl_frabs, frab, frab_next);
ntowrite = frab_left_to_write(frab);
}
if ((ptrdiff_t) ntowrite > end - p)
ntowrite = end - p;
memcpy(frab_write_to(frab), p, ntowrite);
p += ntowrite;
frab->frab_size += ntowrite;
}
fral->fl_size += bufsz;
return 0;
}
size_t
lsquic_frab_list_size (void *ctx)
{
struct frab_list *fral = ctx;
return fral->fl_size;
}
size_t
lsquic_frab_list_read (void *ctx, void *buf, size_t bufsz)
{
struct frab_list *const fral = ctx;
unsigned char *p = buf;
unsigned char *const end = p + bufsz;
struct frame_buf *frab;
size_t ntocopy;
while (p < end && (frab = TAILQ_FIRST(&fral->fl_frabs)))
{
ntocopy = end - p;
if (ntocopy > (size_t) frab_left_to_read(frab))
ntocopy = frab_left_to_read(frab);
memcpy(p, frab->frab_buf + frab->frab_off, ntocopy);
fral->fl_size -= ntocopy;
frab->frab_off += ntocopy;
p += ntocopy;
if (frab->frab_off == frab->frab_size)
{
TAILQ_REMOVE(&fral->fl_frabs, frab, frab_next);
fral->fl_free(fral->fl_alloc_ctx, frab);
}
}
return p - (unsigned char *) buf;
}
size_t
lsquic_frab_list_mem_used (const struct frab_list *fral)
{
struct frame_buf *frab;
size_t size;
size = sizeof(*fral);
TAILQ_FOREACH(frab, &fral->fl_frabs, frab_next)
size += fral->fl_buf_size;
return size;
}