Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / stream / realrtsp / xbuffer.c
blob6b408d68bbf20c2e0fd0a2e9855346911bacd58a
1 /*
2 * xbuffer code
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)
16 #include <stdlib.h>
17 #include <string.h>
18 #include <inttypes.h>
19 #include "xbuffer.h"
22 typedef struct {
23 uint32_t size;
24 uint32_t chunk_size;
25 } xbuffer_header_t;
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) {
45 if (!buf) {
46 return NULL;
49 free(((uint8_t*)buf)-XBUFFER_HEADER_SIZE);
51 return NULL;
56 void *xbuffer_copyin(void *buf, int index, const void *data, int len) {
57 if (!buf || !data) {
58 return NULL;
61 buf = xbuffer_ensure_size(buf, index+len);
62 memcpy(((uint8_t*)buf)+index, data, len);
64 return buf;
69 void *xbuffer_ensure_size(void *buf, int size) {
70 xbuffer_header_t *xbuf;
71 int new_size;
73 if (!buf) {
74 return 0;
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;
86 return buf;
91 void *xbuffer_strcat(void *buf, char *data) {
93 if (!buf || !data) {
94 return NULL;
97 buf = xbuffer_ensure_size(buf, strlen(buf)+strlen(data)+1);
99 strcat(buf, data);
101 return buf;