forked from ansemjo/tinyssh-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.h
90 lines (71 loc) · 3.56 KB
/
buffer.h
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
86
87
88
89
90
/*
* This file is governed by Licenses which are listed in
* the LICENSE file, which shall be included in all copies
* and redistributions of this project.
*
* For additional notices see the file buffer.c
*/
#ifndef _headerguard_buffer_h_
#define _headerguard_buffer_h_
#include <string.h>
#include <stdlib.h>
#include "errors.h"
#include "utilities.h"
#include "base64.h"
/****************************************************************************************/
/* size constraints */
#define BUFFER_ALLOCATION_INCREMENT 2*1024 /* 2 KiB */
#define BUFFER_ALLOCATION_MAXIMUM 64*1024*1024 /* 64 MiB */
/* statuscodes are defined in statuscodes.h */
/* opaque struct */
struct buffer;
/****************************************************************************************/
/* allocate and free buffers */
struct buffer * newbuffer ();
void freebuffer (struct buffer *buf);
void resetbuffer (struct buffer *buf);
/* put data into buffer */
int buffer_reserve (struct buffer *buf, size_t request_size, unsigned char **request_ptr);
int buffer_put (struct buffer *buf, const void *data, size_t datalength);
int buffer_put_u32 (struct buffer *buf, unsigned long value);
int buffer_put_u8 (struct buffer *buf, unsigned char value);
#define buffer_put_char buffer_put_u8
int buffer_put_data (struct buffer *buf, void *data, size_t length);
int buffer_put_string (struct buffer *buf, unsigned char *string);
/* convert from other formats */
int buffer_put_decoded_base64 (struct buffer *buf, const char *base64string);
/* read data from buffer */
int buffer_add_offset (struct buffer *buf, size_t length);
int buffer_read_u32 (struct buffer *buf, unsigned long *read);
int buffer_read_u8 (struct buffer *buf, unsigned char *read);
int buffer_get_stringptr (const struct buffer *buf, const unsigned char **stringptr, size_t *stringlen);
int buffer_read_string (struct buffer *buf, unsigned char **stringptr, size_t *lengthptr, char *nullchar);
/* create new from some other data */
int buffer_new_from_data (struct buffer **newbuf, const char *data, size_t datalen);
int buffer_new_from_buffer (struct buffer **newbuf, const struct buffer *sourcebuf); /* TODO: replace with packing */
int buffer_new_concat_strings (struct buffer **newbuf, struct buffer *sourcebuf);
/* attribute getters & setters */
unsigned char * buffer_get_dataptr (const struct buffer *buf);
size_t buffer_get_offset (const struct buffer *buf);
unsigned char * buffer_get_offsetptr (const struct buffer *buf);
size_t buffer_get_datasize (const struct buffer *buf);
size_t buffer_get_allocation (const struct buffer *buf);
size_t buffer_get_remaining (const struct buffer *buf);
int buffer_reset_offsetptr (struct buffer *buf);
/* debugging */
void buffer_dump (const struct buffer *buf);
/* Macros for decoding/encoding integers */
#define decode_uint32(addr) \
(((unsigned long)(((const unsigned char *)(addr))[0]) << 24) | \
((unsigned long)(((const unsigned char *)(addr))[1]) << 16) | \
((unsigned long)(((const unsigned char *)(addr))[2]) << 8) | \
(unsigned long)(((const unsigned char *)(addr))[3]))
#define encode_uint32(addr, value) \
do { \
const unsigned long __value = (value); \
((unsigned char *)(addr))[0] = (__value >> 24) & 0xff; \
((unsigned char *)(addr))[1] = (__value >> 16) & 0xff; \
((unsigned char *)(addr))[2] = (__value >> 8) & 0xff; \
((unsigned char *)(addr))[3] = __value & 0xff; \
} while (0)
#endif