Lines Matching refs:buffer

43 struct buffer {  struct
54 typedef struct buffer buffer_t; argument
63 static void buffer_free(buffer_t *buffer);
67 static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length);
68 static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error);
69 static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip…
241 buffer_free(buffer_t *buffer) in buffer_free() argument
243 if (buffer == NULL) { in buffer_free()
247 if (buffer->free_data) { in buffer_free()
250 for (i=0; i < buffer->nfragments; i++) { in buffer_free()
251 free(buffer->fragments[i]); in buffer_free()
254 free(buffer->fragments); in buffer_free()
255 free(buffer); in buffer_free()
262 buffer_t *buffer; in buffer_new() local
264 if ((buffer = malloc(sizeof(*buffer))) == NULL) { in buffer_new()
268 buffer->fragment_size = fragment_size; in buffer_new()
269 buffer->offset = 0; in buffer_new()
270 buffer->free_data = 0; in buffer_new()
271 buffer->nfragments = 0; in buffer_new()
272 buffer->fragments_capacity = 0; in buffer_new()
273 buffer->fragments = NULL; in buffer_new()
274 buffer->size = 0; in buffer_new()
276 return buffer; in buffer_new()
283 buffer_t *buffer; in buffer_new_read() local
285 if ((buffer = buffer_new(length)) == NULL) { in buffer_new_read()
289 buffer->size = length; in buffer_new_read()
292 if ((buffer->fragments = malloc(sizeof(*(buffer->fragments)))) == NULL) { in buffer_new_read()
293 buffer_free(buffer); in buffer_new_read()
296 buffer->fragments_capacity = 1; in buffer_new_read()
298 buffer->nfragments = 1; in buffer_new_read()
299 buffer->fragments[0] = (zip_uint8_t *)data; in buffer_new_read()
300 buffer->free_data = free_data; in buffer_new_read()
303 return buffer; in buffer_new_read()
310 buffer_t *buffer; in buffer_new_write() local
312 if ((buffer = buffer_new(fragment_size)) == NULL) { in buffer_new_write()
316 if ((buffer->fragments = malloc(sizeof(*(buffer->fragments)))) == NULL) { in buffer_new_write()
317 buffer_free(buffer); in buffer_new_write()
320 buffer->fragments_capacity = 1; in buffer_new_write()
321 buffer->nfragments = 0; in buffer_new_write()
322 buffer->free_data = 1; in buffer_new_write()
324 return buffer; in buffer_new_write()
329 buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) in buffer_read() argument
333 length = ZIP_MIN(length, buffer->size - buffer->offset); in buffer_read()
342 i = buffer->offset / buffer->fragment_size; in buffer_read()
343 fragment_offset = buffer->offset % buffer->fragment_size; in buffer_read()
346 zip_uint64_t left = ZIP_MIN(length - n, buffer->fragment_size - fragment_offset); in buffer_read()
348 memcpy(data + n, buffer->fragments[i] + fragment_offset, left); in buffer_read()
355 buffer->offset += n; in buffer_read()
361 buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) in buffer_seek() argument
363 …zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, e… in buffer_seek()
369 buffer->offset = (zip_uint64_t)new_offset; in buffer_seek()
375 buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) in buffer_write() argument
380 if (buffer->offset + length + buffer->fragment_size - 1 < length) { in buffer_write()
386 if (buffer->offset + length > buffer->nfragments * buffer->fragment_size) { in buffer_write()
387 …zip_uint64_t needed_fragments = (buffer->offset + length + buffer->fragment_size - 1) / buffer->fr… in buffer_write()
389 if (needed_fragments > buffer->fragments_capacity) { in buffer_write()
390 zip_uint64_t new_capacity = buffer->fragments_capacity; in buffer_write()
396 fragments = realloc(buffer->fragments, new_capacity * sizeof(*fragments)); in buffer_write()
403 buffer->fragments = fragments; in buffer_write()
404 buffer->fragments_capacity = new_capacity; in buffer_write()
407 while (buffer->nfragments < needed_fragments) { in buffer_write()
408 if ((buffer->fragments[buffer->nfragments] = malloc(buffer->fragment_size)) == NULL) { in buffer_write()
412 buffer->nfragments++; in buffer_write()
416 i = buffer->offset / buffer->fragment_size; in buffer_write()
417 fragment_offset = buffer->offset % buffer->fragment_size; in buffer_write()
420 zip_uint64_t left = ZIP_MIN(length - n, buffer->fragment_size - fragment_offset); in buffer_write()
422 memcpy(buffer->fragments[i] + fragment_offset, data + n, left); in buffer_write()
429 buffer->offset += n; in buffer_write()
430 if (buffer->offset > buffer->size) { in buffer_write()
431 buffer->size = buffer->offset; in buffer_write()