4 * Includes a minimalistic replacement for xine_buffer functions used in
5 * Real streaming code. Only function needed by this code are implemented.
7 * Most code comes from xine_buffer.c Copyright (C) 2002 the xine project
9 * WARNING: do not mix original xine_buffer functions with this code!
10 * xbuffers behave like xine_buffers, but are not byte-compatible with them.
11 * You must take care of pointers returned by xbuffers functions (no macro to
12 * do it automatically)
27 #define XBUFFER_HEADER_SIZE sizeof (xbuffer_header_t)
31 void *xbuffer_init(int chunk_size
) {
32 uint8_t *data
=calloc(1,chunk_size
+XBUFFER_HEADER_SIZE
);
34 xbuffer_header_t
*header
=(xbuffer_header_t
*)data
;
36 header
->size
=chunk_size
;
37 header
->chunk_size
=chunk_size
;
39 return data
+XBUFFER_HEADER_SIZE
;
44 void *xbuffer_free(void *buf
) {
49 free(((uint8_t*)buf
)-XBUFFER_HEADER_SIZE
);
56 void *xbuffer_copyin(void *buf
, int index
, const void *data
, int len
) {
61 buf
= xbuffer_ensure_size(buf
, index
+len
);
62 memcpy(((uint8_t*)buf
)+index
, data
, len
);
69 void *xbuffer_ensure_size(void *buf
, int size
) {
70 xbuffer_header_t
*xbuf
;
77 xbuf
= ((xbuffer_header_t
*)(((uint8_t*)buf
)-XBUFFER_HEADER_SIZE
));
79 if (xbuf
->size
< size
) {
80 new_size
= size
+ xbuf
->chunk_size
- (size
% xbuf
->chunk_size
);
81 xbuf
->size
= new_size
;
82 buf
= ((uint8_t*)realloc(((uint8_t*)buf
)-XBUFFER_HEADER_SIZE
,
83 new_size
+XBUFFER_HEADER_SIZE
)) + XBUFFER_HEADER_SIZE
;
91 void *xbuffer_strcat(void *buf
, char *data
) {
97 buf
= xbuffer_ensure_size(buf
, strlen(buf
)+strlen(data
)+1);