aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/common/entriesblk.cpp
blob: f4fbdeb68f3680705e7bd4da3e5ce66cae8faaec (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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <entriesblk.h>
#include <stdlib.h>
#include <string.h>

SWORD_NAMESPACE_START

const int EntriesBlock::METAHEADERSIZE = 4;
	// count(4);
const int EntriesBlock::METAENTRYSIZE = 8;
	// offset(4); size(4);

EntriesBlock::EntriesBlock(const char *iBlock, unsigned long size) {
	block = (char *)calloc(1, size);
	memcpy(block, iBlock, size);
}


EntriesBlock::EntriesBlock() {
	block = (char *)calloc(1, sizeof(__u32));
}


EntriesBlock::~EntriesBlock() {
	free(block);
}


void EntriesBlock::setCount(int count) {
	__u32 rawCount = archtosword32(count);
	memcpy(block, &rawCount, sizeof(__u32));
}


int EntriesBlock::getCount() {
	__u32 count = 0;
	memcpy(&count, block, sizeof(__u32));
	count = swordtoarch32(count);
	return count;
}


void EntriesBlock::getMetaEntry(int index, unsigned long *offset, unsigned long *size) {
	__u32 rawOffset = 0;
	__u32 rawSize = 0;
	*offset = 0;
	*size = 0;
	if (index >= getCount())	// assert index < count
		return;

	// first 4 bytes is count, each 6 bytes after is each meta entry
	memcpy(&rawOffset, block + METAHEADERSIZE + (index * METAENTRYSIZE), sizeof(rawOffset));
	memcpy(&rawSize, block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), sizeof(rawSize));

	*offset = (unsigned long)swordtoarch32(rawOffset);
	*size   = (unsigned long)swordtoarch32(rawSize);
}


void EntriesBlock::setMetaEntry(int index, unsigned long offset, unsigned long size) {
	__u32 rawOffset = archtosword32(offset);
	__u32 rawSize = archtosword32(size);

	if (index >= getCount())	// assert index < count
		return;

	// first 4 bytes is count, each 6 bytes after is each meta entry
	memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE), &rawOffset, sizeof(rawOffset));
	memcpy(block + METAHEADERSIZE + (index * METAENTRYSIZE) + sizeof(rawOffset), &rawSize, sizeof(rawSize));
}


const char *EntriesBlock::getRawData(unsigned long *retSize) {
	unsigned long max = 4;
	int loop;
	unsigned long offset;
	unsigned long size;
	for (loop = 0; loop < getCount(); loop++) {
		getMetaEntry(loop, &offset, &size);
		max = ((offset + size) > max) ? (offset + size) : max;
	}
	*retSize = max;
	return block;
}


int EntriesBlock::addEntry(const char *entry) {
	unsigned long dataSize;
	getRawData(&dataSize);
	unsigned long  len = strlen(entry);
	unsigned long offset;
	unsigned long size;
	int count = getCount();
	unsigned long dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);
	// new meta entry + new data size + 1 because null 
	block = (char *)realloc(block, dataSize + METAENTRYSIZE + len + 1);
	// shift right to make room for new meta entry
	memmove(block + dataStart + METAENTRYSIZE, block + dataStart, dataSize - dataStart);

	for (int loop = 0; loop < count; loop++) {
		getMetaEntry(loop, &offset, &size);
		if (offset) {	// if not a deleted entry
			offset += METAENTRYSIZE;
			setMetaEntry(loop, offset, size);
		}
	}

	offset = dataSize;	// original dataSize before realloc
	size = len + 1;
	// add our text to the end
	memcpy(block + offset + METAENTRYSIZE, entry, size);
	// increment count
	setCount(count + 1);
	// add our meta entry
	setMetaEntry(count, offset + METAENTRYSIZE, size);
	// return index of our new entry
	return count;
}


const char *EntriesBlock::getEntry(int entryIndex) {
	unsigned long offset;
	unsigned long size;
	static char *empty = "";

	getMetaEntry(entryIndex, &offset, &size);
	return (offset) ? block+offset : empty;
}


unsigned long EntriesBlock::getEntrySize(int entryIndex) {
	unsigned long offset;
	unsigned long size;

	getMetaEntry(entryIndex, &offset, &size);
	return (offset) ? size : 0;
}


void EntriesBlock::removeEntry(int entryIndex) {
	unsigned long offset;
	unsigned long size, size2;
	unsigned long dataSize;
	getRawData(&dataSize);
	getMetaEntry(entryIndex, &offset, &size);
	unsigned long len = size - 1;
	int count = getCount();
	unsigned long dataStart = METAHEADERSIZE + (count * METAENTRYSIZE);

	if (!offset)	// already deleted
		return;

	// shift left to retrieve space used for old entry
	memmove(block + offset, block + offset + size, dataSize - (offset + size));

	// fix offset for all entries after our entry that were shifted left
	for (int loop = entryIndex + 1; loop < count; loop++) {
		getMetaEntry(loop, &offset, &size2);
		if (offset) {	// if not a deleted entry
			offset -= size;
			setMetaEntry(loop, offset, size2);
		}
	}

	// zero out our meta entry
	setMetaEntry(entryIndex, 0L, 0);
}


SWORD_NAMESPACE_END