| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IO::Compress::Zlib::Extra; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | require 5.006 ; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 84 |  |  | 84 |  | 571 | use strict ; | 
|  | 84 |  |  |  |  | 173 |  | 
|  | 84 |  |  |  |  | 2554 |  | 
| 6 | 84 |  |  | 84 |  | 418 | use warnings; | 
|  | 84 |  |  |  |  | 153 |  | 
|  | 84 |  |  |  |  | 2054 |  | 
| 7 | 84 |  |  | 84 |  | 446 | use bytes; | 
|  | 84 |  |  |  |  | 220 |  | 
|  | 84 |  |  |  |  | 652 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS); | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | $VERSION = '2.205'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 | 84 |  |  | 84 |  | 10531 | use IO::Compress::Gzip::Constants 2.205 ; | 
|  | 84 |  |  |  |  | 1475 |  | 
|  | 84 |  |  |  |  | 94921 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub ExtraFieldError | 
| 16 |  |  |  |  |  |  | { | 
| 17 | 24 |  |  | 24 | 0 | 68 | return $_[0]; | 
| 18 | 0 |  |  |  |  | 0 | return "Error with ExtraField Parameter: $_[0]" ; | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub validateExtraFieldPair | 
| 22 |  |  |  |  |  |  | { | 
| 23 | 910 |  |  | 910 | 0 | 1345 | my $pair = shift ; | 
| 24 | 910 |  |  |  |  | 1208 | my $strict = shift; | 
| 25 | 910 |  |  |  |  | 1209 | my $gzipMode = shift ; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 910 | 50 | 33 |  |  | 3343 | return ExtraFieldError("Not an array ref") | 
| 28 |  |  |  |  |  |  | unless ref $pair &&  ref $pair eq 'ARRAY'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 910 | 100 |  |  |  | 1948 | return ExtraFieldError("SubField must have two parts") | 
| 31 |  |  |  |  |  |  | unless @$pair == 2 ; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 908 | 100 |  |  |  | 1911 | return ExtraFieldError("SubField ID is a reference") | 
| 34 |  |  |  |  |  |  | if ref $pair->[0] ; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 907 | 100 |  |  |  | 1613 | return ExtraFieldError("SubField Data is a reference") | 
| 37 |  |  |  |  |  |  | if ref $pair->[1] ; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # ID is exactly two chars | 
| 40 | 905 | 100 |  |  |  | 1813 | return ExtraFieldError("SubField ID not two chars long") | 
| 41 |  |  |  |  |  |  | unless length $pair->[0] == GZIP_FEXTRA_SUBFIELD_ID_SIZE ; | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Check that the 2nd byte of the ID isn't 0 | 
| 44 | 901 | 100 | 66 |  |  | 3610 | return ExtraFieldError("SubField ID 2nd byte is 0x00") | 
|  |  |  | 100 |  |  |  |  | 
| 45 |  |  |  |  |  |  | if $strict && $gzipMode && substr($pair->[0], 1, 1) eq "\x00" ; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 898 | 100 |  |  |  | 1721 | return ExtraFieldError("SubField Data too long") | 
| 48 |  |  |  |  |  |  | if length $pair->[1] > GZIP_FEXTRA_SUBFIELD_MAX_SIZE ; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 897 |  |  |  |  | 1623 | return undef ; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub parseRawExtra | 
| 55 |  |  |  |  |  |  | { | 
| 56 | 673 |  |  | 673 | 0 | 1209 | my $data     = shift ; | 
| 57 | 673 |  |  |  |  | 970 | my $extraRef = shift; | 
| 58 | 673 |  |  |  |  | 914 | my $strict   = shift; | 
| 59 | 673 |  |  |  |  | 948 | my $gzipMode = shift ; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | #my $lax = shift ; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | #return undef | 
| 64 |  |  |  |  |  |  | #    if $lax ; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 673 |  |  |  |  | 1101 | my $XLEN = length $data ; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 673 | 100 |  |  |  | 1415 | return ExtraFieldError("Too Large") | 
| 69 |  |  |  |  |  |  | if $XLEN > GZIP_FEXTRA_MAX_SIZE; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 672 |  |  |  |  | 1152 | my $offset = 0 ; | 
| 72 | 672 |  |  |  |  | 1490 | while ($offset < $XLEN) { | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 877 | 100 |  |  |  | 1788 | return ExtraFieldError("Truncated in FEXTRA Body Section") | 
| 75 |  |  |  |  |  |  | if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE  > $XLEN ; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 875 |  |  |  |  | 1552 | my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE); | 
| 78 | 875 |  |  |  |  | 1184 | $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE; | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 875 |  |  |  |  | 1738 | my $subLen =  unpack("v", substr($data, $offset, | 
| 81 |  |  |  |  |  |  | GZIP_FEXTRA_SUBFIELD_LEN_SIZE)); | 
| 82 | 875 |  |  |  |  | 1291 | $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 875 | 100 |  |  |  | 1695 | return ExtraFieldError("Truncated in FEXTRA Body Section") | 
| 85 |  |  |  |  |  |  | if $offset + $subLen > $XLEN ; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 871 |  |  |  |  | 2790 | my $bad = validateExtraFieldPair( [$id, | 
| 88 |  |  |  |  |  |  | substr($data, $offset, $subLen)], | 
| 89 |  |  |  |  |  |  | $strict, $gzipMode ); | 
| 90 | 871 | 100 |  |  |  | 2056 | return $bad if $bad ; | 
| 91 | 869 | 100 |  |  |  | 2826 | push @$extraRef, [$id => substr($data, $offset, $subLen)] | 
| 92 |  |  |  |  |  |  | if defined $extraRef;; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 869 |  |  |  |  | 2055 | $offset += $subLen ; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 664 |  |  |  |  | 1383 | return undef ; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | sub findID | 
| 102 |  |  |  |  |  |  | { | 
| 103 | 0 |  |  | 0 | 0 | 0 | my $id_want = shift ; | 
| 104 | 0 |  |  |  |  | 0 | my $data    = shift; | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 0 |  |  |  |  | 0 | my $XLEN = length $data ; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 |  |  |  |  | 0 | my $offset = 0 ; | 
| 109 | 0 |  |  |  |  | 0 | while ($offset < $XLEN) { | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | return undef | 
| 112 | 0 | 0 |  |  |  | 0 | if $offset + GZIP_FEXTRA_SUBFIELD_HEADER_SIZE  > $XLEN ; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 |  |  |  |  | 0 | my $id = substr($data, $offset, GZIP_FEXTRA_SUBFIELD_ID_SIZE); | 
| 115 | 0 |  |  |  |  | 0 | $offset += GZIP_FEXTRA_SUBFIELD_ID_SIZE; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 0 |  |  |  |  | 0 | my $subLen =  unpack("v", substr($data, $offset, | 
| 118 |  |  |  |  |  |  | GZIP_FEXTRA_SUBFIELD_LEN_SIZE)); | 
| 119 | 0 |  |  |  |  | 0 | $offset += GZIP_FEXTRA_SUBFIELD_LEN_SIZE ; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | return undef | 
| 122 | 0 | 0 |  |  |  | 0 | if $offset + $subLen > $XLEN ; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 0 | 0 |  |  |  | 0 | return substr($data, $offset, $subLen) | 
| 125 |  |  |  |  |  |  | if $id eq $id_want ; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  | 0 | $offset += $subLen ; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 0 |  |  |  |  | 0 | return undef ; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub mkSubField | 
| 135 |  |  |  |  |  |  | { | 
| 136 | 393 |  |  | 393 | 0 | 720 | my $id = shift ; | 
| 137 | 393 |  |  |  |  | 628 | my $data = shift ; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 393 |  |  |  |  | 1540 | return $id . pack("v", length $data) . $data ; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | sub parseExtraField | 
| 143 |  |  |  |  |  |  | { | 
| 144 | 82 |  |  | 82 | 0 | 138 | my $dataRef  = $_[0]; | 
| 145 | 82 |  |  |  |  | 132 | my $strict   = $_[1]; | 
| 146 | 82 |  |  |  |  | 121 | my $gzipMode = $_[2]; | 
| 147 |  |  |  |  |  |  | #my $lax     = @_ == 2 ? $_[1] : 1; | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | # ExtraField can be any of | 
| 151 |  |  |  |  |  |  | # | 
| 152 |  |  |  |  |  |  | #    -ExtraField => $data | 
| 153 |  |  |  |  |  |  | # | 
| 154 |  |  |  |  |  |  | #    -ExtraField => [$id1, $data1, | 
| 155 |  |  |  |  |  |  | #                    $id2, $data2] | 
| 156 |  |  |  |  |  |  | #                     ... | 
| 157 |  |  |  |  |  |  | #                   ] | 
| 158 |  |  |  |  |  |  | # | 
| 159 |  |  |  |  |  |  | #    -ExtraField => [ [$id1 => $data1], | 
| 160 |  |  |  |  |  |  | #                     [$id2 => $data2], | 
| 161 |  |  |  |  |  |  | #                     ... | 
| 162 |  |  |  |  |  |  | #                   ] | 
| 163 |  |  |  |  |  |  | # | 
| 164 |  |  |  |  |  |  | #    -ExtraField => { $id1 => $data1, | 
| 165 |  |  |  |  |  |  | #                     $id2 => $data2, | 
| 166 |  |  |  |  |  |  | #                     ... | 
| 167 |  |  |  |  |  |  | #                   } | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 82 | 100 |  |  |  | 195 | if ( ! ref $dataRef ) { | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | return undef | 
| 172 | 48 | 100 |  |  |  | 156 | if ! $strict; | 
| 173 |  |  |  |  |  |  |  | 
| 174 | 30 |  |  |  |  | 110 | return parseRawExtra($dataRef, undef, 1, $gzipMode); | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 34 |  |  |  |  | 57 | my $data = $dataRef; | 
| 178 | 34 |  |  |  |  | 61 | my $out = '' ; | 
| 179 |  |  |  |  |  |  |  | 
| 180 | 34 | 100 |  |  |  | 98 | if (ref $data eq 'ARRAY') { | 
|  |  | 100 |  |  |  |  |  | 
| 181 | 27 | 100 |  |  |  | 82 | if (ref $data->[0]) { | 
| 182 |  |  |  |  |  |  |  | 
| 183 | 8 |  |  |  |  | 18 | foreach my $pair (@$data) { | 
| 184 | 10 | 100 |  |  |  | 36 | return ExtraFieldError("Not list of lists") | 
| 185 |  |  |  |  |  |  | unless ref $pair eq 'ARRAY' ; | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 8 |  |  |  |  | 22 | my $bad = validateExtraFieldPair($pair, $strict, $gzipMode) ; | 
| 188 | 8 | 100 |  |  |  | 46 | return $bad if $bad ; | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 3 |  |  |  |  | 19 | $out .= mkSubField(@$pair); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  | } | 
| 193 |  |  |  |  |  |  | else { | 
| 194 | 19 | 100 |  |  |  | 85 | return ExtraFieldError("Not even number of elements") | 
| 195 |  |  |  |  |  |  | unless @$data % 2  == 0; | 
| 196 |  |  |  |  |  |  |  | 
| 197 | 18 |  |  |  |  | 85 | for (my $ix = 0; $ix <= @$data -1 ; $ix += 2) { | 
| 198 | 23 |  |  |  |  | 136 | my $bad = validateExtraFieldPair([$data->[$ix], | 
| 199 |  |  |  |  |  |  | $data->[$ix+1]], | 
| 200 |  |  |  |  |  |  | $strict, $gzipMode) ; | 
| 201 | 23 | 100 |  |  |  | 69 | return $bad if $bad ; | 
| 202 |  |  |  |  |  |  |  | 
| 203 | 20 |  |  |  |  | 123 | $out .= mkSubField($data->[$ix], $data->[$ix+1]); | 
| 204 |  |  |  |  |  |  | } | 
| 205 |  |  |  |  |  |  | } | 
| 206 |  |  |  |  |  |  | } | 
| 207 |  |  |  |  |  |  | elsif (ref $data eq 'HASH') { | 
| 208 | 6 |  |  |  |  | 35 | while (my ($id, $info) = each %$data) { | 
| 209 | 8 |  |  |  |  | 27 | my $bad = validateExtraFieldPair([$id, $info], $strict, $gzipMode); | 
| 210 | 8 | 100 |  |  |  | 34 | return $bad if $bad ; | 
| 211 |  |  |  |  |  |  |  | 
| 212 | 5 |  |  |  |  | 16 | $out .= mkSubField($id, $info); | 
| 213 |  |  |  |  |  |  | } | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  | else { | 
| 216 | 1 |  |  |  |  | 6 | return ExtraFieldError("Not a scalar, array ref or hash ref") ; | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  |  | 
| 219 | 19 | 50 |  |  |  | 69 | return ExtraFieldError("Too Large") | 
| 220 |  |  |  |  |  |  | if length $out > GZIP_FEXTRA_MAX_SIZE; | 
| 221 |  |  |  |  |  |  |  | 
| 222 | 19 |  |  |  |  | 40 | $_[0] = $out ; | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 19 |  |  |  |  | 50 | return undef; | 
| 225 |  |  |  |  |  |  | } | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | 1; | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | __END__ |