line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#=============================================================================== |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# FILE: Convert.pm |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION: Conversion between different data formats |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# NOTES: --- |
8
|
|
|
|
|
|
|
# AUTHOR: Michael Bochkaryov (Rattler), <misha@rattler.kiev.ua> |
9
|
|
|
|
|
|
|
# COMPANY: Net.Style |
10
|
|
|
|
|
|
|
# CREATED: 17.08.2008 17:01:48 EEST |
11
|
|
|
|
|
|
|
#=============================================================================== |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
NetSDS::Util::Convert - data formats conversion functions |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 SYNOPSIS |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use NetSDS::Util::Convert qw(...); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 DESCRIPTION |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
C<NetSDS::Util::Convert> module contains miscelaneous functions. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=over |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=item * CLI parameters processing |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=item * types validation |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item * HEX, Base64, URI, BCD encondig |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=item * UUID processing |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=back |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=cut |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
package NetSDS::Util::Convert; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
2
|
|
13500
|
use 5.8.0; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
115
|
|
42
|
2
|
|
|
2
|
|
14
|
use warnings 'all'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
129
|
|
43
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
112
|
|
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
2
|
|
12
|
use base 'Exporter'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
238
|
|
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
2
|
|
26
|
use version; our $VERSION = '1.044'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
14
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
our @EXPORT = qw( |
50
|
|
|
|
|
|
|
conv_str_bcd |
51
|
|
|
|
|
|
|
conv_chr_hex |
52
|
|
|
|
|
|
|
conv_hex_chr |
53
|
|
|
|
|
|
|
conv_str_hex |
54
|
|
|
|
|
|
|
conv_hex_str |
55
|
|
|
|
|
|
|
conv_str_base64 |
56
|
|
|
|
|
|
|
conv_base64_str |
57
|
|
|
|
|
|
|
conv_str_uri |
58
|
|
|
|
|
|
|
conv_uri_str |
59
|
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
2
|
|
2218
|
use MIME::Base64; |
|
2
|
|
|
|
|
1588
|
|
|
2
|
|
|
|
|
149
|
|
62
|
2
|
|
|
2
|
|
1687
|
use URI::Escape; |
|
2
|
|
|
|
|
3316
|
|
|
2
|
|
|
|
|
1163
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
#*********************************************************************** |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 EXPORTED FUNCTIONS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=over |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item B<conv_conv_str_bcd($str)> - convert string to little-endian BCD |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
This function converts string to little-endian BCD encoding |
73
|
|
|
|
|
|
|
filled with F16 value. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
78
|
|
|
|
|
|
|
sub conv_conv_str_bcd { |
79
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
$str = "$str" . 'F' x ( length("$str") % 2 ); |
82
|
0
|
|
|
|
|
|
$str =~ s/([\dF])([\dF])/$2$1/g; |
83
|
0
|
|
|
|
|
|
return conv_hex_str($str); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
#*********************************************************************** |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item B<conv_chr_hex($char)> - encode char to hexadecimal string |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
$hex = conv_chr_hex('a'); # return 61 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
95
|
|
|
|
|
|
|
sub conv_chr_hex { |
96
|
0
|
|
|
0
|
1
|
|
my ($chr) = @_; |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
return defined($chr) ? uc( unpack( "H2", "$chr" ) ) : "$chr"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
#*********************************************************************** |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item B<conv_hex_chr($hex)> - convert hexadecimal string to character |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
$chr = conv_hex_chr('4A'); # return 'J' |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
110
|
|
|
|
|
|
|
sub conv_hex_chr { |
111
|
0
|
|
|
0
|
1
|
|
my ($hex) = @_; |
112
|
|
|
|
|
|
|
|
113
|
0
|
0
|
|
|
|
|
return defined($hex) ? pack( "H2", "$hex" ) : "$hex"; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#*********************************************************************** |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item B<conv_str_hex($str)> - convert byte string to hexadecimal |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$str = 'Want hex dump!'; |
121
|
|
|
|
|
|
|
$hex = conv_hex_str($str); |
122
|
|
|
|
|
|
|
print "Hex string: " . $hex; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
127
|
|
|
|
|
|
|
sub conv_str_hex { |
128
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
129
|
|
|
|
|
|
|
|
130
|
0
|
0
|
|
|
|
|
return defined($str) ? uc( unpack( "H*", "$str" ) ) : ""; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
#*********************************************************************** |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item B<conv_hex_str($string)> - convert hex to byte string |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
$hex = '7A686F7061'; |
138
|
|
|
|
|
|
|
$string = conv_hex_str($hex); |
139
|
|
|
|
|
|
|
print "String from hex: " . $string; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
144
|
|
|
|
|
|
|
sub conv_hex_str { |
145
|
0
|
|
|
0
|
1
|
|
my ($hex) = @_; |
146
|
|
|
|
|
|
|
|
147
|
0
|
0
|
|
|
|
|
return defined($hex) ? pack( "H*", "$hex" ) : ""; #"$hex"; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
#*********************************************************************** |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item B<conv_str_base64($str)> - convert string to Base64 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $b64 = str_base64("Hallo, people!"); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub conv_str_base64 { |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
return encode_base64($str, ""); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
#*********************************************************************** |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item B<conv_base64_str($b64)> - convert Base64 to string |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $str = base64_str($base64_string); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub conv_base64_str { |
179
|
|
|
|
|
|
|
|
180
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
|
return decode_base64($str); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
#*********************************************************************** |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=item B<conv_str_uri($str)> - convert string to URI encoded |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Example: |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
my $uri = str_uri("http://www.google.com/?q=what"); |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=cut |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub conv_str_uri { |
199
|
|
|
|
|
|
|
|
200
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
return uri_escape( $str, "\x00-\xff" ); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
#*********************************************************************** |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=item B<conv_uri_str($uri)> - decode URI encoded string |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Example: |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
my $str = uri_str($uri_string); |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub conv_uri_str { |
219
|
|
|
|
|
|
|
|
220
|
0
|
|
|
0
|
1
|
|
my ($str) = @_; |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
return uri_unescape($str); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
1; |
227
|
|
|
|
|
|
|
__END__ |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=back |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 EXAMPLES |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
None |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=head1 BUGS |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
None |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 TODO |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
1. Add other encodings support |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 SEE ALSO |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
L<Pod::Usage>, L<Data::UUID> |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 AUTHORS |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
Valentyn Solomko <pere@pere.org.ua> |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Michael Bochkaryov <misha@rattler.kiev.ua> |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |