This file is indexed.

/usr/share/perl5/Data/ParseBinary/Adapters.pm is in libdata-parsebinary-perl 0.31~dfsg-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use strict;
use warnings;
use Data::ParseBinary::Core;

package Data::ParseBinary::Enum;
our @ISA = qw{Data::ParseBinary::Adapter};
# TODO: implement as macro in terms of SymmetricMapping (macro)
#   that is implemented as MappingAdapter

sub _init {
    my ($self, @params) = @_;
    my $decode = {};
    my $encode = {};
    $self->{have_default} = 0;
    $self->{default_action} = undef;
    while (@params) {
        my $key = shift @params;
        my $value = shift @params;
        if ($key eq '_default_') {
            $self->{have_default} = 1;
            $self->{default_action} = $value;
            if (ref $value) {
                if ($value != $Data::ParseBinary::BaseConstruct::DefaultPass) {
                    die "Enum Error: got invalid value as default";
                }
            } elsif (exists $encode->{$value}) {
                die "Enum Error: $value should not be defined as regular case";
            } else {
                $self->{default_value} = shift @params;
            }
            next;
        }
        $encode->{$key} = $value;
        $decode->{$value} = $key;
    }
    $self->{encode} = $encode;
    $self->{decode} = $decode;
}

sub _decode {
    my ($self, $value) = @_;
    if (exists $self->{decode}->{$value}) {
        return $self->{decode}->{$value};
    }
    if ($self->{have_default}) {
        if (ref($self->{default_action}) and $self->{default_action} == $Data::ParseBinary::BaseConstruct::DefaultPass) {
            return $value;
        }
        return $self->{default_action};
    }
    die "Enum: unrecognized value $value, and no default defined";
}

sub _encode {
    my ($self, $tvalue) = @_;
    if (exists $self->{encode}->{$tvalue}) {
        return $self->{encode}->{$tvalue};
    }
    if ($self->{have_default}) {
        if (ref($self->{default_action}) and $self->{default_action} == $Data::ParseBinary::BaseConstruct::DefaultPass) {
            return $tvalue;
        }
        return $self->{default_value};
    }
    die "Enum: unrecognized value $tvalue";
}

package Data::ParseBinary::FlagsEnum;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, @mapping) = @_;
    my @pairs;
    die "FlagsEnum: Mapping should be even" if @mapping % 2 == 1;
    while (@mapping) {
        my $name = shift @mapping;
        my $value = shift @mapping;
        push @pairs, [$name, $value];
    }
    $self->{pairs} = \@pairs;
}

sub _decode {
    my ($self, $value) = @_;
    my $hash = {};
    foreach my $rec (@{ $self->{pairs} }) {
        $hash->{$rec->[0]} = 1 if $value & $rec->[1];
    }
    return $hash;
}

sub _encode {
    my ($self, $tvalue) = @_;
    my $value = 0;
    foreach my $rec (@{ $self->{pairs} }) {
        if (exists $tvalue->{$rec->[0]} and $tvalue->{$rec->[0]}) {
            $value |= $rec->[1];
        }
    }
    return $value;
}

package Data::ParseBinary::ExtractingAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $sub_name) = @_;
    $self->{sub_name} = $sub_name;
}

sub _decode {
    my ($self, $value) = @_;
    return $value->{$self->{sub_name}};
}

sub _encode {
    my ($self, $tvalue) = @_;
    return {$self->{sub_name} => $tvalue};
}

package Data::ParseBinary::IndexingAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $index) = @_;
    $self->{index} = $index || 0;
}

sub _decode {
    my ($self, $value) = @_;
    return $value->[$self->{index}];
}

sub _encode {
    my ($self, $tvalue) = @_;
    return [ ('') x $self->{index}, $tvalue ];
}

package Data::ParseBinary::JoinAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _decode {
    my ($self, $value) = @_;
    return join '', @$value;
}

sub _encode {
    my ($self, $tvalue) = @_;
    return [split '', $tvalue];
}

package Data::ParseBinary::ConstAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $value) = @_;
    $self->{value} = $value;
}

sub _decode {
    my ($self, $value) = @_;
    if (not $value eq $self->{value}) {
        die "Const Error: expected $self->{value} got $value";
    }
    return $value;
}

sub _encode {
    my ($self, $tvalue) = @_;
    if (not defined $self->_get_name()) {
        # if we don't have a name, then just use the value
        return $self->{value};
    }
    if (defined $tvalue and $tvalue eq $self->{value}) {
        return $self->{value};
    }
    die "Const Error: expected $self->{value} got ". (defined $tvalue ? $tvalue : "undef");
}


package Data::ParseBinary::LengthValueAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _decode {
    my ($self, $value) = @_;
    return $value->[1];
}

sub _encode {
    my ($self, $tvalue) = @_;
    return [length($tvalue), $tvalue];
}

package Data::ParseBinary::PaddedStringAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, %params) = @_;
    if (not defined $params{length}) {
        die "PaddedStringAdapter: you must specify length";
    }
    $self->{length} = $params{length};
    $self->{encoding} = $params{encoding};
    $self->{padchar} = defined $params{padchar} ? $params{padchar} : "\x00";
    $self->{paddir} = $params{paddir} || "right";
    $self->{trimdir} = $params{trimdir} || "right";
    if (not grep($_ eq $self->{paddir}, qw{right left center})) {
        die "PaddedStringAdapter: paddir should be one of {right left center}";
    }
    if (not grep($_ eq $self->{trimdir}, qw{right left})) {
        die "PaddedStringAdapter: trimdir should be one of {right left}";
    }
}

sub _decode {
    my ($self, $value) = @_;
    my $tvalue = $value;
    my $char = $self->{padchar};
    if ($self->{paddir} eq 'right' or $self->{paddir} eq 'center') {
        $tvalue =~ s/$char*\z//;
    } elsif ($self->{paddir} eq 'left' or $self->{paddir} eq 'center') {
        $tvalue =~ s/\A$char*//;
    }
    return $tvalue;
}

sub _encode {
    my ($self, $tvalue) = @_;
    my $value = $tvalue;
    
    if (length($value) < $self->{length}) {
        my $add = $self->{length} - length($value);
        my $char = $self->{padchar};
        if ($self->{paddir} eq 'right') {
            $value .= $char x $add;
        } elsif ($self->{paddir} eq 'left') {
            $value = ($char x $add) . $value;
        } elsif ($self->{paddir} eq 'center') {
            my $add_left = $add / 2;
            my $add_right = $add_left + ($add % 2 == 0 ? 0 : 1);
            $value = ($char x $add_left) . $value . ($char x $add_right);
        }
    }
    if (length($value) > $self->{length}) {
        my $remove = length($value) - $self->{length};
        if ($self->{trimdir} eq 'right') {
            substr($value, $self->{length}, $remove, '');
        } elsif ($self->{trimdir} eq 'left') {
            substr($value, 0, $remove, '');
        }
    }
    return $value;
}

#package Data::ParseBinary::StringAdapter;
#our @ISA = qw{Data::ParseBinary::Adapter};
#
#sub _init {
#    my ($self, $encoding) = @_;
#    $self->{encoding} = $encoding;
#}
#
#sub _decode {
#    my ($self, $value) = @_;
#    my $tvalue;
#    if ($self->{encoding}) {
#        die "TODO: Should implement different encodings";
#    } else {
#        $tvalue = $value;
#    }
#    return $tvalue;
#}
#
#sub _encode {
#    my ($self, $tvalue) = @_;
#    my $value;
#    if ($self->{encoding}) {
#        die "TODO: Should implement different encodings";
#    } else {
#        $value = $tvalue;
#    }
#    return $value;
#}

package Data::ParseBinary::CStringAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $terminators) = @_;
    $self->{regex} = qr/[$terminators]*\z/;
    $self->{terminator} = substr($terminators, 0, 1);
}

sub _decode {
    my ($self, $value) = @_;
    $value =~ s/$self->{regex}//;
    return $value;
}

sub _encode {
    my ($self, $tvalue) = @_;
    return $tvalue . $self->{terminator};
}

package Data::ParseBinary::LamdaValidator;
our @ISA = qw{Data::ParseBinary::Validator};

sub _init {
    my ($self, @params) = @_;
    $self->{coderef} = shift @params;
}

sub _validate {
    my ($self, $value) = @_;
    return $self->{coderef}->($value);
}

package Data::ParseBinary::FirstUnitAndTheRestAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};
# this adapter move from a length of bytes, to one unit and the rest
# as an array

sub _init {
    my ($self, $unit_length, $first_name, $the_rest) = @_;
    $first_name ||= 'FirstUnit';
    $the_rest ||= 'TheRest';
    $self->{unit_length} = $unit_length;
    $self->{first_name} = $first_name;
    $self->{the_rest} = $the_rest;
}

sub _decode {
    my ($self, $value) = @_;
    $value = join('', $value->{$self->{first_name}}, @{ $value->{$self->{the_rest}} } );
    return $value;
}

sub _encode {
    my ($self, $tvalue) = @_;
    my $u_len = $self->{unit_length};
    die "Length of input should be dividable by unit_length" unless length($tvalue) % $u_len == 0;
    my @units = map substr($tvalue, $_*$u_len, $u_len), 0..(length($tvalue) / $u_len - 1);
    my $first = shift @units;
    my $value = { $self->{first_name} => $first, $self->{the_rest} => \@units };
    return $value;
}

package Data::ParseBinary::CharacterEncodingAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $encoding) = @_;
    $self->{encoding} = $encoding;
    require Encode;
}

sub _decode {
    my ($self, $octets) = @_;
    my $string = Encode::decode($self->{encoding}, $octets);
    return $string;
}

sub _encode {
    my ($self, $string) = @_;
    my $octets = Encode::encode($self->{encoding}, $string);
    return $octets;
}

package Data::ParseBinary::ExtendedNumberAdapter;
our @ISA = qw{Data::ParseBinary::Adapter};

sub _init {
    my ($self, $is_signed, $is_bigendian) = @_;
    $self->{is_signed} = $is_signed;
    $self->{is_bigendian} = $is_bigendian;
    require Math::BigInt;
}

sub _decode {
    my ($self, $value) = @_;
    if (not $self->{is_bigendian}) {
        $value = join '', reverse split '', $value;
    }
    my $is_negative;
    if ($self->{is_signed}) {
        my $first_char = ord($value);
        if ($first_char > 127) {
            $value = ~$value;
            $is_negative = 1;
        }
    }
    
    my $hexed = unpack "H*", $value;
    my $number = Math::BigInt->new("0x$hexed");
    if ($is_negative) {
        $number->binc()->bneg();
    }
    return $number;
}

sub _encode {
    my ($self, $number) = @_;
    $number = Math::BigInt->new($number);

    my $is_negative;
    if ($self->{is_signed}) {
        if ($number->sign() eq '-') {
            $is_negative = 1;
            $number->binc()->babs();
        }
    } else {
        if ($number->sign() eq '-') {
            die "Was given a negative number for unsigned integer";
        }
    }
    
    my $hexed = $number->as_hex();
    substr($hexed, 0, 2, '');
    my $packed = pack "H*", ("0"x(16-length($hexed))).$hexed;
    if ($is_negative) {
        $packed = ~$packed;
    }
    if (not $self->{is_bigendian}) {
        $packed = join '', reverse split '', $packed;
    }
    return $packed;
}



1;