blob: 024dbea95c2e9b4d27c05534f1f6e4e60e499188 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include "fitz_base.h"
#include "fitz_stream.h"
fz_buffer *
fz_newbuffer(int size)
{
fz_buffer *b;
b = fz_malloc(sizeof(fz_buffer));
b->refs = 1;
b->ownsdata = 1;
b->bp = fz_malloc(size);
b->rp = b->bp;
b->wp = b->bp;
b->ep = b->bp + size;
b->eof = 0;
return b;
}
fz_buffer *
fz_newbufferwithmemory(unsigned char *data, int size)
{
fz_buffer *b;
b = fz_malloc(sizeof(fz_buffer));
b->refs = 1;
b->ownsdata = 0;
b->bp = data;
b->rp = b->bp;
b->wp = b->bp + size;
b->ep = b->bp + size;
b->eof = 0;
return b;
}
fz_buffer *
fz_keepbuffer(fz_buffer *buf)
{
buf->refs ++;
return buf;
}
void
fz_dropbuffer(fz_buffer *buf)
{
if (--buf->refs == 0)
{
if (buf->ownsdata)
fz_free(buf->bp);
fz_free(buf);
}
}
void
fz_growbuffer(fz_buffer *buf)
{
unsigned char *newbp;
int rp = buf->rp - buf->bp;
int wp = buf->wp - buf->bp;
int ep = buf->ep - buf->bp;
if (!buf->ownsdata)
{
fz_warn("assert: grow borrowed memory");
return;
}
newbp = fz_realloc(buf->bp, ep * 2);
buf->bp = newbp;
buf->rp = buf->bp + rp;
buf->wp = buf->bp + wp;
buf->ep = buf->bp + ep * 2;
}
void
fz_rewindbuffer(fz_buffer *buf)
{
if (!buf->ownsdata)
{
fz_warn("assert: rewind borrowed memory");
return;
}
memmove(buf->bp, buf->rp, buf->wp - buf->rp);
buf->wp = buf->bp + (buf->wp - buf->rp);
buf->rp = buf->bp;
}
|