1 /* 7zBuf2.c -- Byte Buffer
2 2017-04-03 : Igor Pavlov : Public domain */
3
4 #include "Precomp.h"
5
6 #include <string.h>
7
8 #include "7zBuf.h"
9
DynBuf_Construct(CDynBuf * p)10 void DynBuf_Construct(CDynBuf *p)
11 {
12 p->data = 0;
13 p->size = 0;
14 p->pos = 0;
15 }
16
DynBuf_SeekToBeg(CDynBuf * p)17 void DynBuf_SeekToBeg(CDynBuf *p)
18 {
19 p->pos = 0;
20 }
21
DynBuf_Write(CDynBuf * p,const Byte * buf,size_t size,ISzAllocPtr alloc)22 int DynBuf_Write(CDynBuf *p, const Byte *buf, size_t size, ISzAllocPtr 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 *)ISzAlloc_Alloc(alloc, newSize);
30 if (!data)
31 return 0;
32 p->size = newSize;
33 if (p->pos != 0)
34 memcpy(data, p->data, p->pos);
35 ISzAlloc_Free(alloc, p->data);
36 p->data = data;
37 }
38 if (size != 0)
39 {
40 memcpy(p->data + p->pos, buf, size);
41 p->pos += size;
42 }
43 return 1;
44 }
45
DynBuf_Free(CDynBuf * p,ISzAllocPtr alloc)46 void DynBuf_Free(CDynBuf *p, ISzAllocPtr alloc)
47 {
48 ISzAlloc_Free(alloc, p->data);
49 p->data = 0;
50 p->size = 0;
51 p->pos = 0;
52 }
53