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