Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
16 KiB

  1. /* @(#)CM_VerSion xcf_misc.c atm09 1.2 16426.eco sum= 60874 atm09.001 */
  2. /* @(#)CM_VerSion xcf_misc.c atm08 1.4 16343.eco sum= 47357 atm08.005 */
  3. /***********************************************************************/
  4. /* */
  5. /* Copyright 1995-1996 Adobe Systems Incorporated. */
  6. /* All rights reserved. */
  7. /* */
  8. /* Patents Pending */
  9. /* */
  10. /* NOTICE: All information contained herein is the property of Adobe */
  11. /* Systems Incorporated. Many of the intellectual and technical */
  12. /* concepts contained herein are proprietary to Adobe, are protected */
  13. /* as trade secrets, and are made available only to Adobe licensees */
  14. /* for their internal use. Any reproduction or dissemination of this */
  15. /* software is strictly forbidden unless prior written permission is */
  16. /* obtained from Adobe. */
  17. /* */
  18. /* PostScript and Display PostScript are trademarks of Adobe Systems */
  19. /* Incorporated or its subsidiaries and may be registered in certain */
  20. /* jurisdictions. */
  21. /* */
  22. /***********************************************************************/
  23. /***********************************************************************
  24. Original version: John Felton, March 8, 1996
  25. ************************************************************************/
  26. /* -------------------------------------------------------------------------
  27. Header Includes
  28. --------------------------------------------------------------------------- */
  29. #include "algndjmp.h"
  30. #include "xcf_priv.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #ifdef XCF_DEVELOP
  35. void XCF_FatalErrorHandler(XCF_Handle hndl, int error, char *str, Card32 number)
  36. {
  37. XCF_Handle h = (XCF_Handle) hndl;
  38. DEFINE_ALIGN_SETJMP_VAR;
  39. if (h->callbacks.printfError != NULL)
  40. h->callbacks.printfError("Fatal Error: %s: %ld\n", str, number);
  41. LONGJMP(h->jumpData, error);
  42. }
  43. #else
  44. void XCF_FatalErrorHandler(XCF_Handle hndl, int error)
  45. {
  46. XCF_Handle h = (XCF_Handle) hndl;
  47. DEFINE_ALIGN_SETJMP_VAR;
  48. if (h->callbacks.printfError != (XCF_printfError)NULL)
  49. h->callbacks.printfError("*** Fatal Error ***\n");
  50. LONGJMP(h->jumpData, error);
  51. }
  52. #endif
  53. Card32 XCF_Read(XCF_Handle h, IntX byteCount)
  54. {
  55. Card32 result = 0;
  56. if (h->inBuffer.pos+byteCount > h->inBuffer.end)
  57. XCF_FATAL_ERROR(h, XCF_EarlyEndOfData, "Read Past End Of Data",
  58. (Card32)(h->inBuffer.blockOffset + (h->inBuffer.pos - h->inBuffer.start) + byteCount-1));
  59. else
  60. {
  61. switch (byteCount)
  62. {
  63. case 4 :
  64. result = (*h->inBuffer.pos++)<<8;
  65. case 3 :
  66. result = (result + *h->inBuffer.pos++)<<8;
  67. case 2 :
  68. result = (result + *h->inBuffer.pos++)<<8;
  69. case 1 :
  70. result += *h->inBuffer.pos++;
  71. break;
  72. default :
  73. XCF_FATAL_ERROR(h, XCF_InternalError, "Invalid Byte Count in Read.",(Card32)byteCount);
  74. }
  75. return result;
  76. }
  77. return 0; /* This return prevents a compiler warning */
  78. }
  79. Card8 XCF_Read1(XCF_Handle h)
  80. {
  81. if (h->inBuffer.pos >= h->inBuffer.end)
  82. XCF_FATAL_ERROR(h, XCF_EarlyEndOfData, "One Byte Read Past End Of Data",
  83. (Card32)(h->inBuffer.blockOffset + (h->inBuffer.pos - h->inBuffer.start)));
  84. else
  85. return *h->inBuffer.pos++;
  86. return 0; /* This return prevents a compiler warning */
  87. }
  88. Card16 XCF_Read2(XCF_Handle h)
  89. {
  90. Card16 result;
  91. if (h->inBuffer.pos+2 > h->inBuffer.end)
  92. XCF_FATAL_ERROR(h, XCF_EarlyEndOfData, "Two Byte Read Past End Of Data",(Card32)(h->inBuffer.blockOffset + (h->inBuffer.pos - h->inBuffer.start)));
  93. else
  94. {
  95. result = *h->inBuffer.pos++;
  96. result = (result<<8) + *h->inBuffer.pos++;
  97. return result;
  98. }
  99. return 0; /* This return prevents a compiler warning */
  100. }
  101. long int XCF_OutputPos(XCF_Handle h)
  102. {
  103. XCF_FlushOutputBuffer(h);
  104. return h->callbacks.outputPos(h->callbacks.outputPosHook);
  105. }
  106. void XCF_FlushOutputBuffer(XCF_Handle h)
  107. {
  108. h->callbacks.putBytes(h->outBuffer.outBuffer, -1, h->outBuffer.outBufferCount, h->callbacks.putBytesHook);
  109. h->outBuffer.outBufferCount = 0;
  110. }
  111. void XCF_PutData(XCF_Handle h, Card8 PTR_PREFIX *pData, Card32 length)
  112. {
  113. if ((h->outBuffer.outBufferCount + length) > h->options.maxBlockSize)
  114. XCF_FlushOutputBuffer(h);
  115. while (length > h->options.maxBlockSize)
  116. {
  117. h->callbacks.putBytes(pData, -1, h->options.maxBlockSize, h->callbacks.putBytesHook);
  118. length -= h->options.maxBlockSize;
  119. pData += h->options.maxBlockSize;
  120. }
  121. if (length > 0) /* add remainder to buffer */
  122. {
  123. h->callbacks.memcpy(&h->outBuffer.outBuffer[h->outBuffer.outBufferCount], pData, (Card16) length);
  124. h->outBuffer.outBufferCount += (Card16) length;
  125. }
  126. }
  127. void XCF_PutString(XCF_Handle h, char PTR_PREFIX *str)
  128. {
  129. XCF_PutData(h,(Card8 PTR_PREFIX *)str,h->callbacks.strlen(str));
  130. }
  131. void XCF_SetOuputPosition(XCF_Handle h, Card32 pos)
  132. {
  133. XCF_FlushOutputBuffer(h);
  134. h->callbacks.putBytes((unsigned char *)NULL, pos, 0, h->callbacks.putBytesHook);
  135. }
  136. static void BCDToStr(XCF_Handle h, Card8 PTR_PREFIX *pData, char PTR_PREFIX *str)
  137. {
  138. Card8 currentByte;
  139. Card8 currentNibble;
  140. boolean firstNibble = false;
  141. IntX byteCount = 0;
  142. while (1)
  143. {
  144. if (!firstNibble)
  145. {
  146. currentByte = *pData++;
  147. currentNibble = currentByte>>4;
  148. if (++byteCount > XCF_MAX_BCD_NIBBLES)
  149. XCF_FATAL_ERROR(h, XCF_InvalidNumber, "BCD String Conversion Number of Bytes Exceeds Maximum Length",(Card32)byteCount);
  150. }
  151. else
  152. currentNibble = currentByte & 0x0F;
  153. firstNibble = !firstNibble;
  154. if (currentNibble <= 9)
  155. *str++ = currentNibble + '0';
  156. else if (currentNibble == 10)
  157. *str++ = '.';
  158. else if (currentNibble == 11)
  159. *str++ = 'E';
  160. else if (currentNibble == 12)
  161. {
  162. *str++ = 'E';
  163. *str++ = '-';
  164. }
  165. else if (currentNibble == 14)
  166. *str++ = '-';
  167. else if (currentNibble == 15)
  168. {
  169. *str = '\0';
  170. return;
  171. }
  172. else
  173. XCF_FATAL_ERROR(h, XCF_InvalidNumber, "Invalid Nibble in BCD Number",(Card32)currentNibble);
  174. } /* end while */
  175. }
  176. static Fixed XCF_BCDToFixed(XCF_Handle h,Card8 PTR_PREFIX *pData, boolean fracType)
  177. {
  178. char numbStr[XCF_MAX_BCD_NIBBLES*2 + 1]; /* Add one for null character. */
  179. BCDToStr(h, pData, numbStr);
  180. #if USE_FXL
  181. return (fracType ? XCF_ConvertFrac(h, numbStr) : XCF_ConvertFixed(h,
  182. numbStr));
  183. #else
  184. return (fracType ? (Fixed)REAL_TO_FRAC(h->callbacks.atof(numbStr)) :
  185. (Fixed)(REAL_TO_FIXED(h->callbacks.atof(numbStr))));
  186. #endif
  187. }
  188. #if JUDY
  189. static double XCF_BCDToDouble(XCF_Handle h,Card8 PTR_PREFIX *pData)
  190. {
  191. char numbStr[XCF_MAX_BCD_NIBBLES*2];
  192. BCDToStr(h, pData, numbStr);
  193. return h->callbacks.atof(numbStr);
  194. }
  195. #endif
  196. IntX XCF_FindNextOperator(XCF_Handle h, Card16 PTR_PREFIX *opCode, boolean dict)
  197. {
  198. IntX argCount = 0;
  199. Card8 byteIn;
  200. while (1)
  201. {
  202. byteIn = XCF_Read1(h);
  203. if (byteIn > 31)
  204. {
  205. if (byteIn == 255)
  206. XCF_Read(h,4);
  207. else if (byteIn > 246)
  208. XCF_Read1(h);
  209. }
  210. else if (byteIn == OpCode(cff_shortint))
  211. XCF_Read2(h);
  212. else if (dict && (byteIn == OpCode(cff_longint)))
  213. XCF_Read(h,4);
  214. else if (dict && (byteIn == OpCode(cff_BCD)))
  215. {
  216. byteIn = XCF_Read1(h);
  217. while (((byteIn>>4) != 15) && ((byteIn & 0x0F) != 15))
  218. byteIn = XCF_Read1(h);
  219. }
  220. else
  221. {
  222. if (byteIn == OpCode(tx_escape))
  223. *opCode = cff_ESC(XCF_Read1(h));
  224. else
  225. *opCode = byteIn;
  226. return argCount;
  227. }
  228. ++argCount;
  229. }
  230. }
  231. static Int32 XCF_ArgPtrToInt(XCF_Handle h, Card8 PTR_PREFIX * PTR_PREFIX *ppArgList)
  232. {
  233. Card8 PTR_PREFIX *pArgList = *ppArgList;
  234. Card8 byteIn = *pArgList++;
  235. Int32 result;
  236. long int intNumber;
  237. Fixed fixedNumber;
  238. if (byteIn > 31)
  239. {
  240. if (byteIn <= 246)
  241. {
  242. result = (Int32)byteIn - 139;
  243. }
  244. else if (byteIn <= 250)
  245. {
  246. result = (((Int32)byteIn - 247) << 8) + *pArgList++ + 108;
  247. }
  248. else if (byteIn <= 254)
  249. {
  250. result = -((((Int32)byteIn - 251) << 8) + *pArgList++ + 108);
  251. }
  252. else /* byteIn == 255 */
  253. {
  254. fixedNumber = *pArgList++;
  255. fixedNumber = (fixedNumber << 8) | *pArgList++;
  256. fixedNumber = (fixedNumber << 8) | *pArgList++;
  257. fixedNumber = (fixedNumber << 8) | *pArgList++;
  258. result = ROUND_FIXED_TO_INT(fixedNumber);
  259. }
  260. }
  261. else if (byteIn == OpCode(cff_shortint))
  262. {
  263. intNumber = *pArgList++;
  264. intNumber = (intNumber << 8) | *pArgList++;
  265. result = intNumber;
  266. }
  267. else if (byteIn == OpCode(cff_longint))
  268. {
  269. intNumber = *pArgList++;
  270. intNumber = (intNumber << 8) | *pArgList++;
  271. intNumber = (intNumber << 8) | *pArgList++;
  272. intNumber = (intNumber << 8) | *pArgList++;
  273. result = intNumber;
  274. }
  275. else if (byteIn == OpCode(cff_BCD))
  276. { /* This opcode shouldn't occur for the keywords for which
  277. this procedure is currently called. */
  278. #if JUDY
  279. result = XCF_BCDToDouble(h, pArgList);
  280. tempByte = *pArgList++;
  281. while (((tempByte>>4) != 15) && ((tempByte & 0x0F) != 15))
  282. tempByte = *pArgList++;
  283. #else
  284. XCF_FATAL_ERROR(h, XCF_InternalError,"cff_BCD operator encountered",(Card32)pArgList);
  285. #endif
  286. }
  287. else
  288. XCF_FATAL_ERROR(h, XCF_InternalError,"Command Encountered in Argument List",(Card32)pArgList);
  289. *ppArgList = pArgList;
  290. return result;
  291. }
  292. Fixed XCF_ArgPtrToFixed(XCF_Handle h, Card8 PTR_PREFIX * PTR_PREFIX
  293. *ppArgList, boolean fracType)
  294. {
  295. Card8 PTR_PREFIX *pArgList = *ppArgList;
  296. Card8 byteIn = *pArgList++;
  297. Fixed result;
  298. long int intNumber;
  299. Card8 tempByte;
  300. if (byteIn > 31)
  301. {
  302. if (byteIn <= 246)
  303. {
  304. result = INT_TO_FIXED((Int32)byteIn - 139);
  305. }
  306. else if (byteIn <= 250)
  307. {
  308. result = INT_TO_FIXED((((Int32)byteIn - 247) << 8) + *pArgList++ + 108);
  309. }
  310. else if (byteIn <= 254)
  311. {
  312. result = -INT_TO_FIXED(((((Int32)byteIn - 251) << 8) + *pArgList++ + 108));
  313. }
  314. else /* byteIn == 255 */
  315. {
  316. result = *pArgList++;
  317. result = (result << 8) | *pArgList++;
  318. result = (result << 8) | *pArgList++;
  319. result = (result << 8) | *pArgList++;
  320. }
  321. }
  322. else if (byteIn == OpCode(cff_shortint))
  323. {
  324. intNumber = *pArgList++;
  325. intNumber = (intNumber << 8) | *pArgList++;
  326. result = INT_TO_FIXED(intNumber);
  327. }
  328. else if (byteIn == OpCode(cff_longint))
  329. { /* This just uses the low order bytes when converting a long to fixed. */
  330. intNumber = *pArgList++;
  331. intNumber = (intNumber << 8) | *pArgList++;
  332. intNumber = (intNumber << 8) | *pArgList++;
  333. intNumber = (intNumber << 8) | *pArgList++;
  334. result = INT_TO_FIXED(intNumber);
  335. }
  336. else if (byteIn == OpCode(cff_BCD))
  337. {
  338. result = XCF_BCDToFixed(h, pArgList, fracType);
  339. tempByte = *pArgList++;
  340. while (((tempByte>>4) != 15) && ((tempByte & 0x0F) != 15))
  341. tempByte = *pArgList++;
  342. }
  343. else
  344. XCF_FATAL_ERROR(h, XCF_InternalError,"Command Or Invalid Number Format Encountered in Argument List",(Card32)pArgList);
  345. *ppArgList = pArgList;
  346. return result;
  347. }
  348. /* Assumes that argCount can be read safely without running past end of data. This should always be the case. */
  349. void XCF_SaveDictArgumentList(XCF_Handle h, Fixed PTR_PREFIX *pArgArray,
  350. Card8 PTR_PREFIX *pArgList, IntX argCount,
  351. boolean fracType)
  352. {
  353. IntX loopIndex;
  354. for (loopIndex=1; loopIndex <= argCount; ++loopIndex)
  355. *pArgArray++ = XCF_ArgPtrToFixed(h, &pArgList, fracType);
  356. }
  357. /* Assumes that argCount can be read safely without running past end of data. This should always be the case. */
  358. void XCF_SaveDictIntArgumentList(XCF_Handle h, Int32 PTR_PREFIX *pArgArray, Card8 PTR_PREFIX *pArgList, IntX argCount)
  359. {
  360. IntX loopIndex;
  361. for (loopIndex=1; loopIndex <= argCount; ++loopIndex)
  362. *pArgArray++ = XCF_ArgPtrToInt(h, &pArgList);
  363. }
  364. /* Assumes that argCount can be read safely without running past end of
  365. data. This should always be the case. */
  366. void XCF_SaveFontMatrixStr(XCF_Handle h,
  367. char (PTR_PREFIX *pArgArray)[FONT_MATRIX_ENTRY_SIZE],
  368. Card8 PTR_PREFIX *pArgList, IntX argCount)
  369. {
  370. IntX loopIndex;
  371. Fixed val;
  372. Card8 tempByte;
  373. Card8 byteIn;
  374. for (loopIndex=0; loopIndex < argCount; loopIndex++)
  375. {
  376. byteIn = *pArgList;
  377. if (byteIn == OpCode(cff_BCD))
  378. {
  379. byteIn = *pArgList++;
  380. BCDToStr(h, pArgList, pArgArray[loopIndex]);
  381. tempByte = *pArgList++;
  382. while (((tempByte>>4) != 15) && ((tempByte & 0x0F) != 15))
  383. tempByte = *pArgList++;
  384. }
  385. else
  386. {
  387. val = XCF_ArgPtrToFixed(h, &pArgList, false);
  388. XCF_Fixed2CString(val, pArgArray[loopIndex], 7, false);
  389. }
  390. }
  391. }
  392. /* Assumes that argCount can be read safely without running past end of
  393. data. This should always be the case. */
  394. void XCF_SaveStrArgs(XCF_Handle h, char PTR_PREFIX *pArgArray,
  395. Card8 PTR_PREFIX *pArgList, IntX argCount)
  396. {
  397. IntX loopIndex;
  398. Fixed val;
  399. Card8 tempByte;
  400. Card8 byteIn;
  401. for (loopIndex=0; loopIndex < argCount; loopIndex++)
  402. {
  403. byteIn = *pArgList;
  404. if (byteIn == OpCode(cff_BCD))
  405. {
  406. byteIn = *pArgList++;
  407. BCDToStr(h, pArgList, &pArgArray[loopIndex]);
  408. tempByte = *pArgList++;
  409. while (((tempByte>>4) != 15) && ((tempByte & 0x0F) != 15))
  410. tempByte = *pArgList++;
  411. }
  412. else
  413. {
  414. val = XCF_ArgPtrToFixed(h, &pArgList, false);
  415. XCF_Fixed2CString(val, &pArgArray[loopIndex], 7, false);
  416. }
  417. }
  418. }
  419. #ifdef __cplusplus
  420. }
  421. #endif