| 1 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # File: Protobuf.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Description: Decode protocol buffer data | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Revisions: 2024-12-04 - P. Harvey Created | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # Notes: Tag definitions for Protobuf tags support 'signed', 'unsigned', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # and 'int64s' formats for VARINT (type 0) values, 'int64u', | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # 'int64s', 'rational64u', 'rational64s' and 'double' for I64 | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # (type 1), 'undef', 'string' and 'rational' for LEN (type 2), | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # and 'int32u', 'int32s', 'rational32u', 'rational32s', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # 'fixed32u', 'fixed32s' and 'float' for I32 (type 5) values. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||||||||
| 15 | # References: 1) https://protobuf.dev/programming-guides/encoding/ | ||||||||||||||||||||||||||||||||||||||||||
| 16 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | package Image::ExifTool::Protobuf; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | ▶ | use Image::ExifTool qw(:DataAccess :Utils); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | $VERSION = '1.07'; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | sub ProcessProtobuf($$$;$); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # largest unsigned integer on this system (2^32 or 2^64 - 1) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 12 | my $intMax = ~0; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # smallest unsigned integer that we interpret as int64s (0xffffffff00000000) | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 12 | my $int64sMin = 18446744069414584320; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # Read bytes from dirInfo object | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # Inputs: 0) dirInfo ref (with DataPt and Pos set), 1) number of bytes | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # Returns: binary data or undef on error | ||||||||||||||||||||||||||||||||||||||||||
| 38 | sub GetBytes($$) | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 57578 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
GetBytes: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 57578 | my ($dirInfo, $n) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 57578 | my $dataPt = $$dirInfo{DataPt}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 57578 | my $pos = $$dirInfo{Pos}; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 57578 | ▶ | return undef if $pos + $n > length $$dataPt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 57497 | $$dirInfo{Pos} += $n; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 57497 | return substr($$dataPt, $pos, $n); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # Read variable-length integer | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # Inputs: 0) dirInfo ref | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # Returns: integer value | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # - sets $$dirInfo{Bit0} according to bit 0 of returned value | ||||||||||||||||||||||||||||||||||||||||||
| 53 | # (necessary for cases where a signed integer exceeds $intMax) | ||||||||||||||||||||||||||||||||||||||||||
| 54 | sub VarInt($) | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 39576 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
VarInt: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 39576 | my $dirInfo = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 39576 | my $buff = GetBytes($dirInfo, 1); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 39576 | ▶ | return undef unless defined $buff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 39545 | my $val = ord($buff) & 0x7f; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 39545 | $$dirInfo{Bit0} = $val & 0x01; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 39545 | my $mult = 128; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 39545 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 39545 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 64 | 41222 | ▶ | last unless ord($buff) & 0x80; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 8419 | $buff = GetBytes($dirInfo, 1); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 8419 | ▶ | return undef unless defined $buff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 8419 | $val += (ord($buff) & 0x7f) * $mult; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 8419 | ▶ | last unless ord($buff) & 0x80; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1678 | ▶ | return undef if ++$i > 32; # set a reasonable limit | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1677 | $mult *= 128; # (Note: don't use integer bit shift to avoid integer overflow) | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 39544 | return $val; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # Read protobuf record | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # Inputs: 0) dirInfo ref | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # Returns: 0) record payload (plus tag id and format type in list context) or undef on error | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # Notes: Updates $$dirInfo{Pos} to start of next record, and sets $$dirInfo{Bit0} | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # according to the least significant bit of type 0 (varInt) records | ||||||||||||||||||||||||||||||||||||||||||
| 81 | sub ReadRecord($) | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 22842 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadRecord: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 22842 | my $dirInfo = shift; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 22842 | my $val = VarInt($dirInfo); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 22842 | ▶ | return undef unless defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 22836 | my $id = $val >> 3; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 22836 | my $type = $val & 0x07; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 22836 | my $buff; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 22836 | ▶ | if ($type == 0) { # varInt | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 12535 | $buff = VarInt($dirInfo); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } elsif ($type == 1) { # 64-bit number | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 380 | $buff = GetBytes($dirInfo, 8); | |||||||||||||||||||||||||||||||||||||||||
| 94 | } elsif ($type == 2) { # string, bytes or protobuf | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 4153 | my $len = VarInt($dirInfo); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 4153 | ▶ | if ($len) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3656 | $buff = GetBytes($dirInfo, $len); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 497 | $buff = ''; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | } elsif ($type == 3) { # (deprecated start group) | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 20 | $buff = ''; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } elsif ($type == 4) { # (deprecated end group) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 74 | $buff = ''; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } elsif ($type == 5) { # 32-bit number | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 5547 | $buff = GetBytes($dirInfo, 4); | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 22836 | ▶ | return wantarray ? ($buff, $id, $type) : $buff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # Check to see if this could be a protobuf object | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # Inputs: 0) data reference | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # Retursn: true if this looks like a protobuf | ||||||||||||||||||||||||||||||||||||||||||
| 115 | sub IsProtobuf($) | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 1745 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
IsProtobuf: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1745 | my $pt = shift; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 1745 | my $dirInfo = { DataPt => $pt, Pos => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 1745 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 11713 | ▶ | return 0 unless defined ReadRecord($dirInfo); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 11506 | ▶ | return 1 if $$dirInfo{Pos} == length $$pt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # Process protobuf data (eg. DJI djmd timed data from Action4 videos) (ref 1) | ||||||||||||||||||||||||||||||||||||||||||
| 127 | # Inputs: 0) ExifTool ref, 1) dirInfo ref with DataPt, DataPos, DirName and Base, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # 2) tag table ptr, 3) prefix of parent protobuf ID's | ||||||||||||||||||||||||||||||||||||||||||
| 129 | # Returns: true on success | ||||||||||||||||||||||||||||||||||||||||||
| 130 | sub ProcessProtobuf($$$;$) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 1539 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ProcessProtobuf: called CC=61 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 1539 | my ($et, $dirInfo, $tagTbl, $prefix) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 1539 | my $dataPt = $$dirInfo{DataPt}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1539 | my $dirName = $$dirInfo{DirName}; | |||||||||||||||||||||||||||||||||||||||||
| 135 | 1539 | ▶ | my $dirStart = $$dirInfo{DirStart} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dirInfo->{'DirStart'} || 0
MC/DC0%0/1
$dirInfo->{'DirStart'} || 0
$dirInfo->{'DirStart'}
Truth table50%1/2
A
A= $dirInfo->{'DirStart'}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 1539 | ▶ | my $dirLen = $$dirInfo{DirLen} || (length($$dataPt) - $dirStart); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$dirInfo->{'DirLen'} || length($$dataPt) - $dirStart
MC/DC0%0/2
$dirInfo->{'DirLen'} || length($$dataPt) - $dirStart
$dirInfo->{'DirLen'}
length($$dataPt) - $dirStart
Truth table33%1/3
A || B
A= $dirInfo->{'DirLen'}B= length($$dataPt) - $dirStart
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1539 | my $dirEnd = $dirStart + $dirLen; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 1539 | ▶ | my $dataPos = ($$dirInfo{Base} || 0) + ($$dirInfo{DataPos} || 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition75%3/4
$dirInfo->{'Base'} || 0
$dirInfo->{'DataPos'} || 0
MC/DC0%0/1
$dirInfo->{'Base'} || 0
$dirInfo->{'Base'}
MC/DC100%1/1
$dirInfo->{'DataPos'} || 0
$dirInfo->{'DataPos'}
Truth table50%1/2
A
A= $dirInfo->{'Base'}
Truth table100%2/2
A
A= $dirInfo->{'DataPos'}
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 1539 | ▶ | my $unknown = $et->Options('Unknown') || $et->Options('Verbose'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$et->Options('Unknown') || $et->Options('Verbose')
MC/DC0%0/2
$et->Options('Unknown') || $et->Options('Verbose')
$et->Options('Unknown')
$et->Options('Verbose')
Truth table33%1/3
A || B
A= $et->Options('Unknown')B= $et->Options('Verbose')
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 1539 | ▶ | $$dirInfo{Pos} = $$dirInfo{DirStart} || 0; # initialize buffer Pos | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$dirInfo->{'DirStart'} || 0
MC/DC0%0/1
$dirInfo->{'DirStart'} || 0
$dirInfo->{'DirStart'}
Truth table50%1/2
A
A= $dirInfo->{'DirStart'}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1539 | $et->VerboseDir('Protobuf', undef, $dirLen); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 1539 | ▶ | unless ($prefix) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | $prefix = ''; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | $$et{ProtoPrefix}{$dirName} = '' unless defined $$et{ProtoPrefix}{$dirName}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | SetByteOrder('II'); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # prefix for unknown tags | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 1539 | ▶ | my $unkPre = $$tagTbl{TAG_PREFIX} ? $$tagTbl{TAG_PREFIX} . '_' : 'Protobuf '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # loop through protobuf records | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 1539 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 153 | 12668 | my $pos = $$dirInfo{Pos}; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 12668 | ▶ | last if $pos >= $dirEnd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 11129 | my ($buff, $id, $type) = ReadRecord($dirInfo); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 11129 | ▶ | defined $buff or $et->Warn('Protobuf format error'), last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 11129 | ▶ | if ($type == 2 and $buff =~ /\.proto$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type == 2 and $buff =~ /\.proto$/
MC/DC0%0/2
$type == 2 and $buff =~ /\.proto$/
$type == 2
$buff =~ /\.proto$/
Truth table66%2/3
A and B
A= $type == 2B= $buff =~ /\.proto$/
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | # save protocol name separately for directory type | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $$et{ProtoPrefix}{$dirName} = substr($buff, 0, -6) . '_'; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $et->HandleTag($tagTbl, Protocol => $buff); | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 11129 | my $tag = "$$et{ProtoPrefix}{$dirName}$prefix$id"; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 11129 | my $tagInfo = $$tagTbl{$tag}; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 11129 | ▶ | if ($tagInfo) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 1630 | ▶ | next if $type != 2 and $$tagInfo{Unknown} and not $unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$type != 2 and $tagInfo->{"Unknown"} and not $unknown
$type != 2 and $tagInfo->{"Unknown"}
MC/DC66%2/3
$type != 2 and $tagInfo->{"Unknown"} and not $unknown
$type != 2
$tagInfo->{"Unknown"}
not $unknown
Truth table75%3/4
A and B and not(C)
A= $type != 2B= $tagInfo->{"Unknown"}C= not $unknown
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 9499 | ▶ | next unless $type == 2 or $unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type == 2 or $unknown
MC/DC50%1/2
$type == 2 or $unknown
$type == 2
$unknown
Truth table66%2/3
A or B
A= $type == 2B= $unknown
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 492 | $tagInfo = AddTagToTable($tagTbl, $tag, { Unknown => 1 }); | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | # set IsProtobuf flag (only for Unknown tags) if necessary | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 2071 | ▶ | if ($type == 2 and $$tagInfo{Unknown}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type == 2 and $tagInfo->{"Unknown"}
MC/DC100%2/2
$type == 2 and $tagInfo->{"Unknown"}
$type == 2
$tagInfo->{"Unknown"}
Truth table100%3/3
A and B
A= $type == 2B= $tagInfo->{"Unknown"}
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 2027 | ▶ | if ($$tagInfo{IsProtobuf}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 1175 | ▶ | $$tagInfo{IsProtobuf} = 0 unless IsProtobuf(\$buff); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | ▶ | } elsif (not defined $$tagInfo{IsProtobuf} and $buff =~ /[^\x20-\x7e]/ and | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
not defined $tagInfo->{"IsProtobuf"} and $buff =~ /[^\x20-\x7e]/ and &IsProtobuf(\$buff)
not defined $tagInfo->{"IsProtobuf"} and $buff =~ /[^\x20-\x7e]/
MC/DC100%3/3
not defined $tagInfo->{"IsProtobuf"} and $buff =~ /[^\x20-\x7e]/ and &IsProtobuf(\$buff)
not defined $tagInfo->{"IsProtobuf"}
$buff =~ /[^\x20-\x7e]/
&IsProtobuf(\$buff)
Truth table100%4/4
not(A) and B and C
A= not defined $tagInfo->{"IsProtobuf"}B= $buff =~ /[^\x20-\x7e]/C= &IsProtobuf(\$buff)
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | IsProtobuf(\$buff)) | ||||||||||||||||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 372 | $$tagInfo{IsProtobuf} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 2027 | ▶ | next unless $$tagInfo{IsProtobuf} or $unknown; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tagInfo->{"IsProtobuf"} or $unknown
MC/DC50%1/2
$tagInfo->{"IsProtobuf"} or $unknown
$tagInfo->{"IsProtobuf"}
$unknown
Truth table66%2/3
A or B
A= $tagInfo->{"IsProtobuf"}B= $unknown
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | # format binary payload into a useful value | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 1582 | my $val; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 1582 | ▶ | if ($$tagInfo{Format}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 21 | ▶ | if ($type == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | $val = $buff; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | ▶ | if ($$tagInfo{Format} eq 'signed') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ($val > $intMax) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | # use double math (15 decimal digits precision) | ||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | $val = $$dirInfo{Bit0} ? -int($val / 2) - 1 : $val / 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # use integer math | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $val = ($val & 1) ? -($val >> 1)-1 : ($val >> 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ▶ | } elsif ($$tagInfo{Format} eq 'int64s' and $val >= $int64sMin) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$tagInfo->{"Format"} eq "int64s" and $val >= $int64sMin
MC/DC0%0/2
$tagInfo->{"Format"} eq "int64s" and $val >= $int64sMin
$tagInfo->{"Format"} eq "int64s"
$val >= $int64sMin
Truth table33%1/3
A and B
A= $tagInfo->{"Format"} eq "int64s"B= $val >= $int64sMin
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | # hack for DJI drones which store 64-bit signed integers improperly | ||||||||||||||||||||||||||||||||||||||||||
| 196 | # (just toss upper 32 bits which should be all 1's anyway) | ||||||||||||||||||||||||||||||||||||||||||
| 197 | # Note: do the two subtractions because $int64sMin + 4294967296 | ||||||||||||||||||||||||||||||||||||||||||
| 198 | # is too large for a 64-bit integer | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | $val = $val - $int64sMin - 4294967296; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | ▶ | } elsif ($type == 2 and $$tagInfo{Format} eq 'rational') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$type == 2 and $tagInfo->{"Format"} eq "rational"
MC/DC0%0/2
$type == 2 and $tagInfo->{"Format"} eq "rational"
$type == 2
$tagInfo->{"Format"} eq "rational"
Truth table66%2/3
A and B
A= $type == 2B= $tagInfo->{"Format"} eq "rational"
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my $dir = { DataPt => \$buff, Pos => 0 }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $num = VarInt($dir); | |||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | my $den = VarInt($dir); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | $val = (defined $num and $den) ? $num/$den : 'err'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $num and $den
MC/DC0%0/2
defined $num and $den
defined $num
$den
Truth table0%0/3
A and B
A= defined $numB= $den
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 207 | 18 | $val = ReadValue(\$buff, 0, $$tagInfo{Format}, undef, length($buff)); | |||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||
| 209 | } elsif ($type == 0) { # varInt | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | $val = $buff; | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $hex = sprintf('%x', $val); | |||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | if ($val >= $int64sMin) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $s64 = $val - $int64sMin - 4294967296; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $val .= " (0x$hex, int64s $s64)"; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | my $signed; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($val > $intMax) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | $signed = $$dirInfo{Bit0} ? -int($val / 2) - 1 : $val / 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ▶ | $signed = ($val & 1) ? -($val >> 1)-1 : ($val >> 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $val .= " (0x$hex, signed $signed)"; | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | } elsif ($type == 1) { # 64-bit number | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | $val = '0x' . unpack('H*', $buff) . ' (double ' . GetDouble(\$buff,0) . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } elsif ($type == 2) { # string, bytes or protobuf | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 1561 | ▶ | if ($$tagInfo{SubDirectory}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | # (fall through to process known SubDirectory) | ||||||||||||||||||||||||||||||||||||||||||
| 229 | } elsif ($$tagInfo{IsProtobuf}) { | ||||||||||||||||||||||||||||||||||||||||||
| 230 | # process Unknown protobuf directories | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 1538 | $et->VPrint(1, "$$et{INDENT}${unkPre}$tag (" . length($buff) . " bytes) -->\n"); | |||||||||||||||||||||||||||||||||||||||||
| 232 | 1538 | my $addr = $dataPos + $$dirInfo{Pos} - length($buff); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 1538 | $et->VerboseDump(\$buff, Addr => $addr, Prefix => $$et{INDENT}); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 1538 | my %subdir = ( DataPt => \$buff, DataPos => $addr, DirName => $dirName ); | |||||||||||||||||||||||||||||||||||||||||
| 235 | 1538 | $$et{INDENT} .= '| '; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 1538 | ProcessProtobuf($et, \%subdir, $tagTbl, "$prefix$id-"); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 1538 | $$et{INDENT} = substr($$et{INDENT}, 0, -2); | |||||||||||||||||||||||||||||||||||||||||
| 238 | 1538 | next; | |||||||||||||||||||||||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 240 | # check for rational value (2 varInt values) | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 23 | my $rat; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 23 | my %dir = ( DataPt => \$buff, Pos => 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 23 | my $num = VarInt(\%dir); | |||||||||||||||||||||||||||||||||||||||||
| 244 | 23 | ▶ | if (defined $num) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 23 | my $denom = VarInt(\%dir); | |||||||||||||||||||||||||||||||||||||||||
| 246 | 23 | ▶ | $rat = " (rational $num/$denom)" if $denom and $dir{Pos} == length($buff); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$denom and $dir{"Pos"} == length $buff
MC/DC0%0/2
$denom and $dir{"Pos"} == length $buff
$denom
$dir{"Pos"} == length $buff
Truth table33%1/3
A and B
A= $denomB= $dir{"Pos"} == length $buff
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 23 | ▶ | if ($buff !~ /[^\r\n\t\x20-\x7e]/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 23 | $val = $buff; # assume this is an ASCII string | |||||||||||||||||||||||||||||||||||||||||
| 250 | } elsif (length($buff) % 4) { | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | $val = '0x' . unpack('H*', $buff); | |||||||||||||||||||||||||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $n = length($buff) / 4; | |||||||||||||||||||||||||||||||||||||||||
| 254 | # (do this instead of '(H8)*' because older Perl version didn't support this) | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | $val = '0x' . join(' ', unpack("(H8)$n", $buff)); # (group in 4-byte blocks) | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 23 | ▶ | $val .= $rat if $rat; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | } elsif ($type == 5) { # 32-bit number | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $val = '0x' . unpack('H*', $buff) . ' (int32u ' . Get32u(\$buff, 0); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $val .= ', int32s ' . Get32s(\$buff, 0) if ord(substr($buff,3,1)) & 0x80; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | $val .= ', float ' . GetFloat(\$buff, 0) . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $val = $buff; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | # get length of data in the record | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 44 | ▶ | my $start = $type == 0 ? $pos + 1 : $$dirInfo{Pos} - length $buff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | $et->HandleTag($tagTbl, $tag, $val, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | DataPt => $dataPt, | ||||||||||||||||||||||||||||||||||||||||||
| 270 | DataPos=> $dataPos, | ||||||||||||||||||||||||||||||||||||||||||
| 271 | Start => $start, | ||||||||||||||||||||||||||||||||||||||||||
| 272 | Size => $$dirInfo{Pos} - $start, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | Extra => ", type=$type", | ||||||||||||||||||||||||||||||||||||||||||
| 274 | Format => $$tagInfo{Format}, | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 44 | ); | |||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # warn if we didn't finish exactly at the end of the buffer | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 1539 | ▶ | $et->Warn('Truncated protobuf data') unless $prefix or $$dirInfo{Pos} == $dirEnd; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$prefix or $dirInfo->{"Pos"} == $dirEnd
MC/DC0%0/2
$prefix or $dirInfo->{"Pos"} == $dirEnd
$prefix
$dirInfo->{"Pos"} == $dirEnd
Truth table66%2/3
A or B
A= $prefixB= $dirInfo->{"Pos"} == $dirEnd
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 1539 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | __END__ | ||||||||||||||||||||||||||||||||||||||||||