| 1 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # File: RTF.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Description: Read Rich Text Format meta information | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Revisions: 2010/06/17 - P. Harvey Created | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # References: 1) http://download.microsoft.com/download/2/f/5/2f599e18-07ee-4ec5-a1e7-f4e6a9423592/Word2007RTFSpec9.doc | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # 2) http://search.cpan.org/dist/RTF-Writer/lib/RTF/Cookbook.pod | ||||||||||||||||||||||||||||||||||||||||||
| 10 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | package Image::ExifTool::RTF; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use Image::ExifTool qw(:DataAccess :Utils); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | $VERSION = '1.04'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | sub ProcessUserProps($$$); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # supported RTF character entities | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | my %rtfEntity = ( | |||||||||||||||||||||||||||||||||||||||||
| 24 | par => 0x0a, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | tab => 0x09, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | endash => 0x2013, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | emdash => 0x2014, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | lquote => 0x2018, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | rquote => 0x2019, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | ldblquote => 0x201c, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | rdblquote => 0x201d, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | bullet => 0x2022, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # RTF tags (ref 1) | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | %Image::ExifTool::RTF::Main = ( | |||||||||||||||||||||||||||||||||||||||||
| 37 | GROUPS => { 2 => 'Document' }, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | NOTES => q{ | ||||||||||||||||||||||||||||||||||||||||||
| 39 | This table lists standard tags of the RTF information group, but ExifTool | ||||||||||||||||||||||||||||||||||||||||||
| 40 | will also extract any non-standard tags found in this group. As well, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ExifTool will extract any custom properties that are found. See | ||||||||||||||||||||||||||||||||||||||||||
| 42 | L<http://www.microsoft.com/en-ca/download/details.aspx?id=10725> for the | ||||||||||||||||||||||||||||||||||||||||||
| 43 | specification. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | title => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | subject => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | author => { Groups => { 2 => 'Author' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 48 | manager => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | company => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | copyright=> { Groups => { 2 => 'Author' } }, # (written by Apple TextEdit) | ||||||||||||||||||||||||||||||||||||||||||
| 51 | operator => { Name => 'LastModifiedBy' }, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | category => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | keywords => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 54 | comment => { }, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | doccomm => { Name => 'Comments' }, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | hlinkbase=> { Name => 'HyperlinkBase' }, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | creatim => { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | Name => 'CreateDate', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | Format => 'date', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | Groups => { 2 => 'Time' }, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | PrintConv => '$self->ConvertDateTime($val)', | ||||||||||||||||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | revtim => { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | Name => 'ModifyDate', | ||||||||||||||||||||||||||||||||||||||||||
| 65 | Format => 'date', | ||||||||||||||||||||||||||||||||||||||||||
| 66 | Groups => { 2 => 'Time' }, | ||||||||||||||||||||||||||||||||||||||||||
| 67 | PrintConv => '$self->ConvertDateTime($val)', | ||||||||||||||||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 69 | printim => { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | Name => 'LastPrinted', | ||||||||||||||||||||||||||||||||||||||||||
| 71 | Format => 'date', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | Groups => { 2 => 'Time' }, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | PrintConv => '$self->ConvertDateTime($val)', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | buptim => { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | Name => 'BackupTime', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | Format => 'date', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | Groups => { 2 => 'Time' }, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | PrintConv => '$self->ConvertDateTime($val)', | ||||||||||||||||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | edmins => { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | Name => 'TotalEditTime', # in minutes | ||||||||||||||||||||||||||||||||||||||||||
| 83 | PrintConv => 'ConvertTimeSpan($val, 60)', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | nofpages => { Name => 'Pages' }, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | nofwords => { Name => 'Words' }, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | nofchars => { Name => 'Characters' }, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | nofcharsws=>{ | ||||||||||||||||||||||||||||||||||||||||||
| 89 | Name => 'CharactersWithSpaces', | ||||||||||||||||||||||||||||||||||||||||||
| 90 | Notes => q{ | ||||||||||||||||||||||||||||||||||||||||||
| 91 | according to the 2007 Microsoft RTF specification this is clearly the number | ||||||||||||||||||||||||||||||||||||||||||
| 92 | of characters NOT including spaces, but Microsoft Word writes this as the | ||||||||||||||||||||||||||||||||||||||||||
| 93 | number WITH spaces, so ExifTool names this tag according to the de facto | ||||||||||||||||||||||||||||||||||||||||||
| 94 | standard | ||||||||||||||||||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | id => { Name => 'InternalIDNumber' }, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | version => { Name => 'RevisionNumber' }, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | vern => { Name => 'InternalVersionNumber' }, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | # lookup for user-defined properties | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # (none are pre-defined and this table doesn't appear in the docs) | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | %Image::ExifTool::RTF::UserProps = ( | |||||||||||||||||||||||||||||||||||||||||
| 105 | GROUPS => { 2 => 'Document' }, | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # Read to nested closing curly bracket "}" | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # Inputs: 0) data ref, 1) optional RAF ref to read more data if available | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # Returns: text inside brackets, or undef on error | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # Notes: On entry the current position in the data must be set to immediately | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # after the command that opens the bracket. On return the current | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # position is immediately following the closing brace if the return | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # value is defined. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | sub ReadToNested($;$) | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ReadToNested: called CC=10 SCAR=31.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 14 | my ($dataPt, $raf) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 14 | my $pos = pos $$dataPt; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 14 | my $level = 1; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 14 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | # look for the next bracket | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 46 | ▶ | unless ($$dataPt =~ /(\\*)([{}])/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | # must read some more data | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my $p = length $$dataPt; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $buff; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | last unless $raf and $raf->Read($buff, 65536); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$raf and $raf->Read($buff, 65536)
MC/DC0%0/2
$raf and $raf->Read($buff, 65536)
$raf
$raf->Read($buff, 65536)
Truth table0%0/3
A and B
A= $rafB= $raf->Read($buff, 65536)
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $$dataPt .= $buff; | |||||||||||||||||||||||||||||||||||||||||
| 129 | # rewind position to include any leading backslashes | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | --$p while $p and substr($$dataPt, $p - 1, 1) eq '\\'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$p and substr($$dataPt, $p - 1, 1) eq "\\"
MC/DC0%0/2
$p and substr($$dataPt, $p - 1, 1) eq "\\"
$p
substr($$dataPt, $p - 1, 1) eq "\\"
Truth table0%0/3
A and B
A= $pB= substr($$dataPt, $p - 1, 1) eq "\\"
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | pos($$dataPt) = $p; # set position to continue search | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # bracket is escaped if preceded by an odd number of backslashes | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 46 | ▶ | next if $1 and length($1) & 0x01; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$1 and length $1 & 1
MC/DC100%2/2
$1 and length $1 & 1
$1
length $1 & 1
Truth table100%3/3
A and B
A= $1B= length $1 & 1
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 42 | ▶ | $2 eq '{' and ++$level, next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 28 | ▶ | next unless --$level <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 14 | return substr($$dataPt, $pos, pos($$dataPt) - $pos - 1); | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # Unescape RTF escape sequences | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # Inputs: 0) ExifTool ref, 1) RTF text, 2) RTF character set (for hex characters) | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # Returns: Unescaped text (in current ExifTool Charset) | ||||||||||||||||||||||||||||||||||||||||||
| 147 | sub UnescapeRTF($$$) | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
UnescapeRTF: called CC=17 SCAR=28.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 11 | my ($et, $val, $charset) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | # return now unless we have a control sequence | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 11 | ▶ | unless ($val =~ /\\/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 5 | $val =~ tr/\n\r//d; # ignore CR's and LF's | |||||||||||||||||||||||||||||||||||||||||
| 154 | 5 | return $val; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | # CR/LF is significant if it terminates a control sequence (so change these to a space) | ||||||||||||||||||||||||||||||||||||||||||
| 157 | # (was $val =~ s/(^|[^\\])((?:\\\\)*)(\\[a-zA-Z]+(?:-?\d+)?)[\n\r]/$1$2$3 /g;) | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 6 | ▶ | $val =~ s/\\(?:([a-zA-Z]+(?:-?\d+)?)[\n\r]|(.))/'\\'.($1 ? "$1 " : $2)/sge; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | # protect the newline control sequence by converting to a \par command | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # (was $val =~ s/(^|[^\\])((?:\\\\)*)(\\[\n\r])/$1$2\\par /g;) | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | ▶ | $val =~ s/(\\[\n\r])|(\\.)/$2 || '\\par '/sge; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$2 || '\\par '
MC/DC100%1/1
$2 || '\\par '
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | # all other CR/LF's are ignored (so delete them) | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 6 | $val =~ tr/\n\r//d; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | my $rtnVal = ''; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | my $len = length $val; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | my $skip = 1; # default Unicode skip count | |||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | my $p0 = 0; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 171 | # find next backslash | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 40 | ▶ | my $p1 = ($val =~ /\\/g) ? pos($val) : $len + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | # add text up to start of this control sequence (or up to end) | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 40 | my $n = $p1 - $p0 - 1; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 40 | ▶ | $rtnVal .= substr($val, $p0, $n) if $n > 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | # all done if at the end or if control sequence is empty | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 40 | ▶ | last if $p1 >= $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | # look for an ASCII-letter control word or Unicode control | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 34 | ▶ | if ($val =~ /\G([a-zA-Z]+)(-?\d+)? ?/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 180 | # interpret command if recognized | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 25 | ▶ | if ($1 eq 'uc') { # \ucN | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 2 | $skip = $2; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } elsif ($1 eq 'u') { # \uN | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 20 | ▶ | if ($2 < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | $et->Warn('Invalid Unicode character(s) in text'); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | $rtnVal .= '?'; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 20 | require Image::ExifTool::Charset; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 20 | $rtnVal .= Image::ExifTool::Charset::Recompose($et, [$2]); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 20 | ▶ | if ($skip) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | # must skip the specified number of characters | ||||||||||||||||||||||||||||||||||||||||||
| 192 | # (not simple because RTF control words count as a single character) | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | ▶ | last unless $val =~ /\G([^\\]|\\([a-zA-Z]+)(-?\d+)? ?|\\'.{2}|\\.){$skip}/g; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | } elsif ($rtfEntity{$1}) { | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | require Image::ExifTool::Charset; | |||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | $rtnVal .= Image::ExifTool::Charset::Recompose($et, [$rtfEntity{$1}]); | |||||||||||||||||||||||||||||||||||||||||
| 199 | } # (else ignore the command) | ||||||||||||||||||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 9 | my $ch = substr($val, $p1, 1); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 9 | ▶ | if ($ch eq "'") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | # hex character code | ||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | ▶ | last if $p1 + 3 > $len; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | my $hex = substr($val, $p1 + 1, 2); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | ▶ | if ($hex =~ /^[0-9a-fA-F]{2}$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | require Image::ExifTool::Charset; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | $rtnVal .= $et->Decode(chr(hex($hex)), $charset); | |||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | pos($val) = $p1 + 3; # skip to after the hex code | |||||||||||||||||||||||||||||||||||||||||
| 211 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 212 | # assume a standard control symbol (\, {, }, etc) | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # (note, this may not be valid for some uncommon | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # control symbols like \~ for non-breaking space) | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 4 | $rtnVal .= $ch; | |||||||||||||||||||||||||||||||||||||||||
| 216 | 4 | pos($val) = $p1 + 1; # skip to after this character | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | 34 | $p0 = pos($val); | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 6 | return $rtnVal; | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 225 | # Read information in a RTF document | ||||||||||||||||||||||||||||||||||||||||||
| 226 | # Inputs: 0) ExifTool ref, 1) dirInfo ref | ||||||||||||||||||||||||||||||||||||||||||
| 227 | # Returns: 1 on success, 0 if this wasn't a valid RTF file | ||||||||||||||||||||||||||||||||||||||||||
| 228 | sub ProcessRTF($$) | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ProcessRTF: called CC=33 SCAR=40.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | my ($et, $dirInfo) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | my $raf = $$dirInfo{RAF}; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | my ($buff, $buf2, $cs); | |||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | ▶ | return 0 unless $raf->Read($buff, 64) and $raf->Seek(0,0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$raf->Read($buff, 64) and $raf->Seek(0, 0)
MC/DC0%0/2
$raf->Read($buff, 64) and $raf->Seek(0, 0)
$raf->Read($buff, 64)
$raf->Seek(0, 0)
Truth table33%1/3
A and B
A= $raf->Read($buff, 64)B= $raf->Seek(0, 0)
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | ▶ | return 0 unless $buff =~ /^[\n\r]*\{[\n\r]*\\rtf[^a-zA-Z]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | $et->SetFileType(); | |||||||||||||||||||||||||||||||||||||||||
| 237 | # | ||||||||||||||||||||||||||||||||||||||||||
| 238 | # determine the RTF character set | ||||||||||||||||||||||||||||||||||||||||||
| 239 | # | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | if ($buff=~ /\\ansicpg(\d*)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | $cs = "cp$1"; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } elsif ($buff=~ /\\(ansi|mac|pc|pca)[^a-zA-Z]/) { | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my %trans = ( | |||||||||||||||||||||||||||||||||||||||||
| 244 | ansi => 'Latin', | ||||||||||||||||||||||||||||||||||||||||||
| 245 | mac => 'MacRoman', | ||||||||||||||||||||||||||||||||||||||||||
| 246 | pc => 'cp437', | ||||||||||||||||||||||||||||||||||||||||||
| 247 | pca => 'cp850', | ||||||||||||||||||||||||||||||||||||||||||
| 248 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $cs = $trans{$1}; | |||||||||||||||||||||||||||||||||||||||||
| 250 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | $et->Warn('Unspecified RTF encoding. Will assume Latin'); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $cs = 'Latin'; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | my $charset = $Image::ExifTool::charsetName{lc $cs}; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | ▶ | unless ($charset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $et->Warn("Unsupported RTF encoding $cs. Will assume Latin."); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | $charset = 'Latin'; | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | 1 | my $tagTablePtr = GetTagTable('Image::ExifTool::RTF::Main'); | |||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | undef $buff; | |||||||||||||||||||||||||||||||||||||||||
| 261 | # | ||||||||||||||||||||||||||||||||||||||||||
| 262 | # scan for \info group | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | $raf->Read($buf2, 65536) or last; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | if (defined $buff) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | # read more but leave some overlap for the match | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | $buff = substr($buff, -16) . $buf2; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | $buff = $buf2; | |||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | ▶ | next unless $buff =~ /[^\\]\{[\n\r]*\\info([^a-zA-Z])/g; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | # anything but a space is included in the contents | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | ▶ | pos($buff) = pos($buff) - 1 if $1 ne ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | my $info = ReadToNested(\$buff, $raf); | |||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | unless (defined $info) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $et->Warn('Unterminated information group'); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # process info commands (eg. "\author", "\*\copyright"); | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | ▶ | while ($info =~ /\{[\n\r]*(\\\*[\n\r]*)?\\([a-zA-Z]+)([^a-zA-Z])/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | ▶ | pos($info) = pos($info) - 1 if $3 ne ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 8 | my $tag = $2; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 8 | my $val = ReadToNested(\$info); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 8 | ▶ | last unless defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 8 | my $tagInfo = $$tagTablePtr{$tag}; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 8 | ▶ | if ($tagInfo and $$tagInfo{Format} and $$tagInfo{Format} eq 'date') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$tagInfo and $tagInfo->{"Format"} and $tagInfo->{"Format"} eq "date"
$tagInfo and $tagInfo->{"Format"}
MC/DC33%1/3
$tagInfo and $tagInfo->{"Format"} and $tagInfo->{"Format"} eq "date"
$tagInfo
$tagInfo->{"Format"}
$tagInfo->{"Format"} eq "date"
Truth table50%2/4
A and B and C
A= $tagInfoB= $tagInfo->{"Format"}C= $tagInfo->{"Format"} eq "date"
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | # parse RTF date commands | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my %idx = (yr=>0,mo=>1,dy=>2,hr=>3,min=>4,sec=>5); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | my @t = (0) x 6; | |||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | ▶ | while ($val =~ /\\([a-z]+)(\d+)/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 292 | 4 | ▶ | next unless defined $idx{$1}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 4 | $t[$idx{$1}] = $2; | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | $val = sprintf("%.4d:%.2d:%.2d %.2d:%.2d:%.2d", @t); | |||||||||||||||||||||||||||||||||||||||||
| 296 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 297 | # unescape RTF string value | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 7 | $val = UnescapeRTF($et, $val, $charset); | |||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | # create tagInfo for unknown tags | ||||||||||||||||||||||||||||||||||||||||||
| 301 | 8 | ▶ | if (not $tagInfo) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | AddTagToTable($tagTablePtr, $tag, { Name => ucfirst($tag) }); | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 8 | $et->HandleTag($tagTablePtr, $tag, $val); | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | ▶ | return 1 unless defined $buff; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | # | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # scan for \userprops (but don't read more from file to find the start of this command) | ||||||||||||||||||||||||||||||||||||||||||
| 310 | # | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | pos($buff) = 0; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 1 | ▶ | while ($buff =~ /[^\\]\{[\n\r]*\\\*[\n\r]*\\userprops([^a-zA-Z])/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | # Note: The RTF spec places brackets around each propinfo structure, | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # but Microsoft Word doesn't write it this way, so tolerate either. | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 1 | ▶ | pos($buff) = pos($buff) - 1 if $1 ne ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | my $props = ReadToNested(\$buff, $raf); | |||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | $tagTablePtr = Image::ExifTool::GetTagTable('Image::ExifTool::RTF::UserProps'); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | ▶ | unless (defined $props) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $et->Warn('Unterminated user properties'); | |||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | # process user properties | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 1 | my $tag; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | ▶ | while ($props =~ /\{[\n\r]*(\\\*[\n\r]*)?\\([a-zA-Z]+)([^a-zA-Z])/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 4 | ▶ | pos($props) = pos($props) - 1 if $3 ne ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | my $t = $2; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 4 | my $val = ReadToNested(\$props); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 4 | ▶ | last unless defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 4 | $val = UnescapeRTF($et, $val, $charset); | |||||||||||||||||||||||||||||||||||||||||
| 330 | 4 | ▶ | if ($t eq 'propname') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 2 | $tag = $val; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 333 | ▶ | } elsif ($t ne 'staticval' or not defined $tag) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$t ne "staticval" or not defined $tag
MC/DC0%0/2
$t ne "staticval" or not defined $tag
$t ne "staticval"
not defined $tag
Truth table33%1/3
A or not(B)
A= $t ne "staticval"B= not defined $tag
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | next; # ignore \linkval and \proptype for now | |||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | 2 | $tag =~ s/\s(.)/\U$1/g; # capitalize all words in tag name | |||||||||||||||||||||||||||||||||||||||||
| 337 | 2 | $tag =~ tr/-_a-zA-Z0-9//dc; # remove illegal characters | |||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | ▶ | next unless $tag; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | # create tagInfo for unknown tags | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 2 | ▶ | unless ($$tagTablePtr{$tag}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 2 | AddTagToTable($tagTablePtr, $tag, { Name => $tag }); | |||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 2 | $et->HandleTag($tagTablePtr, $tag, $val); | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | last; # (didn't really want to loop) | |||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 1 | 1; # end | |||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | __END__ | ||||||||||||||||||||||||||||||||||||||||||