aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Buffer.cc
blob: 94e69a56705f6dd84e3646e6437f535bca0cb228 (plain) (blame)
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
#include <qpdf/Buffer.hh>

#include <string.h>

Buffer::Buffer()
{
    init(0, 0, true);
}

Buffer::Buffer(size_t size)
{
    init(size, 0, true);
}

Buffer::Buffer(unsigned char* buf, size_t size)
{
    init(size, buf, false);
}

Buffer::Buffer(Buffer const& rhs)
{
    init(0, 0, true);
    copy(rhs);
}

Buffer&
Buffer::operator=(Buffer const& rhs)
{
    copy(rhs);
    return *this;
}

Buffer::~Buffer()
{
    destroy();
}

void
Buffer::init(size_t size, unsigned char* buf, bool own_memory)
{
    this->own_memory = own_memory;
    this->size = size;
    if (own_memory)
    {
	this->buf = (size ? new unsigned char[size] : 0);
    }
    else
    {
	this->buf = buf;
    }
}

void
Buffer::copy(Buffer const& rhs)
{
    if (this != &rhs)
    {
	this->destroy();
	this->init(rhs.size, 0, true);
	if (this->size)
	{
	    memcpy(this->buf, rhs.buf, this->size);
	}
    }
}

void
Buffer::destroy()
{
    if (this->own_memory)
    {
	delete [] this->buf;
    }
    this->size = 0;
    this->buf = 0;
}

size_t
Buffer::getSize() const
{
    return this->size;
}

unsigned char const*
Buffer::getBuffer() const
{
    return this->buf;
}

unsigned char*
Buffer::getBuffer()
{
    return this->buf;
}