summary.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * summary.c
  11. * Copyright (C) 2002-2005 A.J. van Os; Released under GNU GPL
  12. *
  13. * Description:
  14. * Read the summary information of a Word document
  15. */
  16. #include <time.h>
  17. #include <string.h>
  18. #include "antiword.h"
  19. #define P_HEADER_SZ 28
  20. #define P_SECTIONLIST_SZ 20
  21. #define P_LENGTH_SZ 4
  22. #define P_SECTION_MAX_SZ (2 * P_SECTIONLIST_SZ + P_LENGTH_SZ)
  23. #define P_SECTION_SZ(x) ((x) * P_SECTIONLIST_SZ + P_LENGTH_SZ)
  24. #define PID_TITLE 2
  25. #define PID_SUBJECT 3
  26. #define PID_AUTHOR 4
  27. #define PID_CREATE_DTM 12
  28. #define PID_LASTSAVE_DTM 13
  29. #define PID_APPNAME 18
  30. #define PIDD_MANAGER 14
  31. #define PIDD_COMPANY 15
  32. #define VT_LPSTR 30
  33. #define VT_FILETIME 64
  34. #define TIME_OFFSET_HI 0x019db1de
  35. #define TIME_OFFSET_LO 0xd53e8000
  36. static char *szTitle = NULL;
  37. static char *szSubject = NULL;
  38. static char *szAuthor = NULL;
  39. static time_t tCreateDtm = (time_t)-1;
  40. static time_t tLastSaveDtm= (time_t)-1;
  41. static char *szAppName = NULL;
  42. static char *szManager = NULL;
  43. static char *szCompany = NULL;
  44. static USHORT usLid = (USHORT)-1;
  45. /*
  46. * vDestroySummaryInfo - destroy the summary information
  47. */
  48. void
  49. vDestroySummaryInfo(void)
  50. {
  51. TRACE_MSG("vDestroySummaryInfo");
  52. szTitle = xfree(szTitle);
  53. szSubject = xfree(szSubject);
  54. szAuthor = xfree(szAuthor);
  55. tCreateDtm = (time_t)-1;
  56. tLastSaveDtm = (time_t)-1;
  57. szAppName = xfree(szAppName);
  58. szManager = xfree(szManager);
  59. szCompany = xfree(szCompany);
  60. usLid = (USHORT)-1;
  61. } /* end of vDestroySummaryInfo */
  62. /*
  63. * tConvertDosDate - convert DOS date format
  64. *
  65. * returns Unix time_t or -1
  66. */
  67. static time_t
  68. tConvertDosDate(const char *szDosDate)
  69. {
  70. struct tm tTime;
  71. const char *pcTmp;
  72. time_t tResult;
  73. memset(&tTime, 0, sizeof(tTime));
  74. pcTmp = szDosDate;
  75. /* Get the month */
  76. if (!isdigit(*pcTmp)) {
  77. return (time_t)-1;
  78. }
  79. tTime.tm_mon = (int)(*pcTmp - '0');
  80. pcTmp++;
  81. if (isdigit(*pcTmp)) {
  82. tTime.tm_mon *= 10;
  83. tTime.tm_mon += (int)(*pcTmp - '0');
  84. pcTmp++;
  85. }
  86. /* Get the first separater */
  87. if (isalnum(*pcTmp)) {
  88. return (time_t)-1;
  89. }
  90. pcTmp++;
  91. /* Get the day */
  92. if (!isdigit(*pcTmp)) {
  93. return (time_t)-1;
  94. }
  95. tTime.tm_mday = (int)(*pcTmp - '0');
  96. pcTmp++;
  97. if (isdigit(*pcTmp)) {
  98. tTime.tm_mday *= 10;
  99. tTime.tm_mday += (int)(*pcTmp - '0');
  100. pcTmp++;
  101. }
  102. /* Get the second separater */
  103. if (isalnum(*pcTmp)) {
  104. return (time_t)-1;
  105. }
  106. pcTmp++;
  107. /* Get the year */
  108. if (!isdigit(*pcTmp)) {
  109. return (time_t)-1;
  110. }
  111. tTime.tm_year = (int)(*pcTmp - '0');
  112. pcTmp++;
  113. if (isdigit(*pcTmp)) {
  114. tTime.tm_year *= 10;
  115. tTime.tm_year += (int)(*pcTmp - '0');
  116. pcTmp++;
  117. }
  118. /* Check the values */
  119. if (tTime.tm_mon == 0 || tTime.tm_mday == 0 || tTime.tm_mday > 31) {
  120. return (time_t)-1;
  121. }
  122. /* Correct the values */
  123. tTime.tm_mon--; /* From 01-12 to 00-11 */
  124. if (tTime.tm_year < 80) {
  125. tTime.tm_year += 100; /* 00 means 2000 is 100 */
  126. }
  127. tTime.tm_isdst = -1;
  128. tResult = mktime(&tTime);
  129. NO_DBG_MSG(ctime(&tResult));
  130. return tResult;
  131. } /* end of tConvertDosDate */
  132. /*
  133. * szLpstr - get a zero terminate string property
  134. */
  135. static char *
  136. szLpstr(ULONG ulOffset, const UCHAR *aucBuffer)
  137. {
  138. char *szStart, *szResult, *szTmp;
  139. size_t tSize;
  140. tSize = (size_t)ulGetLong(ulOffset + 4, aucBuffer);
  141. NO_DBG_DEC(tSize);
  142. if (tSize == 0) {
  143. return NULL;
  144. }
  145. /* Remove white space from the start of the string */
  146. szStart = (char *)aucBuffer + ulOffset + 8;
  147. NO_DBG_MSG(szStart);
  148. fail(strlen(szStart) >= tSize);
  149. while (isspace(*szStart)) {
  150. szStart++;
  151. }
  152. if (szStart[0] == '\0') {
  153. return NULL;
  154. }
  155. szResult = xstrdup(szStart);
  156. /* Remove white space from the end of the string */
  157. szTmp = szResult + strlen(szResult) - 1;
  158. while (isspace(*szTmp)) {
  159. *szTmp = '\0';
  160. szTmp--;
  161. }
  162. NO_DBG_MSG(szResult);
  163. return szResult;
  164. } /* end of szLpstr */
  165. /*
  166. * tFiletime - get a filetime property
  167. */
  168. static time_t
  169. tFiletime(ULONG ulOffset, const UCHAR *aucBuffer)
  170. {
  171. double dHi, dLo, dTmp;
  172. ULONG ulHi, ulLo;
  173. time_t tResult;
  174. ulLo = ulGetLong(ulOffset + 4, aucBuffer);
  175. ulHi = ulGetLong(ulOffset + 8, aucBuffer);
  176. NO_DBG_HEX(ulHi);
  177. NO_DBG_HEX(ulLo);
  178. /* Move the starting point from 01 Jan 1601 to 01 Jan 1970 */
  179. dHi = (double)ulHi - (double)TIME_OFFSET_HI;
  180. dLo = (double)ulLo - (double)TIME_OFFSET_LO;
  181. NO_DBG_FLT(dHi);
  182. NO_DBG_FLT(dLo);
  183. /* Combine the values and divide by 10^7 to get seconds */
  184. dTmp = dLo / 10000000.0; /* 10^7 */
  185. dTmp += dHi * 429.4967926; /* 2^32 / 10^7 */
  186. NO_DBG_FLT(dTmp);
  187. /* Make a time_t */
  188. if (dTmp - 0.5 < TIME_T_MIN || dTmp + 0.5 > TIME_T_MAX) {
  189. return (time_t)-1;
  190. }
  191. tResult = dTmp < 0.0 ? (time_t)(dTmp - 0.5) : (time_t)(dTmp + 0.5);
  192. NO_DBG_MSG(ctime(&tResult));
  193. return tResult;
  194. } /* end of tFiletime */
  195. /*
  196. * vAnalyseSummaryInfo - analyse the summary information
  197. */
  198. static void
  199. vAnalyseSummaryInfo(const UCHAR *aucBuffer)
  200. {
  201. ULONG ulOffset;
  202. size_t tIndex, tCount, tPropID, tPropType;
  203. tCount = (size_t)ulGetLong(4, aucBuffer);
  204. DBG_DEC(tCount);
  205. for (tIndex = 0; tIndex < tCount; tIndex++) {
  206. tPropID = (size_t)ulGetLong(8 + tIndex * 8, aucBuffer);
  207. ulOffset = ulGetLong(12 + tIndex * 8, aucBuffer);
  208. NO_DBG_DEC(tPropID);
  209. NO_DBG_HEX(ulOffset);
  210. tPropType = (size_t)ulGetLong(ulOffset, aucBuffer);
  211. NO_DBG_DEC(tPropType);
  212. switch (tPropID) {
  213. case PID_TITLE:
  214. if (tPropType == VT_LPSTR && szTitle == NULL) {
  215. szTitle = szLpstr(ulOffset, aucBuffer);
  216. }
  217. break;
  218. case PID_SUBJECT:
  219. if (tPropType == VT_LPSTR && szSubject == NULL) {
  220. szSubject = szLpstr(ulOffset, aucBuffer);
  221. }
  222. break;
  223. case PID_AUTHOR:
  224. if (tPropType == VT_LPSTR && szAuthor == NULL) {
  225. szAuthor = szLpstr(ulOffset, aucBuffer);
  226. }
  227. break;
  228. case PID_CREATE_DTM:
  229. if (tPropType == VT_FILETIME &&
  230. tCreateDtm == (time_t)-1) {
  231. tCreateDtm = tFiletime(ulOffset, aucBuffer);
  232. }
  233. break;
  234. case PID_LASTSAVE_DTM:
  235. if (tPropType == VT_FILETIME &&
  236. tLastSaveDtm == (time_t)-1) {
  237. tLastSaveDtm = tFiletime(ulOffset, aucBuffer);
  238. }
  239. break;
  240. case PID_APPNAME:
  241. if (tPropType == VT_LPSTR && szAppName == NULL) {
  242. szAppName = szLpstr(ulOffset, aucBuffer);
  243. }
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. } /* end of vAnalyseSummaryInfo */
  250. /*
  251. * vAnalyseDocumentSummaryInfo - analyse the document summary information
  252. */
  253. static void
  254. vAnalyseDocumentSummaryInfo(const UCHAR *aucBuffer)
  255. {
  256. ULONG ulOffset;
  257. size_t tIndex, tCount, tPropID, tPropType;
  258. tCount = (size_t)ulGetLong(4, aucBuffer);
  259. DBG_DEC(tCount);
  260. for (tIndex = 0; tIndex < tCount; tIndex++) {
  261. tPropID = (size_t)ulGetLong(8 + tIndex * 8, aucBuffer);
  262. ulOffset = ulGetLong(12 + tIndex * 8, aucBuffer);
  263. NO_DBG_DEC(tPropID);
  264. NO_DBG_HEX(ulOffset);
  265. tPropType = (size_t)ulGetLong(ulOffset, aucBuffer);
  266. NO_DBG_DEC(tPropType);
  267. switch (tPropID) {
  268. case PIDD_MANAGER:
  269. if (tPropType == VT_LPSTR && szManager == NULL) {
  270. szManager = szLpstr(ulOffset, aucBuffer);
  271. }
  272. break;
  273. case PIDD_COMPANY:
  274. if (tPropType == VT_LPSTR && szCompany == NULL) {
  275. szCompany = szLpstr(ulOffset, aucBuffer);
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. } /* end of vAnalyseDocumentSummaryInfo */
  283. /*
  284. * pucAnalyseSummaryInfoHeader-
  285. */
  286. static UCHAR *
  287. pucAnalyseSummaryInfoHeader(FILE *pFile,
  288. ULONG ulStartBlock, ULONG ulSize,
  289. const ULONG *aulBBD, size_t tBBDLen,
  290. const ULONG *aulSBD, size_t tSBDLen)
  291. {
  292. const ULONG *aulBlockDepot;
  293. UCHAR *aucBuffer;
  294. size_t tBlockDepotLen, tBlockSize, tSectionCount, tLength;
  295. ULONG ulTmp, ulOffset;
  296. USHORT usLittleEndian, usEmpty, usOS, usVersion;
  297. UCHAR aucHdr[P_HEADER_SZ], aucSecLst[P_SECTION_MAX_SZ];
  298. if (ulSize < MIN_SIZE_FOR_BBD_USE) {
  299. /* Use the Small Block Depot */
  300. aulBlockDepot = aulSBD;
  301. tBlockDepotLen = tSBDLen;
  302. tBlockSize = SMALL_BLOCK_SIZE;
  303. } else {
  304. /* Use the Big Block Depot */
  305. aulBlockDepot = aulBBD;
  306. tBlockDepotLen = tBBDLen;
  307. tBlockSize = BIG_BLOCK_SIZE;
  308. }
  309. if (tBlockDepotLen == 0) {
  310. DBG_MSG("The Block Depot length is zero");
  311. return NULL;
  312. }
  313. /* Read the Summery Information header */
  314. if (!bReadBuffer(pFile, ulStartBlock,
  315. aulBlockDepot, tBlockDepotLen, tBlockSize,
  316. aucHdr, 0, P_HEADER_SZ)) {
  317. return NULL;
  318. }
  319. NO_DBG_PRINT_BLOCK(aucHdr, P_HEADER_SZ);
  320. /* Analyse the Summery Information header */
  321. usLittleEndian = usGetWord(0, aucHdr);
  322. if (usLittleEndian != 0xfffe) {
  323. DBG_HEX(usLittleEndian);
  324. DBG_MSG_C(usLittleEndian == 0xfeff, "Big endian");
  325. return NULL;
  326. }
  327. usEmpty = usGetWord(2, aucHdr);
  328. if (usEmpty != 0x0000) {
  329. DBG_DEC(usEmpty);
  330. return NULL;
  331. }
  332. ulTmp = ulGetLong(4, aucHdr);
  333. DBG_HEX(ulTmp);
  334. usOS = (USHORT)(ulTmp >> 16);
  335. usVersion = (USHORT)(ulTmp & 0xffff);
  336. switch (usOS) {
  337. case 0:
  338. DBG_MSG("Win16");
  339. DBG_HEX(usVersion);
  340. break;
  341. case 1:
  342. DBG_MSG("MacOS");
  343. DBG_HEX(usVersion);
  344. break;
  345. case 2:
  346. DBG_MSG("Win32");
  347. DBG_HEX(usVersion);
  348. break;
  349. default:
  350. DBG_DEC(usOS);
  351. DBG_HEX(usVersion);
  352. break;
  353. }
  354. tSectionCount = (size_t)ulGetLong(24, aucHdr);
  355. DBG_DEC_C(tSectionCount != 1 && tSectionCount != 2, tSectionCount);
  356. if (tSectionCount != 1 && tSectionCount != 2) {
  357. return NULL;
  358. }
  359. /* Read the Summery Information Section Lists */
  360. if (!bReadBuffer(pFile, ulStartBlock,
  361. aulBlockDepot, tBlockDepotLen, tBlockSize,
  362. aucSecLst, P_HEADER_SZ, P_SECTION_SZ(tSectionCount))) {
  363. return NULL;
  364. }
  365. NO_DBG_PRINT_BLOCK(aucSecLst, P_SECTION_SZ(tSectionCount));
  366. ulTmp = ulGetLong(0, aucSecLst);
  367. DBG_HEX(ulTmp);
  368. ulTmp = ulGetLong(4, aucSecLst);
  369. DBG_HEX(ulTmp);
  370. ulTmp = ulGetLong(8, aucSecLst);
  371. DBG_HEX(ulTmp);
  372. ulTmp = ulGetLong(12, aucSecLst);
  373. DBG_HEX(ulTmp);
  374. ulOffset = ulGetLong(16, aucSecLst);
  375. DBG_DEC_C(ulOffset != P_HEADER_SZ + P_SECTIONLIST_SZ &&
  376. ulOffset != P_HEADER_SZ + 2 * P_SECTIONLIST_SZ,
  377. ulOffset);
  378. fail(ulOffset != P_HEADER_SZ + P_SECTIONLIST_SZ &&
  379. ulOffset != P_HEADER_SZ + 2 * P_SECTIONLIST_SZ);
  380. tLength =
  381. (size_t)ulGetLong(tSectionCount * P_SECTIONLIST_SZ, aucSecLst);
  382. NO_DBG_HEX(tLength);
  383. fail(ulOffset + tLength > ulSize);
  384. /* Read the Summery Information */
  385. aucBuffer = xmalloc(tLength);
  386. if (!bReadBuffer(pFile, ulStartBlock,
  387. aulBlockDepot, tBlockDepotLen, tBlockSize,
  388. aucBuffer, ulOffset, tLength)) {
  389. aucBuffer = xfree(aucBuffer);
  390. return NULL;
  391. }
  392. NO_DBG_PRINT_BLOCK(aucBuffer, tLength);
  393. return aucBuffer;
  394. } /* end of pucAnalyseSummaryInfoHeader */
  395. /*
  396. * vSet0SummaryInfo - set summary information from a Word for DOS file
  397. */
  398. void
  399. vSet0SummaryInfo(FILE *pFile, const UCHAR *aucHeader)
  400. {
  401. UCHAR *aucBuffer;
  402. ULONG ulBeginSumdInfo, ulBeginNextBlock;
  403. size_t tLen;
  404. USHORT usCodepage, usOffset;
  405. TRACE_MSG("vSet0SummaryInfo");
  406. fail(pFile == NULL || aucHeader == NULL);
  407. /* First check the header */
  408. usCodepage = usGetWord(0x7e, aucHeader);
  409. DBG_DEC(usCodepage);
  410. switch (usCodepage) {
  411. case 850: usLid = 0x0809; break; /* Latin1 -> British English */
  412. case 862: usLid = 0x040d; break; /* Hebrew */
  413. case 866: usLid = 0x0419; break; /* Russian */
  414. case 0:
  415. case 437:
  416. default: usLid = 0x0409; break; /* ASCII -> American English */
  417. }
  418. /* Second check the summary information block */
  419. ulBeginSumdInfo = 128 * (ULONG)usGetWord(0x1c, aucHeader);
  420. DBG_HEX(ulBeginSumdInfo);
  421. ulBeginNextBlock = 128 * (ULONG)usGetWord(0x6a, aucHeader);
  422. DBG_HEX(ulBeginNextBlock);
  423. if (ulBeginSumdInfo >= ulBeginNextBlock || ulBeginNextBlock == 0) {
  424. /* There is no summary information block */
  425. return;
  426. }
  427. tLen = (size_t)(ulBeginNextBlock - ulBeginSumdInfo);
  428. aucBuffer = xmalloc(tLen);
  429. /* Read the summary information block */
  430. if (!bReadBytes(aucBuffer, tLen, ulBeginSumdInfo, pFile)) {
  431. return;
  432. }
  433. usOffset = usGetWord(0, aucBuffer);
  434. if (aucBuffer[usOffset] != 0) {
  435. NO_DBG_MSG(aucBuffer + usOffset);
  436. szTitle = xstrdup((char *)aucBuffer + usOffset);
  437. }
  438. usOffset = usGetWord(2, aucBuffer);
  439. if (aucBuffer[usOffset] != 0) {
  440. NO_DBG_MSG(aucBuffer + usOffset);
  441. szAuthor = xstrdup((char *)aucBuffer + usOffset);
  442. }
  443. usOffset = usGetWord(12, aucBuffer);
  444. if (aucBuffer[usOffset] != 0) {
  445. NO_DBG_STRN(aucBuffer + usOffset, 8);
  446. tLastSaveDtm = tConvertDosDate((char *)aucBuffer + usOffset);
  447. }
  448. usOffset = usGetWord(14, aucBuffer);
  449. if (aucBuffer[usOffset] != 0) {
  450. NO_DBG_STRN(aucBuffer + usOffset, 8);
  451. tCreateDtm = tConvertDosDate((char *)aucBuffer + usOffset);
  452. }
  453. aucBuffer = xfree(aucBuffer);
  454. } /* end of vSet0SummaryInfo */
  455. /*
  456. * vSet2SummaryInfo - set summary information from a WinWord 1/2 file
  457. */
  458. void
  459. vSet2SummaryInfo(FILE *pFile, int iWordVersion, const UCHAR *aucHeader)
  460. {
  461. UCHAR *aucBuffer;
  462. ULONG ulBeginSumdInfo, ulBeginDocpInfo, ulTmp;
  463. size_t tSumdInfoLen, tDocpInfoLen, tLen, tCounter, tStart;
  464. TRACE_MSG("vSet2SummaryInfo");
  465. fail(pFile == NULL || aucHeader == NULL);
  466. fail(iWordVersion != 1 && iWordVersion != 2);
  467. /* First check the header */
  468. usLid = usGetWord(0x06, aucHeader); /* Language IDentification */
  469. DBG_HEX(usLid);
  470. if (usLid < 999 && iWordVersion == 1) {
  471. switch (usLid) {
  472. case 1: usLid = 0x0409; break; /* American English */
  473. case 2: usLid = 0x0c0c; break; /* Canadian French */
  474. case 31: usLid = 0x0413; break; /* Dutch */
  475. case 33: usLid = 0x040c; break; /* French */
  476. case 34: usLid = 0x040a; break; /* Spanish */
  477. case 36: usLid = 0x040e; break; /* Hungarian */
  478. case 39: usLid = 0x0410; break; /* Italian */
  479. case 44: usLid = 0x0809; break; /* British English */
  480. case 45: usLid = 0x0406; break; /* Danish */
  481. case 46: usLid = 0x041f; break; /* Swedish */
  482. case 47: usLid = 0x0414; break; /* Norwegian */
  483. case 48: usLid = 0x0415; break; /* Polish */
  484. case 49: usLid = 0x0407; break; /* German */
  485. case 351: usLid = 0x0816; break; /* Portuguese */
  486. case 358: usLid = 0x040b; break; /* Finnish */
  487. default:
  488. DBG_DEC(usLid);
  489. DBG_FIXME();
  490. usLid = 0x0409; /* American English */
  491. break;
  492. }
  493. }
  494. if (iWordVersion != 2) {
  495. /* Unknown where to find the associated strings */
  496. return;
  497. }
  498. /* Second check the associated strings */
  499. ulBeginSumdInfo = ulGetLong(0x118, aucHeader); /* fcSttbfAssoc */
  500. DBG_HEX(ulBeginSumdInfo);
  501. tSumdInfoLen = (size_t)usGetWord(0x11c, aucHeader); /* cbSttbfAssoc */
  502. DBG_DEC(tSumdInfoLen);
  503. if (tSumdInfoLen == 0) {
  504. /* There is no summary information */
  505. return;
  506. }
  507. aucBuffer = xmalloc(tSumdInfoLen);
  508. if (!bReadBytes(aucBuffer, tSumdInfoLen, ulBeginSumdInfo, pFile)) {
  509. aucBuffer = xfree(aucBuffer);
  510. return;
  511. }
  512. NO_DBG_PRINT_BLOCK(aucBuffer, tSumdInfoLen);
  513. tLen = (size_t)ucGetByte(0, aucBuffer);
  514. DBG_DEC_C(tSumdInfoLen != tLen, tSumdInfoLen);
  515. DBG_DEC_C(tSumdInfoLen != tLen, tLen);
  516. tStart = 1;
  517. for (tCounter = 0; tCounter < 17; tCounter++) {
  518. if (tStart >= tSumdInfoLen) {
  519. break;
  520. }
  521. tLen = (size_t)ucGetByte(tStart, aucBuffer);
  522. if (tLen != 0) {
  523. NO_DBG_DEC(tCounter);
  524. NO_DBG_STRN(aucBuffer + tStart + 1, tLen);
  525. switch (tCounter) {
  526. case 3:
  527. szTitle = xmalloc(tLen + 1);
  528. strncpy(szTitle,
  529. (char *)aucBuffer + tStart + 1,
  530. tLen);
  531. szTitle[tLen] = '\0';
  532. break;
  533. case 4:
  534. szSubject = xmalloc(tLen + 1);
  535. strncpy(szSubject,
  536. (char *)aucBuffer + tStart + 1,
  537. tLen);
  538. szSubject[tLen] = '\0';
  539. break;
  540. case 7:
  541. szAuthor = xmalloc(tLen + 1);
  542. strncpy(szAuthor,
  543. (char *)aucBuffer + tStart + 1,
  544. tLen);
  545. szAuthor[tLen] = '\0';
  546. break;
  547. default:
  548. break;
  549. }
  550. }
  551. tStart += tLen + 1;
  552. }
  553. aucBuffer = xfree(aucBuffer);
  554. /* Third check the document properties */
  555. ulBeginDocpInfo = ulGetLong(0x112, aucHeader); /* fcDop */
  556. DBG_HEX(ulBeginDocpInfo);
  557. tDocpInfoLen = (size_t)usGetWord(0x116, aucHeader); /* cbDop */
  558. DBG_DEC(tDocpInfoLen);
  559. if (tDocpInfoLen < 12) {
  560. return;
  561. }
  562. aucBuffer = xmalloc(tDocpInfoLen);
  563. if (!bReadBytes(aucBuffer, tDocpInfoLen, ulBeginDocpInfo, pFile)) {
  564. aucBuffer = xfree(aucBuffer);
  565. return;
  566. }
  567. ulTmp = ulGetLong(0x14, aucBuffer); /* dttmCreated */
  568. tCreateDtm = tConvertDTTM(ulTmp);
  569. ulTmp = ulGetLong(0x18, aucBuffer); /* dttmRevised */
  570. tLastSaveDtm = tConvertDTTM(ulTmp);
  571. aucBuffer = xfree(aucBuffer);
  572. } /* end of vSet2SummaryInfo */
  573. /*
  574. * vSetSummaryInfoOLE - set summary information from a Word 6+ file
  575. */
  576. static void
  577. vSetSummaryInfoOLE(FILE *pFile, const pps_info_type *pPPS,
  578. const ULONG *aulBBD, size_t tBBDLen,
  579. const ULONG *aulSBD, size_t tSBDLen)
  580. {
  581. UCHAR *pucBuffer;
  582. fail(pFile == NULL || pPPS == NULL);
  583. fail(aulBBD == NULL || aulSBD == NULL);
  584. /* Summary Information */
  585. pucBuffer = pucAnalyseSummaryInfoHeader(pFile,
  586. pPPS->tSummaryInfo.ulSB, pPPS->tSummaryInfo.ulSize,
  587. aulBBD, tBBDLen, aulSBD, tSBDLen);
  588. if (pucBuffer != NULL) {
  589. vAnalyseSummaryInfo(pucBuffer);
  590. pucBuffer = xfree(pucBuffer);
  591. }
  592. /* Document Summary Information */
  593. pucBuffer = pucAnalyseSummaryInfoHeader(pFile,
  594. pPPS->tDocSummaryInfo.ulSB, pPPS->tDocSummaryInfo.ulSize,
  595. aulBBD, tBBDLen, aulSBD, tSBDLen);
  596. if (pucBuffer != NULL) {
  597. vAnalyseDocumentSummaryInfo(pucBuffer);
  598. pucBuffer = xfree(pucBuffer);
  599. }
  600. } /* end of vSetSummaryInfoOLE */
  601. /*
  602. * vSet6SummaryInfo - set summary information from a Word 6/7 file
  603. */
  604. void
  605. vSet6SummaryInfo(FILE *pFile, const pps_info_type *pPPS,
  606. const ULONG *aulBBD, size_t tBBDLen,
  607. const ULONG *aulSBD, size_t tSBDLen,
  608. const UCHAR *aucHeader)
  609. {
  610. TRACE_MSG("vSet6SummaryInfo");
  611. /* Header Information */
  612. usLid = usGetWord(0x06, aucHeader); /* Language IDentification */
  613. DBG_HEX(usLid);
  614. /* Summery Information */
  615. vSetSummaryInfoOLE(pFile, pPPS, aulBBD, tBBDLen, aulSBD, tSBDLen);
  616. } /* end of vSet6SummaryInfo */
  617. /*
  618. * vSet8SummaryInfo - set summary information a Word 8/9/10 file
  619. */
  620. void
  621. vSet8SummaryInfo(FILE *pFile, const pps_info_type *pPPS,
  622. const ULONG *aulBBD, size_t tBBDLen,
  623. const ULONG *aulSBD, size_t tSBDLen,
  624. const UCHAR *aucHeader)
  625. {
  626. USHORT usTmp;
  627. TRACE_MSG("vSet8SummaryInfo");
  628. /* Header Information */
  629. usTmp = usGetWord(0x0a, aucHeader);
  630. if (usTmp & BIT(14)) {
  631. /* Language IDentification Far East */
  632. usLid = usGetWord(0x3c, aucHeader);
  633. } else {
  634. /* Language IDentification */
  635. usLid = usGetWord(0x06, aucHeader);
  636. }
  637. DBG_HEX(usLid);
  638. /* Summery Information */
  639. vSetSummaryInfoOLE(pFile, pPPS, aulBBD, tBBDLen, aulSBD, tSBDLen);
  640. } /* end of vSet8SummaryInfo */
  641. /*
  642. * szGetTitle - get the title field
  643. */
  644. const char *
  645. szGetTitle(void)
  646. {
  647. return szTitle;
  648. } /* end of szGetTitle */
  649. /*
  650. * szGetSubject - get the subject field
  651. */
  652. const char *
  653. szGetSubject(void)
  654. {
  655. return szSubject;
  656. } /* end of szGetSubject */
  657. /*
  658. * szGetAuthor - get the author field
  659. */
  660. const char *
  661. szGetAuthor(void)
  662. {
  663. return szAuthor;
  664. } /* end of szGetAuthor */
  665. /*
  666. * szGetLastSaveDtm - get the last save date field
  667. */
  668. const char *
  669. szGetLastSaveDtm(void)
  670. {
  671. static char szTime[12];
  672. struct tm *pTime;
  673. if (tLastSaveDtm == (time_t)-1) {
  674. return NULL;
  675. }
  676. pTime = localtime(&tLastSaveDtm);
  677. if (pTime == NULL) {
  678. return NULL;
  679. }
  680. sprintf(szTime, "%04d-%02d-%02d",
  681. pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday);
  682. return szTime;
  683. } /* end of szGetLastSaveDtm */
  684. /*
  685. * szGetModDate - get the last save date field
  686. */
  687. const char *
  688. szGetModDate(void)
  689. {
  690. static char szTime[20];
  691. struct tm *pTime;
  692. if (tLastSaveDtm == (time_t)-1) {
  693. return NULL;
  694. }
  695. pTime = localtime(&tLastSaveDtm);
  696. if (pTime == NULL) {
  697. return NULL;
  698. }
  699. sprintf(szTime, "D:%04d%02d%02d%02d%02d",
  700. pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday,
  701. pTime->tm_hour, pTime->tm_min);
  702. return szTime;
  703. } /* end of szGetModDate */
  704. /*
  705. * szGetCreationDate - get the last save date field
  706. */
  707. const char *
  708. szGetCreationDate(void)
  709. {
  710. static char szTime[20];
  711. struct tm *pTime;
  712. if (tCreateDtm == (time_t)-1) {
  713. return NULL;
  714. }
  715. pTime = localtime(&tCreateDtm);
  716. if (pTime == NULL) {
  717. return NULL;
  718. }
  719. sprintf(szTime, "D:%04d%02d%02d%02d%02d",
  720. pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday,
  721. pTime->tm_hour, pTime->tm_min);
  722. return szTime;
  723. } /* end of szGetCreationDate */
  724. /*
  725. * szGetCompany - get the company field
  726. */
  727. const char *
  728. szGetCompany(void)
  729. {
  730. return szCompany;
  731. } /* end of szGetCompany */
  732. /*
  733. * szGetLanguage - get de language field
  734. */
  735. const char *
  736. szGetLanguage(void)
  737. {
  738. if (usLid == (USHORT)-1) {
  739. /* No Language IDentification */
  740. return NULL;
  741. }
  742. if (usLid < 999) {
  743. /* This is a Locale, not a Language IDentification */
  744. DBG_DEC(usLid);
  745. return NULL;
  746. }
  747. /* Exceptions to the general rule */
  748. switch (usLid) {
  749. case 0x0404: return "zh_TW"; /* Traditional Chinese */
  750. case 0x0804: return "zh_CN"; /* Simplified Chinese */
  751. case 0x0c04: return "zh_HK"; /* Hong Kong Chinese */
  752. case 0x1004: return "zh_SG"; /* Singapore Chinese */
  753. case 0x0807: return "de_CH"; /* Swiss German */
  754. case 0x0409: return "en_US"; /* American English */
  755. case 0x0809: return "en_GB"; /* British English */
  756. case 0x0c09: return "en_AU"; /* Australian English */
  757. case 0x080a: return "es_MX"; /* Mexican Spanish */
  758. case 0x080c: return "fr_BE"; /* Belgian French */
  759. case 0x0c0c: return "fr_CA"; /* Canadian French */
  760. case 0x100c: return "fr_CH"; /* Swiss French */
  761. case 0x0810: return "it_CH"; /* Swiss Italian */
  762. case 0x0813: return "nl_BE"; /* Belgian Dutch */
  763. case 0x0416: return "pt_BR"; /* Brazilian Portuguese */
  764. case 0x081a:
  765. case 0x0c1a: return "sr"; /* Serbian */
  766. case 0x081d: return "sv_FI"; /* Finland Swedish */
  767. default:
  768. break;
  769. }
  770. /* The general rule */
  771. switch (usLid & 0x00ff) {
  772. case 0x01: return "ar"; /* Arabic */
  773. case 0x02: return "bg"; /* Bulgarian */
  774. case 0x03: return "ca"; /* Catalan */
  775. case 0x04: return "zh"; /* Chinese */
  776. case 0x05: return "cs"; /* Czech */
  777. case 0x06: return "da"; /* Danish */
  778. case 0x07: return "de"; /* German */
  779. case 0x08: return "el"; /* Greek */
  780. case 0x09: return "en"; /* English */
  781. case 0x0a: return "es"; /* Spanish */
  782. case 0x0b: return "fi"; /* Finnish */
  783. case 0x0c: return "fr"; /* French */
  784. case 0x0d: return "he"; /* Hebrew */
  785. case 0x0e: return "hu"; /* Hungarian */
  786. case 0x0f: return "is"; /* Icelandic */
  787. case 0x10: return "it"; /* Italian */
  788. case 0x11: return "ja"; /* Japanese */
  789. case 0x12: return "ko"; /* Korean */
  790. case 0x13: return "nl"; /* Dutch */
  791. case 0x14: return "no"; /* Norwegian */
  792. case 0x15: return "pl"; /* Polish */
  793. case 0x16: return "pt"; /* Portuguese */
  794. case 0x17: return "rm"; /* Rhaeto-Romance */
  795. case 0x18: return "ro"; /* Romanian */
  796. case 0x19: return "ru"; /* Russian */
  797. case 0x1a: return "hr"; /* Croatian */
  798. case 0x1b: return "sk"; /* Slovak */
  799. case 0x1c: return "sq"; /* Albanian */
  800. case 0x1d: return "sv"; /* Swedish */
  801. case 0x1e: return "th"; /* Thai */
  802. case 0x1f: return "tr"; /* Turkish */
  803. case 0x20: return "ur"; /* Urdu */
  804. case 0x21: return "id"; /* Indonesian */
  805. case 0x22: return "uk"; /* Ukrainian */
  806. case 0x23: return "be"; /* Belarusian */
  807. case 0x24: return "sl"; /* Slovenian */
  808. case 0x25: return "et"; /* Estonian */
  809. case 0x26: return "lv"; /* Latvian */
  810. case 0x27: return "lt"; /* Lithuanian */
  811. case 0x29: return "fa"; /* Farsi */
  812. case 0x2a: return "vi"; /* Viet Nam */
  813. case 0x2b: return "hy"; /* Armenian */
  814. case 0x2c: return "az"; /* Azeri */
  815. case 0x2d: return "eu"; /* Basque */
  816. case 0x2f: return "mk"; /* Macedonian */
  817. case 0x36: return "af"; /* Afrikaans */
  818. case 0x37: return "ka"; /* Georgian */
  819. case 0x38: return "fo"; /* Faeroese */
  820. case 0x39: return "hi"; /* Hindi */
  821. case 0x3e: return "ms"; /* Malay */
  822. case 0x3f: return "kk"; /* Kazakh */
  823. default:
  824. DBG_HEX(usLid);
  825. DBG_FIXME();
  826. return NULL;
  827. }
  828. } /* end of szGetLanguage */