@@ -45,7 +45,6 @@ sub new {
45
45
my $self = {
46
46
debug => 0,
47
47
data => undef ,
48
- index => 0,
49
48
leading_junk => ' ' ,
50
49
l_msb => undef ,
51
50
packet_max_length => 255,
@@ -73,23 +72,14 @@ sub addData {
73
72
my ($self , $buf ) = @_ ;
74
73
75
74
my $state = $self -> {' state' };
76
- my $index = $self -> {' index' }; # Index of byte number within frame (0x7e byte is index 0)
77
75
78
76
foreach my $c (split (// , $buf )) {
79
- # Sometimes, introduce an error
80
- if (int (rand (200)) == 0) {
81
- my $o_c = $c ;
82
- $c = chr (int (rand (256)));
83
- $self -> error(" Corrupted byte index %d from %02x to %02x" ,
84
- $index , ord ($o_c ), ord ($c ));
85
- }
86
77
87
78
if ($state == 0) {
88
79
if ($c eq chr (0x7e)) {
89
80
$self -> {' data' } = undef ;
90
81
$self -> {' done' } = 0;
91
82
$self -> {' cksum' } = 0;
92
- $index = 1; # Next byte is index 1
93
83
$state = 1;
94
84
if ($self -> {' leading_junk' } ne ' ' ) {
95
85
$self -> printHex(" Skipping junk pre frame start:" , $self -> {' leading_junk' });
@@ -101,12 +91,10 @@ sub addData {
101
91
}
102
92
elsif ($state == 1) {
103
93
$self -> {' l_msb' } = ord ($c );
104
- $index ++;
105
94
$state = 2;
106
95
}
107
96
elsif ($state == 2) {
108
97
$self -> {' l_lsb' } = ord ($c );
109
- $index ++;
110
98
my $length = ($self -> {' l_msb' } << 8) + $self -> {' l_lsb' };
111
99
if ($length > $self -> {' packet_max_length' }) {
112
100
# Don't allow arbitrarily long packets
@@ -123,7 +111,6 @@ sub addData {
123
111
$self -> {' data' } .= $c ;
124
112
$self -> {' cksum' } += ord ($c );
125
113
$self -> {' to_read' } --;
126
- $index ++;
127
114
if ($self -> {' to_read' } == 0) {
128
115
$state = 4;
129
116
}
@@ -143,16 +130,14 @@ sub addData {
143
130
}
144
131
145
132
$state = 0;
146
- $index = 0;
147
133
}
148
134
else {
149
135
die " Illegal state $state " ;
150
136
}
151
137
}
152
138
153
- # Remember state and index within frame for next time
139
+ # Remember state within frame for next time
154
140
$self -> {' state' } = $state ;
155
- $self -> {' index' } = $index ;
156
141
157
142
return 1;
158
143
}
0 commit comments