Util.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 85 CC 4 SCAR 13.1
CC
4
Cov
0%
quantise
6.0
decode_number
6.0
encode_number
6.0
CRAP 3.7
1 package CORBA::IOP::Util;
2  
3 require Exporter;
4  
5 use strict;
6 use vars qw(@ISA @EXPORT $IOR_MAGIC $TAG_INTERNET_IOP);
7  
8 @ISA = qw(Exporter);
9 @EXPORT = qw(decode_number decode_string decode_encapsulation $IOR_MAGIC $TAG_INTERNET_IOP
10 encode_number encode_string encode_encapsulation);
11  
12  
13 $IOR_MAGIC = "IOR:";
14 $TAG_INTERNET_IOP = 0;
15  
16  
17 sub quantise {
18 my ($index, $quantum) = @_;
19 my ($offset);
20  
21 $offset = $index % $quantum;
22  
23 return $offset != 0 ? $index + $quantum - $offset : $index;
24 }
25  
26  
27 #
28 # decode hex string
29 #
30 sub decode_hex {
31 my ($what, $index, $size) = @_;
32 my (@array, $where, $i);
33  
34 for ($i = 0; $i < $size; $i++) {
35 $where = ($index + $i) * 2; # every 2 characters is 1 hex digit
36 $array[$i] = hex(unpack("x$where a2", $what));
37 }
38 return (pack("c$size", @array), $index + $size);
39 }
40  
41  
42 #
43 # decode an unsigned number
44 #
45 sub decode_number {
46 my ($what, $index, $size, $little_endian) = @_;
47 my (@array, $where, $i);
48  
49 $index = quantise($index, $size);
50  
51 # every 2 characters is 1 hex digit
52 for ($i = 0; $i < $size; $i++) {
53 $where = ($index + $i) * 2;
54 $array[$i] = unpack("x$where a2", $what);
55 }
56  
57 return (hex(join("", $little_endian ? reverse(@array) : @array)), $index + $size);
58 }
59  
60  
61 #
62 # decode a string (length + chars + null)
63 #
64 sub decode_string {
65 my ($what, $index, $little_endian) = @_;
66 my ($size, $string);
67  
68 # first decode the length (ulong)
69 ($size, $index) = decode_number($what, $index, 4, $little_endian);
70  
71 # decode the rest, ignoring null termination character
72 ($string, $index) = decode_hex($what, $index, $size - 1);
73  
74 return $string, $index + 1;
75 }
76  
77  
78 #
79 # decode an encapsulation (length + octets)
80 #
81 sub decode_encapsulation {
82 my ($what, $index, $little_endian) = @_;
83 my $size;
84  
85 # first decode the length (ulong)
86 ($size, $index) = decode_number($what, $index, 4, $little_endian);
87  
88 # decode the rest
89 return decode_hex($what, $index, $size);
90 }
91  
92  
93 sub encode_number {
94 my ($length, $size, $little_endian, $value) = @_;
95  
96 my ($result, @array, $i);
97  
98 # String length is twice the byte position.
99 $result = "00" x (quantise($length/2, $size) - $length/2);
100  
101 for ($i=0; $i<$size; $i++){
102 $array[$i] = sprintf("%.2x", $value % 0x100);
103 $value = int($value / 0x100);
104 }
105  
106 $result .= join("", !$little_endian ? reverse(@array) : @array);
107  
108 return $result;
109 }
110  
111  
112 sub encode_string {
113 my ($length, $little_endian, $string) = @_;
114  
115 return encode_number($length, 4, $little_endian, length($string) + 1) # Length
116 . unpack("H*", $string) . "00"; # Encoded string and terminator.
117 }
118  
119  
120 sub encode_encapsulation {
121 my ($length, $little_endian, $string) = @_;
122  
123 return encode_number($length, 4, $little_endian, length($string)) # Length
124 . unpack("H*", $string); # Octet stream.
125 }
126  
127  
128 1;