Storage/Var.pm untested

filter: Statementstmt 0.0% 0 / 339 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 136 Podpod 0.0% 0 / 131 totaltotal 0.0% 0 / 698 CC 18 SCAR 11.2
CC
18
Cov
0%
_date_string
12.0
_filetime_to_date
12.0
cs_from
6.0
CRAP 3.1
1 #
2 # $Id: Var.pm,v 1.1.1.1 1998/02/25 21:13:00 schwartz Exp $
3 #
4 # OLE::Storage::Var
5 #
6 # Property variable handling.
7 #
8 # Copyright (C) 1996, 1997, 1998 Martin Schwartz
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, you should find it at:
22 #
23 # http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/COPYING
24 #
25 # Contact: schwartz@cs.tu-berlin.de
26 #
27  
28 package OLE::Storage::Var;
29 use strict;
30 my $VERSION=do{my@R=('$Revision: 1.1.1.1 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R};
31  
32 use OLE::Storage::Std;
33 use OLE::Storage::Handler();
34 use OLE::Storage::Property();
35 use Unicode::Map();
36  
37 my $uncool_debug = 0;
38  
39 ##
40 ## --- Public --------------------------------------------------------------
41 ##
42  
43 sub new {
44 #
45 # $Var = new Var;
46 #
47 bless ({}, ref($_[0]) || $_[0])
48 -> _init_handling()
49 ;
50 }
51  
52 sub cs_from { my $S=shift; $S->{CS_FROM}=shift if @_; $S->{CS_FROM} }
53 sub map { my $S=shift; $S->{MAP}=shift if @_; $S->{MAP} }
54 sub handler { my $S=shift; $S->{H}=shift if @_; $S->{H} }
55 sub property { OLE::Storage::Property->new(@_) }
56  
57  
58 ##
59 ## --- Private -------------------------------------------------------------
60 ##
61  
62 sub error {
63 my ($Var, $str) = @_;
64 OLE::Storage::Property->new($Var, \$str, 0, "myerror")
65 }
66  
67  
68 #
69 # --- Interface to OLE::Storage::Property ------------------------------------
70 #
71  
72 sub _IS_SCALAR { !(($_[1] || 0) & 0x1000) }
73 sub _IS_ARRAY { ($_[1] || 0) & 0x1000 }
74 sub _IS_VARRAY { ($_[1] || 0) == 0x100c }
75 sub _TO_SCALAR { ($_[1] || 0) & 0xfff }
76 sub _TO_ARRAY { ($_[1] || 0) | 0x1000 }
77 sub _TO_VARRAY { 0x100c }
78  
79 sub _STORE {
80 my ($S, $bufR, $oR, $from) = @_;
81 $S->handler()->convert($from, "store", $bufR, $oR)
82 || $uncool_debug &&
83 (printf "Error: o=%x, type=%x, buf=".("%08x "x5)."...\n",
84 $$oR, $from, get_nlong(5, $bufR, $$oR)
85 )
86 || "";
87 }
88  
89 sub _RETRIEVE {
90 my ($S, $from_ext, $to, $bufR) = @_;
91 if (ref($bufR)) {
92 $S->handler()->convert([$from_ext, $bufR->[0]], $to, $bufR->[1]);
93 }
94 }
95  
96 sub _TYPESTR {
97 shift->handler()->typestr(shift());
98 }
99  
100 #
101 # --- Init -----------------------------------------------------------------
102 #
103  
104 sub _init_handling {
105 #
106 # This installs all the methods used to convert properties of type1 to
107 # variables or properties of type2 (type1 is allowed to equal type2).
108 # This module stores all variables together with an extra type information,
109 # that is strictly private and must not be interesting to other modules.
110 # The idea is to store variables internally in some few standard
111 # representations, like strings, integers or floats.
112 #
113 # You see immediately, that this concept is quite slow: as any e.g. integer
114 # $number will be stored as a data structure: ["int", $number] or any string
115 # $text would be stored as: ["string", \$text]. (s.b.)
116 #
117 my $S = shift;
118 my $H = $S->handler(OLE::Storage::Handler->new);
119  
120 $S->map(
121 new Unicode::Map ({
122 ID => ( $ENV{LC_CTYPE} || "CP1252" )
123 })
124 );
125 return 0 if !$S->map;
126  
127 $H->add (0x00, "empty", "store", \&_0x00_store);
128 $H->add (0x01, "null", "store", \&_0x01_store);
129 $H->add (0x02, "i2", "store", \&_0x02_store);
130 $H->add (0x03, "i4", "store", \&_0x03_store);
131 $H->add (0x04, "r4", "store", \&_0x04_store);
132 $H->add (0x05, "r8", "store", \&_0x05_store);
133 $H->add (0x06, "cy", "store", \&_0x06_store);
134 $H->add (0x07, "date", "store", \&_0x05_store);
135 $H->add (0x08, "bstr");
136 $H->add (0x0a, "error", "store", \&_0x0a_store);
137 $H->add (0x0b, "bool", "store", \&_0x0b_store);
138 $H->add (0x0c, "variant"); # does exist only as array!
139 $H->add (0x11, "ui1", "store", \&_0x11_store);
140 $H->add (0x12, "ui2", "store", \&_0x12_store);
141 $H->add (0x13, "ui4", "store", \&_0x13_store);
142 $H->add (0x14, "i8");
143 $H->add (0x15, "ui8");
144 $H->add (0x1e, "lpstr", "store", \&_0x1e_store);
145 $H->add (0x1f, "lpwstr", "store", \&_0x1f_store);
146 $H->add (0x40, "filetime", "store", \&_0x40_store);
147 $H->add (0x41, "blob", "store", \&_0x41_store);
148 $H->add (0x42, "stream");
149 $H->add (0x43, "storage");
150 $H->add (0x44, "streamed_object");
151 $H->add (0x45, "stored_object");
152 $H->add (0x46, "blobobject");
153 $H->add (0x47, "cf");
154 $H->add (0x48, "clsid", "store", \&_0x48_store);
155  
156 #
157 # Normal procedure to store will be to create some variable like a string
158 # or a buffer and to pass a reference of this to the following handling
159 # functions. These would take the reference, pass it further and store it
160 # finally. But sometimes it should be clever to pass not references, as e.g.
161 # for integers. The internal types listed below are followed by a dot for
162 # pure data and by an "R" for reference data. (provisorically...)
163 #
164 $S->_init_bool(); # .
165 $S->_init_buf(); # R
166 $S->_init_date(); # R
167 $S->_init_int(); # .
168 $S->_init_string(); # R
169 $S->_init_float(); # .
170 $S->_init_guid(); # R
171 $S->_init_wstring(); # R
172 $S->_init_myerror(); # R
173  
174 $H->add ("zstr", "zstr", "store", \&_zstr_store);
175 $H->add ("zwstr", "zwstr", "store", \&_zwstr_store);
176
177 $S;
178 }
179  
180 #
181 # --- Store ----------------------------------------------------------------
182 #
183  
184 sub _0x00_store { # 0x00 == empty
185 ["string", \""]
186 }
187 sub _0x01_store { # 0x01 == null
188 ["int", 0]
189 }
190 sub _0x02_store { # 0x02 == i2
191 my $int = &get_word;
192 $int = - (($int^0xffff) +1) if ($int & 0x8000);
193 ["int", $int];
194 }
195 sub _0x03_store { # 0x03 == i4
196 my $int = &get_long;
197 $int = - (($int^0xffffffff) +1) if ($int & 0x80000000);
198 ["int", $int];
199 }
200 sub _0x04_store { # 0x04 == r4
201 ["float", &get_real]
202 }
203 sub _0x05_store { # 0x05 == r8
204 ["float", &get_double]
205 }
206 sub _0x06_store { # 0x06 == cy
207 [0x06, ""];
208 }
209 sub _0x0a_store { # 0x0a == error
210 ["int", &get_long];
211 }
212 sub _0x0b_store { # 0x0b == bool
213 ["bool", &get_long];
214 }
215 sub _0x11_store { # 0x11 == ui1
216 ["int", &get_byte];
217 }
218 sub _0x12_store { # 0x12 == ui2
219 ["int", &get_word];
220 }
221 sub _0x13_store { # 0x13 == ui4
222 ["int", &get_long];
223 }
224 sub _0x1e_store { # 0x1e == lpstr
225 my ($bufR, $oR) = @_;
226 ["string", \get_zstr($bufR, $oR, &get_long)];
227 }
228 sub _0x1f_store { # 0x1f == lpwstr
229 my ($bufR, $oR) = @_;
230 ["wstring", \get_rzwstr($bufR, $oR, &get_long)];
231 }
232 sub _0x40_store {
233 ["date", &_filetime_to_date];
234 }
235 sub _0x41_store {
236 my ($bufR, $oR) = @_;
237 ["buf", \get_str($bufR, $oR, &get_long)];
238 }
239 sub _0x48_store {
240 ["guid", [get_struct("LWWBBBBBBBB", @_)] ];
241 }
242  
243 sub _zstr_store {
244 my ($bufR, $oR) = @_;
245 ["string", \get_zstr($bufR, $oR, length($$bufR))];
246 }
247 sub _zwstr_store {
248 my ($bufR, $oR) = @_;
249 ["wstring", \get_rzwstr($bufR, $oR, length($$bufR))];
250 }
251  
252 #
253 # --- bool -----------------------------------------------------------------
254 #
255  
256 sub _init_bool {
257 my $H = shift()->handler();
258 $H->add (["bool", "bool",
259 "store", \&_bool_store, "", # Y
260 "bool", \&_bool_bool, "", # Y
261 "buf", \&_bool_buf, "", # .
262 "date", \&_bool_date, "", # .
263 "int", \&_bool_int, "", # Y
264 "float", \&_bool_float, "", # Y
265 "guid", \&_bool_guid, "", # .
266 "string", \&_bool_string, ["Yes", "No"], # Y
267 "wstring", \&_bool_wstring, [nwstr("Yes", "No")], # Y
268 ]);
269 }
270 sub _bool_store { ["bool", shift()] }
271 sub _bool_bool { shift() }
272 sub _bool_buf { undef }
273 sub _bool_date { undef }
274 sub _bool_int { shift() ? -1 : 0 }
275 sub _bool_float { shift() ? -1.0 : 0.0 }
276 sub _bool_guid { undef }
277 sub _bool_string { my ($val, $x, $par) = @_; $val ? $par->[0] : $par->[1] }
278 sub _bool_wstring { my ($val, $x, $par) = @_; $val ? $par->[0] : $par->[1] }
279  
280 #
281 # --- buf ------------------------------------------------------------------
282 #
283  
284 sub _init_buf {
285 my $H = shift->handler();
286 $H->add (["buf", "buf",
287 "store", \&buf_store, "", # Y
288 "bool", \&_buf_bool, "", # .
289 "buf", \&_buf_buf, "", # Y
290 "date", \&_buf_date, "", # .
291 "int", \&_buf_int, "", # .
292 "float", \&_buf_float, "", # .
293 "guid", \&_buf_guid, "", # .
294 "string", \&_buf_string, "", # .
295 "wstring", \&_buf_wstring, "", # .
296 ]);
297 }
298 sub _buf_store { ["buf", shift()] }
299 sub _buf_bool { undef }
300 sub _buf_buf { my $valR = shift; $$valR }
301 sub _buf_date { undef }
302 sub _buf_int { undef }
303 sub _buf_float { undef }
304 sub _buf_guid { undef }
305 sub _buf_string { undef }
306 sub _buf_wstring { undef }
307  
308 #
309 # --- date -----------------------------------------------------------------
310 #
311  
312 sub _init_date {
313 my $H = shift->handler();
314 $H->add (["date", "date",
315 "store", \&_date_store, "", # Y
316 "bool", \&_date_bool, "", # .
317 "buf", \&_date_buf, "", # .
318 "date", \&_date_date, "", # Y
319 "int", \&_date_int, "", # Y
320 "float", \&_date_float, "", # Y
321 "guid", \&_date_guid, "", # .
322 "string", \&_date_string, # Y
323 ["%02d.%02d.%04d, %02d:%02d:%02d", "%02d:%02d:%02d", "<undef>"],
324 "wstring", \&_date_wstring, # Y
325 ["%02d.%02d.%04d, %02d:%02d:%02d", "%02d:%02d:%02d", "<undef>"],
326 ]);
327 }
328 sub _date_store { ["date", shift()] }
329 sub _date_bool { undef }
330 sub _date_buf { undef }
331 sub _date_date { my $valR = shift; @$valR }
332 sub _date_int { int(_date_float(@_)) }
333 sub _date_float {
334 my $valR = shift;
335 my ($d, $h) = _date_to_filetime(@$valR);
336 $d*2**6+$h/2**26;
337 }
338 sub _date_guid { undef }
339 sub _date_string {
340 #
341 # $datestr = _date_string(
342 # \$buf, \$o,
343 # [] || [$da, $mo, $ye, $ho, $mi, $se],
344 # [$date_and_time_mask, $time_mask, $undefined_mask]
345 # );
346 #
347 my ($valR, $oR, $par) = @_;
348 return $par->[2] if !@$valR;
349 my ($da, $mo, $ye, $ho, $mi, $se) = @$valR;
350 if ($ye) {
351 sprintf ($par->[0], $da, $mo, $ye, $ho, $mi, $se);
352 } else {
353 sprintf ($par->[1], $ho, $mi, $se);
354 }
355 }
356 sub _date_wstring { _string_wstring (\_date_string(@_), 0, "%s") }
357  
358 #
359 # --- int ------------------------------------------------------------------
360 #
361  
362 sub _init_int {
363 my $H = shift->handler();
364 $H->add (["int", "int",
365 "store", \&_int_store, "",
366 "bool", \&_int_bool, "", # Y
367 "buf", \&_int_buf, "", # .
368 "date", \&_int_date, "", # Y
369 "int", \&_int_int, "", # Y
370 "float", \&_int_float, "", # Y
371 "guid", \&_int_guid, "", # .
372 "string", \&_int_string, "%d", # Y
373 "wstring", \&_int_wstring, "%d", # Y
374 ]);
375 }
376 sub _int_store { ["int", shift()] }
377 sub _int_bool { shift() ? 1 : 0 }
378 sub _int_buf { undef }
379 sub _int_date { _float_date(@_) }
380 sub _int_int { shift() }
381 sub _int_float { shift() + 0.0 }
382 sub _int_guid { undef }
383 sub _int_string { my ($val, $x, $par) = @_; sprintf $par, $val }
384 sub _int_wstring { _string_wstring (\_int_string(@_), 0, "%s") }
385  
386 #
387 # --- float ----------------------------------------------------------------
388 #
389  
390 sub _init_float {
391 my $H = shift->handler();
392 $H->add (["float", "float",
393 "store", \&_float_store, "",
394 "bool", \&_float_bool, "", # Y
395 "buf", \&_float_buf, "", # .
396 "date", \&_float_date, "", # Y
397 "int", \&_float_int, "", # Y
398 "float", \&_float_float, "", # Y
399 "guid", \&_float_guid, "", # .
400 "string", \&_float_string, "%.2f", # Y
401 "wstring", \&_float_wstring, "%.2f", # Y
402 ]);
403 }
404 sub _float_store { ["float", shift()] }
405 sub _float_bool { shift() ? 1 : 0 }
406 sub _float_buf { undef }
407 sub _float_date {
408 my $val = shift;
409 _filetime_to_date(\nlong([$val*2**26, $val/2**6]));
410 }
411 sub _float_int { int(shift()) }
412 sub _float_float { shift() }
413 sub _float_guid { undef }
414 sub _float_string { my ($val, $oR, $par) = @_; sprintf $par, $val }
415 sub _float_wstring { _string_wstring (\_float_string(@_), 0, "%s") }
416  
417 #
418 # --- guid -----------------------------------------------------------------
419 #
420  
421 sub _init_guid {
422 my $H = shift->handler();
423 $H->add (["guid", "guid",
424 "store", \&_guid_store, "", # Y
425 "bool", \&_guid_bool, "", # .
426 "buf", \&_guid_buf, "", # .
427 "date", \&_guid_date, "", # .
428 "int", \&_guid_int, "", # .
429 "float", \&_guid_float, "", # .
430 "guid", \&_guid_guid, "", # Y
431 "string", \&_guid_string, "%08X-%04X-%04X-%02X%02X-".("%02X"x6), # Y
432 "wstring", \&_guid_wstring, "%08X-%04X-%04X-%02X%02X-".("%02X"x6), # Y
433 ]);
434 }
435 sub _guid_store { ["guid", shift()] }
436 sub _guid_bool { undef }
437 sub _guid_buf { undef }
438 sub _guid_date { undef }
439 sub _guid_int { undef }
440 sub _guid_float { undef }
441 sub _guid_guid { my $valR = shift; $$valR }
442 sub _guid_string { my ($valR, $x, $par) = @_; sprintf $par, @$valR }
443 sub _guid_wstring { _string_wstring (\_guid_string(@_), 0, "%s") }
444  
445 #
446 # CLSIDs:
447 #
448 # 00020810-0000-0000-C000-000000000046 Excel.Sheet.5
449 # 00020900-0000-0000-C000-000000000046 Word.Document.6
450 # 00020901-0000-0000-C000-000000000046 Word.Picture.6
451 # 00020906-0000-0000-C000-000000000046 Word.Document.8
452 # 00021A11-0000-0000-C000-000000000046 Visio
453 #
454  
455  
456 #
457 # --- string ---------------------------------------------------------------
458 #
459  
460 sub _init_string {
461 my $S = shift;
462 my $H = $S->handler();
463 $H->add (["string", "string",
464 "store", \&_string_store, "",
465 "bool", \&_string_bool, "", # .
466 "buf", \&_string_buf, "", # .
467 "date", \&_string_date, "", # .
468 "int", \&_string_int, "", # .
469 "float", \&_string_float, "", # .
470 "guid", \&_string_guid, "", # .
471 "string", \&_string_string, "", # Y
472 "wstring", \&_string_wstring, $S->map # Y
473 ]);
474 }
475 sub _string_store { ["string", shift()] }
476 sub _string_bool { undef }
477 sub _string_buf { undef }
478 sub _string_date { undef }
479 sub _string_int { undef }
480 sub _string_float { undef }
481 sub _string_guid { undef }
482 sub _string_string { ${$_[0]} }
483 sub _string_wstring { $_[2]->to_unicode($_[0]) }
484  
485 #
486 # --- wstring -----------------------------------------------------------------
487 #
488  
489 sub _init_wstring {
490 my $S = shift;
491 my $H = $S->handler();
492 $H->add (["wstring", "wstring",
493 "store", \&_wstring_store, "", # Y
494 "bool", \&_wstring_bool, "", # .
495 "buf", \&_wstring_buf, "", # .
496 "date", \&_wstring_date, "", # .
497 "int", \&_wstring_int, "", # .
498 "float", \&_wstring_float, "", # .
499 "guid", \&_wstring_guid, "", # .
500 "wstring", \&_wstring_wstring, "", # Y
501 "string", \&_wstring_string, $S->map # Y
502 ]);
503 }
504 sub _wstring_store { ["wstring", shift()] }
505 sub _wstring_bool { undef }
506 sub _wstring_buf { undef }
507 sub _wstring_date { undef }
508 sub _wstring_int { undef }
509 sub _wstring_float { undef }
510 sub _wstring_guid { undef }
511 sub _wstring_string { $_[2]->from_unicode($_[0]) }
512 sub _wstring_wstring { ${$_[0]} }
513  
514 #
515 # --- myerror --------------------------------------------------------------
516 #
517 # I'm thinking about not installing an error handling for properties.
518 # Anyway, meanwhile...
519 #
520  
521 sub _init_myerror {
522 my $H = shift->handler();
523 $H->add (["myerror", "myerror",
524 "store", \&_myerror_store, "",
525 "bool", \&_myerror_bool, "",
526 "buf", \&_myerror_buf, "",
527 "date", \&_myerror_date, "",
528 "int", \&_myerror_int, "",
529 "float", \&_myerror_float, "",
530 "guid", \&_myerror_guid, "",
531 "string", \&_myerror_string, "",
532 "wstring", \&_myerror_wstring, "",
533 "myerror", \&_myerror_myerror, "%s",
534 ]);
535 }
536 sub _myerror_store { ["myerror", shift()] }
537 sub _myerror_bool { undef }
538 sub _myerror_buf { undef }
539 sub _myerror_date { undef }
540 sub _myerror_int { undef }
541 sub _myerror_float { undef }
542 sub _myerror_guid { undef }
543 sub _myerror_string { "" }
544 sub _myerror_wstring { "" }
545 sub _myerror_myerror { &_string_string }
546  
547 #
548 # -- FILETIME --------------------------------------------------------------
549 #
550  
551 # filetime is a 64 bit ulong. It counts every second 10 * 10 ^ 6,
552 # starting at 01/01/1601. When the 64 bit int gets evaluated as
553 # two 32 bit integers, the faster running ("least significant long")
554 # can hold just 0x100000000 / 10000000.0 (about 429.5) seconds. So the
555 # slower running ("most significant long") increments every 429.5 seconds.
556  
557 my @monsum = (
558 [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
559 [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]
560 );
561 my $a_minute = 60 * 10000000.0 / (0x10000000 * 16);
562  
563 sub _filetime_to_date {
564 my ($ds, $dd) = get_nlong(2, @_);
565 return [] if (!$ds) && (!$dd);
566  
567 my ($day, $month, $year, $hour, $min, $sec);
568 my ($i, $m, $d, $dsum, $tmpsec);
569  
570 $dsum = $dd + ($ds / (0x10000000 * 16.0));
571  
572 $d= int( $dsum/($a_minute*60*24) )+1;
573 $m= $dsum - ($d-1)*$a_minute*60*24;
574  
575 $year = int( $d/365.2425 ) + 1601;
576 my $switch = _is_schaltjahr($year);
577  
578 $d -= _years_to_days ($year, 1601);
579 for( $i=11; $i && ($d <= _days($switch, $i+1)); $i--) {}
580 $month = $i+1;
581 $day = $d - _days($switch, $i+1);
582  
583 $hour = int ( $m / ($a_minute*60) );
584 $min = int ( $m/$a_minute - $hour*60 );
585 $sec = ( ($m/$a_minute - $hour*60 - $min) * 60);
586  
587 $year -= 1601 if $year == 1601;
588 [$day, $month, $year, $hour, $min, $sec];
589 }
590  
591 sub _date_to_filetime {
592 my ($day, $month, $year, $hour, $min, $sec) = @_;
593 my ($d, $tss, $tsd);
594 my $switch = _is_schaltjahr($year);
595  
596 $d = _years_to_days($year, 1601) + _days($switch, $month) + $day-1;
597 $tsd = (24*60*$d + 60*$hour +$min +$sec/60.0) * $a_minute;
598 $tss = ($tsd-int($tsd)) * 0x10000000 * 16;
599  
600 ( int($tsd), int($tss) );
601 }
602  
603 sub _is_schaltjahr {
604 my $year = shift;
605 !($year%4) && ($year%100 || !($year%400) ) && 1 || 0;
606 }
607  
608 sub _years_to_days {
609 my ($year, $baseyear) = @_;
610 int($year-$baseyear) * 365
611 + int( ($year-$baseyear) / 4 )
612 - int( ($year-$baseyear) / 100 )
613 + int( ($year-$baseyear) / 400 )
614 ;
615 }
616  
617 sub _days {
618 $monsum[shift]->[-1+shift]
619 }
620  
621 "Atomkraft? Nein, danke!"
622  
623 __END__