Home | History | Annotate | Download | only in C
      1 /* 7zBuf2.c -- Byte Buffer
      2 2008-10-04 : Igor Pavlov : Public domain */
      3 
      4 #include <string.h>
      5 #include "7zBuf.h"
      6 
      7 void DynBuf_Construct(CDynBuf *p)
      8 {
      9   p->data = 0;
     10   p->size = 0;
     11   p->pos = 0;
     12 }
     13 
     14 void DynBuf_SeekToBeg(CDynBuf *p)
     15 {
     16   p->pos = 0;
     17 }
     18 
     19 int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAlloc *alloc)
     20 {
     21   if (size > p->size - p->pos)
     22   {
     23     size_t newSize = p->pos + size;
     24     Byte *data;
     25     newSize += newSize / 4;
     26     data = (Byte *)alloc->Alloc(alloc, newSize);
     27     if (data == 0)
     28       return 0;
     29     p->size = newSize;
     30     memcpy(data, p->data, p->pos);
     31     alloc->Free(alloc, p->data);
     32     p->data = data;
     33   }
     34   memcpy(p->data + p->pos, buf, size);
     35   p->pos += size;
     36   return 1;
     37 }
     38 
     39 void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc)
     40 {
     41   alloc->Free(alloc, p->data);
     42   p->data = 0;
     43   p->size = 0;
     44   p->pos = 0;
     45 }
     46