PostGIS/Codec/WKB/Decoder.pm

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