Palm.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #------------------------------------------------------------------------------
  2. # File: Palm.pm
  3. #
  4. # Description: Read Palm Database files
  5. #
  6. # Revisions: 2014/05/28 - P. Harvey Created
  7. #
  8. # References: 1) http://wiki.mobileread.com/wiki/PDB
  9. # 2) http://wiki.mobileread.com/wiki/MOBI
  10. #------------------------------------------------------------------------------
  11. package Image::ExifTool::Palm;
  12. use strict;
  13. use vars qw($VERSION);
  14. use Image::ExifTool qw(:DataAccess :Utils);
  15. $VERSION = '1.00';
  16. sub ProcessEXTH($$$);
  17. # type/creator ID's for Palm database files
  18. my %palmTypes = (
  19. '.pdfADBE' => 'Adobe Reader',
  20. 'TEXtREAd' => 'PalmDOC',
  21. 'BVokBDIC' => 'BDicty',
  22. 'DB99DBOS' => 'DB (Database program)',
  23. 'PNRdPPrs' => 'eReader',
  24. 'DataPPrs' => 'eReader',
  25. 'vIMGView' => 'FireViewer (ImageViewer)',
  26. 'PmDBPmDB' => 'HanDBase',
  27. 'InfoINDB' => 'InfoView',
  28. 'ToGoToGo' => 'iSilo',
  29. 'SDocSilX' => 'iSilo 3',
  30. 'JbDbJBas' => 'JFile',
  31. 'JfDbJFil' => 'JFile Pro',
  32. 'DATALSdb' => 'LIST',
  33. 'Mdb1Mdb1' => 'MobileDB',
  34. 'BOOKMOBI' => 'Mobipocket',
  35. 'DataPlkr' => 'Plucker',
  36. 'DataSprd' => 'QuickSheet',
  37. 'SM01SMem' => 'SuperMemo',
  38. 'TEXtTlDc' => 'TealDoc',
  39. 'InfoTlIf' => 'TealInfo',
  40. 'DataTlMl' => 'TealMeal',
  41. 'DataTlPt' => 'TealPaint',
  42. 'dataTDBP' => 'ThinkDB',
  43. 'TdatTide' => 'Tides',
  44. 'ToRaTRPW' => 'TomeRaider',
  45. 'zTXTGPlm' => 'Weasel',
  46. 'BDOCWrdS' => 'WordSmith',
  47. );
  48. my %dateTimeInfo = (
  49. # like QuickTime, the time zero should be Jan 1, 1904, but not all software writes this,
  50. # so assume a time zero of Jan 1, 1970 if the date is before this
  51. RawConv => q{
  52. my $offset = (66 * 365 + 17) * 24 * 3600;
  53. return $val - $offset if $val >= $offset;
  54. return $val;
  55. },
  56. ValueConv => 'ConvertUnixTime($val, 1)', # (UTC written by "EPUB Converter", ref PH)
  57. PrintConv => '$self->ConvertDateTime($val)',
  58. );
  59. # Palm Database header information
  60. %Image::ExifTool::Palm::Main = (
  61. GROUPS => { 0 => 'Palm', 1 => 'Palm', 2 => 'Document' },
  62. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  63. FORMAT => 'int32u',
  64. NOTES => q{
  65. Information extracted from Palm database files (PDB and PRC extensions),
  66. Mobipocket electronic books (MOBI), and Amazon Kindle KF7 and KF8 books (AZW
  67. and AZW3).
  68. },
  69. 0 => { Name => 'DatabaseName', Format => 'string[32]' },
  70. # 8 - int16u: file attributes (not very useful)
  71. # 8.5 - int16u: version
  72. 9 => {
  73. Name => 'CreateDate',
  74. Groups => { 2 => 'Time' },
  75. %dateTimeInfo,
  76. },
  77. 10 => {
  78. Name => 'ModifyDate',
  79. Groups => { 2 => 'Time' },
  80. %dateTimeInfo,
  81. },
  82. 11 => {
  83. Name => 'LastBackupDate',
  84. Groups => { 2 => 'Time' },
  85. %dateTimeInfo,
  86. },
  87. 12 => 'ModificationNumber',
  88. 15 => {
  89. Name => 'PalmFileType',
  90. Format => 'undef[8]',
  91. PrintConv => \%palmTypes,
  92. },
  93. );
  94. # MOBI header tags
  95. %Image::ExifTool::Palm::MOBI = (
  96. GROUPS => { 0 => 'Palm', 1 => 'MOBI', 2 => 'Document' },
  97. NOTES => q{
  98. Information extracted from the MOBI header of Mobipocket and Amazon Kindle
  99. KF7 and KF8 files.
  100. },
  101. PROCESS_PROC => \&Image::ExifTool::ProcessBinaryData,
  102. FORMAT => 'int32u',
  103. 0 => {
  104. Name => 'Compression',
  105. Format => 'int16u',
  106. PrintConv => {
  107. 1 => 'None',
  108. 2 => 'PalmDOC',
  109. 17480 => 'HUFF/CDIC',
  110. },
  111. },
  112. 1 => {
  113. Name => 'UncompressedTextLength',
  114. PrintConv => \&Image::ExifTool::ConvertFileSize,
  115. },
  116. 3 => {
  117. Name => 'Encryption',
  118. PrintConv => {
  119. 0 => 'None',
  120. 1 => 'Old Mobipocket',
  121. 2 => 'Mobipocket',
  122. },
  123. },
  124. 6 => {
  125. Name => 'MobiType',
  126. PrintConv => {
  127. 2 => 'Mobipocket Book',
  128. 3 => 'PalmDoc Book',
  129. 4 => 'Audio',
  130. 232 => 'mobipocket? generated by kindlegen1.2',
  131. 248 => 'KF8: generated by kindlegen2',
  132. 257 => 'News',
  133. 258 => 'News_Feed',
  134. 259 => 'News_Magazine',
  135. 513 => 'PICS',
  136. 514 => 'WORD',
  137. 515 => 'XLS',
  138. 516 => 'PPT',
  139. 517 => 'TEXT',
  140. 518 => 'HTML',
  141. },
  142. },
  143. 7 => {
  144. Name => 'CodePage',
  145. RawConv => '$$self{CodePage} = $val',
  146. PrintConv => {
  147. # just define commonly used code pages
  148. # (a much more complete list may be found in FlashPix.pm)
  149. 1252 => 'Windows Latin 1 (Western European)',
  150. 65001 => 'Unicode (UTF-8)',
  151. },
  152. },
  153. 9 => 'MobiVersion',
  154. 21 => 'BookName', # this is actually an offset, but replace it with the string later
  155. 26 => 'MinimumVersion',
  156. );
  157. # MOBI extended header tags
  158. %Image::ExifTool::Palm::EXTH = (
  159. GROUPS => { 0 => 'Palm', 1 => 'MOBI', 2 => 'Document' },
  160. FORMAT => 'string',
  161. NOTES => 'Information extracted from the MOBI extended header.',
  162. PROCESS_PROC => \&ProcessEXTH,
  163. 1 => 'DRMServerID',
  164. 2 => 'DRMCommerceID',
  165. 3 => 'DRM_E-BookBaseID',
  166. 100 => { Name => 'Author', Groups => { 2 => 'Author' } },
  167. 101 => 'Publisher',
  168. 102 => 'Imprint',
  169. 103 => 'Description',
  170. 104 => 'ISBN',
  171. 105 => { Name => 'Subject', List => 1 },
  172. 106 => {
  173. Name => 'PublishDate',
  174. Groups => { 2 => 'Time' },
  175. ValueConv => q{
  176. require Image::ExifTool::XMP;
  177. Image::ExifTool::XMP::ConvertXMPDate($val, 1);
  178. },
  179. PrintConv => '$self->ConvertDateTime($val)',
  180. },
  181. 107 => 'Review',
  182. 108 => 'Contributor',
  183. 109 => { Name => 'Rights', Groups => { 2 => 'Author' } },
  184. 110 => 'SubjectCode',
  185. 111 => 'BookType',
  186. 112 => 'Source',
  187. 113 => 'ASIN',
  188. 114 => 'BookVersion',
  189. 115 => { Name => 'SampleFlag', Format => 'int32u' },
  190. 116 => { Name => 'StartReading', Format => 'int32u' },
  191. 117 => 'Adult',
  192. 118 => 'RetailPrice',
  193. 119 => 'RetailPriceCurrency',
  194. # 121 => 'KF8BoundaryOffset',
  195. 125 => { Name => 'ResourceCount', Format => 'int32u' },
  196. 129 => 'KF8CoverURI',
  197. 200 => 'DictionaryShortName',
  198. # 201 => { Name => 'CoverOffset', Format => 'int32u' },
  199. # 202 => { Name => 'ThumbOffset', Format => 'int32u' },
  200. # 203 => 'HasFakeCover',
  201. 204 => {
  202. Name => 'CreatorSoftware',
  203. Format => 'int32u',
  204. PrintConv => {
  205. 1 => 'Mobigen',
  206. 2 => 'Mobipocket',
  207. 200 => 'Kindlegen (Windows)',
  208. 201 => 'Kindlegen (Linux)',
  209. 202 => 'Kindlegen (Mac)',
  210. },
  211. },
  212. 205 => { Name => 'CreatorMajorVersion', Format => 'int32u' },
  213. 206 => { Name => 'CreatorMinorVersion', Format => 'int32u' },
  214. 207 => { Name => 'CreatorBuildNumber', Format => 'int32u' },
  215. 208 => 'Watermark',
  216. 209 => 'Tamper-proofKeys',
  217. # 300 => 'FontSignature',
  218. 401 => { Name => 'ClippingLimit', Format => 'int8u' },
  219. 402 => 'PublisherLimit',
  220. 404 => {
  221. Name => 'TextToSpeech',
  222. Format => 'int8u',
  223. PrintConv => { 0 => 'Enabled', 1 => 'Disabled' },
  224. },
  225. 405 => { Name => 'RentalFlag', Format => 'int8u' }, #?
  226. 406 => 'RentalExpirationDate',
  227. 501 => { Name => 'CDEType', Format => 'int32u' },
  228. 502 => 'LastUpdateTime',
  229. 503 => 'UpdatedTitle',
  230. 504 => 'ASIN2',
  231. 524 => 'Language',
  232. 525 => 'Alignment',
  233. 535 => 'CreatorBuildNumber2',
  234. );
  235. #------------------------------------------------------------------------------
  236. # Process the MOBI extended header
  237. # Inputs: 0) ExifTool ref, 1) dirInfo ref, 2) tag table ref
  238. # Returns: 1 (EXTH should have already been validated)
  239. sub ProcessEXTH($$$)
  240. {
  241. my ($et, $dirInfo, $tagTablePtr) = @_;
  242. my $dataPt = $$dirInfo{DataPt};
  243. my $dataPos = $$dirInfo{DataPos};
  244. my $enc = $$dirInfo{Encoding} || 'UTF8';
  245. my $dirLen = length $$dataPt;
  246. my ($index, $pos);
  247. $et->VerboseDir('EXTH', $$dirInfo{NumEntries}, $dirLen);
  248. # process the EXTH entries
  249. for ($index=0, $pos=0; ; ++$index) {
  250. last if $pos + 8 > $dirLen;
  251. my $tag = Get32u($dataPt, $pos);
  252. my $len = Get32u($dataPt, $pos + 4);
  253. last if $len < 8 or $pos + $len > $dirLen;
  254. my $key = $et->HandleTag($tagTablePtr, $tag, undef,
  255. DataPt => $dataPt,
  256. DataPos => $dataPos,
  257. Start => $pos + 8,
  258. Size => $len - 8,
  259. Index => $index,
  260. );
  261. # recode text if necessary
  262. $$et{VALUE}{$key} = $et->Decode($$et{VALUE}{$key}, $enc) if $key;
  263. $pos += $len;
  264. }
  265. return 1;
  266. }
  267. #------------------------------------------------------------------------------
  268. # Extract information from a Palm DB file
  269. # Inputs: 0) ExifTool ref, 1) dirInfo reference
  270. # Returns: 1 if this was a recognized PDB file, 0 otherwise
  271. sub ProcessPDB($$)
  272. {
  273. my ($et, $dirInfo) = @_;
  274. my $raf = $$dirInfo{RAF};
  275. my ($buff, $buf2, $size, $enc);
  276. my $verbose = $et->Options('Verbose');
  277. # verify this is a valid Palm DB file
  278. return 0 unless $raf->Read($buff, 86) == 86;
  279. my $type = $palmTypes{substr($buff, 60, 8)};
  280. return 0 unless $type;
  281. #
  282. # Read and process the Palm DB file header
  283. #
  284. $et->SetFileType($type eq 'Mobipocket' ? 'MOBI' : 'PDB');
  285. SetByteOrder('MM');
  286. my $tagTablePtr = GetTagTable('Image::ExifTool::Palm::Main');
  287. $et->ProcessDirectory({ DataPt => \$buff }, $tagTablePtr);
  288. return 1 unless $type eq 'Mobipocket' and Get16u(\$buff, 76);
  289. #
  290. # Read and process MOBI header (should be the first record)
  291. #
  292. my $offset = Get32u(\$buff, 78); # get offset to first record
  293. unless ($raf->Seek($offset, 0) and $raf->Read($buff, 274) == 274) {
  294. $et->Warn('Truncated MOBI header');
  295. return 1;
  296. }
  297. unless (substr($buff, 16, 4) eq 'MOBI') {
  298. $et->Warn('Invalid MOBI header');
  299. return 1;
  300. }
  301. $tagTablePtr = GetTagTable('Image::ExifTool::Palm::MOBI');
  302. $et->ProcessDirectory({ DataPt => \$buff }, $tagTablePtr);
  303. # get text encoding
  304. $enc = $Image::ExifTool::charsetName{"cp$$et{CodePage}"} if $$et{CodePage};
  305. $enc = 'UTF8' unless $enc;
  306. # extract the BookName string
  307. my $off = Get32u(\$buff, 84);
  308. my $len = Get32u(\$buff, 88);
  309. $raf->Seek($offset+$off, 0) and $raf->Read($buf2, $len) == $len or $buf2 = '<err>';
  310. $$et{VALUE}{BookName} = $et->Decode($buf2, $enc);
  311. #
  312. # Process the MOBI extended header if it exists
  313. #
  314. # first, check the flag bit to see if the EXTH record should exist
  315. my $flag = Get32u(\$buff, 128);
  316. return 1 unless $flag & 0x40; # check extended header flag
  317. $len = Get32u(\$buff, 20) + 16; # MOBI header length (including PalmDOC header)
  318. unless ($raf->Seek($offset+$len, 0) and $raf->Read($buf2, 12) == 12 and
  319. substr($buf2,0,4) eq 'EXTH' and ($size = Get32u(\$buf2, 4)) > 12)
  320. {
  321. $et->Warn('Invalid MOBI extended header');
  322. return 1;
  323. }
  324. # read and process the MOBI extended header
  325. $size -= 12;
  326. $raf->Read($buff, $size) == $size or $et->Warn('Truncated MOBI extended header'), return 1;
  327. my %dirInfo = (
  328. DataPt => \$buff,
  329. DataPos => $offset + $len + 12,
  330. NumEntries => Get32u(\$buf2, 8),
  331. Encoding => $enc,
  332. );
  333. $tagTablePtr = GetTagTable('Image::ExifTool::Palm::EXTH');
  334. $et->ProcessDirectory(\%dirInfo, $tagTablePtr);
  335. return 1;
  336. }
  337. 1; # end
  338. __END__
  339. =head1 NAME
  340. Image::ExifTool::Palm - Read Palm Database files
  341. =head1 SYNOPSIS
  342. This module is used by Image::ExifTool
  343. =head1 DESCRIPTION
  344. This module contains code to extract metadata from Palm database files (PDB
  345. and PRC extensions), Mobipocket electronic books (MOBI), and Amazon Kindle
  346. KF7 and KF8 books (AZW and AZW3).
  347. =head1 AUTHOR
  348. Copyright 2003-2016, Phil Harvey (phil at owl.phy.queensu.ca)
  349. This library is free software; you can redistribute it and/or modify it
  350. under the same terms as Perl itself.
  351. =head1 REFERENCES
  352. =over 4
  353. =item L<http://wiki.mobileread.com/wiki/PDB>
  354. =item L<http://wiki.mobileread.com/wiki/MOBI>
  355. =back
  356. =head1 SEE ALSO
  357. L<Image::ExifTool::TagNames/Palm Tags>,
  358. L<Image::ExifTool(3pm)|Image::ExifTool>
  359. =cut