wickr-crypto-c

buffer.h
1 /*
2 * Copyright © 2012-2020 Wickr Inc. All rights reserved.
3 *
4 * This code is being released for EDUCATIONAL, ACADEMIC, AND CODE REVIEW PURPOSES
5 * ONLY. COMMERCIAL USE OF THE CODE IS EXPRESSLY PROHIBITED. For additional details,
6 * please see LICENSE
7 *
8 * THE CODE IS MADE AVAILABLE "AS-IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED GUARANTEES AS TO FITNESS, MERCHANTABILITY, NON-
10 * INFRINGEMENT OR OTHERWISE. IT IS NOT BEING PROVIDED IN TRADE BUT ON
11 * A VOLUNTARY BASIS ON BEHALF OF THE AUTHOR’S PART FOR THE BENEFIT
12 * OF THE LICENSEE AND IS NOT MADE AVAILABLE FOR CONSUMER USE OR ANY
13 * OTHER USE OUTSIDE THE TERMS OF THIS LICENSE. ANYONE ACCESSING THE
14 * CODE SHOULD HAVE THE REQUISITE EXPERTISE TO SECURE THEIR SYSTEM
15 * AND DEVICES AND TO ACCESS AND USE THE CODE FOR REVIEW PURPOSES
16 * ONLY. LICENSEE BEARS THE RISK OF ACCESSING AND USING THE CODE. IN
17 * PARTICULAR, AUTHOR BEARS NO LIABILITY FOR ANY INTERFERENCE WITH OR
18 * ADVERSE EFFECT THAT MAY OCCUR AS A RESULT OF THE LICENSEE
19 * ACCESSING AND/OR USING THE CODE ON LICENSEE’S SYSTEM.
20 */
21
22 #ifndef buffer_h
23 #define buffer_h
24
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include "array.h"
29 #include "limits.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
51 struct wickr_buffer {
52 size_t length;
53 uint8_t *bytes;
54 };
55
56 typedef struct wickr_buffer wickr_buffer_t;
57
71 #define BUFFER_ARRAY_LEN(x) (sizeof(x) / sizeof(wickr_buffer_t *))
72
73 /* Define a MAX_BUFFER_SIZE that limits the total memory consumed by a buffer to INT32_MAX (~2GB) */
74 static const size_t MAX_BUFFER_SIZE = INT32_MAX - sizeof(wickr_buffer_t);
75
76 typedef int (*wickr_buffer_compare_func)(const volatile void *, const volatile void *, size_t);
77
89 wickr_buffer_t *wickr_buffer_create_empty(size_t len);
90
102 wickr_buffer_t *wickr_buffer_create_empty_zero(size_t len);
103
114 wickr_buffer_t *wickr_buffer_create(const uint8_t *bytes, size_t len);
115
125 wickr_buffer_t *wickr_buffer_copy(const wickr_buffer_t *source);
126
138 wickr_buffer_t *wickr_buffer_copy_section(const wickr_buffer_t *source, size_t start, size_t len);
139
154 bool wickr_buffer_modify_section(const wickr_buffer_t *buffer, const uint8_t *bytes, size_t start, size_t len);
155
156
167 wickr_buffer_t *wickr_buffer_concat(const wickr_buffer_t *buffer1, const wickr_buffer_t *buffer2);
168
179 wickr_buffer_t *wickr_buffer_concat_multi(wickr_buffer_t **buffers, uint8_t n_buffers);
180
192 bool wickr_buffer_is_equal(const wickr_buffer_t *b1,
193 const wickr_buffer_t *b2,
194 wickr_buffer_compare_func compare_func);
205 void wickr_buffer_destroy(wickr_buffer_t **buffer);
206
207
216 void wickr_buffer_destroy_zero(wickr_buffer_t **buffer);
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif /* buffer_h */
wickr_buffer_create_empty
wickr_buffer_t * wickr_buffer_create_empty(size_t len)
Creates an empty buffer of size length.
wickr_buffer_concat_multi
wickr_buffer_t * wickr_buffer_concat_multi(wickr_buffer_t **buffers, uint8_t n_buffers)
Concatenate n buffers.
wickr_buffer_is_equal
bool wickr_buffer_is_equal(const wickr_buffer_t *b1, const wickr_buffer_t *b2, wickr_buffer_compare_func compare_func)
Compare buffers for equality.
wickr_buffer_modify_section
bool wickr_buffer_modify_section(const wickr_buffer_t *buffer, const uint8_t *bytes, size_t start, size_t len)
Modify a subsection of a buffer.
wickr_buffer_destroy
void wickr_buffer_destroy(wickr_buffer_t **buffer)
Destroy a buffer.
wickr_buffer_create_empty_zero
wickr_buffer_t * wickr_buffer_create_empty_zero(size_t len)
Creates an zeroed empty buffer of size length.
wickr_buffer_create
wickr_buffer_t * wickr_buffer_create(const uint8_t *bytes, size_t len)
Creates a buffer by copying an existing pointer to bytes of a specified length len.
wickr_buffer_copy_section
wickr_buffer_t * wickr_buffer_copy_section(const wickr_buffer_t *source, size_t start, size_t len)
Create a buffer using a subsection of another buffer.
wickr_buffer_concat
wickr_buffer_t * wickr_buffer_concat(const wickr_buffer_t *buffer1, const wickr_buffer_t *buffer2)
Concatenate two buffers into one new buffer.
wickr_buffer_copy
wickr_buffer_t * wickr_buffer_copy(const wickr_buffer_t *source)
Copy a buffer.
wickr_buffer_destroy_zero
void wickr_buffer_destroy_zero(wickr_buffer_t **buffer)
Zero-then-deallocate a buffer.
wickr_buffer
Represents an array of bytes and the length of the allocation associated with those bytes.
Definition: buffer.h:51
wickr_buffer::length
size_t length
Definition: buffer.h:52
wickr_buffer::bytes
uint8_t * bytes
Definition: buffer.h:53