A
download bzr.pm
Language: Perl
License: GPL
Copyright: (C) 2005, Andrew Rodland
LOC: 205
Project Info
WikiOnCD
Server: BerliOS (SVN)
Type: svn
...\w\wikioncd\trunk\wikioncd\
   build-dictionary.pl
   bzr-inline.pm
   bzr.pm
   convert.pl
   count-words.pl
   dictcomp.pl
   dictcomp.pm
   gen-dict.pl
   inline-bzip2.pl
   parser.pl
   retrieve.pl
   server.pl
   w2h.pl

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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
###
# Compress::Bzip2::RandomAccess
# Part of WikiOnCD
# Copyright (C) 2005, Andrew Rodland <arodland@entermail.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

package Compress::Bzip2::RandomAccess;

use Compress::Bzip2;

our $default_blocksize = 600;
our $debug = 0;

sub debug {
	our $debug;
	
	print STDERR @_ if $debug;
}

sub new_to_file {
	my ($class, $file, $blocksize) = @_;

	my $package = ref($class) || $class || __PACKAGE__;

	open my $fh, '>:raw', $file or return undef;

	our $default_blocksize;
	
	$blocksize = $blocksize || $default_blocksize;

	print $fh pack("N", 0xc001d00d), pack("N", $blocksize);

	$blocksize *= 1024;
	
	my $self = {
		fh => $fh,
		blocksize => $blocksize,
	};

	bless $self, $package;
}

sub file {
	my ($self, $filename, $len) = @_;

	print { $self->{fh} } pack("N", $len);
	print { $self->{fh} } "$filename\n";
	
	debug "wrote header for $len bytes file $filename.\n";
}

sub data {
	my ($self, $data) = @_;
	$self->{buffer} .= $data;

	while (length($self->{buffer}) >= $self->{blocksize}) {
		$self->flush;
	}
}


sub write_file {
	my ($self, $filename, $data) = @_;

	my $len = length $data;

	$self->file($filename, $len);
	$self->data($data);
}

sub write_file_from_file {
	my ($self, $key, $filename) = @_;

	my $data;
	
	open my $fh, '<:raw', $filename;
	my $size = -s $fh;
	
	$self->file($key, $size);

	while (read $fh, $data, $self->{blocksize}) {
		$self->data($data);
	}
	close $fh;
}


sub flush {
	my ($self) = @_;
	
	my $buf = substr $self->{buffer}, 0, $self->{blocksize}, '';

	my $compressed = Compress::Bzip2::compress($buf);

	my $len = length $compressed;
	
	if ($len >= 0x80000000) {
		die "Trying to write a block of 2^31 bytes or more. Sorry.";
	}
	
	print { $self->{fh} } pack("N", $len | 0x80000000);
	print { $self->{fh} } $compressed;

	debug "Wrote header for $len bytes block.\n";	
}

sub close_for_write {
	my ($self) = @_;
	
	$self->flush;
	close $self->{fh};
}

sub new_from_file {
	my ($class, $file) = @_;

	my $package = ref($class) || $class || __PACKAGE__;

	open my $fh, '<:raw', $file or return undef;

	read $fh, my $magic, 4;
	read $fh, my $blocksize, 4;
	
	$blocksize = unpack("N", $blocksize);
	
	$blocksize *= 1024;

	if (!$blocksize) {
		return undef;
	}

	my $self = {
		fh => $fh,
		blocksize => $blocksize,
	};

	bless $self, $package;

	return $self;

}

sub cache_offsets {
	my $self = shift;

	my $file_pos = 0; my $block_pos = 8; my $block = 0;

	seek $self->{fh}, 0, SEEK_SET; # Avoid possible EOF on first read

	while (!eof($self->{fh})) { # Won't likely trigger, but...

		seek $self->{fh}, $block_pos, SEEK_SET;
		
		read $self->{fh}, my $code, 4;
		last if eof($self->{fh});
		my $len = unpack("N", $code);

		if ($len & 0x80000000) {
			$len &= 0x7fffffff;
			$self->{block}[$block] = [ $block_pos, $len ];
			$block_pos += 4 + $len;
			$block ++;
#			debug "(C) found $len bytes block.\n";
			
		} else {
			my $filename = readline $self->{fh};
			$block_pos += 4 + length $filename;
			chomp($filename);

			$self->{files}{$filename} = [ $file_pos, $len ];
			$file_pos += $len;

#			debug "(C) found $len bytes file $filename.\n";
		}
	}
}	

sub find_file_cached {
	my ($self, $filename) = @_;

	debug "In FFC for $filename.\n";
	
	my ($fpos, $flen) = @{ $self->{files}{$filename} };
	
	my $block = int($fpos / $self->{blocksize});
	my $skip = $fpos % $self->{blocksize};
	
	my ($bpos, $blen) = @{ $self->{block}[$block] };

	seek $self->{fh}, $bpos, SEEK_SET;

	return ($flen, $skip);
}

sub find_file_nocache {
	my ($self, $wantfile) = @_;
	
	debug "In FFNC.\n";

	my $file_pos = 0; my $block_pos = 8; my $block = 0;
	my ($want_pos, $want_len);

	seek $self->{fh}, 0, SEEK_SET; # Avoid possible EOF on first read.

	while (!eof($self->{fh})) {

		seek $self->{fh}, $block_pos, SEEK_SET;
		read $self->{fh}, my $code, 4 or return undef;
		my $len = unpack("N", $code);

		if ($len & 0x80000000) {
			if (defined $want_pos && ($block + 1) * $self->{blocksize} >= $want_pos) {
				debug "(U) And we're there!\n";
				seek $self->{fh}, $block_pos, SEEK_SET; #Back it up a bit.
				return ($want_len, $want_pos % $self->{blocksize});
			}
				
			$len &= 0x7fffffff;
			$block_pos += 4 + $len;
			$block ++;

			debug "(U) found $len bytes block.\n";
			
		} else {
			my $filename = readline $self->{fh};
			$block_pos += 4 + length $filename;
			chomp($filename);

			debug "(U) found $len bytes file $filename.\n";

			if ($filename eq $wantfile) {
				$want_pos = $file_pos;
				$want_len = $len;
				debug "(U) That's it!\n";
			}
			$file_pos += $len;
		}
	}
}

sub decompress_one_block {
	my $self = shift;
	my $len;

	while (!$len) {	
		my $code;
		read $self->{fh}, $code, 4;
		$len = unpack("N", $code);
		if ($len & 0x80000000) {
			$len &= 0x7fffffff;
		} else {
			readline $self->{fh};
			$len = 0;
			debug "Ignored filename.\n";
		}
	}

	my $data;
	read $self->{fh}, $data, $len or die $!;

	$data = Compress::Bzip2::decompress($data);
	
	my $elen = length $data;
	
	debug "Read $len bytes block ($elen bytes).\n";
	return $data;

}

sub read_file {
	my ($self, $file) = @_;

	my ($len, $skip);
	
	if (defined $self->{block} && defined $self->{files}) {
		($len, $skip) = $self->find_file_cached($file);
	} else {
		($len, $skip) = $self->find_file_nocache($file);
	}

	unless ($len) {
		debug "File $file not found.\n";
		return undef;
	}

	debug "Getting $len bytes data.\n";

	my $data = $self->decompress_one_block();
	substr ($data, 0, $skip) = undef;
	debug ",";

	while (length($data) < $len) {
		$data .= $self->decompress_one_block();
		debug ".";
	}

	substr($data, $len) = undef;
	debug "\n";
	return $data;
}

sub close_for_read {
	my ($self) = @_;

	close $self->{fh};
}

1;

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us