| 1 | #------------------------------------------------------------------------------ | ||
| 2 | # File: CBOR.pm | ||
| 3 | # | ||
| 4 | # Description: Read CBOR format metadata | ||
| 5 | # | ||
| 6 | # Revisions: 2021-09-30 - P. Harvey Created | ||
| 7 | # | ||
| 8 | # References: 1) https://c2pa.org/public-draft/ | ||
| 9 | # 2) https://datatracker.ietf.org/doc/html/rfc7049 | ||
| 10 | # 3) https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml | ||
| 11 | #------------------------------------------------------------------------------ | ||
| 12 | |||
| 13 | package Image::ExifTool::CBOR; | ||
| 14 | use strict; | ||
| 15 | use vars qw($VERSION); | ||
| 16 | use Image::ExifTool qw(:DataAccess :Utils); | ||
| 17 | use Image::ExifTool::JSON; | ||
| 18 | |||
| 19 | $VERSION = '1.04'; | ||
| 20 | |||
| 21 | sub ProcessCBOR($$$); | ||
| 22 | sub ReadCBORValue($$$$); | ||
| 23 | |||
| 24 | # optional CBOR type code | ||
| 25 | my %cborType6 = ( | ||
| 26 | 0 => 'date/time string', | ||
| 27 | 1 => 'epoch-based date/time', | ||
| 28 | 2 => 'positive bignum', | ||
| 29 | 3 => 'negative bignum', | ||
| 30 | 4 => 'decimal fraction', | ||
| 31 | 5 => 'bigfloat', | ||
| 32 | 16 => 'COSE Encrypt0', #3 (COSE Single Recipient Encrypted Data Object) | ||
| 33 | 17 => 'COSE Mac0', #3 (COSE Mac w/o Recipients Object) | ||
| 34 | 18 => 'COSE Sign1', #3 (COSE Single Signer Data Object) | ||
| 35 | 19 => 'COSE Countersignature', #3 (COSE standalone V2 countersignature) | ||
| 36 | 21 => 'expected base64url encoding', | ||
| 37 | 22 => 'expected base64 encoding', | ||
| 38 | 23 => 'expected base16 encoding', | ||
| 39 | 24 => 'encoded CBOR data', | ||
| 40 | 25 => 'string number', #3 (reference the nth previously seen string) | ||
| 41 | 26 => 'serialized Perl', #3 (Serialised Perl object with classname and constructor arguments) | ||
| 42 | 27 => 'serialized code', #3 (Serialised language-independent object with type name and constructor arguments) | ||
| 43 | 28 => 'shared value', #3 (mark value as (potentially) shared) | ||
| 44 | 29 => 'shared value number', #3 (reference nth marked value) | ||
| 45 | 30 => 'rational', #3 (Rational number) | ||
| 46 | 31 => 'missing array value', #3 (Absent value in a CBOR Array) | ||
| 47 | 32 => 'URI', | ||
| 48 | 33 => 'base64url', | ||
| 49 | 34 => 'base64', | ||
| 50 | 35 => 'regular expression', | ||
| 51 | 36 => 'MIME message', | ||
| 52 | # (lots more after this in ref 3, but don't include them unless we see them) | ||
| 53 | 55799 => 'CBOR magic number', | ||
| 54 | ); | ||
| 55 | |||
| 56 | my %cborType7 = ( | ||
| 57 | 20 => 'False', | ||
| 58 | 21 => 'True', | ||
| 59 | 22 => 'null', | ||
| 60 | 23 => 'undef', | ||
| 61 | ); | ||
| 62 | |||
| 63 | %Image::ExifTool::CBOR::Main = ( | ||
| 64 | GROUPS => { 0 => 'JUMBF', 1 => 'CBOR', 2 => 'Other' }, | ||
| 65 | VARS => { ID_FMT => 'none' }, | ||
| 66 | PROCESS_PROC => \&ProcessCBOR, | ||
| 67 | NOTES => q{ | ||
| 68 | The tags below are extracted from CBOR (Concise Binary Object | ||
| 69 | Representation) metadata. The C2PA specification uses this format for some | ||
| 70 | metadata. As well as these tags, ExifTool will read any existing tags. | ||
| 71 | }, | ||
| 72 | 'dc:title' => 'Title', | ||
| 73 | 'dc:format' => 'Format', | ||
| 74 | # my sample file has the following 2 tags in CBOR, but they should be JSON | ||
| 75 | authorName => { Name => 'AuthorName', Groups => { 2 => 'Author' } }, | ||
| 76 | authorIdentifier=> { Name => 'AuthorIdentifier', Groups => { 2 => 'Author' } }, | ||
| 77 | documentID => { }, | ||
| 78 | instanceID => { }, | ||
| 79 | thumbnailHash => { List => 1 }, | ||
| 80 | thumbnailUrl => { Name => 'ThumbnailURL' }, | ||
| 81 | relationship => { } | ||
| 82 | ); | ||
| 83 | |||
| 84 | #------------------------------------------------------------------------------ | ||
| 85 | # Read CBOR value | ||
| 86 | # Inputs: 0) ExifTool ref, 1) data ref, 2) position in data, 3) data end | ||
| 87 | # Returns: 0) value, 1) error string, 2) new data position | ||
| 88 | sub ReadCBORValue($$$$) | ||
| 89 | { | ||
| 90 | my ($et, $dataPt, $pos, $end) = @_; | ||
| 91 | return(undef, 'Truncated CBOR data', $pos) if $pos >= $end; | ||
| 92 | my $verbose = $$et{OPTIONS}{Verbose}; | ||
| 93 | my $indent = $$et{INDENT}; | ||
| 94 | my $dumpStart = $pos; | ||
| 95 | my $fmt = Get8u($dataPt, $pos++); | ||
| 96 | my $dat = $fmt & 0x1f; | ||
| 97 | my ($num, $val, $err, $size); | ||
| 98 | $fmt >>= 5; | ||
| 99 | if ($dat < 24) { | ||
| 100 | $num = $dat; | ||
| 101 | } elsif ($dat == 31) { # indefinite count (not used in C2PA) | ||
| 102 | $num = -1; # (flag for indefinite count) | ||
| 103 | $et->VPrint(1, "$$et{INDENT} (indefinite count):\n"); | ||
| 104 | } else { | ||
| 105 | my $format = { 24 => 'int8u', 25 => 'int16u', 26 => 'int32u', 27 => 'int64u' }->{$dat}; | ||
| 106 | return(undef, "Invalid CBOR integer type $dat", $pos) unless $format; | ||
| 107 | $size = Image::ExifTool::FormatSize($format); | ||
| 108 | return(undef, 'Truncated CBOR integer value', $pos) if $pos + $size > $end; | ||
| 109 | $num = ReadValue($dataPt, $pos, $format, 1, $size); | ||
| 110 | $pos += $size; | ||
| 111 | } | ||
| 112 | my $pre = ''; | ||
| 113 | if (defined $$et{cbor_pre} and $fmt != 6) { | ||
| 114 | $pre = $$et{cbor_pre}; | ||
| 115 | delete $$et{cbor_pre}; | ||
| 116 | } | ||
| 117 | if ($fmt == 0) { # positive integer | ||
| 118 | $val = $num; | ||
| 119 | $et->VPrint(1, "$$et{INDENT} ${pre}int+: $val\n"); | ||
| 120 | } elsif ($fmt == 1) { # negative integer | ||
| 121 | $val = -1 * $num; | ||
| 122 | $et->VPrint(1, "$$et{INDENT} ${pre}int-: $val\n"); | ||
| 123 | } elsif ($fmt == 2 or $fmt == 3) { # byte/UTF8 string | ||
| 124 | return(undef, 'Truncated CBOR string value', $pos) if $pos + $num > $end; | ||
| 125 | if ($num < 0) { # (should not happen in C2PA) | ||
| 126 | my $string = ''; | ||
| 127 | $$et{INDENT} .= ' '; | ||
| 128 | for (;;) { | ||
| 129 | ($val, $err, $pos) = ReadCBORValue($et, $dataPt, $pos, $end); | ||
| 130 | return(undef, $err, $pos) if $err; | ||
| 131 | last if not defined $val; # hit the break? | ||
| 132 | # (note: strictly we should be checking that this was a string we read) | ||
| 133 | $string .= $val; | ||
| 134 | } | ||
| 135 | $$et{INDENT} = $indent; | ||
| 136 | return($string, undef, $pos); # return concatenated byte/text string | ||
| 137 | } else { | ||
| 138 | $val = substr($$dataPt, $pos, $num); | ||
| 139 | } | ||
| 140 | $pos += $num; | ||
| 141 | if ($fmt == 2) { # (byte string) | ||
| 142 | $et->VPrint(1, "$$et{INDENT} ${pre}byte: <binary data ".length($val)." bytes>\n"); | ||
| 143 | my $dat = $val; | ||
| 144 | $val = \$dat; # use scalar reference for binary data | ||
| 145 | } else { # (text string) | ||
| 146 | $val = $et->Decode($val, 'UTF8'); | ||
| 147 | $et->VPrint(1, "$$et{INDENT} ${pre}text: '${val}'\n"); | ||
| 148 | } | ||
| 149 | } elsif ($fmt == 4 or $fmt == 5) { # list/hash | ||
| 150 | if ($fmt == 4) { | ||
| 151 | $et->VPrint(1, "$$et{INDENT} ${pre}list: <$num elements>\n"); | ||
| 152 | } else { | ||
| 153 | $et->VPrint(1, "$$et{INDENT} ${pre}hash: <$num pairs>\n"); | ||
| 154 | $num *= 2; | ||
| 155 | } | ||
| 156 | $$et{INDENT} .= ' '; | ||
| 157 | my $i = 0; | ||
| 158 | my @list; | ||
| 159 | Image::ExifTool::HexDump($dataPt, $pos - $dumpStart, | ||
| 160 | Start => $dumpStart, | ||
| 161 | DataPos => $$et{cbor_datapos}, | ||
| 162 | Prefix => $$et{INDENT}, | ||
| 163 | Out => $et->Options('TextOut'), | ||
| 164 | ) if $verbose > 2; | ||
| 165 | while ($num) { | ||
| 166 | $$et{cbor_pre} = "$i) "; | ||
| 167 | if ($fmt == 4) { | ||
| 168 | ++$i; | ||
| 169 | } elsif ($num & 0x01) { | ||
| 170 | $$et{cbor_pre} = ' ' x length($$et{cbor_pre}); | ||
| 171 | ++$i; | ||
| 172 | } | ||
| 173 | ($val, $err, $pos) = ReadCBORValue($et, $dataPt, $pos, $end); | ||
| 174 | return(undef, $err, $pos) if $err; | ||
| 175 | if (not defined $val) { | ||
| 176 | return(undef, 'Unexpected list terminator', $pos) unless $num < 0; | ||
| 177 | last; | ||
| 178 | } | ||
| 179 | push @list, $val; | ||
| 180 | --$num; | ||
| 181 | } | ||
| 182 | $dumpStart = $pos; | ||
| 183 | $$et{INDENT} = $indent; | ||
| 184 | if ($fmt == 5) { | ||
| 185 | my ($i, @keys); | ||
| 186 | my %hash = ( _ordered_keys_ => \@keys ); | ||
| 187 | for ($i=0; $i<@list-1; $i+=2) { | ||
| 188 | $hash{$list[$i]} = $list[$i+1]; | ||
| 189 | push @keys, $list[$i]; # save ordered list of keys | ||
| 190 | } | ||
| 191 | $val = \%hash; | ||
| 192 | } else { | ||
| 193 | $val = \@list; | ||
| 194 | } | ||
| 195 | } elsif ($fmt == 6) { # optional tag | ||
| 196 | if ($verbose) { | ||
| 197 | my $str = "$num (" . ($cborType6{$num} || 'unknown') . ')'; | ||
| 198 | my $spc = $$et{cbor_pre} ? (' ' x length $$et{cbor_pre}) : ''; | ||
| 199 | $et->VPrint(1, "$$et{INDENT} $spc<CBOR optional type $str>\n"); | ||
| 200 | Image::ExifTool::HexDump($dataPt, $pos - $dumpStart, | ||
| 201 | Start => $dumpStart, | ||
| 202 | DataPos => $$et{cbor_datapos}, | ||
| 203 | Prefix => $$et{INDENT} . ' ', | ||
| 204 | Out => $et->Options('TextOut'), | ||
| 205 | ) if $verbose > 2; | ||
| 206 | } | ||
| 207 | # read next value (note: in the case of multiple tags, | ||
| 208 | # this nesting will apply the tags in the correct order) | ||
| 209 | ($val, $err, $pos) = ReadCBORValue($et, $dataPt, $pos, $end); | ||
| 210 | return(undef, $err, $pos) if $err; | ||
| 211 | $dumpStart = $pos; | ||
| 212 | # convert some values according to the optional tag number (untested) | ||
| 213 | if ($num == 0 and not ref $val) { # date/time string | ||
| 214 | require Image::ExifTool::XMP; | ||
| 215 | $val = Image::ExifTool::XMP::ConvertXMPDate($val); | ||
| 216 | } elsif ($num == 1 and not ref $val) { # epoch-based date/time | ||
| 217 | if (Image::ExifTool::IsFloat($val)) { | ||
| 218 | my $dec = ($val == int($val)) ? undef : 6; | ||
| 219 | $val = Image::ExifTool::ConvertUnixTime($val, 1, $dec); | ||
| 220 | } | ||
| 221 | } elsif (($num == 2 or $num == 3) and ref($val) eq 'SCALAR') { # pos/neg bignum | ||
| 222 | my $big = 0; | ||
| 223 | $big = 256 * $big + Get8u($val,$_) foreach 0..(length($$val) - 1); | ||
| 224 | $val = $num==2 ? $big : -$big; | ||
| 225 | } elsif (($num == 4 or $num == 5) and # decimal fraction or bigfloat | ||
| 226 | ref($val) eq 'ARRAY' and @$val == 2 and | ||
| 227 | Image::ExifTool::IsInt($$val[0]) and Image::ExifTool::IsInt($$val[1])) | ||
| 228 | { | ||
| 229 | $val = $$val[1] * ($num == 4 ? 10 : 2) ** $$val[0]; | ||
| 230 | } | ||
| 231 | } elsif ($fmt == 7) { | ||
| 232 | if ($dat == 31) { | ||
| 233 | undef $val; # "break" = end of indefinite array/hash (not used in C2PA) | ||
| 234 | } elsif ($dat < 24) { | ||
| 235 | $val = $cborType7{$num}; | ||
| 236 | $val = "Unknown ($val)" unless defined $val; | ||
| 237 | } elsif ($dat == 25) { # half-precision float | ||
| 238 | my $exp = ($num >> 10) & 0x1f; | ||
| 239 | my $mant = $num & 0x3ff; | ||
| 240 | if ($exp == 0) { | ||
| 241 | $val = $mant ** -24; | ||
| 242 | $val *= -1 if $num & 0x8000; | ||
| 243 | } elsif (exp != 31) { | ||
| 244 | $val = ($mant + 1024) ** ($exp - 25); | ||
| 245 | $val *= -1 if $num & 0x8000; | ||
| 246 | } else { | ||
| 247 | $val = $mant == 0 ? '<inf>' : '<nan>'; | ||
| 248 | } | ||
| 249 | } elsif ($dat == 26) { # float | ||
| 250 | $val = GetFloat($dataPt, $pos - $size); | ||
| 251 | } elsif ($dat == 27) { # double | ||
| 252 | $val = GetDouble($dataPt, $pos - $size); | ||
| 253 | } else { | ||
| 254 | return(undef, "Invalid CBOR type 7 variant $num", $pos); | ||
| 255 | } | ||
| 256 | $et->VPrint(1, "$$et{INDENT} ${pre}typ7: ".(defined $val ? $val : '<break>')."\n"); | ||
| 257 | } else { | ||
| 258 | return(undef, "Unknown CBOR format $fmt", $pos); | ||
| 259 | } | ||
| 260 | Image::ExifTool::HexDump($dataPt, $pos - $dumpStart, | ||
| 261 | Start => $dumpStart, | ||
| 262 | DataPos => $$et{cbor_datapos}, | ||
| 263 | Prefix => $$et{INDENT} . ' ', | ||
| 264 | MaxLen => $verbose < 5 ? ($verbose == 3 ? 96 : 2048) : undef, | ||
| 265 | Out => $et->Options('TextOut'), | ||
| 266 | ) if $verbose > 2; | ||
| 267 | return($val, $err, $pos); | ||
| 268 | } | ||
| 269 | |||
| 270 | #------------------------------------------------------------------------------ | ||
| 271 | # Read CBOR box | ||
| 272 | # Inputs: 0) ExifTool ref, 1) dirInfo ref, 2) tag table ref | ||
| 273 | # Returns: 1 on success | ||
| 274 | sub ProcessCBOR($$$) | ||
| 275 | { | ||
| 276 | my ($et, $dirInfo, $tagTablePtr) = @_; | ||
| 277 | my $dataPt = $$dirInfo{DataPt}; | ||
| 278 | my $pos = $$dirInfo{DirStart}; | ||
| 279 | my $end = $pos + $$dirInfo{DirLen}; | ||
| 280 | my ($val, $err, $tag, $i); | ||
| 281 | |||
| 282 | $et->VerboseDir('CBOR', undef, $$dirInfo{DirLen}); | ||
| 283 | SetByteOrder('MM'); | ||
| 284 | |||
| 285 | $$et{cbor_datapos} = $$dirInfo{DataPos} + $$dirInfo{Base}; | ||
| 286 | |||
| 287 | while ($pos < $end) { | ||
| 288 | ($val, $err, $pos) = ReadCBORValue($et, $dataPt, $pos, $end); | ||
| 289 | $err and $et->Warn($err), last; | ||
| 290 | if (ref $val eq 'HASH') { | ||
| 291 | foreach $tag (@{$$val{_ordered_keys_}}) { | ||
| 292 | Image::ExifTool::JSON::ProcessTag($et, $tagTablePtr, $tag, $$val{$tag}); | ||
| 293 | } | ||
| 294 | } elsif (ref $val eq 'ARRAY') { | ||
| 295 | for ($i=0; $i<@$val; ++$i) { | ||
| 296 | Image::ExifTool::JSON::ProcessTag($et, $tagTablePtr, "Item$i", $$val[$i]); | ||
| 297 | } | ||
| 298 | } elsif ($val eq '0') { | ||
| 299 | $et->VPrint(1, "$$et{INDENT} <CBOR end>\n"); | ||
| 300 | last; # (treat as padding) | ||
| 301 | } else { | ||
| 302 | $et->VPrint(1, "$$et{INDENT} Unknown value: $val\n"); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | return 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | 1; # end | ||
| 309 | |||
| 310 | __END__ |