ghostscript-9.20-remove-and-reimplement-ConvertUTF.patch 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. From 273a133110838ee5702e7eb6409a853c598211b2 Mon Sep 17 00:00:00 2001
  2. From: Ken Sharp <ken.sharp@artifex.com>
  3. Date: Thu, 29 Sep 2016 17:35:05 +0100
  4. Subject: [PATCH] Remove (and re-implement) ConvertUTF.c
  5. Bug #697122 " embedded ConvertUTF.c is buggy and licensed incompatibly with GPL/APGL"
  6. Its not clear that this code is incompatible with GPL, nor do we think
  7. any 'bugginess' in the code affects us, since we are using a comparatively
  8. small part of the included code.
  9. Nevertheless its possible to remove the code, and re-implement the small
  10. part we actually need, and that is done here.
  11. Also removed the DSCEncodingToUnicode option which was insanely difficult
  12. to use, and incorrectly documented.
  13. Yhis shows one difference, 692486_-_heap_overflow_in_pdf_to_ucs2.pdf
  14. now correctly throws an error, because the PDF file contains document
  15. information (Application) which has an invalid UTF16-BE sequence.
  16. ---
  17. base/ConvertUTF.c | 539 -----------------------------------------
  18. base/ConvertUTF.h | 155 ------------
  19. base/lib.mak | 4 -
  20. devices/devs.mak | 5 +-
  21. devices/vector/gdevpdf.c | 16 +-
  22. devices/vector/gdevpdfb.h | 1 -
  23. devices/vector/gdevpdfe.c | 270 +++++++++++----------
  24. devices/vector/gdevpdfp.c | 1 -
  25. devices/vector/gdevpdfx.h | 17 +-
  26. windows/ghostscript.vcproj | 8 -
  27. windows/ghostscript_rt.vcxproj | 2 -
  28. 11 files changed, 155 insertions(+), 863 deletions(-)
  29. delete mode 100644 base/ConvertUTF.c
  30. delete mode 100644 base/ConvertUTF.h
  31. diff --git a/base/ConvertUTF.c b/base/ConvertUTF.c
  32. deleted file mode 100644
  33. index cb2e2de..0000000
  34. --- a/base/ConvertUTF.c
  35. +++ /dev/null
  36. @@ -1,539 +0,0 @@
  37. -/*
  38. - * Copyright 2001-2004 Unicode, Inc.
  39. - *
  40. - * Disclaimer
  41. - *
  42. - * This source code is provided as is by Unicode, Inc. No claims are
  43. - * made as to fitness for any particular purpose. No warranties of any
  44. - * kind are expressed or implied. The recipient agrees to determine
  45. - * applicability of information provided. If this file has been
  46. - * purchased on magnetic or optical media from Unicode, Inc., the
  47. - * sole remedy for any claim will be exchange of defective media
  48. - * within 90 days of receipt.
  49. - *
  50. - * Limitations on Rights to Redistribute This Code
  51. - *
  52. - * Unicode, Inc. hereby grants the right to freely use the information
  53. - * supplied in this file in the creation of products supporting the
  54. - * Unicode Standard, and to make copies of this file in any form
  55. - * for internal or external distribution as long as this notice
  56. - * remains attached.
  57. - */
  58. -
  59. -
  60. -/* ---------------------------------------------------------------------
  61. -
  62. - Conversions between UTF32, UTF-16, and UTF-8. Source code file.
  63. - Author: Mark E. Davis, 1994.
  64. - Rev History: Rick McGowan, fixes & updates May 2001.
  65. - Sept 2001: fixed const & error conditions per
  66. - mods suggested by S. Parent & A. Lillich.
  67. - June 2002: Tim Dodd added detection and handling of incomplete
  68. - source sequences, enhanced error detection, added casts
  69. - to eliminate compiler warnings.
  70. - July 2003: slight mods to back out aggressive FFFE detection.
  71. - Jan 2004: updated switches in from-UTF8 conversions.
  72. - Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
  73. -
  74. - See the header file "ConvertUTF.h" for complete documentation.
  75. -
  76. ------------------------------------------------------------------------- */
  77. -
  78. -#include "ConvertUTF.h"
  79. -#ifdef CVTUTF_DEBUG
  80. -#include <stdio.h>
  81. -#endif
  82. -
  83. -static const int halfShift = 10; /* used for shifting by 10 bits */
  84. -
  85. -static const UTF32 halfBase = 0x0010000UL;
  86. -static const UTF32 halfMask = 0x3FFUL;
  87. -
  88. -#define UNI_SUR_HIGH_START (UTF32)0xD800
  89. -#define UNI_SUR_HIGH_END (UTF32)0xDBFF
  90. -#define UNI_SUR_LOW_START (UTF32)0xDC00
  91. -#define UNI_SUR_LOW_END (UTF32)0xDFFF
  92. -#define false 0
  93. -#define true 1
  94. -
  95. -/* --------------------------------------------------------------------- */
  96. -
  97. -ConversionResult ConvertUTF32toUTF16 (
  98. - const UTF32** sourceStart, const UTF32* sourceEnd,
  99. - UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
  100. - ConversionResult result = conversionOK;
  101. - const UTF32* source = *sourceStart;
  102. - UTF16* target = *targetStart;
  103. - while (source < sourceEnd) {
  104. - UTF32 ch;
  105. - if (target >= targetEnd) {
  106. - result = targetExhausted; break;
  107. - }
  108. - ch = *source++;
  109. - if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
  110. - /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
  111. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
  112. - if (flags == strictConversion) {
  113. - --source; /* return to the illegal value itself */
  114. - result = sourceIllegal;
  115. - break;
  116. - } else {
  117. - *target++ = UNI_REPLACEMENT_CHAR;
  118. - }
  119. - } else {
  120. - *target++ = (UTF16)ch; /* normal case */
  121. - }
  122. - } else if (ch > UNI_MAX_LEGAL_UTF32) {
  123. - if (flags == strictConversion) {
  124. - result = sourceIllegal;
  125. - } else {
  126. - *target++ = UNI_REPLACEMENT_CHAR;
  127. - }
  128. - } else {
  129. - /* target is a character in range 0xFFFF - 0x10FFFF. */
  130. - if (target + 1 >= targetEnd) {
  131. - --source; /* Back up source pointer! */
  132. - result = targetExhausted; break;
  133. - }
  134. - ch -= halfBase;
  135. - *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
  136. - *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
  137. - }
  138. - }
  139. - *sourceStart = source;
  140. - *targetStart = target;
  141. - return result;
  142. -}
  143. -
  144. -/* --------------------------------------------------------------------- */
  145. -
  146. -ConversionResult ConvertUTF16toUTF32 (
  147. - const UTF16** sourceStart, const UTF16* sourceEnd,
  148. - UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
  149. - ConversionResult result = conversionOK;
  150. - const UTF16* source = *sourceStart;
  151. - UTF32* target = *targetStart;
  152. - UTF32 ch, ch2;
  153. - while (source < sourceEnd) {
  154. - const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
  155. - ch = *source++;
  156. - /* If we have a surrogate pair, convert to UTF32 first. */
  157. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
  158. - /* If the 16 bits following the high surrogate are in the source buffer... */
  159. - if (source < sourceEnd) {
  160. - ch2 = *source;
  161. - /* If it's a low surrogate, convert to UTF32. */
  162. - if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
  163. - ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
  164. - + (ch2 - UNI_SUR_LOW_START) + halfBase;
  165. - ++source;
  166. - } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
  167. - --source; /* return to the illegal value itself */
  168. - result = sourceIllegal;
  169. - break;
  170. - }
  171. - } else { /* We don't have the 16 bits following the high surrogate. */
  172. - --source; /* return to the high surrogate */
  173. - result = sourceExhausted;
  174. - break;
  175. - }
  176. - } else if (flags == strictConversion) {
  177. - /* UTF-16 surrogate values are illegal in UTF-32 */
  178. - if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
  179. - --source; /* return to the illegal value itself */
  180. - result = sourceIllegal;
  181. - break;
  182. - }
  183. - }
  184. - if (target >= targetEnd) {
  185. - source = oldSource; /* Back up source pointer! */
  186. - result = targetExhausted; break;
  187. - }
  188. - *target++ = ch;
  189. - }
  190. - *sourceStart = source;
  191. - *targetStart = target;
  192. -#ifdef CVTUTF_DEBUG
  193. -if (result == sourceIllegal) {
  194. - fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
  195. - fflush(stderr);
  196. -}
  197. -#endif
  198. - return result;
  199. -}
  200. -
  201. -/* --------------------------------------------------------------------- */
  202. -
  203. -/*
  204. - * Index into the table below with the first byte of a UTF-8 sequence to
  205. - * get the number of trailing bytes that are supposed to follow it.
  206. - * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
  207. - * left as-is for anyone who may want to do such conversion, which was
  208. - * allowed in earlier algorithms.
  209. - */
  210. -static const char trailingBytesForUTF8[256] = {
  211. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  212. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  213. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  214. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  215. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  216. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  217. - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  218. - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
  219. -};
  220. -
  221. -/*
  222. - * Magic values subtracted from a buffer value during UTF8 conversion.
  223. - * This table contains as many values as there might be trailing bytes
  224. - * in a UTF-8 sequence.
  225. - */
  226. -static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
  227. - 0x03C82080UL, 0xFA082080UL, 0x82082080UL };
  228. -
  229. -/*
  230. - * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
  231. - * into the first byte, depending on how many bytes follow. There are
  232. - * as many entries in this table as there are UTF-8 sequence types.
  233. - * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
  234. - * for *legal* UTF-8 will be 4 or fewer bytes total.
  235. - */
  236. -static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  237. -
  238. -/* --------------------------------------------------------------------- */
  239. -
  240. -/* The interface converts a whole buffer to avoid function-call overhead.
  241. - * Constants have been gathered. Loops & conditionals have been removed as
  242. - * much as possible for efficiency, in favor of drop-through switches.
  243. - * (See "Note A" at the bottom of the file for equivalent code.)
  244. - * If your compiler supports it, the "isLegalUTF8" call can be turned
  245. - * into an inline function.
  246. - */
  247. -
  248. -/* --------------------------------------------------------------------- */
  249. -
  250. -ConversionResult ConvertUTF16toUTF8 (
  251. - const UTF16** sourceStart, const UTF16* sourceEnd,
  252. - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
  253. - ConversionResult result = conversionOK;
  254. - const UTF16* source = *sourceStart;
  255. - UTF8* target = *targetStart;
  256. - while (source < sourceEnd) {
  257. - UTF32 ch;
  258. - unsigned short bytesToWrite = 0;
  259. - const UTF32 byteMask = 0xBF;
  260. - const UTF32 byteMark = 0x80;
  261. - const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
  262. - ch = *source++;
  263. - /* If we have a surrogate pair, convert to UTF32 first. */
  264. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
  265. - /* If the 16 bits following the high surrogate are in the source buffer... */
  266. - if (source < sourceEnd) {
  267. - UTF32 ch2 = *source;
  268. - /* If it's a low surrogate, convert to UTF32. */
  269. - if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
  270. - ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
  271. - + (ch2 - UNI_SUR_LOW_START) + halfBase;
  272. - ++source;
  273. - } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
  274. - --source; /* return to the illegal value itself */
  275. - result = sourceIllegal;
  276. - break;
  277. - }
  278. - } else { /* We don't have the 16 bits following the high surrogate. */
  279. - --source; /* return to the high surrogate */
  280. - result = sourceExhausted;
  281. - break;
  282. - }
  283. - } else if (flags == strictConversion) {
  284. - /* UTF-16 surrogate values are illegal in UTF-32 */
  285. - if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
  286. - --source; /* return to the illegal value itself */
  287. - result = sourceIllegal;
  288. - break;
  289. - }
  290. - }
  291. - /* Figure out how many bytes the result will require */
  292. - if (ch < (UTF32)0x80) { bytesToWrite = 1;
  293. - } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
  294. - } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
  295. - } else if (ch < (UTF32)0x110000) { bytesToWrite = 4;
  296. - } else { bytesToWrite = 3;
  297. - ch = UNI_REPLACEMENT_CHAR;
  298. - }
  299. -
  300. - target += bytesToWrite;
  301. - if (target > targetEnd) {
  302. - source = oldSource; /* Back up source pointer! */
  303. - target -= bytesToWrite; result = targetExhausted; break;
  304. - }
  305. - switch (bytesToWrite) { /* note: everything falls through. */
  306. - case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  307. - case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  308. - case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  309. - case 1: *--target = (UTF8)(ch | firstByteMark[bytesToWrite]);
  310. - }
  311. - target += bytesToWrite;
  312. - }
  313. - *sourceStart = source;
  314. - *targetStart = target;
  315. - return result;
  316. -}
  317. -
  318. -/* --------------------------------------------------------------------- */
  319. -
  320. -/*
  321. - * Utility routine to tell whether a sequence of bytes is legal UTF-8.
  322. - * This must be called with the length pre-determined by the first byte.
  323. - * If not calling this from ConvertUTF8to*, then the length can be set by:
  324. - * length = trailingBytesForUTF8[*source]+1;
  325. - * and the sequence is illegal right away if there aren't that many bytes
  326. - * available.
  327. - * If presented with a length > 4, this returns false. The Unicode
  328. - * definition of UTF-8 goes up to 4-byte sequences.
  329. - */
  330. -
  331. -static Boolean isLegalUTF8(const UTF8 *source, int length) {
  332. - UTF8 a;
  333. - const UTF8 *srcptr = source+length;
  334. - switch (length) {
  335. - default: return false;
  336. - /* Everything else falls through when "true"... */
  337. - case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
  338. - case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
  339. - case 2: if ((a = (*--srcptr)) > 0xBF) return false;
  340. -
  341. - switch (*source) {
  342. - /* no fall-through in this inner switch */
  343. - case 0xE0: if (a < 0xA0) return false; break;
  344. - case 0xED: if (a > 0x9F) return false; break;
  345. - case 0xF0: if (a < 0x90) return false; break;
  346. - case 0xF4: if (a > 0x8F) return false; break;
  347. - default: if (a < 0x80) return false;
  348. - }
  349. -
  350. - case 1: if (*source >= 0x80 && *source < 0xC2) return false;
  351. - }
  352. - if (*source > 0xF4) return false;
  353. - return true;
  354. -}
  355. -
  356. -/* --------------------------------------------------------------------- */
  357. -
  358. -/*
  359. - * Exported function to return whether a UTF-8 sequence is legal or not.
  360. - * This is not used here; it's just exported.
  361. - */
  362. -Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
  363. - int length = trailingBytesForUTF8[*source]+1;
  364. - if (source+length > sourceEnd) {
  365. - return false;
  366. - }
  367. - return isLegalUTF8(source, length);
  368. -}
  369. -
  370. -/* --------------------------------------------------------------------- */
  371. -
  372. -ConversionResult ConvertUTF8toUTF16 (
  373. - const UTF8** sourceStart, const UTF8* sourceEnd,
  374. - UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
  375. - ConversionResult result = conversionOK;
  376. - const UTF8* source = *sourceStart;
  377. - UTF16* target = *targetStart;
  378. - while (source < sourceEnd) {
  379. - UTF32 ch = 0;
  380. - unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
  381. - if (source + extraBytesToRead >= sourceEnd) {
  382. - result = sourceExhausted; break;
  383. - }
  384. - /* Do this check whether lenient or strict */
  385. - if (! isLegalUTF8(source, extraBytesToRead+1)) {
  386. - result = sourceIllegal;
  387. - break;
  388. - }
  389. - /*
  390. - * The cases all fall through. See "Note A" below.
  391. - */
  392. - switch (extraBytesToRead) {
  393. - case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
  394. - case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
  395. - case 3: ch += *source++; ch <<= 6;
  396. - case 2: ch += *source++; ch <<= 6;
  397. - case 1: ch += *source++; ch <<= 6;
  398. - case 0: ch += *source++;
  399. - }
  400. - ch -= offsetsFromUTF8[extraBytesToRead];
  401. -
  402. - if (target >= targetEnd) {
  403. - source -= (extraBytesToRead+1); /* Back up source pointer! */
  404. - result = targetExhausted; break;
  405. - }
  406. - if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
  407. - /* UTF-16 surrogate values are illegal in UTF-32 */
  408. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
  409. - if (flags == strictConversion) {
  410. - source -= (extraBytesToRead+1); /* return to the illegal value itself */
  411. - result = sourceIllegal;
  412. - break;
  413. - } else {
  414. - *target++ = UNI_REPLACEMENT_CHAR;
  415. - }
  416. - } else {
  417. - *target++ = (UTF16)ch; /* normal case */
  418. - }
  419. - } else if (ch > UNI_MAX_UTF16) {
  420. - if (flags == strictConversion) {
  421. - result = sourceIllegal;
  422. - source -= (extraBytesToRead+1); /* return to the start */
  423. - break; /* Bail out; shouldn't continue */
  424. - } else {
  425. - *target++ = UNI_REPLACEMENT_CHAR;
  426. - }
  427. - } else {
  428. - /* target is a character in range 0xFFFF - 0x10FFFF. */
  429. - if (target + 1 >= targetEnd) {
  430. - source -= (extraBytesToRead+1); /* Back up source pointer! */
  431. - result = targetExhausted; break;
  432. - }
  433. - ch -= halfBase;
  434. - *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
  435. - *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
  436. - }
  437. - }
  438. - *sourceStart = source;
  439. - *targetStart = target;
  440. - return result;
  441. -}
  442. -
  443. -/* --------------------------------------------------------------------- */
  444. -
  445. -ConversionResult ConvertUTF32toUTF8 (
  446. - const UTF32** sourceStart, const UTF32* sourceEnd,
  447. - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
  448. - ConversionResult result = conversionOK;
  449. - const UTF32* source = *sourceStart;
  450. - UTF8* target = *targetStart;
  451. - while (source < sourceEnd) {
  452. - UTF32 ch;
  453. - unsigned short bytesToWrite = 0;
  454. - const UTF32 byteMask = 0xBF;
  455. - const UTF32 byteMark = 0x80;
  456. - ch = *source++;
  457. - if (flags == strictConversion ) {
  458. - /* UTF-16 surrogate values are illegal in UTF-32 */
  459. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
  460. - --source; /* return to the illegal value itself */
  461. - result = sourceIllegal;
  462. - break;
  463. - }
  464. - }
  465. - /*
  466. - * Figure out how many bytes the result will require. Turn any
  467. - * illegally large UTF32 things (> Plane 17) into replacement chars.
  468. - */
  469. - if (ch < (UTF32)0x80) { bytesToWrite = 1;
  470. - } else if (ch < (UTF32)0x800) { bytesToWrite = 2;
  471. - } else if (ch < (UTF32)0x10000) { bytesToWrite = 3;
  472. - } else if (ch <= UNI_MAX_LEGAL_UTF32) { bytesToWrite = 4;
  473. - } else { bytesToWrite = 3;
  474. - ch = UNI_REPLACEMENT_CHAR;
  475. - result = sourceIllegal;
  476. - }
  477. -
  478. - target += bytesToWrite;
  479. - if (target > targetEnd) {
  480. - --source; /* Back up source pointer! */
  481. - target -= bytesToWrite; result = targetExhausted; break;
  482. - }
  483. - switch (bytesToWrite) { /* note: everything falls through. */
  484. - case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  485. - case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  486. - case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
  487. - case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
  488. - }
  489. - target += bytesToWrite;
  490. - }
  491. - *sourceStart = source;
  492. - *targetStart = target;
  493. - return result;
  494. -}
  495. -
  496. -/* --------------------------------------------------------------------- */
  497. -
  498. -ConversionResult ConvertUTF8toUTF32 (
  499. - const UTF8** sourceStart, const UTF8* sourceEnd,
  500. - UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
  501. - ConversionResult result = conversionOK;
  502. - const UTF8* source = *sourceStart;
  503. - UTF32* target = *targetStart;
  504. - while (source < sourceEnd) {
  505. - UTF32 ch = 0;
  506. - unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
  507. - if (source + extraBytesToRead >= sourceEnd) {
  508. - result = sourceExhausted; break;
  509. - }
  510. - /* Do this check whether lenient or strict */
  511. - if (! isLegalUTF8(source, extraBytesToRead+1)) {
  512. - result = sourceIllegal;
  513. - break;
  514. - }
  515. - /*
  516. - * The cases all fall through. See "Note A" below.
  517. - */
  518. - switch (extraBytesToRead) {
  519. - case 5: ch += *source++; ch <<= 6;
  520. - case 4: ch += *source++; ch <<= 6;
  521. - case 3: ch += *source++; ch <<= 6;
  522. - case 2: ch += *source++; ch <<= 6;
  523. - case 1: ch += *source++; ch <<= 6;
  524. - case 0: ch += *source++;
  525. - }
  526. - ch -= offsetsFromUTF8[extraBytesToRead];
  527. -
  528. - if (target >= targetEnd) {
  529. - source -= (extraBytesToRead+1); /* Back up the source pointer! */
  530. - result = targetExhausted; break;
  531. - }
  532. - if (ch <= UNI_MAX_LEGAL_UTF32) {
  533. - /*
  534. - * UTF-16 surrogate values are illegal in UTF-32, and anything
  535. - * over Plane 17 (> 0x10FFFF) is illegal.
  536. - */
  537. - if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
  538. - if (flags == strictConversion) {
  539. - source -= (extraBytesToRead+1); /* return to the illegal value itself */
  540. - result = sourceIllegal;
  541. - break;
  542. - } else {
  543. - *target++ = UNI_REPLACEMENT_CHAR;
  544. - }
  545. - } else {
  546. - *target++ = ch;
  547. - }
  548. - } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
  549. - result = sourceIllegal;
  550. - *target++ = UNI_REPLACEMENT_CHAR;
  551. - }
  552. - }
  553. - *sourceStart = source;
  554. - *targetStart = target;
  555. - return result;
  556. -}
  557. -
  558. -/* ---------------------------------------------------------------------
  559. -
  560. - Note A.
  561. - The fall-through switches in UTF-8 reading code save a
  562. - temp variable, some decrements & conditionals. The switches
  563. - are equivalent to the following loop:
  564. - {
  565. - int tmpBytesToRead = extraBytesToRead+1;
  566. - do {
  567. - ch += *source++;
  568. - --tmpBytesToRead;
  569. - if (tmpBytesToRead) ch <<= 6;
  570. - } while (tmpBytesToRead > 0);
  571. - }
  572. - In UTF-8 writing code, the switches on "bytesToWrite" are
  573. - similarly unrolled loops.
  574. -
  575. - --------------------------------------------------------------------- */
  576. diff --git a/base/ConvertUTF.h b/base/ConvertUTF.h
  577. deleted file mode 100644
  578. index 538bec6..0000000
  579. --- a/base/ConvertUTF.h
  580. +++ /dev/null
  581. @@ -1,155 +0,0 @@
  582. -/*
  583. - * Copyright 2001-2004 Unicode, Inc.
  584. - *
  585. - * Disclaimer
  586. - *
  587. - * This source code is provided as is by Unicode, Inc. No claims are
  588. - * made as to fitness for any particular purpose. No warranties of any
  589. - * kind are expressed or implied. The recipient agrees to determine
  590. - * applicability of information provided. If this file has been
  591. - * purchased on magnetic or optical media from Unicode, Inc., the
  592. - * sole remedy for any claim will be exchange of defective media
  593. - * within 90 days of receipt.
  594. - *
  595. - * Limitations on Rights to Redistribute This Code
  596. - *
  597. - * Unicode, Inc. hereby grants the right to freely use the information
  598. - * supplied in this file in the creation of products supporting the
  599. - * Unicode Standard, and to make copies of this file in any form
  600. - * for internal or external distribution as long as this notice
  601. - * remains attached.
  602. - */
  603. -
  604. -
  605. -#ifndef ConvertUTF_INCLUDED
  606. -#define ConvertUTF_INCLUDED
  607. -
  608. -/* ---------------------------------------------------------------------
  609. -
  610. - Conversions between UTF32, UTF-16, and UTF-8. Header file.
  611. -
  612. - Several funtions are included here, forming a complete set of
  613. - conversions between the three formats. UTF-7 is not included
  614. - here, but is handled in a separate source file.
  615. -
  616. - Each of these routines takes pointers to input buffers and output
  617. - buffers. The input buffers are const.
  618. -
  619. - Each routine converts the text between *sourceStart and sourceEnd,
  620. - putting the result into the buffer between *targetStart and
  621. - targetEnd. Note: the end pointers are *after* the last item: e.g.
  622. - *(sourceEnd - 1) is the last item.
  623. -
  624. - The return result indicates whether the conversion was successful,
  625. - and if not, whether the problem was in the source or target buffers.
  626. - (Only the first encountered problem is indicated.)
  627. -
  628. - After the conversion, *sourceStart and *targetStart are both
  629. - updated to point to the end of last text successfully converted in
  630. - the respective buffers.
  631. -
  632. - Input parameters:
  633. - sourceStart - pointer to a pointer to the source buffer.
  634. - The contents of this are modified on return so that
  635. - it points at the next thing to be converted.
  636. - targetStart - similarly, pointer to pointer to the target buffer.
  637. - sourceEnd, targetEnd - respectively pointers to the ends of the
  638. - two buffers, for overflow checking only.
  639. -
  640. - These conversion functions take a ConversionFlags argument. When this
  641. - flag is set to strict, both irregular sequences and isolated surrogates
  642. - will cause an error. When the flag is set to lenient, both irregular
  643. - sequences and isolated surrogates are converted.
  644. -
  645. - Whether the flag is strict or lenient, all illegal sequences will cause
  646. - an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
  647. - or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
  648. - must check for illegal sequences.
  649. -
  650. - When the flag is set to lenient, characters over 0x10FFFF are converted
  651. - to the replacement character; otherwise (when the flag is set to strict)
  652. - they constitute an error.
  653. -
  654. - Output parameters:
  655. - The value "sourceIllegal" is returned from some routines if the input
  656. - sequence is malformed. When "sourceIllegal" is returned, the source
  657. - value will point to the illegal value that caused the problem. E.g.,
  658. - in UTF-8 when a sequence is malformed, it points to the start of the
  659. - malformed sequence.
  660. -
  661. - Author: Mark E. Davis, 1994.
  662. - Rev History: Rick McGowan, fixes & updates May 2001.
  663. - Fixes & updates, Sept 2001.
  664. -
  665. ------------------------------------------------------------------------- */
  666. -
  667. -/* ---------------------------------------------------------------------
  668. - The following 4 definitions are compiler-specific.
  669. - The C standard does not guarantee that wchar_t has at least
  670. - 16 bits, so wchar_t is no less portable than unsigned short!
  671. - All should be unsigned values to avoid sign extension during
  672. - bit mask & shift operations.
  673. ------------------------------------------------------------------------- */
  674. -
  675. -typedef unsigned long UTF32; /* at least 32 bits */
  676. -typedef unsigned short UTF16; /* at least 16 bits */
  677. -typedef unsigned char UTF8; /* typically 8 bits */
  678. -typedef unsigned char Boolean; /* 0 or 1 */
  679. -
  680. -/* Some fundamental constants */
  681. -#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
  682. -#define UNI_MAX_BMP (UTF32)0x0000FFFF
  683. -#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
  684. -#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
  685. -#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
  686. -
  687. -typedef enum {
  688. - conversionOK, /* conversion successful */
  689. - sourceExhausted, /* partial character in source, but hit end */
  690. - targetExhausted, /* insuff. room in target for conversion */
  691. - sourceIllegal /* source sequence is illegal/malformed */
  692. -} ConversionResult;
  693. -
  694. -typedef enum {
  695. - strictConversion = 0,
  696. - lenientConversion
  697. -} ConversionFlags;
  698. -
  699. -/* This is for C++ and does no harm in C */
  700. -#ifdef __cplusplus
  701. -extern "C" {
  702. -#endif
  703. -
  704. -ConversionResult ConvertUTF8toUTF16 (
  705. - const UTF8** sourceStart, const UTF8* sourceEnd,
  706. - UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  707. -
  708. -ConversionResult ConvertUTF16toUTF8 (
  709. - const UTF16** sourceStart, const UTF16* sourceEnd,
  710. - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  711. -
  712. -ConversionResult ConvertUTF8toUTF32 (
  713. - const UTF8** sourceStart, const UTF8* sourceEnd,
  714. - UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  715. -
  716. -ConversionResult ConvertUTF32toUTF8 (
  717. - const UTF32** sourceStart, const UTF32* sourceEnd,
  718. - UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  719. -
  720. -ConversionResult ConvertUTF16toUTF32 (
  721. - const UTF16** sourceStart, const UTF16* sourceEnd,
  722. - UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  723. -
  724. -ConversionResult ConvertUTF32toUTF16 (
  725. - const UTF32** sourceStart, const UTF32* sourceEnd,
  726. - UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  727. -
  728. -Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
  729. -
  730. -#ifdef __cplusplus
  731. -}
  732. -#endif
  733. -
  734. -/* --------------------------------------------------------------------- */
  735. -
  736. -#endif /* ConvertUTF_INCLUDED */
  737. diff --git a/base/lib.mak b/base/lib.mak
  738. index 173e2c6..2de6565 100644
  739. --- a/base/lib.mak
  740. +++ b/base/lib.mak
  741. @@ -52,7 +52,6 @@ GLLCMS2CC=$(CC) $(LCMS2_CFLAGS) $(CFLAGS) $(I_)$(GLI_) $(II)$(LCMS2SRCDIR)$(D)in
  742. lcms2_h=$(LCMS2SRCDIR)$(D)include$(D)lcms2.h
  743. lcms2_plugin_h=$(LCMS2SRCDIR)$(D)include$(D)lcms2_plugin.h
  744. -ConvertUTF_h=$(GLSRC)ConvertUTF.h
  745. gdevdcrd_h=$(GLSRC)gdevdcrd.h
  746. gdevpccm_h=$(GLSRC)gdevpccm.h
  747. @@ -1097,9 +1096,6 @@ $(GLOBJ)gdevpccm.$(OBJ) : $(GLSRC)gdevpccm.c $(AK)\
  748. $(gx_h) $(gsmatrix_h) $(gxdevice_h) $(gdevpccm_h) $(LIB_MAK) $(MAKEDIRS)
  749. $(GLCC) $(GLO_)gdevpccm.$(OBJ) $(C_) $(GLSRC)gdevpccm.c
  750. -$(GLOBJ)ConvertUTF.$(OBJ) : $(GLSRC)ConvertUTF.c $(ConvertUTF_h) $(LIB_MAK) $(MAKEDIRS)
  751. - $(GLCC) $(GLO_)ConvertUTF.$(OBJ) $(C_) $(GLSRC)ConvertUTF.c
  752. -
  753. ### Memory devices
  754. $(GLOBJ)gdevmem.$(OBJ) : $(GLSRC)gdevmem.c $(AK) $(gx_h) $(gserrors_h) \
  755. diff --git a/devices/devs.mak b/devices/devs.mak
  756. index ea27ab0..51ec363 100644
  757. --- a/devices/devs.mak
  758. +++ b/devices/devs.mak
  759. @@ -835,9 +835,8 @@ pdfwrite5_=$(DEVOBJ)gdevpdfm.$(OBJ)
  760. pdfwrite6_=$(DEVOBJ)gdevpdfo.$(OBJ) $(DEVOBJ)gdevpdfp.$(OBJ) $(DEVOBJ)gdevpdft.$(OBJ)
  761. pdfwrite7_=$(DEVOBJ)gdevpdfr.$(OBJ)
  762. pdfwrite8_=$(DEVOBJ)gdevpdfu.$(OBJ) $(DEVOBJ)gdevpdfv.$(OBJ) $(DEVOBJ)gdevagl.$(OBJ)
  763. -pdfwrite9_= $(GLOBJ)ConvertUTF.$(OBJ)
  764. -pdfwrite10_=$(DEVOBJ)gsflip.$(OBJ)
  765. -pdfwrite11_=$(DEVOBJ)scantab.$(OBJ) $(DEVOBJ)sfilter2.$(OBJ)
  766. +pdfwrite9_=$(DEVOBJ)gsflip.$(OBJ)
  767. +pdfwrite10_=$(DEVOBJ)scantab.$(OBJ) $(DEVOBJ)sfilter2.$(OBJ)
  768. pdfwrite_=$(pdfwrite1_) $(pdfwrite2_) $(pdfwrite3_) $(pdfwrite4_)\
  769. $(pdfwrite5_) $(pdfwrite6_) $(pdfwrite7_) $(pdfwrite8_) $(pdfwrite9_)\
  770. $(pdfwrite10_) $(pdfwrite11_)
  771. diff --git a/devices/vector/gdevpdf.c b/devices/vector/gdevpdf.c
  772. index 2b3186d..20e0ae8 100644
  773. --- a/devices/vector/gdevpdf.c
  774. +++ b/devices/vector/gdevpdf.c
  775. @@ -111,14 +111,13 @@ ENUM_PTRS_WITH(device_pdfwrite_enum_ptrs, gx_device_pdf *pdev)
  776. ENUM_PTR(32, gx_device_pdf, pres_soft_mask_dict);
  777. ENUM_PTR(33, gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data);
  778. ENUM_PTR(34, gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data);
  779. - ENUM_PTR(35, gx_device_pdf, DSCEncodingToUnicode.data);
  780. - ENUM_PTR(36, gx_device_pdf, Identity_ToUnicode_CMaps[0]);
  781. - ENUM_PTR(37, gx_device_pdf, Identity_ToUnicode_CMaps[1]);
  782. - ENUM_PTR(38, gx_device_pdf, vgstack);
  783. - ENUM_PTR(39, gx_device_pdf, outline_levels);
  784. - ENUM_PTR(40, gx_device_pdf, EmbeddedFiles);
  785. - ENUM_PTR(41, gx_device_pdf, pdf_font_dir);
  786. - ENUM_PTR(42, gx_device_pdf, ExtensionMetadata);
  787. + ENUM_PTR(35, gx_device_pdf, Identity_ToUnicode_CMaps[0]);
  788. + ENUM_PTR(36, gx_device_pdf, Identity_ToUnicode_CMaps[1]);
  789. + ENUM_PTR(37, gx_device_pdf, vgstack);
  790. + ENUM_PTR(38, gx_device_pdf, outline_levels);
  791. + ENUM_PTR(39, gx_device_pdf, EmbeddedFiles);
  792. + ENUM_PTR(40, gx_device_pdf, pdf_font_dir);
  793. + ENUM_PTR(41, gx_device_pdf, ExtensionMetadata);
  794. #define e1(i,elt) ENUM_PARAM_STRING_PTR(i + gx_device_pdf_num_ptrs, gx_device_pdf, elt);
  795. gx_device_pdf_do_param_strings(e1)
  796. #undef e1
  797. @@ -165,7 +164,6 @@ static RELOC_PTRS_WITH(device_pdfwrite_reloc_ptrs, gx_device_pdf *pdev)
  798. RELOC_PTR(gx_device_pdf, pres_soft_mask_dict);
  799. RELOC_PTR(gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data);
  800. RELOC_PTR(gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data);
  801. - RELOC_PTR(gx_device_pdf, DSCEncodingToUnicode.data);
  802. RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[0]);
  803. RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[1]);
  804. RELOC_PTR(gx_device_pdf, vgstack);
  805. diff --git a/devices/vector/gdevpdfb.h b/devices/vector/gdevpdfb.h
  806. index 08f18c5..447f0f5 100644
  807. --- a/devices/vector/gdevpdfb.h
  808. +++ b/devices/vector/gdevpdfb.h
  809. @@ -141,7 +141,6 @@ const gx_device_pdf PDF_DEVICE_IDENT =
  810. 12000, /* MaxClipPathSize */ /* HP LaserJet 1320 hangs with 14000. */
  811. 256000, /* MaxShadingBitmapSize */
  812. PDF_DEVICE_MaxInlineImageSize, /* MaxInlineImageSize */
  813. - {0, 0}, /* DSCEncodingToUnicode */
  814. {0, 0, 0}, /* OwnerPassword */
  815. {0, 0, 0}, /* UserPassword */
  816. 0, /* KeyLength */
  817. diff --git a/devices/vector/gdevpdfe.c b/devices/vector/gdevpdfe.c
  818. index 1aa1f25..f23a02d 100644
  819. --- a/devices/vector/gdevpdfe.c
  820. +++ b/devices/vector/gdevpdfe.c
  821. @@ -26,7 +26,6 @@
  822. #include "gdevpdfx.h"
  823. #include "gdevpdfg.h"
  824. #include "gdevpdfo.h"
  825. -#include "ConvertUTF.h"
  826. char PDFDocEncodingLookup [92] = {
  827. 0x20, 0x22, 0x20, 0x20, 0x20, 0x21, 0x20, 0x26,
  828. @@ -343,155 +342,162 @@ decode_escape(const byte *data, int data_length, int *index)
  829. return c; /* A wrong escapement sequence. */
  830. }
  831. -static int
  832. -pdf_xmp_write_translated(gx_device_pdf *pdev, stream *s, const byte *data, int data_length,
  833. - void(*write)(stream *s, const byte *data, int data_length))
  834. +/*
  835. + * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
  836. + * into the first byte, depending on how many bytes follow. There are
  837. + * as many entries in this table as there are UTF-8 sequence types.
  838. + * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
  839. + * for *legal* UTF-8 will be 4 or fewer bytes total.
  840. + */
  841. +static const char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  842. +
  843. +static int gs_ConvertUTF16(char *UTF16, int UTF16Len, unsigned char **UTF8Start, int UTF8Len)
  844. {
  845. - if (pdev->DSCEncodingToUnicode.data == 0) {
  846. - int i, j=0;
  847. - unsigned char *buf0;
  848. + int i, bytes = 0;
  849. + short U16;
  850. + unsigned char *UTF8 = *UTF8Start;
  851. + unsigned char *UTF8End = UTF8 + UTF8Len;
  852. - buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, data_length * sizeof(unsigned char),
  853. - "pdf_xmp_write_translated");
  854. - if (buf0 == NULL)
  855. - return_error(gs_error_VMerror);
  856. - for (i = 0; i < data_length; i++) {
  857. - byte c = data[i];
  858. + if (fabs(UTF16Len % sizeof(short)) != 0)
  859. + return gs_note_error(gs_error_rangecheck);
  860. +
  861. + for (i=0;i<UTF16Len / sizeof(short);i++)
  862. + {
  863. + U16 = (*UTF16++) << 8;
  864. + U16 += *UTF16++;
  865. - if (c == '\\')
  866. - c = decode_escape(data, data_length, &i);
  867. - buf0[j] = c;
  868. - j++;
  869. + if (U16 >= 0xD800 && U16 <= 0xDBFF) {
  870. + return gs_note_error(gs_error_rangecheck);
  871. }
  872. - if (buf0[0] != 0xfe || buf0[1] != 0xff) {
  873. - unsigned char *buf1;
  874. - /* We must assume that the information is PDFDocEncoding. In this case
  875. - * we need to convert it into UTF-8. If we just convert it to UTF-16
  876. - * then we can safely fall through to the code below.
  877. - */
  878. - /* NB the code below skips the BOM in positions 0 and 1, so we need
  879. - * two extra bytes, to be ignored.
  880. - */
  881. - buf1 = (unsigned char *)gs_alloc_bytes(pdev->memory, (j * sizeof(UTF16)) + 2,
  882. - "pdf_xmp_write_translated");
  883. - if (buf1 == NULL) {
  884. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  885. - return_error(gs_error_VMerror);
  886. - }
  887. - memset(buf1, 0x00, (j * sizeof(UTF16)) + 2);
  888. - for (i = 0; i < j; i++) {
  889. - if (buf0[i] <= 0x7f || buf0[i] >= 0xAE) {
  890. - if (buf0[i] == 0x7f) {
  891. - emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
  892. - buf0[i]);
  893. - } else
  894. - buf1[(i * 2) + 3] = buf0[i];
  895. + if (U16 >= 0xDC00 && U16 <= 0xDFFF) {
  896. + return gs_note_error(gs_error_rangecheck);
  897. + }
  898. +
  899. + if(U16 < 0x80) {
  900. + bytes = 1;
  901. + } else {
  902. + if (U16 < 0x800) {
  903. + bytes = 2;
  904. + } else {
  905. + if (U16 < 0x10000) {
  906. + bytes = 3;
  907. } else {
  908. - buf1[(i * 2) + 2] = PDFDocEncodingLookup[(buf0[i] - 0x80) * 2];
  909. - buf1[(i * 2) + 3] = PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1];
  910. - if (PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1] == 0x00)
  911. - emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
  912. - PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1]);
  913. + if (U16 < 0x111000) {
  914. + bytes = 4;
  915. + } else {
  916. + bytes = 3;
  917. + U16 = 0xFFFD;
  918. + }
  919. }
  920. }
  921. + }
  922. + if (UTF8 + bytes > UTF8End)
  923. + return gs_note_error(gs_error_VMerror);
  924. +
  925. + /* Write from end to beginning, low bytes first */
  926. + UTF8 += bytes;
  927. +
  928. + switch(bytes) {
  929. + case 4:
  930. + *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
  931. + U16 >>= 6;
  932. + case 3:
  933. + *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
  934. + U16 >>= 6;
  935. + case 2:
  936. + *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
  937. + U16 >>= 6;
  938. + case 1:
  939. + *--UTF8 = (unsigned char)(U16 | firstByteMark[bytes]);
  940. + break;
  941. + default:
  942. + return gs_note_error(gs_error_rangecheck);
  943. + }
  944. +
  945. + /* Move to start of next set */
  946. + UTF8 += bytes;
  947. + }
  948. + *UTF8Start = UTF8;
  949. + return 0;
  950. +}
  951. +
  952. +static int
  953. +pdf_xmp_write_translated(gx_device_pdf *pdev, stream *s, const byte *data, int data_length,
  954. + void(*write)(stream *s, const byte *data, int data_length))
  955. +{
  956. + int i, j=0;
  957. + unsigned char *buf0;
  958. +
  959. + buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, data_length * sizeof(unsigned char),
  960. + "pdf_xmp_write_translated");
  961. + if (buf0 == NULL)
  962. + return_error(gs_error_VMerror);
  963. + for (i = 0; i < data_length; i++) {
  964. + byte c = data[i];
  965. +
  966. + if (c == '\\')
  967. + c = decode_escape(data, data_length, &i);
  968. + buf0[j] = c;
  969. + j++;
  970. + }
  971. + if (buf0[0] != 0xfe || buf0[1] != 0xff) {
  972. + unsigned char *buf1;
  973. + /* We must assume that the information is PDFDocEncoding. In this case
  974. + * we need to convert it into UTF-8. If we just convert it to UTF-16
  975. + * then we can safely fall through to the code below.
  976. + */
  977. + /* NB the code below skips the BOM in positions 0 and 1, so we need
  978. + * two extra bytes, to be ignored.
  979. + */
  980. + buf1 = (unsigned char *)gs_alloc_bytes(pdev->memory, (j * sizeof(short)) + 2,
  981. + "pdf_xmp_write_translated");
  982. + if (buf1 == NULL) {
  983. gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  984. - buf0 = buf1;
  985. - data_length = j = (j * 2) + 2;
  986. + return_error(gs_error_VMerror);
  987. }
  988. - {
  989. - /* Its a Unicode (UTF-16BE) string, convert to UTF-8 */
  990. - UTF16 *buf0b, U16;
  991. - UTF8 *buf1, *buf1b;
  992. -
  993. - /* A single UTF-16 (2 bytes) can end up as 4 bytes in UTF-8 */
  994. - buf1 = (UTF8 *)gs_alloc_bytes(pdev->memory, data_length * 2 * sizeof(unsigned char),
  995. - "pdf_xmp_write_translated");
  996. - if (buf1 == NULL) {
  997. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  998. - return_error(gs_error_VMerror);
  999. - }
  1000. - buf1b = buf1;
  1001. - /* Skip the Byte Order Mark (0xfe 0xff) */
  1002. - buf0b = (UTF16 *)(buf0 + 2);
  1003. - /* ConvertUTF16to UTF8 expects a buffer of UTF16s in the local
  1004. - * endian-ness, but the data is big-endian. In case this is a little-endian
  1005. - * machine, process the buffer from big-endian to whatever is right for this platform.
  1006. - */
  1007. - for (i = 2; i < j; i+=2) {
  1008. - U16 = (buf0[i] << 8) + buf0[i + 1];
  1009. - *(buf0b++) = U16;
  1010. - }
  1011. - buf0b = (UTF16 *)(buf0 + 2);
  1012. - switch (ConvertUTF16toUTF8((const UTF16**)&buf0b, (UTF16 *)(buf0 + j),
  1013. - &buf1b, buf1 + (data_length * 2 * sizeof(unsigned char)), strictConversion)) {
  1014. - case conversionOK:
  1015. - write(s, buf1, buf1b - buf1);
  1016. - gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
  1017. - break;
  1018. - case sourceExhausted:
  1019. - case targetExhausted:
  1020. - case sourceIllegal:
  1021. - default:
  1022. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1023. - gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
  1024. - return_error(gs_error_rangecheck);
  1025. + memset(buf1, 0x00, (j * sizeof(short)) + 2);
  1026. + for (i = 0; i < j; i++) {
  1027. + if (buf0[i] <= 0x7f || buf0[i] >= 0xAE) {
  1028. + if (buf0[i] == 0x7f) {
  1029. + emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
  1030. + buf0[i]);
  1031. + } else
  1032. + buf1[(i * 2) + 3] = buf0[i];
  1033. + } else {
  1034. + buf1[(i * 2) + 2] = PDFDocEncodingLookup[(buf0[i] - 0x80) * 2];
  1035. + buf1[(i * 2) + 3] = PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1];
  1036. + if (PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1] == 0x00)
  1037. + emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
  1038. + PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1]);
  1039. }
  1040. }
  1041. gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1042. - return 0;
  1043. - } else {
  1044. - UTF16 *buf0;
  1045. - const UTF16 *buf0b;
  1046. - UTF8 *buf1, *buf1b;
  1047. - int i, j = 0;
  1048. -
  1049. - buf0 = (UTF16 *)gs_alloc_bytes(pdev->memory, data_length * sizeof(UTF16),
  1050. - "pdf_xmp_write_translated");
  1051. - if (buf0 == NULL)
  1052. - return_error(gs_error_VMerror);
  1053. - buf1 = (UTF8 *)gs_alloc_bytes(pdev->memory, data_length * 2,
  1054. - "pdf_xmp_write_translated");
  1055. + buf0 = buf1;
  1056. + data_length = j = (j * 2) + 2;
  1057. + }
  1058. + {
  1059. + /* Its a Unicode (UTF-16BE) string, convert to UTF-8 */
  1060. + short *buf0b;
  1061. + char *buf1, *buf1b;
  1062. + int code;
  1063. +
  1064. + /* A single UTF-16 (2 bytes) can end up as 4 bytes in UTF-8 */
  1065. + buf1 = (char *)gs_alloc_bytes(pdev->memory, data_length * 2 * sizeof(unsigned char),
  1066. + "pdf_xmp_write_translated");
  1067. if (buf1 == NULL) {
  1068. gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1069. return_error(gs_error_VMerror);
  1070. }
  1071. - buf0b = buf0;
  1072. buf1b = buf1;
  1073. - for (i = 0; i < data_length; i++) {
  1074. - byte c = data[i];
  1075. - int v;
  1076. -
  1077. - if (c == '\\')
  1078. - c = decode_escape(data, data_length, &i);
  1079. - if (c > pdev->DSCEncodingToUnicode.size) {
  1080. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1081. - gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
  1082. - return_error(gs_error_rangecheck);
  1083. - }
  1084. -
  1085. - v = pdev->DSCEncodingToUnicode.data[c];
  1086. - if (v == -1)
  1087. - v = '?'; /* Arbitrary. */
  1088. - buf0[j] = v;
  1089. - j++;
  1090. - }
  1091. - switch (ConvertUTF16toUTF8(&buf0b, buf0 + j,
  1092. - &buf1b, buf1 + data_length * 2, strictConversion)) {
  1093. - case conversionOK:
  1094. - write(s, buf1, buf1b - buf1);
  1095. - break;
  1096. - case sourceExhausted:
  1097. - case targetExhausted:
  1098. - case sourceIllegal:
  1099. - default:
  1100. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1101. - gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
  1102. - return_error(gs_error_rangecheck);
  1103. - }
  1104. - gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1105. - gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
  1106. - return 0;
  1107. + /* Skip the Byte Order Mark (0xfe 0xff) */
  1108. + buf0b = (short *)(buf0 + 2);
  1109. + code = gs_ConvertUTF16((char *)buf0b, j - 2, (unsigned char **)&buf1b, data_length * 2 * sizeof(unsigned char));
  1110. + if (code < 0)
  1111. + return code;
  1112. + write(s, (const byte *)buf1, buf1b - buf1);
  1113. }
  1114. + gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
  1115. + return 0;
  1116. }
  1117. static int
  1118. diff --git a/devices/vector/gdevpdfp.c b/devices/vector/gdevpdfp.c
  1119. index 0fa07e3..6ebcb0d 100644
  1120. --- a/devices/vector/gdevpdfp.c
  1121. +++ b/devices/vector/gdevpdfp.c
  1122. @@ -77,7 +77,6 @@ static const gs_param_item_t pdf_param_items[] = {
  1123. pi("CompressStreams", gs_param_type_bool, CompressStreams),
  1124. pi("PrintStatistics", gs_param_type_bool, PrintStatistics),
  1125. pi("MaxInlineImageSize", gs_param_type_long, MaxInlineImageSize),
  1126. - pi("DSCEncodingToUnicode", gs_param_type_int_array, DSCEncodingToUnicode),
  1127. /* PDF Encryption */
  1128. pi("OwnerPassword", gs_param_type_string, OwnerPassword),
  1129. diff --git a/devices/vector/gdevpdfx.h b/devices/vector/gdevpdfx.h
  1130. index 308900a..c436220 100644
  1131. --- a/devices/vector/gdevpdfx.h
  1132. +++ b/devices/vector/gdevpdfx.h
  1133. @@ -601,7 +601,6 @@ struct gx_device_pdf_s {
  1134. a bitmap representation of a shading.
  1135. (Bigger shadings to be downsampled). */
  1136. long MaxInlineImageSize;
  1137. - gs_param_int_array DSCEncodingToUnicode;
  1138. /* Encryption parameters */
  1139. gs_param_string OwnerPassword;
  1140. gs_param_string UserPassword;
  1141. @@ -911,14 +910,14 @@ struct gx_device_pdf_s {
  1142. m(28,sbstack) m(29,substream_Resources) m(30,font3)\
  1143. m(31,accumulating_substream_resource) \
  1144. m(32,pres_soft_mask_dict) m(33,PDFXTrimBoxToMediaBoxOffset.data)\
  1145. - m(34,PDFXBleedBoxToTrimBoxOffset.data) m(35, DSCEncodingToUnicode.data)\
  1146. - m(36,Identity_ToUnicode_CMaps[0]) m(37,Identity_ToUnicode_CMaps[1])\
  1147. - m(38,vgstack)\
  1148. - m(39, outline_levels)
  1149. - m(40, gx_device_pdf, EmbeddedFiles);
  1150. - m(41, gx_device_pdf, pdf_font_dir);
  1151. - m(42, gx_device_pdf, Extension_Metadata);*/
  1152. -#define gx_device_pdf_num_ptrs 43
  1153. + m(34,PDFXBleedBoxToTrimBoxOffset.data)
  1154. + m(35,Identity_ToUnicode_CMaps[0]) m(36,Identity_ToUnicode_CMaps[1])\
  1155. + m(37,vgstack)\
  1156. + m(38, outline_levels)
  1157. + m(39, gx_device_pdf, EmbeddedFiles);
  1158. + m(40, gx_device_pdf, pdf_font_dir);
  1159. + m(41, gx_device_pdf, Extension_Metadata);*/
  1160. +#define gx_device_pdf_num_ptrs 42
  1161. #define gx_device_pdf_do_param_strings(m)\
  1162. m(0, OwnerPassword) m(1, UserPassword) m(2, NoEncrypt)\
  1163. m(3, DocumentUUID) m(4, InstanceUUID)
  1164. diff --git a/windows/ghostscript.vcproj b/windows/ghostscript.vcproj
  1165. index a96d317..450cb26 100644
  1166. --- a/windows/ghostscript.vcproj
  1167. +++ b/windows/ghostscript.vcproj
  1168. @@ -1794,10 +1794,6 @@
  1169. >
  1170. </File>
  1171. <File
  1172. - RelativePath="..\base\ConvertUTF.c"
  1173. - >
  1174. - </File>
  1175. - <File
  1176. RelativePath="..\base\echogs.c"
  1177. >
  1178. </File>
  1179. @@ -3330,10 +3326,6 @@
  1180. >
  1181. </File>
  1182. <File
  1183. - RelativePath="..\base\ConvertUTF.h"
  1184. - >
  1185. - </File>
  1186. - <File
  1187. RelativePath="..\base\ctype_.h"
  1188. >
  1189. </File>
  1190. diff --git a/windows/ghostscript_rt.vcxproj b/windows/ghostscript_rt.vcxproj
  1191. index 2348f08..fae2e1f 100644
  1192. --- a/windows/ghostscript_rt.vcxproj
  1193. +++ b/windows/ghostscript_rt.vcxproj
  1194. @@ -427,7 +427,6 @@
  1195. <ItemGroup>
  1196. <ClCompile Include="..\base\aes.c" />
  1197. <ClCompile Include="..\base\bench.c" />
  1198. - <ClCompile Include="..\base\ConvertUTF.c" />
  1199. <ClCompile Include="..\base\echogs.c" />
  1200. <ClCompile Include="..\base\gconf.c" />
  1201. <ClCompile Include="..\base\genarch.c" />
  1202. @@ -1689,7 +1688,6 @@
  1203. <ClInclude Include="..\jasper\src\libjasper\ras\ras_enc.h" />
  1204. <ClInclude Include="..\base\aes.h" />
  1205. <ClInclude Include="..\base\assert_.h" />
  1206. - <ClInclude Include="..\base\ConvertUTF.h" />
  1207. <ClInclude Include="..\base\ctype_.h" />
  1208. <ClInclude Include="..\base\dirent_.h" />
  1209. <ClInclude Include="..\base\dos_.h" />
  1210. --
  1211. 2.9.3