kinetic-c  v0.12.0
Seagate Kinetic Protocol Client Library for C
byte_array.h
Go to the documentation of this file.
1 /*
2 * kinetic-c
3 * Copyright (C) 2015 Seagate Technology.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20 #ifndef _BYTE_ARRAY_H
21 #define _BYTE_ARRAY_H
22 
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 
34 typedef struct _ByteArray {
35  size_t len;
36  uint8_t* data;
37 } ByteArray;
38 
40 #define BYTE_ARRAY_NONE (ByteArray){.len = 0, .data = NULL}
41 
42 ByteArray ByteArray_Create(void* data, size_t len);
43 ByteArray ByteArray_CreateWithCString(const char* str);
44 ByteArray ByteArray_GetSlice(const ByteArray array, size_t start, size_t len);
45 void ByteArray_FillWithDummyData(const ByteArray array);
46 
53 typedef struct {
55  size_t bytesUsed;
56 } ByteBuffer;
57 
58 typedef struct {
60  size_t count;
61  size_t used;
63 
65 #define BYTE_BUFFER_NONE (ByteBuffer){.array = BYTE_ARRAY_NONE, .bytesUsed = 0}
66 
67 ByteBuffer ByteBuffer_Create(void* data, size_t max_len, size_t used);
69 ByteBuffer ByteBuffer_CreateAndAppend(void* data, size_t max_len, const void* value, size_t value_len);
70 ByteBuffer ByteBuffer_CreateAndAppendArray(void* data, size_t max_len, const ByteArray value);
71 ByteBuffer ByteBuffer_CreateAndAppendCString(void* data, size_t max_len, const char* value);
72 ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void* data, size_t max_len, const char * format, ...);
73 ByteBuffer ByteBuffer_CreateAndAppendDummyData(void* data, size_t max_len, size_t len);
74 void ByteBuffer_Reset(ByteBuffer* buffer);
75 long ByteBuffer_BytesRemaining(const ByteBuffer buffer);
76 ByteArray ByteBuffer_Consume(ByteBuffer* buffer, size_t max_len);
77 ByteBuffer* ByteBuffer_Append(ByteBuffer* buffer, const void* data, size_t len);
79 ByteBuffer* ByteBuffer_AppendBuffer(ByteBuffer* buffer, const ByteBuffer bufferToAppend);
80 ByteBuffer* ByteBuffer_AppendCString(ByteBuffer* buffer, const char* data);
81 ByteBuffer* ByteBuffer_AppendFormattedCString(ByteBuffer* buffer, const char * format, ...);
83 bool ByteBuffer_IsNull(ByteBuffer const buffer);
84 ByteBuffer ByteBuffer_Malloc(size_t size);
85 ByteBuffer ByteBuffer_MallocAndAppend(const void* data, size_t len);
86 void ByteBuffer_Free(ByteBuffer buffer);
87 
88 #endif // _BYTE_ARRAY_H
Structure for handling generic arrays of bytes.
Definition: byte_array.h:34
void ByteBuffer_Reset(ByteBuffer *buffer)
Definition: byte_array.c:62
ByteBuffer * ByteBuffer_Append(ByteBuffer *buffer, const void *data, size_t len)
Definition: byte_array.c:135
Structure for an embedded ByteArray as a buffer.
Definition: byte_array.h:53
ByteBuffer ByteBuffer_Malloc(size_t size)
Definition: byte_array.c:254
ByteBuffer ByteBuffer_CreateWithArray(ByteArray array)
Definition: byte_array.c:78
ByteBuffer * buffers
Definition: byte_array.h:59
ByteBuffer * ByteBuffer_AppendDummyData(ByteBuffer *buffer, size_t len)
Definition: byte_array.c:235
ByteBuffer ByteBuffer_CreateAndAppendDummyData(void *data, size_t max_len, size_t len)
Definition: byte_array.c:104
ByteBuffer * ByteBuffer_AppendArray(ByteBuffer *buffer, const ByteArray array)
Definition: byte_array.c:149
ByteBuffer * ByteBuffer_AppendBuffer(ByteBuffer *buffer, const ByteBuffer bufferToAppend)
Definition: byte_array.c:162
ByteArray ByteArray_GetSlice(const ByteArray array, size_t start, size_t len)
Definition: byte_array.c:52
ByteArray array
ByteArray holding allocated array w/length = allocated size.
Definition: byte_array.h:54
void ByteArray_FillWithDummyData(const ByteArray array)
Definition: byte_array.c:45
ByteBuffer * ByteBuffer_AppendCString(ByteBuffer *buffer, const char *data)
Definition: byte_array.c:176
ByteBuffer ByteBuffer_Create(void *data, size_t max_len, size_t used)
Definition: byte_array.c:68
size_t len
Number of bytes in the data field.
Definition: byte_array.h:35
long ByteBuffer_BytesRemaining(const ByteBuffer buffer)
Definition: byte_array.c:111
bool ByteBuffer_IsNull(ByteBuffer const buffer)
Definition: byte_array.c:249
uint8_t * data
Pointer to an allocated array of data bytes.
Definition: byte_array.h:36
ByteArray ByteBuffer_Consume(ByteBuffer *buffer, size_t max_len)
Definition: byte_array.c:117
ByteBuffer ByteBuffer_CreateAndAppendFormattedCString(void *data, size_t max_len, const char *format,...)
Definition: byte_array.c:221
ByteArray ByteArray_Create(void *data, size_t len)
Definition: byte_array.c:31
ByteBuffer ByteBuffer_MallocAndAppend(const void *data, size_t len)
Definition: byte_array.c:262
ByteBuffer ByteBuffer_CreateAndAppendArray(void *data, size_t max_len, const ByteArray value)
Definition: byte_array.c:90
size_t bytesUsed
Reflects the number of bytes used from the array
Definition: byte_array.h:55
ByteBuffer ByteBuffer_CreateAndAppend(void *data, size_t max_len, const void *value, size_t value_len)
Definition: byte_array.c:83
void ByteBuffer_Free(ByteBuffer buffer)
Definition: byte_array.c:272
ByteBuffer * ByteBuffer_AppendFormattedCString(ByteBuffer *buffer, const char *format,...)
Definition: byte_array.c:205
ByteArray ByteArray_CreateWithCString(const char *str)
Definition: byte_array.c:38
ByteBuffer ByteBuffer_CreateAndAppendCString(void *data, size_t max_len, const char *value)
Definition: byte_array.c:97