-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.c
193 lines (135 loc) · 4.5 KB
/
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#ifdef __gnu_linux__
#include <unistd.h>
#endif
#include <string.h>
#include "crypt.h"
//----------------------------------------------------------------------------//
int pl_buf_seek(pl_buffer *buffer, unsigned int seek) {
buffer->pos = seek;
return PL_COMP_E_OK;
}
//----------------------------------------------------------------------------//
int pl_reallocate(pl_buffer *buffer, unsigned int new_size) {
if(buffer->allocated_size == new_size)
return PL_COMP_E_OK;
buffer->buffer = realloc(buffer->buffer, new_size);
if(!buffer) {
memset(buffer, 0, sizeof(pl_buffer));
return PL_COMP_E_INVALID;
}
buffer->allocated_size = new_size;
return PL_COMP_E_OK;
}
//----------------------------------------------------------------------------//
pl_buffer* pl_buf_create(unsigned int size) {
pl_buffer *buffer = (pl_buffer*)malloc(sizeof(pl_buffer));
if(!buffer)
return NULL;
memset(buffer, 0, sizeof(pl_buffer));
buffer->buffer = (void*)malloc(size);
if(!buffer->buffer) {
pl_buf_destroy(buffer);
return NULL;
}
buffer->allocated_size = size;
return buffer;
}
//----------------------------------------------------------------------------//
pl_buffer* pl_buf_create_from(void *buf, unsigned int size) {
pl_buffer *buffer = (pl_buffer*)malloc(sizeof(pl_buffer));
if(!buffer)
return NULL;
memset(buffer, 0, sizeof(pl_buffer));
buffer->buffer = (void*)malloc(size);
if(!buffer->buffer) {
pl_buf_destroy(buffer);
return NULL;
}
memcpy(buffer->buffer, buf, size);
buffer->allocated_size = size;
buffer->length = size;
return buffer;
}
//----------------------------------------------------------------------------//
int pl_buf_assign(pl_buffer *buffer, void *buf, unsigned int size) {
if(!buffer)
return PL_COMP_E_INVALID;
if(pl_reallocate(buffer, size))
return PL_COMP_E_MEMORY;
memcpy(buffer->buffer, buf, size);
buffer->pos = 0;
buffer->length = size;
return PL_COMP_E_OK;
}
//----------------------------------------------------------------------------//
int pl_buf_read_file(pl_buffer *buffer, char *filename) {
FILE *fp;
unsigned int size;
if(!buffer)
return PL_COMP_E_INVALID;
fp = fopen(filename, "rb");
if(fp == NULL)
return PL_COMP_E_FILE;
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if(pl_reallocate(buffer, size)) {
fclose(fp);
return PL_COMP_E_MEMORY;
}
fread(buffer->buffer, 1, size, fp);
buffer->pos = 0;
buffer->length = size;
fclose(fp);
return PL_COMP_E_OK;
}
//----------------------------------------------------------------------------//
int pl_buf_write_file(pl_buffer *buffer, char *filename) {
FILE *fp;
if(!buffer)
return PL_COMP_E_INVALID;
fp = fopen(filename, "wb");
if(fp == NULL)
return PL_COMP_E_FILE;
fwrite(buffer->buffer, 1, buffer->length, fp);
fclose(fp);
return PL_COMP_E_OK;
}
//----------------------------------------------------------------------------//
int pl_buf_write(pl_buffer *buffer, void *data, unsigned int size) {
if(!buffer)
return PL_COMP_E_INVALID;
if(buffer->pos + size >= buffer->allocated_size) {
if(pl_reallocate(buffer, buffer->pos + size + 8192))
return PL_COMP_E_MEMORY;
buffer->length = buffer->pos + size;
}
if(buffer->pos + size > buffer->length)
buffer->length = buffer->pos + size;
memcpy((char*)buffer->buffer + buffer->pos, data, size);
buffer->pos += size;
return size;
}
//----------------------------------------------------------------------------//
int pl_buf_read(pl_buffer *buffer, void *data, unsigned int size) {
unsigned int readed_size = 0;
if(!buffer)
return PL_COMP_E_INVALID;
if(buffer->pos + size > buffer->length)
size = buffer->length - buffer->pos;
memcpy(data, (char*)buffer->buffer + buffer->pos, size);
buffer->pos += size;
return size;
}
//----------------------------------------------------------------------------//
void pl_buf_destroy(pl_buffer *ptr) {
if(!ptr)
return;
if(ptr->buffer && ptr->allocated_size)
free(ptr->buffer);
ptr->buffer = NULL;
ptr->allocated_size = 0;
free(ptr);
}