forked from sam-itt/sofis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-buffer.c
85 lines (73 loc) · 1.8 KB
/
http-buffer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* SPDX-FileCopyrightText: 2021 Samuel Cuella <samuel.cuella@gmail.com>
*
* This file is part of SoFIS - an open source EFIS
*
* SPDX-License-Identifier: GPL-2.0-only
*/
//#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "http-buffer.h"
HttpBuffer *http_buffer_new(size_t len)
{
HttpBuffer *self;
self = calloc(1, sizeof(HttpBuffer));
if(self){
if(len){
self->buffer = malloc(sizeof(char) * len);
if(!self->buffer){
free(self);
return(NULL);
}
self->allocated = len;
}
}
return(self);
}
/**
* @brief Ensure that @param self have enough rooom to store @param size bytes
*
*
*/
bool http_buffer_resize(HttpBuffer *self, size_t size)
{
if(self->allocated < size){
void *tmp = realloc(self->buffer, size);
if(!tmp) return false;
self->buffer = tmp;
self->allocated = size;
}
return true;
}
/**
* @brief Puts @param content into @param self, ensuring @param self is
* NULL-terminated
*
*/
bool http_buffer_set_content(HttpBuffer *self, const void *content, size_t len)
{
bool rv;
rv = http_buffer_resize(self, len+1); /*TODO: Handle overflow*/
if(!rv) return 0;
memcpy(self->buffer, content, len);
self->buffer[len+1] = '\0';
self->len = len;
return true;
}
/**
* @brief Puts @param content into @param self, ensuring @param self is
* NULL-terminated
*
*/
bool http_buffer_add_content(HttpBuffer *self, const void *content, size_t len)
{
bool rv;
rv = http_buffer_resize(self, self->allocated+len+1); /*TODO: Handle overflow*/
if(!rv) return 0;
memcpy(self->buffer+self->len, content, len);
self->len += len;
self->buffer[self->len+1] = '\0';
return true;
}