forked from dazedsheep/DASHTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.cpp
More file actions
executable file
·125 lines (101 loc) · 3.48 KB
/
Copy pathchunk.cpp
File metadata and controls
executable file
·125 lines (101 loc) · 3.48 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
*****************************************************************************
* Copyright (C) 2012 - 2014 Institute of Information Technology,
* Alpen-Adria-Universität Klagenfurt
*
* Created on: May 20, 2014
* Authors: Benjamin Rainer <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "chunk.h"
#include "log.h"
// a chunk will hold the data, it will not free anything that is appended to it
chunk::chunk()
{
this->size = DEFAULT_CHUNK_SIZE;
mem = (uint8_t *)calloc(size, sizeof(uint8_t));
memset(mem, 0x66, this->size * sizeof(uint8_t));
offset = 0;
allowGrowth = false;
}
chunk::chunk(uint32_t size, bool allowGrowth)
{
this->size = (size > 0 ? size : DEFAULT_CHUNK_SIZE);
mem = (uint8_t *)calloc(this->size, sizeof(uint8_t));
memset(mem, 0x66, this->size * sizeof(uint8_t));
offset = 0;
this->allowGrowth = allowGrowth;
}
chunk::~chunk()
{
if(mem != NULL) free(mem);
}
uint32_t chunk::getSize()
{
return this->size;
}
uint32_t chunk::getOffset()
{
return this->offset;
}
void chunk::grow(uint32_t len)
{
uint32_t oldsize = this->size;
this->size += len;
uint8_t *newMemLocation = (uint8_t *) malloc(this->size * sizeof(uint8_t));
memcpy(newMemLocation, mem, sizeof(uint8_t) * oldsize);
free(mem);
mem = newMemLocation;
// the offset can be let untouched because we just extended our space. ..
}
int32_t chunk::append(chunk *ch)
{
int32_t ret;
if((this->offset + ch->getSize()) > size)
{
if(!this->allowGrowth)
{
mde_log(MDE_LOG_ERROR, "%s: error at appending data to current chunk (0x%x), chunk is smaller than the current data to append plus the precious size.\n", __FILE__, this);
return _ERR_OUT_OF_BOUNDS;
}else
this->grow( (ch->getSize() > DEFAULT_CHUNK_SIZE ? ch->getSize() : DEFAULT_CHUNK_SIZE) ); // do not only grow by the wanted amount if len << DEFAULT_CHUNK_SIZE
}
ret = this->append(ch->getPointer(), ch->getSize());
delete ch; // get rid of the merged chunk
return ret;
}
int32_t chunk::append(uint8_t *rdata, uint32_t len)
{
if((this->offset + len) > size)
{
if(!this->allowGrowth) return _ERR_OUT_OF_BOUNDS;
else
this->grow( (len > DEFAULT_CHUNK_SIZE ? len : DEFAULT_CHUNK_SIZE) ); // do not only grow by the wanted amount if len << DEFAULT_CHUNK_SIZE
}
memcpy(this->mem + this->offset, rdata, len);
this->offset += len;
return len;
}
uint8_t *chunk::getPointer()
{
return this->mem;
}
chunk *chunk::dup()
{
chunk *c = new chunk(size, allowGrowth);
c->append(this->getPointer(), this->getOffset());
return c;
}