PostGIS/Codec/WKB/Decoder.pm

filter: Statementstmt 100% 125 / 125 Branchbran 100% 66 / 66 Conditioncond 93.9% 31 / 33 MC/DCmcdc 90.0% 18 / 20 Subroutinesub 100% 18 / 18 Podpod 100% 2 / 2 totaltotal 98.4% 260 / 264 CC 47 SCAR 19.1
CC
47
Cov
99%
_read_geometry
11.0
_read_subgeom
10.0
_is_empty
9.0
CRAP 6.7
« PostGIS/Geometry.pm ↑ Summary PostGIS/ResultSet.pm »
1 package DBIO::PostgreSQL::PostGIS::Codec::WKB::Decoder;
2 # ABSTRACT: EWKB-hex decoder for PostGIS geometry values
3  
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.900004';
8  
9 11 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use DBIO::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 # The type ids PostGIS writes into the type word of an EWKB, as read off
14 # a PostGIS 3.6 server. t/34-undecoded-live.t holds the names from 8 on
15 # against the server it runs on.
16 11 my %WKB_TYPES = (
17 1 => 'point', 2 => 'linestring', 3 => 'polygon',
18 4 => 'multipoint', 5 => 'multilinestring', 6 => 'multipolygon',
19 7 => 'geometrycollection',
20 8 => 'circularstring', 9 => 'compoundcurve', 10 => 'curvepolygon',
21 11 => 'multicurve', 12 => 'multisurface',
22 15 => 'polyhedralsurface', 16 => 'tin', 17 => 'triangle',
23 );
24  
25 # The types whose body is decoded. Every other one is named, if it has a
26 # name, and its body is left alone.
27 11 my %DECODED = map { $_ => 1 } 1 .. 7;
28  
29 # What the members of a MULTI* are. A GEOMETRYCOLLECTION takes any type.
30 11 my %MEMBER_TYPE = (4 => 1, 5 => 2, 6 => 3);
31  
32 # Collections nest at most this deep; the outermost counts as level 1.
33 11 my $MAX_DEPTH = 32;
34  
35 11 my $SRID_FLAG = 0x20000000;
36 11 my $Z_FLAG = 0x80000000;
37 11 my $M_FLAG = 0x40000000;
38  
39  
40 175 ▶sub decode_hex {
Subroutine decode_hex: called CC=4 SCAR=13.9
Pod documented
41 175 my ($class, $hex) = @_;
42 175 my $bin = _unhex($hex);
43 168 my $len = length($bin);
44 168 my $pos = 0;
45  
46 168 my ($header, $le, $type_id) = _read_header(\$bin, \$pos, $len);
47 my $coords = _read_geometry(
48 161 \$bin, \$pos, $le, $type_id, $header->{has_z}, $header->{has_m}, $len, 0,
49 );
50  
51 # Where an undecoded body ends is not known, so only a decoded value
52 # can have bytes left behind it.
53 140 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if defined $coords and $pos < $len 4 136 140
Condition100%3/3
defined $coords and $pos < $len
!ll&&!rl&&r
38984
MC/DC100%2/2
defined $coords and $pos < $len
defined $coords $pos < $len
Truth table100%3/3
A and B
A= defined $coordsB= $pos < $len
ABresult
0X0
100
111
54 "decode_hex: trailing bytes at pos $pos (" . ($len - $pos) . " after the geometry)"
55 ) if defined $coords && $pos < $len;
56  
57 # Nothing is known about a body that is not decoded, empty or not.
58 136 ▶ my $is_empty = defined $coords ? _is_empty($type_id, $coords) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $coords ? : 98 38 136
59  
60 136 return { %$header, coords => $coords, is_empty => $is_empty };
61 }
62  
63  
64 22 ▶sub decode_header {
Subroutine decode_header: called CC=1 SCAR=0.0
Pod documented
65 22 my ($class, $hex) = @_;
66 22 my $bin = _unhex($hex);
67 15 my $pos = 0;
68 15 my ($header) = _read_header(\$bin, \$pos, length($bin));
69 10 return $header;
70 }
71  
72 # The bytes of a hex string. pack('H*') takes anything: it pads a lone
73 # last nibble with zero and reads any other character as a digit, so a cut
74 # off or damaged string came out as a shifted, wrong geometry.
75 197 ▶sub _unhex {
Subroutine _unhex: called CC=2 SCAR=6.9
76 197 my ($hex) = @_;
77 197 ▶ DBIO::Exception->throw('decode_hex: input is not hex (odd length or a non-hex character)')
Branch: 1 branch
BranchTrueFalseTotal
unless $hex =~ /\A(?:[0-9A-Fa-f]{2})*\z/ 14 183 197
78 unless $hex =~ /\A(?:[0-9A-Fa-f]{2})*\z/;
79 183 return pack('H*', $hex);
80 }
81  
82 # The header, and what the body reader needs to go on: the byte order and
83 # the numeric type.
84 183 ▶sub _read_header {
Subroutine _read_header: called CC=8 SCAR=20.8
85 183 my ($buf, $pos, $len) = @_;
86  
87 183 ▶ DBIO::Exception->throw('decode_hex: empty input') unless $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $len > 0 2 181 183
88  
89 181 my $byte_order = _read_uint8($buf, $pos, $len);
90 181 ▶ DBIO::Exception->throw("decode_hex: invalid byte_order") unless $byte_order == 1 || $byte_order == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $byte_order == 1 or $byte_order == 0 6 175 181
Condition100%3/3
$byte_order == 1 or $byte_order == 0
l!l&&r!l&&!r
164116
MC/DC100%2/2
$byte_order == 1 or $byte_order == 0
$byte_order == 1 $byte_order == 0
Truth table100%3/3
A or B
A= $byte_order == 1B= $byte_order == 0
ABresult
1X1
011
000
91 175 my $le = ($byte_order == 1);
92  
93 175 my $type_raw = _read_uint32($buf, $pos, $le, $len);
94 173 ▶ my $has_srid = ($type_raw & $SRID_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $SRID_FLAG ? : 122 51 173
95 173 ▶ my $has_z = ($type_raw & $Z_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $Z_FLAG ? : 25 148 173
96 173 ▶ my $has_m = ($type_raw & $M_FLAG) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type_raw & $M_FLAG ? : 25 148 173
97 173 my $type_id = $type_raw & 0xFFFF;
98  
99 173 my $srid;
100 173 ▶ if ($has_srid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_srid) 122 51 173
101 122 $srid = _read_uint32($buf, $pos, $le, $len);
102 }
103  
104 my $header = {
105 171 type => $WKB_TYPES{$type_id}, srid => $srid, has_z => $has_z, has_m => $has_m,
106 };
107 171 return ($header, $le, $type_id);
108 }
109  
110 1860 ▶sub _check_bounds {
Subroutine _check_bounds: called CC=2 SCAR=6.9
111 1860 my ($buf, $pos, $need, $len) = @_;
112 1860 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if $$pos + $need > $len 12 1848 1860
113 "decode_hex: underrun at pos $$pos (need $need, have " . ($len - $$pos) . ")"
114 ) if $$pos + $need > $len;
115 }
116  
117 400 ▶sub _read_uint8 {
Subroutine _read_uint8: called CC=1 SCAR=0.0
118 400 my ($buf, $pos, $len) = @_;
119 400 _check_bounds($buf, $pos, 1, $len);
120 400 my $v = unpack('C', substr($$buf, $$pos, 1));
121 400 $$pos += 1; $v;
122 }
123 778 ▶sub _read_uint32 {
Subroutine _read_uint32: called CC=2 SCAR=6.9
124 778 my ($buf, $pos, $le, $len) = @_;
125 778 _check_bounds($buf, $pos, 4, $len);
126 774 ▶ my $v = unpack($le ? 'V' : 'N', substr($$buf, $$pos, 4));
Branch: 1 branch
BranchTrueFalseTotal
$le ? : 721 53 774
127 774 $$pos += 4; $v;
128 }
129 421 ▶sub _read_double {
Subroutine _read_double: called CC=2 SCAR=6.9
130 421 my ($buf, $pos, $le, $len) = @_;
131 421 _check_bounds($buf, $pos, 8, $len);
132 418 ▶ my $v = unpack($le ? 'd<' : 'd>', substr($$buf, $$pos, 8));
Branch: 1 branch
BranchTrueFalseTotal
$le ? : 349 69 418
133 418 $$pos += 8; $v;
134 }
135  
136 # The point of a POINT, or of a MULTIPOINT or a collection: PostGIS writes
137 # POINT EMPTY as a point of NaN ordinates. That is no point; its
138 # coordinates are an empty list. (A point in a line is read by _read_point
139 # and is never empty.)
140 79 ▶sub _read_geometry_point {
Subroutine _read_geometry_point: called CC=4 SCAR=13.9
141 79 my ($c) = @_;
142 79 ▶ return [] if @$c >= 2 && $c->[0] != $c->[0] && $c->[1] != $c->[1];
Branch: 1 branch
BranchTrueFalseTotal
if @$c >= 2 and $c->[0] != $c->[0] and $c->[1] != $c->[1] 12 67 79
Condition83%5/6
@$c >= 2 and $c->[0] != $c->[0] and $c->[1] != $c->[1]
!ll&&!rl&&r
65212
@$c >= 2 and $c->[0] != $c->[0]
!ll&&!rl&&r
06514
MC/DC66%2/3
@$c >= 2 and $c->[0] != $c->[0] and $c->[1] != $c->[1]
@$c >= 2 $c->[0] != $c->[0] $c->[1] != $c->[1]
Truth table75%3/4
A and B and C
A= @$c >= 2B= $c->[0] != $c->[0]C= $c->[1] != $c->[1]
ABCresult
0XX0
10X0
1100
1111
143 67 return $c;
144 }
145  
146 195 ▶sub _read_point {
Subroutine _read_point: called CC=3 SCAR=11.0
147 195 my ($buf, $pos, $le, $has_z, $has_m, $len) = @_;
148 195 my @c = (_read_double($buf, $pos, $le, $len), _read_double($buf, $pos, $le, $len));
149 192 ▶ push @c, _read_double($buf, $pos, $le, $len) if $has_z;
Branch: 1 branch
BranchTrueFalseTotal
if $has_z 17 175 192
150 192 ▶ push @c, _read_double($buf, $pos, $le, $len) if $has_m;
Branch: 1 branch
BranchTrueFalseTotal
if $has_m 14 178 192
151 192 return \@c;
152 }
153  
154 # A count of things that follow, each of them at least $min bytes long. A
155 # count the rest of the input cannot hold is refused here, before a list
156 # is sized by it: the four bytes are input like any other, and a list of
157 # 0xFFFFFFFF elements is the end of the process, not an exception.
158 261 ▶sub _read_count {
Subroutine _read_count: called CC=1 SCAR=0.0
159 261 my ($buf, $pos, $le, $len, $min) = @_;
160 261 my $n = _read_uint32($buf, $pos, $le, $len);
161 261 _check_bounds($buf, $pos, $n * $min, $len);
162 256 return $n;
163 }
164  
165 # The coordinates of the geometry at $pos, or undef for one that is not
166 # decoded. undef is not an error and says nothing about the bytes; it has
167 # to reach the caller as it is, so nothing here may go on reading after
168 # it -- where an undecoded body ends is not known.
169 370 ▶sub _read_geometry {
Subroutine _read_geometry: called CC=11 SCAR=24.0
170 370 my ($buf, $pos, $le, $type_id, $has_z, $has_m, $len, $depth) = @_;
171 370 ▶ return undef unless $DECODED{$type_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $DECODED{$type_id} 38 332 370
172  
173 332 ▶ DBIO::Exception->throw('decode_hex: nesting too deep')
Branch: 1 branch
BranchTrueFalseTotal
if $type_id == 7 and ++$depth > $MAX_DEPTH 3 329 332
Condition100%3/3
$type_id == 7 and ++$depth > $MAX_DEPTH
!ll&&!rl&&r
1681613
MC/DC100%2/2
$type_id == 7 and ++$depth > $MAX_DEPTH
$type_id == 7 ++$depth > $MAX_DEPTH
Truth table100%3/3
A and B
A= $type_id == 7B= ++$depth > $MAX_DEPTH
ABresult
0X0
100
111
174 if $type_id == 7 && ++$depth > $MAX_DEPTH;
175  
176 329 ▶ if ($type_id == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 1) 82 247 329
177 82 return _read_geometry_point(_read_point($buf, $pos, $le, $has_z, $has_m, $len));
178 }
179 247 my $point_size = 8 * (2 + $has_z + $has_m);
180 247 ▶ if ($type_id == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 2) 32 215 247
181 32 my $n = _read_count($buf, $pos, $le, $len, $point_size);
182 31 return [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$n ];
183 }
184 215 ▶ if ($type_id == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_id == 3) 18 197 215
185 # A ring is at least its own count.
186 18 my $n = _read_count($buf, $pos, $le, $len, 4);
187 17 my @rings;
188 17 for (1..$n) {
189 14 my $m = _read_count($buf, $pos, $le, $len, $point_size);
190 13 push @rings, [ map { _read_point($buf, $pos, $le, $has_z, $has_m, $len) } 1..$m ];
191 }
192 16 return \@rings;
193 }
194  
195 # Members, each with a header of its own: byte order and type word. A
196 # MULTI* holds their coordinates, a collection the members themselves,
197 # with their type.
198 197 my $n = _read_count($buf, $pos, $le, $len, 5);
199 195 my @members;
200 195 for (1..$n) {
201 my ($member_type, $member) = _read_subgeom(
202 219 $buf, $pos, $le, $MEMBER_TYPE{$type_id}, $has_z, $has_m, $len, $depth,
203 );
204 113 ▶ return undef unless defined $member;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $member 6 107 113
205 push @members, $type_id == 7
206 107 ▶ ? { type => $WKB_TYPES{$member_type}, coords => $member,
Branch: 1 branch
BranchTrueFalseTotal
$type_id == 7 ? : 69 38 107
207 is_empty => _is_empty($member_type, $member) }
208 : $member;
209 }
210 83 return \@members;
211 }
212  
213 # Empty as ST_IsEmpty says: no parts, or nothing but empty ones. $coords
214 # is that of a decoded geometry; an empty point is [], a point is never a
215 # list of parts.
216 167 ▶sub _is_empty {
Subroutine _is_empty: called CC=9 SCAR=22.0
217 167 my ($type_id, $coords) = @_;
218 167 ▶ return @$coords ? 0 : 1 if $type_id == 1 || $type_id == 2 || $type_id == 3;
Branch: 2 branches
BranchTrueFalseTotal
if $type_id == 1 or $type_id == 2 or $type_id == 3 85 82 167
@$coords ? : 68 17 85
Condition100%6/6
$type_id == 1 or $type_id == 2 or $type_id == 3
l!l&&r!l&&!r
76982
$type_id == 1 or $type_id == 2
l!l&&r!l&&!r
522491
MC/DC100%3/3
$type_id == 1 or $type_id == 2 or $type_id == 3
$type_id == 1 $type_id == 2 $type_id == 3
Truth table100%4/4
A or B or C
A= $type_id == 1B= $type_id == 2C= $type_id == 3
ABCresult
1XX1
01X1
0011
0000
219 82 ▶ return (grep { @$_ } @$coords) ? 0 : 1 if $type_id >= 4 && $type_id <= 6;
Branch: 2 branches
BranchTrueFalseTotal
if $type_id >= 4 and $type_id <= 6 27 55 82
grep({@$_;} @$coords) ? : 20 7 27
Condition66%2/3
$type_id >= 4 and $type_id <= 6
!ll&&!rl&&r
05527
MC/DC50%1/2
$type_id >= 4 and $type_id <= 6
$type_id >= 4 $type_id <= 6
Truth table66%2/3
A and B
A= $type_id >= 4B= $type_id <= 6
ABresult
0X0
100
111
220 55 ▶ return (grep { !$_->{is_empty} } @$coords) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({not $_->{'is_empty'};} @$coords) ? : 16 39 55
221 }
222  
223 # One member of a MULTI* or of a collection: its type id and its
224 # coordinates (undef if it is not decoded). $expect is the type a MULTI*
225 # takes; a collection passes none and takes them all. A member has a
226 # header of its own: it is read in its own byte order, not that of the
227 # whole, and its Z and M flags are those of the whole or the value is
228 # refused, as PostGIS does ("Dimensions mismatch in lwcollection"). PostGIS
229 # writes no SRID into a member; one that is there is read and dropped.
230 219 ▶sub _read_subgeom {
Subroutine _read_subgeom: called CC=10 SCAR=23.0
231 219 my ($buf, $pos, $le, $expect, $has_z, $has_m, $len, $depth) = @_;
232 219 my $start = $$pos;
233 219 my $byte_order = _read_uint8($buf, $pos, $len);
234 219 ▶ DBIO::Exception->throw("decode_hex: invalid byte_order") unless $byte_order == 1 || $byte_order == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $byte_order == 1 or $byte_order == 0 1 218 219
Condition100%3/3
$byte_order == 1 or $byte_order == 0
l!l&&r!l&&!r
203151
MC/DC100%2/2
$byte_order == 1 or $byte_order == 0
$byte_order == 1 $byte_order == 0
Truth table100%3/3
A or B
A= $byte_order == 1B= $byte_order == 0
ABresult
1X1
011
000
235 218 $le = ($byte_order == 1);
236 218 my $type_raw = _read_uint32($buf, $pos, $le, $len);
237 218 my $type_id = $type_raw & 0xFFFF;
238 218 ▶ _read_uint32($buf, $pos, $le, $len) if $type_raw & $SRID_FLAG;
Branch: 1 branch
BranchTrueFalseTotal
if $type_raw & $SRID_FLAG 2 216 218
239 218 ▶ DBIO::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
if defined $expect and $type_id != $expect 4 214 218
Condition100%3/3
defined $expect and $type_id != $expect
!ll&&!rl&&r
174404
MC/DC100%2/2
defined $expect and $type_id != $expect
defined $expect $type_id != $expect
Truth table100%3/3
A and B
A= defined $expectB= $type_id != $expect
ABresult
0X0
100
111
240 "decode_hex: member of type $type_id at pos $start"
241 . ", where only type $expect can be"
242 ) if defined $expect && $type_id != $expect;
243 214 ▶ DBIO::Exception->throw(
Branch: 3 branches
BranchTrueFalseTotal
if ($type_raw & $Z_FLAG ? 1 : 0) != $has_z or ($type_raw & $M_FLAG ? 1 : 0) != $has_m 5 209 214
$type_raw & $Z_FLAG ? : 8 206 214
$type_raw & $M_FLAG ? : 5 206 211
Condition100%3/3
($type_raw & $Z_FLAG ? 1 : 0) != $has_z or ($type_raw & $M_FLAG ? 1 : 0) != $has_m
l!l&&r!l&&!r
32209
MC/DC100%2/2
($type_raw & $Z_FLAG ? 1 : 0) != $has_z or ($type_raw & $M_FLAG ? 1 : 0) != $has_m
($type_raw & $Z_FLAG ? 1 : 0) != $has_z ($type_raw & $M_FLAG ? 1 : 0) != $has_m
Truth table100%3/3
A or B
A= ($type_raw & $Z_FLAG ? 1 : 0) != $has_zB= ($type_raw & $M_FLAG ? 1 : 0) != $has_m
ABresult
1X1
011
000
244 "decode_hex: member at pos $start has other dimensions than the whole"
245 ) if (($type_raw & $Z_FLAG) ? 1 : 0) != $has_z || (($type_raw & $M_FLAG) ? 1 : 0) != $has_m;
246 209 return ($type_id, _read_geometry($buf, $pos, $le, $type_id, $has_z, $has_m, $len, $depth));
247 }
248  
249 11 1;
250  
251 __END__
« PostGIS/Geometry.pm ↑ Summary PostGIS/ResultSet.pm »