diff options
author | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2021-12-27 00:00:00 +0000 |
---|---|---|
committer | Igor Pavlov <87184205+ip7z@users.noreply.github.com> | 2022-03-18 15:35:13 +0500 |
commit | f19f813537c7aea1c20749c914e756b54a9c3cf5 (patch) | |
tree | 816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /C/7zBuf2.c | |
parent | 98e06a519b63b81986abe76d28887f6984a7732b (diff) | |
download | 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.tar.gz 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.tar.bz2 7zip-f19f813537c7aea1c20749c914e756b54a9c3cf5.zip |
'21.07'21.07
Diffstat (limited to 'C/7zBuf2.c')
-rw-r--r-- | C/7zBuf2.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/C/7zBuf2.c b/C/7zBuf2.c new file mode 100644 index 0000000..2083474 --- /dev/null +++ b/C/7zBuf2.c | |||
@@ -0,0 +1,52 @@ | |||
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 | |||
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, 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 | |||
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 | } | ||