blob: 8a1e0850b0a605e1a0f0f4f8f923766587a30cd0 (
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#include "fitz-base.h"
#include "fitz-stream.h"
fz_error *
fz_newbuffer(fz_buffer **bp, int size)
{
fz_buffer *b;
b = *bp = fz_malloc(sizeof(fz_buffer));
if (!b)
return fz_throw("outofmem: buffer struct");
b->refs = 1;
b->ownsdata = 1;
b->bp = fz_malloc(size);
if (!b->bp)
{
fz_free(b);
return fz_throw("outofmem: buffer memory");
}
b->rp = b->bp;
b->wp = b->bp;
b->ep = b->bp + size;
b->eof = 0;
return fz_okay;
}
fz_error *
fz_newbufferwithmemory(fz_buffer **bp, unsigned char *data, int size)
{
fz_buffer *b;
b = *bp = fz_malloc(sizeof(fz_buffer));
if (!b)
return fz_throw("outofmem: buffer struct");
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 fz_okay;
}
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);
}
}
fz_error *
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)
return fz_throw("assert: grow borrowed memory");
newbp = fz_realloc(buf->bp, ep * 2);
if (!newbp)
return fz_throw("outofmem: resize buffer memory");
buf->bp = newbp;
buf->rp = buf->bp + rp;
buf->wp = buf->bp + wp;
buf->ep = buf->bp + ep * 2;
return fz_okay;
}
fz_error *
fz_rewindbuffer(fz_buffer *buf)
{
if (!buf->ownsdata)
return fz_throw("assert: rewind borrowed memory");
memmove(buf->bp, buf->rp, buf->wp - buf->rp);
buf->wp = buf->bp + (buf->wp - buf->rp);
buf->rp = buf->bp;
return fz_okay;
}
|