Storage.pm untested

filter: Statementstmt 0.0% 0 / 300 Branchbran 0.0% 0 / 102 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 55 Podpod 0.0% 0 / 50 totaltotal 0.0% 0 / 579 CC 52 SCAR 37.4
CC
52
Cov
0%
_get_iolist
210.0
_init_doc
90.0
_read_ppss_buf
30.0
CRAP 42.0
1 #
2 # $Id: Storage.pm,v 1.8 1998/04/28 00:39:43 schwartz Exp $
3 #
4 # OLE::Storage, a Structured Storage interface
5 #
6 # Also known as: Laola filesystem.
7 #
8 # (POD documentation at end of file)
9 #
10 # This perl 5 library gives raw access to "Ole/Com" documents. These are
11 # documents like created by Microsoft Word 6.0+ or newer Star Divisions
12 # Word by using so called "Structured Storage" technology. Write access
13 # still is nearly not supported, but will be done one day. This library
14 # should have come along with a couple of other files. The whole
15 # distribution can be found at:
16 #
17 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/index.html
18 # or
19 # http://www.cs.tu-berlin.de/~schwartz/pmh/index.html
20 #
21 # Copyright (C) 1996, 1997 Martin Schwartz
22 #
23 # This program is free software; you can redistribute it and/or modify
24 # it under the terms of the GNU General Public License as published by
25 # the Free Software Foundation; either version 2 of the License, or
26 # (at your option) any later version.
27 #
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, you should find it at:
35 #
36 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
37 #
38 # Diese Veröffentlichung erfolgt ohne Berücksichtigung eines eventuellen
39 # Patentschutzes. Warennamen werden ohne Gewährleistung einer freien
40 # Verwendung benutzt. ;-)
41 #
42 # Contact: schwartz@cs.tu-berlin.de
43 #
44  
45 #
46 # Really important topics still MISSING until now:
47 #
48 # - Human rights and civil rights where you live.
49 #
50 # - Reformfraktion president for Technische Universität Berlin.
51 #
52 # - creating documents
53 # - many property set things:
54 # * documentation of variable types
55 # * code page support
56 # - consistant name giving, checked against MS'
57 #
58  
59 package OLE::Storage;
60 my $VERSION=do{my@R=('$Revision: 1.8 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R};
61 $[=0;
62  
63 #
64 # Storage.pm has public method interfaces and private functions. Functions
65 # assume a local($S) as $self. I put some efforts into making it strict-proof,
66 # especially to "methodize" all functions, but I didn't like the resulting
67 # code. May be I'll regret this once ;-), but momentarily I'm quite ok with:
68 #
69 no strict; $^W=0;
70  
71 #
72 # Abbreviations
73 #
74 # bbd Big Block Depot
75 # pps Property Storage
76 # ppset Property Set
77 # ppss Property Set Storage
78 # sb Start Block
79 # sbd Small Block Depot
80 #
81  
82 use OLE::Storage::Std;
83 use OLE::Storage::Var();
84 use OLE::Storage::Io();
85 use OLE::Storage::Iolist();
86  
87 sub Startup { my $S=shift; $S->{STARTUP} = shift if @_; $S->{STARTUP} }
88 sub Var { my $S=shift; $S->{VAR} = shift if @_; $S->{VAR} }
89 sub NewVar { OLE::Storage::Var::new qw(OLE::Storage::Var) }
90  
91 sub _error { $S->{STARTUP} ? $S->{STARTUP}->error(@_) : 0 }
92  
93 ##
94 ## File and directory handling
95 ##
96  
97 sub check {
98 #
99 # 1||0 = check($Startup, $file [,$mode [,\$streambuf]]);
100 #
101 # to do!
102 my ($proto, $Startup, $name, $mode, $bufR) = @_;
103 my $Io = OLE::Storage::Io->open($Startup, $name, $mode, $bufR)
104 }
105  
106 sub open {
107 #
108 # $Doc||0 = open($Startup, $Var, $file [,$mode [,\$streambuf]]);
109 #
110 # mode bitmask (0 is default):
111 #
112 # Bit 0 (0 read only, 1 read and write)
113 # Bit 4 (0 file mode, 1 buffer mode)
114 #
115 # Own errors:
116 #
117 # 1 "'Var' object not specified!"
118 # 2 IO->open error
119 # 3 _init_doc error
120 #
121 my ($proto, $Startup, $Var, $name, $mode, $bufR) = @_;
122 my $class = ref($proto) || $proto;
123 local $S = bless(_init_vars(), $class);
124  
125 $S->Startup($Startup);
126  
127 unless ($S->Var($Var)) {
128 return _error("'Var' object not specified!", 1)
129 }
130  
131 unless ($S->{IO} = OLE::Storage::Io->open($Startup, $name, $mode, $bufR)) {
132 return _error("", 2);
133 }
134  
135 unless (_init_doc()) {
136 $S->{IO} -> close($bufR);
137 return _error("", 3);
138 }
139  
140 $S;
141 }
142  
143 sub close {
144 #
145 # 1 = close([\$streambuf])
146 #
147 my ($S, $bufR) = @_;
148 $S->{IO}->close($bufR);
149 1}
150  
151 sub is_directory {
152 #
153 # 1||0 = is_directory($pps)
154 #
155 my ($S, $pps) = @_;
156 ($S->{PPS}[$pps]->{TYPE} == 1) || $S->is_root($pps);
157 }
158  
159 sub is_stream { goto &is_file }
160  
161 sub is_file {
162 #
163 # 1||0 = is_file($pps)
164 #
165 shift->{PPS}[shift]->{TYPE} == 2;
166 }
167  
168 sub is_root {
169 #
170 # 1||0 = is_root($pps)
171 #
172 shift->{PPS}[shift]->{TYPE} == 5;
173 }
174  
175 sub dirhandles {
176 #
177 # @pps = dirhandles($pps);
178 #
179 local ($S, $pps) = @_;
180 $S->is_directory($pps)
181 && _get_ppss_chain($S->{PPS}[$pps]->{DIR})
182 ;
183 }
184  
185 sub directory {
186 #
187 # 1||0 = directory($pps, \%directory [,property_method]);
188 #
189 my ($S, $pps, $dirR, $method) = @_;
190 return 0 if !$S->is_directory($pps);
191 for ($S->dirhandles($pps)) {
192 if ($method) {
193 $$dirR{$S->name($_)->$method()} = $_;
194 } else {
195 $$dirR{$S->name($_)} = $_;
196 }
197 }
198 1}
199  
200 sub clsid {
201 #
202 # Property(lpwstr) = clsid($pps);
203 #
204 $_[0]->{PPS}[$_[1]]->{CLSID};
205 }
206  
207 sub color {
208 #
209 # 0(Black)||1(Red) = color($pps);
210 #
211 $_[0]->{PPS}[$_[1]]->{COLOR};
212 }
213  
214 sub name {
215 #
216 # Property(lpwstr) = name($pps);
217 #
218 $_[0]->{PPS}[$_[1]]->{NAME};
219 }
220  
221 sub date {
222 #
223 # Property(filetime) = date($pps [,$par])
224 #
225 my ($S, $pps, $par) = @_;
226 return $S->Var->error("No timestamps on files!") if $S->is_file();
227 $S->{PPS}[$pps]->{$par? TS1: TS2};
228 }
229  
230 sub size {
231 #
232 # $filesize||undef = size($pps);
233 #
234 my ($S, $pps) = @_;
235 $S->is_file($pps)
236 && $S->{PPS}[$pps]->{SIZE}
237 || undef;
238 }
239  
240 sub read {
241 #
242 # 1||0 = read($pps, \$buf [,$offset, $size]);
243 #
244 local $S=shift; _rw_file("r", @_);
245 }
246  
247 sub modify {
248 #
249 # 1||0 = modify($pps, \$buf, $offset, $size);
250 #
251 local $S=shift; _rw_file("w", @_);
252 }
253  
254 ##
255 ## Trash handling
256 ##
257  
258 sub size_trash {
259 #
260 # $sizeof_trash_section = size_trash($type)
261 #
262 local $S = shift; _get_trash_size(@_);
263 }
264  
265 sub read_trash {
266 #
267 # 1||0 = read_trash ($type, \$buf [,$offset, $size]);
268 #
269 local $S = shift; _rw_trash("r", @_);
270 }
271  
272 sub modify_trash {
273 #
274 # 1||0 = modify_trash ($type, \$buf [,$offset, $size]);
275 #
276 local $S = shift; _rw_trash("w", @_);
277 }
278  
279  
280 ##
281 ## ----------------------------- "private" ---------------------------------
282 ##
283  
284 #
285 # open ->
286 #
287  
288 sub _init_vars {
289 {
290 IO => undef, # IO.pm object
291 CURPPS => undef, # current pps
292 CURPPS_IO => undef, # Iolist for current pps
293 HEAD => undef, # OLE Header struct
294 PPS => [], # OLE Property structs
295 USE => {
296 BB => [], # big blocks usage
297 SB => [], # small blocks usage
298 KNOWN => undef
299 },
300 TRASH => {
301 IO => [], # array of iolists
302 S => [], # size of corresponding iolist
303 KNOWN => undef
304 },
305 BL => {
306 B_NUM => undef, # max big block
307 B_D => [], # big block depot
308 B_DL => [], # big block depot list
309 S_NUM => undef, # max small block
310 S_D => [], # small block depot
311 S_DL => [], # small block depot list
312 ROOTL => [], # root blocks list
313 SBL => [], # small blocks list
314 },
315 }
316 }
317  
318 sub _init_doc {
319 #
320 # read bbd,
321 # get bbd -> root-chain, get bbd -> sbd-chain
322 #
323 # Own errors:
324 # 1 "$fn is no Ole / Compound Document!"
325 # 2 "Document is corrupt (size too small)."
326 # 3 "Document is corrupt (no root entry defined)."
327 # 4 "Document is corrupt (Cannot read root entry)."
328 #
329 my ($i, $tmp, @tmp);
330  
331 # little integrity check 1
332 {
333 unless (
334 (read_long($S->{IO}, 0)==0xe011cfd0) &&
335 (read_long($S->{IO}, 4)==0xe11ab1a1)
336 ) {
337 my $fn = '"'.$S->{IO}->name().'"';
338 return _error("$fn is no Ole / Compound Document!", 1);
339 }
340 }
341  
342 # header data
343 {
344 $S->{HEAD}->{B_S} = 2 ** read_word($S->{IO}, 0x1e);
345 $S->{HEAD}->{S_S} = 2 ** read_word($S->{IO}, 0x20);
346 $S->{HEAD}->{B_DL_L} = undef;
347 $S->{HEAD}->{B_D_NUM} = read_long($S->{IO}, 0x2c);
348 $S->{HEAD}->{ROOT_SB} = read_long($S->{IO}, 0x30);
349 $S->{HEAD}->{B_S_MIN} = read_long($S->{IO}, 0x38);
350 $S->{HEAD}->{S_D_SB} = read_long($S->{IO}, 0x3c);
351 $S->{HEAD}->{S_D_NUM} = read_long($S->{IO}, 0x40);
352 $S->{HEAD}->{B_XD_SB} = read_long($S->{IO}, 0x44);
353 $S->{HEAD}->{B_XD_NUM} = read_long($S->{IO}, 0x48);
354  
355 my $root = _h_s() + ($S->{HEAD}->{ROOT_SB})*_b_s();
356 $S->{HEAD}->{S_SB} = read_long($S->{IO}, $root+0x74);
357 $S->{BL}->{S_NUM} = int (read_long($S->{IO}, $root+0x78)/_s_s() -1);
358 }
359  
360 # little integrity check 2
361 {
362 $S->{BL}->{B_NUM} = int ( ($S->{IO}->size()-_h_s()) / _b_s() -1 );
363 return _error("Document is corrupt (size too small).", 2)
364 if $S->{BL}->{B_NUM}<1
365 ;
366 }
367  
368 # construct big block depot Iolist
369 {
370 my $Iolist = new OLE::Storage::Iolist([0x4c], [_h_s()-0x4c]);
371 my $bl = $S->{HEAD}->{B_XD_SB};
372 for (1..$S->{HEAD}->{B_XD_NUM}) {
373 $Iolist->append(_h_s() + $bl*_b_s(), _b_s()-4);
374 $bl = read_long($S->{IO}, _h_s() + $bl*_b_s() + _b_s()-4);
375 last if $bl >= _B_EOC();
376 }
377 $S->{HEAD}->{B_DL_L} = $Iolist;
378 }
379  
380 # read big block depot
381 {
382 return 0 if !$S->{IO}->read_iolist(\$tmp,
383 $S->{HEAD}->{B_DL_L}
384 );
385 @{$S->{BL}->{B_DL}} = get_nlong($S->{HEAD}->{B_D_NUM}, \$tmp, 0);
386  
387 return 0 if !$S->{IO}->read_iolist(\$tmp,
388 _get_iolist(3, 0, _S_MAX(), 0, $S->{BL}->{B_DL})
389 );
390 @{$S->{BL}->{B_D}} = get_nlong($S->{BL}->{B_NUM}+1, \$tmp, 0);
391 }
392  
393 # read small block depot
394 {
395 $S->{BL}->{S_DL} = _get_list_from_depot($S->{HEAD}->{S_D_SB}, "B");
396 return 0 if !$S->{IO}->read_iolist(\$tmp,
397 _get_iolist(3, 0, _S_MAX(), 0, $S->{BL}->{S_DL})
398 );
399 @{$S->{BL}->{S_D}} = get_nlong($S->{BL}->{S_NUM}+1, \$tmp, 0);
400 }
401  
402 # root chain
403 {
404 return _error("Document is corrupt (no root entry defined).", 3) if !@{
405 $S->{BL}->{ROOTL} = _get_list_from_depot($S->{HEAD}->{ROOT_SB}, "B")
406 };
407 }
408  
409 # small block chain
410 {
411 # An empty small block list is ok.
412 $S->{BL}->{SBL} = _get_list_from_depot($S->{HEAD}->{S_SB}, "B");
413 }
414  
415 # property storages, just read root entry.
416 {
417 return _error ("Document is corrupt (Cannot read root entry).", 4)
418 if !_read_ppss(0)
419 ;
420 }
421  
422 1}
423  
424  
425 ##
426 ## -------------------------- File IO ------------------------------
427 ##
428  
429 sub _rw_file {
430 #
431 # 1||0 = _rw_file ("r"||"w", $pps, \$buf||$buf||undef [,$offset, $size])
432 # $buf||undef = _rw_file ("r" , $pps, undef, [,$offset, $size])
433 #
434 my ($rw, $pps, $buf_or_bufR) = (shift, shift, shift);
435  
436 return _error("pps is no file!") if !$S->is_file($pps);
437 my $size = $S->{PPS}[$pps]->{SIZE};
438 return 0 if !(($o, $l) = $S->{IO}->default_iosize($size, $rw, @_));
439  
440 return _error("Bad document structure!")
441 if !_get_curpps_iolist($pps)
442 ;
443  
444 $S->{IO}->rw_iolist(
445 $rw, $buf_or_bufR, _get_iolist(4, $o, $l, 0, $S->{CURPPS_IO})
446 );
447 }
448  
449 sub _get_curpps_iolist {
450 #
451 # 1||0 = _get_curpps_iolist($pps)
452 #
453 # Gets Iolist for current $pps
454 #
455 my $pps = shift;
456 return 1 if (defined $S->{CURPPS}) && ($S->{CURPPS}==$pps);
457 $S->{CURPPS_IO} = _get_iolist(
458 _bdepottype($pps), 0, $S->{PPS}[$pps]->{SIZE}, $S->{PPS}[$pps]->{SB}
459 );
460 $S->{CURPPS} = $pps;
461 1}
462  
463 sub _get_all_filehandles {
464 #
465 # @file_handles = _get_all_filehandles($starting_directory_pps)
466 #
467 # !recursive!
468 # Recurse over all files and directories, return all file handles.
469 #
470 map (
471 $S -> is_file($_) ? $_ : _get_all_filehandles($_),
472 $S -> dirhandles(shift)
473 );
474 }
475  
476 #
477 # -- some macros -----------------------------------------------------------
478 #
479  
480 sub _h_s { 0x200 } # header size
481 sub _b_s { $S->{HEAD}->{B_S} } # big block size
482 sub _s_s { $S->{HEAD}->{S_S} } # small block size
483 sub _b_s_min { $S->{HEAD}->{B_S_MIN} } # big file minimal size
484 sub _bdepottype { $S->{PPS}[shift]->{SIZE} >= _b_s_min() }
485 sub _blocktype { $S->{PPS}[shift]->{SIZE} >= _b_s_min() ? "B" : "S" }
486  
487 sub _B_FREE { 0xffffffff } # block entry, free block
488 sub _B_EOC { 0xfffffffe } # block entry, end of chain
489 sub _B_BD { 0xfffffffd } # block entry, block depot
490 sub _B_XBD { 0xfffffffc } # block entry, extra block depot
491  
492 sub _S_MAX { 0xffffffff } # maximal stream size (* byte)
493  
494 ##
495 ## --------------------------- IO logic ------------------------------------
496 ##
497  
498 sub ole_head {
499 \{
500 MAGIC => undef, # 00
501 CLSID => undef, # guid 08
502 REVISION => undef, # word 18
503 VERSION => undef, # word 1a
504 BYTEORDER => undef, # word 1c
505 B_S_LOG => undef, # word 1e big block size = 2^b_s_log
506 S_S_LOG => undef, # word 20 small block size = 2^s_s_log
507 UK1 => undef, # word(5) 22
508 B_D_NUM => undef, # long 2c bbd num of blocks
509 ROOT_SB => undef, # long 30 root start block
510 UK2 => undef, # long 34
511 B_S_MIN => undef, # long 38 minimum size of big_block
512 S_D_SB => undef, # long 3c sbd start block
513 S_D_NUM => undef, # long 40 number of sbd blocks
514 B_XD_SB => undef, # long 44
515 B_XD_NUM => undef, # long 48
516 };
517 }
518  
519 sub _read_ppss_buf {
520 #
521 # 1||0 = _read_ppss_buf ($i, \$buf, $bufoffset)
522 #
523 # 0..NLEN char() NAME
524 # 40 word NLEN
525 # 42 byte TYPE
526 # 43 byte COLOR
527 # 44 long PREV
528 # 48 long NEXT
529 # 4c long DIR
530 # 64 char(8) TS1
531 # 6c char(8) TS2
532 # 74 long SB
533 # 78 long SIZE
534 #
535 my ($i, $bufR, $o) = @_;
536 return 0 if !$bufR;
537  
538 $o = 0 if !defined $o;
539 return 1 if defined $S->{PPS}[$i];
540  
541 my $P = {};
542 ($P->{NLEN}, $P->{TYPE}, $P->{COLOR}, $P->{PREV}, $P->{NEXT}, $P->{DIR})
543 = get_struct("WBBLLL", $bufR, $o+0x40)
544 ;
545  
546 ($P->{SB}, $P->{SIZE})
547 = get_struct("LL", $bufR, $o+0x74)
548 ;
549  
550 $P->{NAME} = $S->Var->property (
551 \get_rzwstr($bufR, $o+0, $P->{NLEN}),
552 0, "wstring"
553 );
554  
555 $P->{CLSID} = $S->Var->property ($bufR, 0x50, 0x48);
556  
557 if ($P->{TYPE} != 2) {
558 # !!
559 # Files (type==2) have no timestamp (until now?). Not reading this
560 # data speeds up tremendously (sigh)...
561 # !!
562 $P->{TS1} = $S->Var->property($bufR, 0x64, 0x40);
563 $P->{TS2} = $S->Var->property($bufR, 0x6c, 0x40);
564 }
565  
566 $S->{PPS}[$i] = $P;
567 1}
568  
569 sub _read_ppss {
570 #
571 # 1||() = _read_ppss ($pps)
572 #
573 my $pps = shift;
574 return 1 if defined $S->{PPS}[$pps];
575  
576 _read_ppss_buf($pps,
577 \$S->{IO}->read_iolist(
578 undef, _get_iolist(3, $pps*0x80, 0x80, 0, $S->{BL}->{ROOTL})
579 )
580 ) || ();
581 }
582  
583 sub _get_ppss_chain {
584 #
585 # @blocks = _get_ppss_chain($ppss)
586 #
587 # !recursive!, doesn't care about red/black sorting
588 #
589 ($_[0] == 0xffffffff ?
590 () :
591 _read_ppss($_[0]) && (
592 _get_ppss_chain($S->{PPS}[$_[0]]->{PREV}),
593 $_[0],
594 _get_ppss_chain($S->{PPS}[$_[0]]->{NEXT}),
595 )
596 );
597 }
598  
599 sub _get_list_from_depot {
600 #
601 # [@list] = _get_list_from_depot ($start, depottype)
602 #
603 # Read a block chain starting with block $start out of a either
604 # depot @bbd (for $t=="B") or depot @sbd (for $t=="S").
605 #
606 my ($b, $t) = @_;
607 my @list = ();
608  
609 while ($b != _B_EOC()) {
610 push (@list, $b);
611 $b = $S->{BL}->{$t."_D"}[$b];
612 }
613  
614 \@list;
615 }
616  
617 sub _get_iolist {
618 #
619 # $iolistO = _get_iolist(
620 # $depottype,
621 # $offset, $size,
622 # $startblock
623 # [, \@blocklist || $iolistO]
624 # )
625 #
626 # This is the main IO logic. Returns the iolist for a data stream according
627 # to depot type $t. The stream may start at offset $offset and can have a
628 # size $size. If size is bigger than the total size of the stream according
629 # to its depot, it will be cut correctly. (So if you want to read until the
630 # files end without knowing how many bytes that are, take 0xffffffff as size).
631 #
632 # depottype $t:
633 # 0 small (for @sbd) small block depot
634 # 1 big (for @bbd) big block depot
635 # 2 small (for \@blocks) some small blocks
636 # 3 big (for \@blocks) some big blocks
637 # 4 variable (for $Iolist) an Iolist
638 #
639 my $t = shift||0;
640 my ($offset, $size, $sb) = (shift||0, shift||0, shift||0);
641 local ($blockR) = shift if ($t==2 || $t==3);
642 local ($IoL) = shift if $t==4;
643 local ($di);
644  
645 my $io_out = new OLE::Storage::Iolist();
646 my ($begin, $bs, $done, $len, $max);
647 return $io_out if !$size;
648  
649 $bs = ($t==1 || $t==3) ? _b_s() : _s_s();
650  
651 if ($t<2) {
652 # To skip these offsets, stream chains would have to be resolved
653 # before.
654 } elsif ($t<4) {
655 $max = $#{$blockR};
656 # Skip whole blocks, when offset given
657 $sb += int ($offset / $bs);
658 $offset -= int ($offset / $bs) * $bs;
659 } elsif ($t==4) {
660 $max = $IoL->max();
661 } else {
662 return $io_out;
663 }
664  
665 $done = 0;
666 for ( $di=$sb;
667 ($t<2) ? ($di!=_B_EOC()) : ($di<=$max);
668 $di=&{$__next_dl->[$t]}
669 ) {
670 last if ($done == $size);
671 $bs = $IoL->length($di) if $t==4;
672 if ($offset) {
673 if ($bs <= $offset) {
674 $offset -= $bs;
675 next;
676 } else {
677 $begin = &{$__depot_offset->[$t]} + $offset;
678 $len = $bs - $offset;
679 $offset = 0;
680 }
681 } else {
682 $begin = &{$__depot_offset->[$t]};
683 $len = $bs;
684 }
685 if ( ($done+$len) > $size ) {
686 $len = $size - $done;
687 }
688 $io_out->append($begin, $len);
689 $done += $len;
690 }
691 $io_out;
692 }
693 $__next_dl = [
694 #
695 # index = depot ($di==index)
696 # Returns next chain link
697 #
698 sub { $S->{BL}->{S_D}[$di] },
699 sub { $S->{BL}->{B_D}[$di] },
700 sub { $di+1 },
701 sub { $di+1 },
702 sub { $di+1 },
703 ];
704 $__depot_offset = [
705 #
706 # offset = __depot_offset ($di==index)
707 #
708 sub { (($S->{BL}->{SBL}[$di/8]+1)*8 + ($di%8)) * _s_s() },
709 sub { _h_s() + $di*_b_s() },
710 sub { (($S->{BL}->{SBL}[$$blockR[$di]/8]+1)*8 + ($$blockR[$di]%8)) * _s_s()},
711 sub { _h_s() + $$blockR[$di]*_b_s() },
712 sub { $IoL->offset($di) }
713 ];
714  
715 ##
716 ## ------------------------- Trash Handling ------------------------------
717 ##
718  
719 sub _make_blockuse_statistic {
720 #
721 # block statistic:
722 # 0 == irregular free (block depot entry != -1) (== undef)
723 # 1 == regular free (block depot entry == -1)
724 # 2 == used for ole system
725 # 3 == used for ole application
726 #
727 return 1 if $S->{USE}{KNOWN};
728 my ($i, @list);
729 my $bt;
730 my $Bl = $S->{BL};
731  
732 # default: all small and big blocks are undef
733  
734 #
735 # regular system data
736 #
737  
738 # ole system blocks
739 for (@{$Bl->{B_DL}}, @{$Bl->{S_DL}}, @{$Bl->{ROOTL}}, @{$Bl->{SBL}}) {
740 $S->{USE}->{BB}[$_]=2;
741 }
742  
743 # free blocks according to block depots
744 for (0..@{$Bl->{B_D}}) {
745 $S->{USE}->{BB}[$_]=1 if $Bl->{B_D}[$_]==_B_FREE()
746 }
747 for (0..@{$Bl->{S_D}}) {
748 $S->{USE}->{SB}[$_]=1 if $Bl->{S_D}[$_]==_B_FREE()
749 }
750  
751 #
752 # OLE application blocks
753 #
754 foreach $file (_get_all_filehandles(0)) {
755 $bt = _blocktype($file);
756 for (@{_get_list_from_depot($S->{PPS}[$file]->{SB}, $bt)}) {
757 $S->{USE}->{$bt."B"}[$_]=3
758 }
759 }
760  
761 $S->{USE}{KNOWN}=1;
762 }
763  
764 sub _get_trash_info {
765 #
766 # 1 = _get_trash_info();
767 #
768 # Trash types:
769 #
770 # 0 == all
771 # 1 == unused big blocks
772 # 2 == unused small blocks
773 # 4 == unused file space, according to sizeof pps_size (incl. root_entry)
774 # 8 == unused system space (header, sb_table, bb_table)
775 #
776 return 1 if $S->{TRASH}{KNOWN};
777 _make_blockuse_statistic();
778 my ($begin, $len);
779
780 unused_big_blocks: {
781 _store_trash_iolist(0,
782 _get_iolist(
783 3, 0, _S_MAX(), 0,
784 [grep {$S->{USE}->{BB}[$_]<=1} (0..$S->{BL}->{B_NUM})]
785 )
786 );
787 }
788  
789 unused_small_blocks: {
790 _store_trash_iolist(1,
791 _get_iolist(
792 2, 0, _S_MAX(), 0,
793 [grep {$S->{USE}->{SB}[$_]<=1} (0..$S->{BL}->{S_NUM})]
794 )
795 );
796 }
797  
798 unused_file_space: {
799 # 3.1. normal files
800 for (_get_all_filehandles(0)) {
801 _store_trash_iolist(2, _get_iolist(
802 _bdepottype($_), $S->{PPS}[$_]->{SIZE}, _S_MAX(),
803 $S->{PPS}[$_]->{SB}
804 ));
805 }
806  
807 # 3.2. system file of root_entry (small block file)
808 $begin = $#{$S->{BL}->{S_D}}+1;
809 _store_trash_iolist(2,
810 _get_iolist(2, 0, _S_MAX(), 0, [$begin .. $begin+(7-$begin%8)])
811 );
812  
813 # 3.3. unused root list entries
814 # _get_all_filehandles is done by 3.1, so array $S->{PPS} is complete.
815 $begin = ($#{$S->{PPS}}+1)*0x80;
816 _store_trash_iolist(2,
817 _get_iolist(3, $begin, _S_MAX(), 0, $S->{BL}->{ROOTL})
818 );
819 }
820  
821 unused_system_space: {
822 _store_trash_iolist(3,
823 # 4.1. header block
824 _get_iolist(4,
825 ($S->{HEAD}->{B_D_NUM}+1)*4, _S_MAX(), 0, $S->{HEAD}->{B_DL_L}
826 ),
827  
828 # 4.2. big block depot
829 _get_iolist(3, ($S->{BL}->{B_NUM}+1)*4, _S_MAX(), 0, $S->{BL}->{B_DL}),
830  
831 # 4.3. small block depot
832 _get_iolist(3, ($S->{BL}->{S_NUM}+1)*4, _S_MAX(), 0, $S->{BL}->{S_DL})
833 );
834 }
835  
836 $S->{TRASH}{KNOWN} = 1;
837 1}
838  
839  
840 sub _store_trash_iolist {
841 #
842 # 1 = _store_trash_iolist($index, $Iolist)
843 #
844 my $i = shift;
845 my $IoL;
846 while (@_) {
847 $IoL = shift;
848 if (defined $S->{TRASH}{IO}[$i]) {
849 $S->{TRASH}{IO}[$i] -> push( $IoL );
850 } else {
851 $S->{TRASH}{IO}[$i] = $IoL;
852 }
853 ${$S->{TRASH}{S}[$i]} += $IoL->sumlen();
854 }
855 1}
856  
857 sub _get_trash_size {
858 my $type = shift || (1|2|4|8);
859 my $size = 0;
860  
861 _get_trash_info();
862 for (0..3) {
863 $size += ${$S->{TRASH}{S}[$_]} if $type & 2**$_;
864 }
865  
866 $size;
867 }
868  
869 sub _rw_trash {
870 #
871 # 1||0 = _rw_trash ("r"||"w", $type, \$buf||$buf||undef [,$offset,$size])
872 # $buf||undef = _rw_trash ("r" , $type, undef, [,$offset,$size])
873 #
874 my ($rw, $type, $buf_or_bufR) = (shift, shift, shift);
875  
876 _get_trash_info();
877 my $size = $S->size_trash($type || (1|2|4|8));
878 return 0 if !(($o, $l) = $S->{IO}->default_iosize($size, $rw, @_));
879  
880 my $trR = new OLE::Storage::Iolist();
881 for (0..3) {
882 $trR -> push ($S->{TRASH}{IO}[$_]) if ($type & 2**$_);
883 }
884  
885 $S->{IO}->rw_iolist(
886 $rw, $buf_or_bufR, _get_iolist(4, $o, $l, 0, $trR->aggregate(1))
887 );
888 }
889  
890 "Atomkraft? Nein, danke!"
891  
892 __END__