MPMinus/MainTools/TCD04.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 71 SCAR 45.0
CC
9
Cov
0%
tcd04c
20.0
tcd042string
12.0
tcd04d
12.0
CRAP 90.0
1 package MPMinus::MainTools::TCD04; # $Id: TCD04.pm 122 2013-05-07 13:05:41Z minus $
2 use strict;
3
4 =head1 NAME
5
6 MPMinus::MainTools::TCD04 - TCD04 functions
7
8 =head1 VERSION
9
10 Version 1.01
11
12 =head1 SYNOPSIS
13
14 use MPMinus::MainTools::TCD04;
15
16 my $tcd04 = new MPMinus::MainTools::TCD04;
17
18 my $code = $tcd04->tcd04c('u'); # 1 char
19 my $decode = $tcd04->tcd04d($code); # 1 word
20
21 print $tcd04->tcd042string($tcd04->string2tcd04('hello world!'));
22
23 =head1 DESCRIPTION
24
25 TCD04 functions. Simple cryptografy's algorythm of D&D Corporation
26
27 =head1 METHODS
28
29 =over 8
30
31 =item B<tcd04c>
32
33 my $code = $tcd04->tcd04c('u'); # 1 char
34
35 =item B<tcd04d>
36
37 my $decode = $tcd04->tcd04d($code); # 1 word
38
39 =item B<tcd042string>
40
41 $tcd04->tcd042string($tcd04->string2tcd04('hello world!'));
42
43 =item B<string2tcd04>
44
45 $tcd04->tcd042string($tcd04->string2tcd04('hello world!'));
46
47 =back
48
49 =head1 HISTORY
50
51 Version 1.00.0001 (08.01.2007)
52
53 =head1 AUTHOR
54
55 Serz Minus (Lepenkov Sergey) L<http://serzik.ru> E<lt>minus@mail333.comE<gt>
56
57 =head1 COPYRIGHT
58
59 Copyright (C) 1998-2013 D&D Corporation. All Rights Reserved
60
61 =head1 LICENSE
62
63 This program is free software: you can redistribute it and/or modify
64 it under the terms of the GNU General Public License as published by
65 the Free Software Foundation, either version 3 of the License, or
66 (at your option) any later version.
67
68 This program is distributed in the hope that it will be useful,
69 but WITHOUT ANY WARRANTY; without even the implied warranty of
70 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 GNU General Public License for more details.
72
73 See C<LICENSE> file
74
75 =cut
76
77 use vars qw($VERSION);
78 $VERSION = 1.01;
79
80 sub new {
81 my $class = shift;
82 my $self = bless {},$class;
83 return $self
84 }
85 sub string2tcd04 {
86 my $self = shift;
87 my $string = shift;
88 return '' if length $string == 0;
89 return join "",map {$_=$self->tcd04c($_)} split //,$string;
90 }
91 sub tcd042string {
92 my $self = shift;
93 my $string = shift;
94 return '' if length $string == 0;
95 my $ch2 ='';
96 my $outstr = '';
97 foreach (split //,$string) {
98 $ch2.=$_;
99 if (length($ch2) == 2) {
100 $outstr.=$self->tcd04d($ch2);
101 $ch2='';
102 }
103 }
104 return $outstr;
105 }
106 sub tcd04c {
107 my $self = shift;
108 my $ch = shift;
109 return '' if length $ch != 1;
110 my $kod1 = ord($ch)>>4;
111 my $kod2 = (ord($ch)&(2**4-1));
112 return chr($kod1>0?int(rand 16)*15 + $kod1:0).chr($kod2>0?int(rand 16)*15 + $kod2:0);
113 }
114 sub tcd04d {
115 my $self = shift;
116 my $ch2 = shift;
117 return '' if length $ch2 != 2;
118 my ($kod1,$kod2) = map {(((ord($_)%15)==0)&&ord($_)>0)?15:ord($_)%15} split //,$ch2;
119 return chr($kod1<<4|$kod2); #return sprintf "%X", $kod1<<4|$kod2;
120 }
121 1;