File: relfile.cc

package info (click to toggle)
ht 2.1.0%2Brepack1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 5,840 kB
  • sloc: cpp: 88,922; ansic: 12,693; sh: 4,083; lex: 226; makefile: 184; yacc: 128
file content (173 lines) | stat: -rw-r--r-- 4,600 bytes parent folder | download | duplicates (7)
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
170
171
172
173
/* 
 *	HT Editor
 *	relfile.cc
 *
 *	Copyright (C) 1999-2002 Stefan Weyergraf
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	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 General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <string.h>

#include "htdebug.h"
#include "relfile.h"
#include "tools.h"

#define MAX_RELOC_ITEM_LEN 8

/*
 *	ht_reloc_file
 */
ht_reloc_file::ht_reloc_file(File *s, bool os)
	: FileLayer(s, os)
{
	relocs = new AVLTree(true);
	enabled = true;
}

ht_reloc_file::~ht_reloc_file()
{
	delete relocs;
}

void ht_reloc_file::finalize()
{
//	relocs->set_compare_keys(compare_keys_uint);
}

int ht_reloc_file::vcntl(uint cmd, va_list vargs)
{
	switch (cmd) {
		case FCNTL_GET_RELOC: {
			bool *e = va_arg(vargs, bool*);
			*e = enabled;
			return 0;
		}
		case FCNTL_SET_RELOC: {
			enabled = (bool)(va_arg(vargs, int));
			return 0;
		}
	}
	return FileLayer::vcntl(cmd, vargs);
}

void ht_reloc_file::insert_reloc(FileOfs o, Object *reloc)
{
	relocs->insert(new KeyValue(new UInt64(o), reloc));
}

uint ht_reloc_file::read(void *buf, uint size)
{
	FileOfs o = tell();
	/* read fine data. */
	uint ret = FileLayer::read(buf, size);
	uint c = ret;
	if (enabled) {
		ObjHandle oh;
		if ((MAX_RELOC_ITEM_LEN+1) <= o) {
			KeyValue kv(new UInt64(o - (MAX_RELOC_ITEM_LEN+1)), NULL);
			oh = relocs->findG(&kv);
		} else {
			oh = relocs->findFirst();
		}
		/* enum through 'relocs' - the tree that contains all our
		 * dear relocations - starting some bytes before the current
		 * (stream) offset to get all the relocation items that may
		 * intersect with our fine read data. */
		while (oh != invObjHandle) {
			KeyValue *kv = (KeyValue *)relocs->get(oh);
			UInt64 *k = (UInt64 *)kv->mKey;

			/* buffer to apply relocation to */
			byte b[MAX_RELOC_ITEM_LEN];
			/* stop if the item is "behind" the area this function
			 * should work on. */
			if (k->value >= o+c) break;

			/* if relocation item intersects with the beginning of
			 * this read, copy buf to b+s */
			uint s = (k->value < o) ? o - k->value : 0;
			/* if relocation item intersects with the end of
			 * this read, copy buf+e to b */
			uint e = (k->value > o) ? k->value - o : 0;

			/* complicated calculation to get the size of the
			 * intended intersection (=: mm) of the read and b. */
			uint l = (k->value + sizeof b > o+c) ?
				k->value + sizeof b - o - c : 0;
			uint mm = MIN(sizeof b - l, sizeof b - s);

			/* probably cleaner to clear it all before we start
			 * because if the read is smaller then the reloc item
			 * we'd have some undefined bytes in b. */
			memset(b, 0, sizeof b);

			/* never memmove beyond Bounds of b. (maybe you didn't call finalize()?) */
			assert(mm+s <= sizeof b);

			/* move read data to b as good as we can. */
			memmove(b+s, ((byte*)buf)+e, mm);
			/* apply complete relocation item. */
			reloc_apply(kv->mValue, b);
			/* overwrite read with relocated/read data as good as we can. */
			memcpy(((byte*)buf)+e, b+s, mm);

			oh = relocs->findG(kv);
		}
	}
	return ret;
}

uint ht_reloc_file::write(const void *buf, uint size)
{
	/* documentation: see read(). */
	FileOfs o;
	if (enabled) {
		o = tell();
		uint c = size;
		ObjHandle oh;
		if ((MAX_RELOC_ITEM_LEN+1) <= o) {
			KeyValue kv(new UInt64(o - (MAX_RELOC_ITEM_LEN+1)), NULL); 
			oh = relocs->findG(&kv);
		} else {
			oh = relocs->findFirst();
		}

		while (oh != invObjHandle) {
			KeyValue *kv = (KeyValue *)relocs->get(oh);
			UInt64 *k = (UInt64 *)kv->mKey;

			byte b[MAX_RELOC_ITEM_LEN];
			if (k->value >= o+c) break;

			uint s = (k->value < o) ? o - k->value : 0;
			uint e = (k->value > o) ? k->value - o : 0;

			uint l = (k->value+sizeof b > o+c) ?
				k->value + sizeof b - o - c : 0;

			memset(b, 0, sizeof b);
			uint mm = MIN(sizeof b - l, sizeof b - s);

			assert(mm+s <= sizeof b);
			memmove(b+s, ((byte*)buf)+e, mm);
			// FIXME: return here ???
			if (!reloc_unapply(kv->mValue, b)) /*return 0*/;
			// FIXME: violation of function declaration "const void *buf"
			memcpy(((byte*)buf)+e, b+s, mm);
			oh = relocs->findG(kv);
		}
	}
	return FileLayer::write(buf, size);
}