Home | History | Annotate | Download | only in C
      1 /* 7zBuf2.c -- Byte Buffer
      2 2014-08-22 : 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   if (size != 0)
     38   {
     39     memcpy(p->data + p->pos, buf, size);
     40     p->pos += size;
     41   }
     42   return 1;
     43 }
     44 
     45 void DynBuf_Free(CDynBuf *p, ISzAlloc *alloc)
     46 {
     47   alloc->Free(alloc, p->data);
     48   p->data = 0;
     49   p->size = 0;
     50   p->pos = 0;
     51 }
     52