254a997
From 273a133110838ee5702e7eb6409a853c598211b2 Mon Sep 17 00:00:00 2001
254a997
From: Ken Sharp <ken.sharp@artifex.com>
254a997
Date: Thu, 29 Sep 2016 17:35:05 +0100
254a997
Subject: [PATCH] Remove (and re-implement) ConvertUTF.c
254a997
254a997
Bug #697122 " embedded ConvertUTF.c is buggy and licensed incompatibly with GPL/APGL"
254a997
254a997
Its not clear that this code is incompatible with GPL, nor do we think
254a997
any 'bugginess' in the code affects us, since we are using a comparatively
254a997
small part of the included code.
254a997
254a997
Nevertheless its possible to remove the code, and re-implement the small
254a997
part we actually need, and that is done here.
254a997
254a997
Also removed the DSCEncodingToUnicode option which was insanely difficult
254a997
to use, and incorrectly documented.
254a997
254a997
Yhis shows one difference, 692486_-_heap_overflow_in_pdf_to_ucs2.pdf
254a997
now correctly throws an error, because the PDF file contains document
254a997
information (Application) which has an invalid UTF16-BE sequence.
254a997
---
254a997
 base/ConvertUTF.c              | 539 -----------------------------------------
254a997
 base/ConvertUTF.h              | 155 ------------
254a997
 base/lib.mak                   |   4 -
254a997
 devices/devs.mak               |   5 +-
254a997
 devices/vector/gdevpdf.c       |  16 +-
254a997
 devices/vector/gdevpdfb.h      |   1 -
254a997
 devices/vector/gdevpdfe.c      | 270 +++++++++++----------
254a997
 devices/vector/gdevpdfp.c      |   1 -
254a997
 devices/vector/gdevpdfx.h      |  17 +-
254a997
 windows/ghostscript.vcproj     |   8 -
254a997
 windows/ghostscript_rt.vcxproj |   2 -
254a997
 11 files changed, 155 insertions(+), 863 deletions(-)
254a997
 delete mode 100644 base/ConvertUTF.c
254a997
 delete mode 100644 base/ConvertUTF.h
254a997
254a997
diff --git a/base/ConvertUTF.c b/base/ConvertUTF.c
254a997
deleted file mode 100644
254a997
index cb2e2de..0000000
254a997
--- a/base/ConvertUTF.c
254a997
+++ /dev/null
254a997
@@ -1,539 +0,0 @@
254a997
-/*
254a997
- * Copyright 2001-2004 Unicode, Inc.
254a997
- *
254a997
- * Disclaimer
254a997
- *
254a997
- * This source code is provided as is by Unicode, Inc. No claims are
254a997
- * made as to fitness for any particular purpose. No warranties of any
254a997
- * kind are expressed or implied. The recipient agrees to determine
254a997
- * applicability of information provided. If this file has been
254a997
- * purchased on magnetic or optical media from Unicode, Inc., the
254a997
- * sole remedy for any claim will be exchange of defective media
254a997
- * within 90 days of receipt.
254a997
- *
254a997
- * Limitations on Rights to Redistribute This Code
254a997
- *
254a997
- * Unicode, Inc. hereby grants the right to freely use the information
254a997
- * supplied in this file in the creation of products supporting the
254a997
- * Unicode Standard, and to make copies of this file in any form
254a997
- * for internal or external distribution as long as this notice
254a997
- * remains attached.
254a997
- */
254a997
-
254a997
-
254a997
-/* ---------------------------------------------------------------------
254a997
-
254a997
-    Conversions between UTF32, UTF-16, and UTF-8. Source code file.
254a997
-    Author: Mark E. Davis, 1994.
254a997
-    Rev History: Rick McGowan, fixes & updates May 2001.
254a997
-    Sept 2001: fixed const & error conditions per
254a997
-        mods suggested by S. Parent & A. Lillich.
254a997
-    June 2002: Tim Dodd added detection and handling of incomplete
254a997
-        source sequences, enhanced error detection, added casts
254a997
-        to eliminate compiler warnings.
254a997
-    July 2003: slight mods to back out aggressive FFFE detection.
254a997
-    Jan 2004: updated switches in from-UTF8 conversions.
254a997
-    Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.
254a997
-
254a997
-    See the header file "ConvertUTF.h" for complete documentation.
254a997
-
254a997
------------------------------------------------------------------------- */
254a997
-
254a997
-#include "ConvertUTF.h"
254a997
-#ifdef CVTUTF_DEBUG
254a997
-#include <stdio.h>
254a997
-#endif
254a997
-
254a997
-static const int halfShift  = 10; /* used for shifting by 10 bits */
254a997
-
254a997
-static const UTF32 halfBase = 0x0010000UL;
254a997
-static const UTF32 halfMask = 0x3FFUL;
254a997
-
254a997
-#define UNI_SUR_HIGH_START  (UTF32)0xD800
254a997
-#define UNI_SUR_HIGH_END    (UTF32)0xDBFF
254a997
-#define UNI_SUR_LOW_START   (UTF32)0xDC00
254a997
-#define UNI_SUR_LOW_END     (UTF32)0xDFFF
254a997
-#define false	   0
254a997
-#define true	    1
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF32toUTF16 (
254a997
-        const UTF32** sourceStart, const UTF32* sourceEnd,
254a997
-        UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF32* source = *sourceStart;
254a997
-    UTF16* target = *targetStart;
254a997
-    while (source < sourceEnd) {
254a997
-        UTF32 ch;
254a997
-        if (target >= targetEnd) {
254a997
-            result = targetExhausted; break;
254a997
-        }
254a997
-        ch = *source++;
254a997
-        if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
254a997
-            /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */
254a997
-            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
254a997
-                if (flags == strictConversion) {
254a997
-                    --source; /* return to the illegal value itself */
254a997
-                    result = sourceIllegal;
254a997
-                    break;
254a997
-                } else {
254a997
-                    *target++ = UNI_REPLACEMENT_CHAR;
254a997
-                }
254a997
-            } else {
254a997
-                *target++ = (UTF16)ch; /* normal case */
254a997
-            }
254a997
-        } else if (ch > UNI_MAX_LEGAL_UTF32) {
254a997
-            if (flags == strictConversion) {
254a997
-                result = sourceIllegal;
254a997
-            } else {
254a997
-                *target++ = UNI_REPLACEMENT_CHAR;
254a997
-            }
254a997
-        } else {
254a997
-            /* target is a character in range 0xFFFF - 0x10FFFF. */
254a997
-            if (target + 1 >= targetEnd) {
254a997
-                --source; /* Back up source pointer! */
254a997
-                result = targetExhausted; break;
254a997
-            }
254a997
-            ch -= halfBase;
254a997
-            *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
254a997
-            *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
254a997
-        }
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF16toUTF32 (
254a997
-        const UTF16** sourceStart, const UTF16* sourceEnd,
254a997
-        UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF16* source = *sourceStart;
254a997
-    UTF32* target = *targetStart;
254a997
-    UTF32 ch, ch2;
254a997
-    while (source < sourceEnd) {
254a997
-        const UTF16* oldSource = source; /*  In case we have to back up because of target overflow. */
254a997
-        ch = *source++;
254a997
-        /* If we have a surrogate pair, convert to UTF32 first. */
254a997
-        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
254a997
-            /* If the 16 bits following the high surrogate are in the source buffer... */
254a997
-            if (source < sourceEnd) {
254a997
-                ch2 = *source;
254a997
-                /* If it's a low surrogate, convert to UTF32. */
254a997
-                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
254a997
-                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
254a997
-                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
254a997
-                    ++source;
254a997
-                } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
254a997
-                    --source; /* return to the illegal value itself */
254a997
-                    result = sourceIllegal;
254a997
-                    break;
254a997
-                }
254a997
-            } else { /* We don't have the 16 bits following the high surrogate. */
254a997
-                --source; /* return to the high surrogate */
254a997
-                result = sourceExhausted;
254a997
-                break;
254a997
-            }
254a997
-        } else if (flags == strictConversion) {
254a997
-            /* UTF-16 surrogate values are illegal in UTF-32 */
254a997
-            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
254a997
-                --source; /* return to the illegal value itself */
254a997
-                result = sourceIllegal;
254a997
-                break;
254a997
-            }
254a997
-        }
254a997
-        if (target >= targetEnd) {
254a997
-            source = oldSource; /* Back up source pointer! */
254a997
-            result = targetExhausted; break;
254a997
-        }
254a997
-        *target++ = ch;
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-#ifdef CVTUTF_DEBUG
254a997
-if (result == sourceIllegal) {
254a997
-    fprintf(stderr, "ConvertUTF16toUTF32 illegal seq 0x%04x,%04x\n", ch, ch2);
254a997
-    fflush(stderr);
254a997
-}
254a997
-#endif
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-/*
254a997
- * Index into the table below with the first byte of a UTF-8 sequence to
254a997
- * get the number of trailing bytes that are supposed to follow it.
254a997
- * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
254a997
- * left as-is for anyone who may want to do such conversion, which was
254a997
- * allowed in earlier algorithms.
254a997
- */
254a997
-static const char trailingBytesForUTF8[256] = {
254a997
-    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,
254a997
-    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,
254a997
-    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,
254a997
-    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,
254a997
-    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,
254a997
-    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,
254a997
-    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,
254a997
-    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
254a997
-};
254a997
-
254a997
-/*
254a997
- * Magic values subtracted from a buffer value during UTF8 conversion.
254a997
- * This table contains as many values as there might be trailing bytes
254a997
- * in a UTF-8 sequence.
254a997
- */
254a997
-static const UTF32 offsetsFromUTF8[6] = { 0x00000000UL, 0x00003080UL, 0x000E2080UL,
254a997
-                     0x03C82080UL, 0xFA082080UL, 0x82082080UL };
254a997
-
254a997
-/*
254a997
- * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
254a997
- * into the first byte, depending on how many bytes follow.  There are
254a997
- * as many entries in this table as there are UTF-8 sequence types.
254a997
- * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
254a997
- * for *legal* UTF-8 will be 4 or fewer bytes total.
254a997
- */
254a997
-static const UTF8 firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-/* The interface converts a whole buffer to avoid function-call overhead.
254a997
- * Constants have been gathered. Loops & conditionals have been removed as
254a997
- * much as possible for efficiency, in favor of drop-through switches.
254a997
- * (See "Note A" at the bottom of the file for equivalent code.)
254a997
- * If your compiler supports it, the "isLegalUTF8" call can be turned
254a997
- * into an inline function.
254a997
- */
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF16toUTF8 (
254a997
-        const UTF16** sourceStart, const UTF16* sourceEnd,
254a997
-        UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF16* source = *sourceStart;
254a997
-    UTF8* target = *targetStart;
254a997
-    while (source < sourceEnd) {
254a997
-        UTF32 ch;
254a997
-        unsigned short bytesToWrite = 0;
254a997
-        const UTF32 byteMask = 0xBF;
254a997
-        const UTF32 byteMark = 0x80;
254a997
-        const UTF16* oldSource = source; /* In case we have to back up because of target overflow. */
254a997
-        ch = *source++;
254a997
-        /* If we have a surrogate pair, convert to UTF32 first. */
254a997
-        if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_HIGH_END) {
254a997
-            /* If the 16 bits following the high surrogate are in the source buffer... */
254a997
-            if (source < sourceEnd) {
254a997
-                UTF32 ch2 = *source;
254a997
-                /* If it's a low surrogate, convert to UTF32. */
254a997
-                if (ch2 >= UNI_SUR_LOW_START && ch2 <= UNI_SUR_LOW_END) {
254a997
-                    ch = ((ch - UNI_SUR_HIGH_START) << halfShift)
254a997
-                        + (ch2 - UNI_SUR_LOW_START) + halfBase;
254a997
-                    ++source;
254a997
-                } else if (flags == strictConversion) { /* it's an unpaired high surrogate */
254a997
-                    --source; /* return to the illegal value itself */
254a997
-                    result = sourceIllegal;
254a997
-                    break;
254a997
-                }
254a997
-            } else { /* We don't have the 16 bits following the high surrogate. */
254a997
-                --source; /* return to the high surrogate */
254a997
-                result = sourceExhausted;
254a997
-                break;
254a997
-            }
254a997
-        } else if (flags == strictConversion) {
254a997
-            /* UTF-16 surrogate values are illegal in UTF-32 */
254a997
-            if (ch >= UNI_SUR_LOW_START && ch <= UNI_SUR_LOW_END) {
254a997
-                --source; /* return to the illegal value itself */
254a997
-                result = sourceIllegal;
254a997
-                break;
254a997
-            }
254a997
-        }
254a997
-        /* Figure out how many bytes the result will require */
254a997
-        if (ch < (UTF32)0x80) {	     bytesToWrite = 1;
254a997
-        } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
254a997
-        } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
254a997
-        } else if (ch < (UTF32)0x110000) {  bytesToWrite = 4;
254a997
-        } else {			    bytesToWrite = 3;
254a997
-                                            ch = UNI_REPLACEMENT_CHAR;
254a997
-        }
254a997
-
254a997
-        target += bytesToWrite;
254a997
-        if (target > targetEnd) {
254a997
-            source = oldSource; /* Back up source pointer! */
254a997
-            target -= bytesToWrite; result = targetExhausted; break;
254a997
-        }
254a997
-        switch (bytesToWrite) { /* note: everything falls through. */
254a997
-            case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 1: *--target =  (UTF8)(ch | firstByteMark[bytesToWrite]);
254a997
-        }
254a997
-        target += bytesToWrite;
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-/*
254a997
- * Utility routine to tell whether a sequence of bytes is legal UTF-8.
254a997
- * This must be called with the length pre-determined by the first byte.
254a997
- * If not calling this from ConvertUTF8to*, then the length can be set by:
254a997
- *  length = trailingBytesForUTF8[*source]+1;
254a997
- * and the sequence is illegal right away if there aren't that many bytes
254a997
- * available.
254a997
- * If presented with a length > 4, this returns false.  The Unicode
254a997
- * definition of UTF-8 goes up to 4-byte sequences.
254a997
- */
254a997
-
254a997
-static Boolean isLegalUTF8(const UTF8 *source, int length) {
254a997
-    UTF8 a;
254a997
-    const UTF8 *srcptr = source+length;
254a997
-    switch (length) {
254a997
-    default: return false;
254a997
-        /* Everything else falls through when "true"... */
254a997
-    case 4: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
254a997
-    case 3: if ((a = (*--srcptr)) < 0x80 || a > 0xBF) return false;
254a997
-    case 2: if ((a = (*--srcptr)) > 0xBF) return false;
254a997
-
254a997
-        switch (*source) {
254a997
-            /* no fall-through in this inner switch */
254a997
-            case 0xE0: if (a < 0xA0) return false; break;
254a997
-            case 0xED: if (a > 0x9F) return false; break;
254a997
-            case 0xF0: if (a < 0x90) return false; break;
254a997
-            case 0xF4: if (a > 0x8F) return false; break;
254a997
-            default:   if (a < 0x80) return false;
254a997
-        }
254a997
-
254a997
-    case 1: if (*source >= 0x80 && *source < 0xC2) return false;
254a997
-    }
254a997
-    if (*source > 0xF4) return false;
254a997
-    return true;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-/*
254a997
- * Exported function to return whether a UTF-8 sequence is legal or not.
254a997
- * This is not used here; it's just exported.
254a997
- */
254a997
-Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd) {
254a997
-    int length = trailingBytesForUTF8[*source]+1;
254a997
-    if (source+length > sourceEnd) {
254a997
-        return false;
254a997
-    }
254a997
-    return isLegalUTF8(source, length);
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF8toUTF16 (
254a997
-        const UTF8** sourceStart, const UTF8* sourceEnd,
254a997
-        UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF8* source = *sourceStart;
254a997
-    UTF16* target = *targetStart;
254a997
-    while (source < sourceEnd) {
254a997
-        UTF32 ch = 0;
254a997
-        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
254a997
-        if (source + extraBytesToRead >= sourceEnd) {
254a997
-            result = sourceExhausted; break;
254a997
-        }
254a997
-        /* Do this check whether lenient or strict */
254a997
-        if (! isLegalUTF8(source, extraBytesToRead+1)) {
254a997
-            result = sourceIllegal;
254a997
-            break;
254a997
-        }
254a997
-        /*
254a997
-         * The cases all fall through. See "Note A" below.
254a997
-         */
254a997
-        switch (extraBytesToRead) {
254a997
-            case 5: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
254a997
-            case 4: ch += *source++; ch <<= 6; /* remember, illegal UTF-8 */
254a997
-            case 3: ch += *source++; ch <<= 6;
254a997
-            case 2: ch += *source++; ch <<= 6;
254a997
-            case 1: ch += *source++; ch <<= 6;
254a997
-            case 0: ch += *source++;
254a997
-        }
254a997
-        ch -= offsetsFromUTF8[extraBytesToRead];
254a997
-
254a997
-        if (target >= targetEnd) {
254a997
-            source -= (extraBytesToRead+1); /* Back up source pointer! */
254a997
-            result = targetExhausted; break;
254a997
-        }
254a997
-        if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */
254a997
-            /* UTF-16 surrogate values are illegal in UTF-32 */
254a997
-            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
254a997
-                if (flags == strictConversion) {
254a997
-                    source -= (extraBytesToRead+1); /* return to the illegal value itself */
254a997
-                    result = sourceIllegal;
254a997
-                    break;
254a997
-                } else {
254a997
-                    *target++ = UNI_REPLACEMENT_CHAR;
254a997
-                }
254a997
-            } else {
254a997
-                *target++ = (UTF16)ch; /* normal case */
254a997
-            }
254a997
-        } else if (ch > UNI_MAX_UTF16) {
254a997
-            if (flags == strictConversion) {
254a997
-                result = sourceIllegal;
254a997
-                source -= (extraBytesToRead+1); /* return to the start */
254a997
-                break; /* Bail out; shouldn't continue */
254a997
-            } else {
254a997
-                *target++ = UNI_REPLACEMENT_CHAR;
254a997
-            }
254a997
-        } else {
254a997
-            /* target is a character in range 0xFFFF - 0x10FFFF. */
254a997
-            if (target + 1 >= targetEnd) {
254a997
-                source -= (extraBytesToRead+1); /* Back up source pointer! */
254a997
-                result = targetExhausted; break;
254a997
-            }
254a997
-            ch -= halfBase;
254a997
-            *target++ = (UTF16)((ch >> halfShift) + UNI_SUR_HIGH_START);
254a997
-            *target++ = (UTF16)((ch & halfMask) + UNI_SUR_LOW_START);
254a997
-        }
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF32toUTF8 (
254a997
-        const UTF32** sourceStart, const UTF32* sourceEnd,
254a997
-        UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF32* source = *sourceStart;
254a997
-    UTF8* target = *targetStart;
254a997
-    while (source < sourceEnd) {
254a997
-        UTF32 ch;
254a997
-        unsigned short bytesToWrite = 0;
254a997
-        const UTF32 byteMask = 0xBF;
254a997
-        const UTF32 byteMark = 0x80;
254a997
-        ch = *source++;
254a997
-        if (flags == strictConversion ) {
254a997
-            /* UTF-16 surrogate values are illegal in UTF-32 */
254a997
-            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
254a997
-                --source; /* return to the illegal value itself */
254a997
-                result = sourceIllegal;
254a997
-                break;
254a997
-            }
254a997
-        }
254a997
-        /*
254a997
-         * Figure out how many bytes the result will require. Turn any
254a997
-         * illegally large UTF32 things (> Plane 17) into replacement chars.
254a997
-         */
254a997
-        if (ch < (UTF32)0x80) {	     bytesToWrite = 1;
254a997
-        } else if (ch < (UTF32)0x800) {     bytesToWrite = 2;
254a997
-        } else if (ch < (UTF32)0x10000) {   bytesToWrite = 3;
254a997
-        } else if (ch <= UNI_MAX_LEGAL_UTF32) {  bytesToWrite = 4;
254a997
-        } else {			    bytesToWrite = 3;
254a997
-                                            ch = UNI_REPLACEMENT_CHAR;
254a997
-                                            result = sourceIllegal;
254a997
-        }
254a997
-
254a997
-        target += bytesToWrite;
254a997
-        if (target > targetEnd) {
254a997
-            --source; /* Back up source pointer! */
254a997
-            target -= bytesToWrite; result = targetExhausted; break;
254a997
-        }
254a997
-        switch (bytesToWrite) { /* note: everything falls through. */
254a997
-            case 4: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 3: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 2: *--target = (UTF8)((ch | byteMark) & byteMask); ch >>= 6;
254a997
-            case 1: *--target = (UTF8) (ch | firstByteMark[bytesToWrite]);
254a997
-        }
254a997
-        target += bytesToWrite;
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-ConversionResult ConvertUTF8toUTF32 (
254a997
-        const UTF8** sourceStart, const UTF8* sourceEnd,
254a997
-        UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags) {
254a997
-    ConversionResult result = conversionOK;
254a997
-    const UTF8* source = *sourceStart;
254a997
-    UTF32* target = *targetStart;
254a997
-    while (source < sourceEnd) {
254a997
-        UTF32 ch = 0;
254a997
-        unsigned short extraBytesToRead = trailingBytesForUTF8[*source];
254a997
-        if (source + extraBytesToRead >= sourceEnd) {
254a997
-            result = sourceExhausted; break;
254a997
-        }
254a997
-        /* Do this check whether lenient or strict */
254a997
-        if (! isLegalUTF8(source, extraBytesToRead+1)) {
254a997
-            result = sourceIllegal;
254a997
-            break;
254a997
-        }
254a997
-        /*
254a997
-         * The cases all fall through. See "Note A" below.
254a997
-         */
254a997
-        switch (extraBytesToRead) {
254a997
-            case 5: ch += *source++; ch <<= 6;
254a997
-            case 4: ch += *source++; ch <<= 6;
254a997
-            case 3: ch += *source++; ch <<= 6;
254a997
-            case 2: ch += *source++; ch <<= 6;
254a997
-            case 1: ch += *source++; ch <<= 6;
254a997
-            case 0: ch += *source++;
254a997
-        }
254a997
-        ch -= offsetsFromUTF8[extraBytesToRead];
254a997
-
254a997
-        if (target >= targetEnd) {
254a997
-            source -= (extraBytesToRead+1); /* Back up the source pointer! */
254a997
-            result = targetExhausted; break;
254a997
-        }
254a997
-        if (ch <= UNI_MAX_LEGAL_UTF32) {
254a997
-            /*
254a997
-             * UTF-16 surrogate values are illegal in UTF-32, and anything
254a997
-             * over Plane 17 (> 0x10FFFF) is illegal.
254a997
-             */
254a997
-            if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {
254a997
-                if (flags == strictConversion) {
254a997
-                    source -= (extraBytesToRead+1); /* return to the illegal value itself */
254a997
-                    result = sourceIllegal;
254a997
-                    break;
254a997
-                } else {
254a997
-                    *target++ = UNI_REPLACEMENT_CHAR;
254a997
-                }
254a997
-            } else {
254a997
-                *target++ = ch;
254a997
-            }
254a997
-        } else { /* i.e., ch > UNI_MAX_LEGAL_UTF32 */
254a997
-            result = sourceIllegal;
254a997
-            *target++ = UNI_REPLACEMENT_CHAR;
254a997
-        }
254a997
-    }
254a997
-    *sourceStart = source;
254a997
-    *targetStart = target;
254a997
-    return result;
254a997
-}
254a997
-
254a997
-/* ---------------------------------------------------------------------
254a997
-
254a997
-    Note A.
254a997
-    The fall-through switches in UTF-8 reading code save a
254a997
-    temp variable, some decrements & conditionals.  The switches
254a997
-    are equivalent to the following loop:
254a997
-        {
254a997
-            int tmpBytesToRead = extraBytesToRead+1;
254a997
-            do {
254a997
-                ch += *source++;
254a997
-                --tmpBytesToRead;
254a997
-                if (tmpBytesToRead) ch <<= 6;
254a997
-            } while (tmpBytesToRead > 0);
254a997
-        }
254a997
-    In UTF-8 writing code, the switches on "bytesToWrite" are
254a997
-    similarly unrolled loops.
254a997
-
254a997
-   --------------------------------------------------------------------- */
254a997
diff --git a/base/ConvertUTF.h b/base/ConvertUTF.h
254a997
deleted file mode 100644
254a997
index 538bec6..0000000
254a997
--- a/base/ConvertUTF.h
254a997
+++ /dev/null
254a997
@@ -1,155 +0,0 @@
254a997
-/*
254a997
- * Copyright 2001-2004 Unicode, Inc.
254a997
- *
254a997
- * Disclaimer
254a997
- *
254a997
- * This source code is provided as is by Unicode, Inc. No claims are
254a997
- * made as to fitness for any particular purpose. No warranties of any
254a997
- * kind are expressed or implied. The recipient agrees to determine
254a997
- * applicability of information provided. If this file has been
254a997
- * purchased on magnetic or optical media from Unicode, Inc., the
254a997
- * sole remedy for any claim will be exchange of defective media
254a997
- * within 90 days of receipt.
254a997
- *
254a997
- * Limitations on Rights to Redistribute This Code
254a997
- *
254a997
- * Unicode, Inc. hereby grants the right to freely use the information
254a997
- * supplied in this file in the creation of products supporting the
254a997
- * Unicode Standard, and to make copies of this file in any form
254a997
- * for internal or external distribution as long as this notice
254a997
- * remains attached.
254a997
- */
254a997
-
254a997
-
254a997
-#ifndef ConvertUTF_INCLUDED
254a997
-#define ConvertUTF_INCLUDED
254a997
-
254a997
-/* ---------------------------------------------------------------------
254a997
-
254a997
-    Conversions between UTF32, UTF-16, and UTF-8.  Header file.
254a997
-
254a997
-    Several funtions are included here, forming a complete set of
254a997
-    conversions between the three formats.  UTF-7 is not included
254a997
-    here, but is handled in a separate source file.
254a997
-
254a997
-    Each of these routines takes pointers to input buffers and output
254a997
-    buffers.  The input buffers are const.
254a997
-
254a997
-    Each routine converts the text between *sourceStart and sourceEnd,
254a997
-    putting the result into the buffer between *targetStart and
254a997
-    targetEnd. Note: the end pointers are *after* the last item: e.g.
254a997
-    *(sourceEnd - 1) is the last item.
254a997
-
254a997
-    The return result indicates whether the conversion was successful,
254a997
-    and if not, whether the problem was in the source or target buffers.
254a997
-    (Only the first encountered problem is indicated.)
254a997
-
254a997
-    After the conversion, *sourceStart and *targetStart are both
254a997
-    updated to point to the end of last text successfully converted in
254a997
-    the respective buffers.
254a997
-
254a997
-    Input parameters:
254a997
-        sourceStart - pointer to a pointer to the source buffer.
254a997
-                The contents of this are modified on return so that
254a997
-                it points at the next thing to be converted.
254a997
-        targetStart - similarly, pointer to pointer to the target buffer.
254a997
-        sourceEnd, targetEnd - respectively pointers to the ends of the
254a997
-                two buffers, for overflow checking only.
254a997
-
254a997
-    These conversion functions take a ConversionFlags argument. When this
254a997
-    flag is set to strict, both irregular sequences and isolated surrogates
254a997
-    will cause an error.  When the flag is set to lenient, both irregular
254a997
-    sequences and isolated surrogates are converted.
254a997
-
254a997
-    Whether the flag is strict or lenient, all illegal sequences will cause
254a997
-    an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
254a997
-    or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
254a997
-    must check for illegal sequences.
254a997
-
254a997
-    When the flag is set to lenient, characters over 0x10FFFF are converted
254a997
-    to the replacement character; otherwise (when the flag is set to strict)
254a997
-    they constitute an error.
254a997
-
254a997
-    Output parameters:
254a997
-        The value "sourceIllegal" is returned from some routines if the input
254a997
-        sequence is malformed.  When "sourceIllegal" is returned, the source
254a997
-        value will point to the illegal value that caused the problem. E.g.,
254a997
-        in UTF-8 when a sequence is malformed, it points to the start of the
254a997
-        malformed sequence.
254a997
-
254a997
-    Author: Mark E. Davis, 1994.
254a997
-    Rev History: Rick McGowan, fixes & updates May 2001.
254a997
-                 Fixes & updates, Sept 2001.
254a997
-
254a997
------------------------------------------------------------------------- */
254a997
-
254a997
-/* ---------------------------------------------------------------------
254a997
-    The following 4 definitions are compiler-specific.
254a997
-    The C standard does not guarantee that wchar_t has at least
254a997
-    16 bits, so wchar_t is no less portable than unsigned short!
254a997
-    All should be unsigned values to avoid sign extension during
254a997
-    bit mask & shift operations.
254a997
------------------------------------------------------------------------- */
254a997
-
254a997
-typedef unsigned long	UTF32;	/* at least 32 bits */
254a997
-typedef unsigned short	UTF16;	/* at least 16 bits */
254a997
-typedef unsigned char	UTF8;	/* typically 8 bits */
254a997
-typedef unsigned char	Boolean; /* 0 or 1 */
254a997
-
254a997
-/* Some fundamental constants */
254a997
-#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
254a997
-#define UNI_MAX_BMP (UTF32)0x0000FFFF
254a997
-#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
254a997
-#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
254a997
-#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
254a997
-
254a997
-typedef enum {
254a997
-        conversionOK, 		/* conversion successful */
254a997
-        sourceExhausted,	/* partial character in source, but hit end */
254a997
-        targetExhausted,	/* insuff. room in target for conversion */
254a997
-        sourceIllegal		/* source sequence is illegal/malformed */
254a997
-} ConversionResult;
254a997
-
254a997
-typedef enum {
254a997
-        strictConversion = 0,
254a997
-        lenientConversion
254a997
-} ConversionFlags;
254a997
-
254a997
-/* This is for C++ and does no harm in C */
254a997
-#ifdef __cplusplus
254a997
-extern "C" {
254a997
-#endif
254a997
-
254a997
-ConversionResult ConvertUTF8toUTF16 (
254a997
-                const UTF8** sourceStart, const UTF8* sourceEnd,
254a997
-                UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
254a997
-
254a997
-ConversionResult ConvertUTF16toUTF8 (
254a997
-                const UTF16** sourceStart, const UTF16* sourceEnd,
254a997
-                UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
254a997
-
254a997
-ConversionResult ConvertUTF8toUTF32 (
254a997
-                const UTF8** sourceStart, const UTF8* sourceEnd,
254a997
-                UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
254a997
-
254a997
-ConversionResult ConvertUTF32toUTF8 (
254a997
-                const UTF32** sourceStart, const UTF32* sourceEnd,
254a997
-                UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
254a997
-
254a997
-ConversionResult ConvertUTF16toUTF32 (
254a997
-                const UTF16** sourceStart, const UTF16* sourceEnd,
254a997
-                UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
254a997
-
254a997
-ConversionResult ConvertUTF32toUTF16 (
254a997
-                const UTF32** sourceStart, const UTF32* sourceEnd,
254a997
-                UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
254a997
-
254a997
-Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
254a997
-
254a997
-#ifdef __cplusplus
254a997
-}
254a997
-#endif
254a997
-
254a997
-/* --------------------------------------------------------------------- */
254a997
-
254a997
-#endif /* ConvertUTF_INCLUDED */
254a997
diff --git a/base/lib.mak b/base/lib.mak
254a997
index 173e2c6..2de6565 100644
254a997
--- a/base/lib.mak
254a997
+++ b/base/lib.mak
254a997
@@ -52,7 +52,6 @@ GLLCMS2CC=$(CC) $(LCMS2_CFLAGS) $(CFLAGS) $(I_)$(GLI_) $(II)$(LCMS2SRCDIR)$(D)in
254a997
 lcms2_h=$(LCMS2SRCDIR)$(D)include$(D)lcms2.h
254a997
 lcms2_plugin_h=$(LCMS2SRCDIR)$(D)include$(D)lcms2_plugin.h
254a997
 
254a997
-ConvertUTF_h=$(GLSRC)ConvertUTF.h
254a997
 gdevdcrd_h=$(GLSRC)gdevdcrd.h
254a997
 gdevpccm_h=$(GLSRC)gdevpccm.h
254a997
 
254a997
@@ -1097,9 +1096,6 @@ $(GLOBJ)gdevpccm.$(OBJ) : $(GLSRC)gdevpccm.c $(AK)\
254a997
  $(gx_h) $(gsmatrix_h) $(gxdevice_h) $(gdevpccm_h) $(LIB_MAK) $(MAKEDIRS)
254a997
 	$(GLCC) $(GLO_)gdevpccm.$(OBJ) $(C_) $(GLSRC)gdevpccm.c
254a997
 
254a997
-$(GLOBJ)ConvertUTF.$(OBJ) : $(GLSRC)ConvertUTF.c $(ConvertUTF_h) $(LIB_MAK) $(MAKEDIRS)
254a997
-	$(GLCC) $(GLO_)ConvertUTF.$(OBJ) $(C_) $(GLSRC)ConvertUTF.c
254a997
-
254a997
 ### Memory devices
254a997
 
254a997
 $(GLOBJ)gdevmem.$(OBJ) : $(GLSRC)gdevmem.c $(AK) $(gx_h) $(gserrors_h) \
254a997
diff --git a/devices/devs.mak b/devices/devs.mak
254a997
index ea27ab0..51ec363 100644
254a997
--- a/devices/devs.mak
254a997
+++ b/devices/devs.mak
254a997
@@ -835,9 +835,8 @@ pdfwrite5_=$(DEVOBJ)gdevpdfm.$(OBJ)
254a997
 pdfwrite6_=$(DEVOBJ)gdevpdfo.$(OBJ) $(DEVOBJ)gdevpdfp.$(OBJ) $(DEVOBJ)gdevpdft.$(OBJ)
254a997
 pdfwrite7_=$(DEVOBJ)gdevpdfr.$(OBJ)
254a997
 pdfwrite8_=$(DEVOBJ)gdevpdfu.$(OBJ) $(DEVOBJ)gdevpdfv.$(OBJ) $(DEVOBJ)gdevagl.$(OBJ)
254a997
-pdfwrite9_= $(GLOBJ)ConvertUTF.$(OBJ)
254a997
-pdfwrite10_=$(DEVOBJ)gsflip.$(OBJ)
254a997
-pdfwrite11_=$(DEVOBJ)scantab.$(OBJ) $(DEVOBJ)sfilter2.$(OBJ)
254a997
+pdfwrite9_=$(DEVOBJ)gsflip.$(OBJ)
254a997
+pdfwrite10_=$(DEVOBJ)scantab.$(OBJ) $(DEVOBJ)sfilter2.$(OBJ)
254a997
 pdfwrite_=$(pdfwrite1_) $(pdfwrite2_) $(pdfwrite3_) $(pdfwrite4_)\
254a997
  $(pdfwrite5_) $(pdfwrite6_) $(pdfwrite7_) $(pdfwrite8_) $(pdfwrite9_)\
254a997
  $(pdfwrite10_) $(pdfwrite11_)
254a997
diff --git a/devices/vector/gdevpdf.c b/devices/vector/gdevpdf.c
254a997
index 2b3186d..20e0ae8 100644
254a997
--- a/devices/vector/gdevpdf.c
254a997
+++ b/devices/vector/gdevpdf.c
254a997
@@ -111,14 +111,13 @@ ENUM_PTRS_WITH(device_pdfwrite_enum_ptrs, gx_device_pdf *pdev)
254a997
  ENUM_PTR(32, gx_device_pdf, pres_soft_mask_dict);
254a997
  ENUM_PTR(33, gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data);
254a997
  ENUM_PTR(34, gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data);
254a997
- ENUM_PTR(35, gx_device_pdf, DSCEncodingToUnicode.data);
254a997
- ENUM_PTR(36, gx_device_pdf, Identity_ToUnicode_CMaps[0]);
254a997
- ENUM_PTR(37, gx_device_pdf, Identity_ToUnicode_CMaps[1]);
254a997
- ENUM_PTR(38, gx_device_pdf, vgstack);
254a997
- ENUM_PTR(39, gx_device_pdf, outline_levels);
254a997
- ENUM_PTR(40, gx_device_pdf, EmbeddedFiles);
254a997
- ENUM_PTR(41, gx_device_pdf, pdf_font_dir);
254a997
- ENUM_PTR(42, gx_device_pdf, ExtensionMetadata);
254a997
+ ENUM_PTR(35, gx_device_pdf, Identity_ToUnicode_CMaps[0]);
254a997
+ ENUM_PTR(36, gx_device_pdf, Identity_ToUnicode_CMaps[1]);
254a997
+ ENUM_PTR(37, gx_device_pdf, vgstack);
254a997
+ ENUM_PTR(38, gx_device_pdf, outline_levels);
254a997
+ ENUM_PTR(39, gx_device_pdf, EmbeddedFiles);
254a997
+ ENUM_PTR(40, gx_device_pdf, pdf_font_dir);
254a997
+ ENUM_PTR(41, gx_device_pdf, ExtensionMetadata);
254a997
 #define e1(i,elt) ENUM_PARAM_STRING_PTR(i + gx_device_pdf_num_ptrs, gx_device_pdf, elt);
254a997
 gx_device_pdf_do_param_strings(e1)
254a997
 #undef e1
254a997
@@ -165,7 +164,6 @@ static RELOC_PTRS_WITH(device_pdfwrite_reloc_ptrs, gx_device_pdf *pdev)
254a997
  RELOC_PTR(gx_device_pdf, pres_soft_mask_dict);
254a997
  RELOC_PTR(gx_device_pdf, PDFXTrimBoxToMediaBoxOffset.data);
254a997
  RELOC_PTR(gx_device_pdf, PDFXBleedBoxToTrimBoxOffset.data);
254a997
- RELOC_PTR(gx_device_pdf, DSCEncodingToUnicode.data);
254a997
  RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[0]);
254a997
  RELOC_PTR(gx_device_pdf, Identity_ToUnicode_CMaps[1]);
254a997
  RELOC_PTR(gx_device_pdf, vgstack);
254a997
diff --git a/devices/vector/gdevpdfb.h b/devices/vector/gdevpdfb.h
254a997
index 08f18c5..447f0f5 100644
254a997
--- a/devices/vector/gdevpdfb.h
254a997
+++ b/devices/vector/gdevpdfb.h
254a997
@@ -141,7 +141,6 @@ const gx_device_pdf PDF_DEVICE_IDENT =
254a997
  12000,				/* MaxClipPathSize */ /* HP LaserJet 1320 hangs with 14000. */
254a997
  256000,			/* MaxShadingBitmapSize */
254a997
  PDF_DEVICE_MaxInlineImageSize,	/* MaxInlineImageSize */
254a997
- {0, 0},                        /* DSCEncodingToUnicode */
254a997
  {0, 0, 0},			/* OwnerPassword */
254a997
  {0, 0, 0},			/* UserPassword */
254a997
  0,				/* KeyLength */
254a997
diff --git a/devices/vector/gdevpdfe.c b/devices/vector/gdevpdfe.c
254a997
index 1aa1f25..f23a02d 100644
254a997
--- a/devices/vector/gdevpdfe.c
254a997
+++ b/devices/vector/gdevpdfe.c
254a997
@@ -26,7 +26,6 @@
254a997
 #include "gdevpdfx.h"
254a997
 #include "gdevpdfg.h"
254a997
 #include "gdevpdfo.h"
254a997
-#include "ConvertUTF.h"
254a997
 
254a997
 char PDFDocEncodingLookup [92] = {
254a997
     0x20, 0x22, 0x20, 0x20, 0x20, 0x21, 0x20, 0x26,
254a997
@@ -343,155 +342,162 @@ decode_escape(const byte *data, int data_length, int *index)
254a997
     return c; /* A wrong escapement sequence. */
254a997
 }
254a997
 
254a997
-static int
254a997
-pdf_xmp_write_translated(gx_device_pdf *pdev, stream *s, const byte *data, int data_length,
254a997
-                         void(*write)(stream *s, const byte *data, int data_length))
254a997
+/*
254a997
+ * Once the bits are split out into bytes of UTF-8, this is a mask OR-ed
254a997
+ * into the first byte, depending on how many bytes follow.  There are
254a997
+ * as many entries in this table as there are UTF-8 sequence types.
254a997
+ * (I.e., one byte sequence, two byte... etc.). Remember that sequencs
254a997
+ * for *legal* UTF-8 will be 4 or fewer bytes total.
254a997
+ */
254a997
+static const char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
254a997
+
254a997
+static int gs_ConvertUTF16(char *UTF16, int UTF16Len, unsigned char **UTF8Start, int UTF8Len)
254a997
 {
254a997
-    if (pdev->DSCEncodingToUnicode.data == 0) {
254a997
-        int i, j=0;
254a997
-        unsigned char *buf0;
254a997
+    int i, bytes = 0;
254a997
+    short U16;
254a997
+    unsigned char *UTF8 = *UTF8Start;
254a997
+    unsigned char *UTF8End = UTF8 + UTF8Len;
254a997
 
254a997
-        buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, data_length * sizeof(unsigned char),
254a997
-                        "pdf_xmp_write_translated");
254a997
-        if (buf0 == NULL)
254a997
-            return_error(gs_error_VMerror);
254a997
-        for (i = 0; i < data_length; i++) {
254a997
-            byte c = data[i];
254a997
+    if (fabs(UTF16Len % sizeof(short)) != 0)
254a997
+        return gs_note_error(gs_error_rangecheck);
254a997
+
254a997
+    for (i=0;i
254a997
+    {
254a997
+        U16 = (*UTF16++) << 8;
254a997
+        U16 += *UTF16++;
254a997
 
254a997
-            if (c == '\\')
254a997
-                c = decode_escape(data, data_length, &i);
254a997
-            buf0[j] = c;
254a997
-            j++;
254a997
+        if (U16 >= 0xD800 && U16 <= 0xDBFF) {
254a997
+            return gs_note_error(gs_error_rangecheck);
254a997
         }
254a997
-        if (buf0[0] != 0xfe || buf0[1] != 0xff) {
254a997
-            unsigned char *buf1;
254a997
-            /* We must assume that the information is PDFDocEncoding. In this case
254a997
-             * we need to convert it into UTF-8. If we just convert it to UTF-16
254a997
-             * then we can safely fall through to the code below.
254a997
-             */
254a997
-            /* NB the code below skips the BOM in positions 0 and 1, so we need
254a997
-             * two extra bytes, to be ignored.
254a997
-             */
254a997
-            buf1 = (unsigned char *)gs_alloc_bytes(pdev->memory, (j * sizeof(UTF16)) + 2,
254a997
-                        "pdf_xmp_write_translated");
254a997
-            if (buf1 == NULL) {
254a997
-                gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-                return_error(gs_error_VMerror);
254a997
-            }
254a997
-            memset(buf1, 0x00, (j * sizeof(UTF16)) + 2);
254a997
-            for (i = 0; i < j; i++) {
254a997
-                if (buf0[i] <= 0x7f || buf0[i] >= 0xAE) {
254a997
-                    if (buf0[i] == 0x7f) {
254a997
-                        emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
254a997
-                            buf0[i]);
254a997
-                    } else
254a997
-                        buf1[(i * 2) + 3] = buf0[i];
254a997
+        if (U16 >= 0xDC00 && U16 <= 0xDFFF) {
254a997
+            return gs_note_error(gs_error_rangecheck);
254a997
+        }
254a997
+
254a997
+        if(U16 < 0x80) {
254a997
+            bytes = 1;
254a997
+        } else {
254a997
+            if (U16 < 0x800) {
254a997
+                    bytes = 2;
254a997
+            } else {
254a997
+                if (U16 < 0x10000) {
254a997
+                    bytes = 3;
254a997
                 } else {
254a997
-                    buf1[(i * 2) + 2] = PDFDocEncodingLookup[(buf0[i] - 0x80) * 2];
254a997
-                    buf1[(i * 2) + 3] = PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1];
254a997
-                    if (PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1] == 0x00)
254a997
-                        emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
254a997
-                            PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1]);
254a997
+                    if (U16 < 0x111000) {
254a997
+                        bytes = 4;
254a997
+                    } else {
254a997
+                        bytes = 3;
254a997
+                        U16 = 0xFFFD;
254a997
+                    }
254a997
                 }
254a997
             }
254a997
+        }
254a997
+        if (UTF8 + bytes > UTF8End)
254a997
+            return gs_note_error(gs_error_VMerror);
254a997
+
254a997
+        /* Write from end to beginning, low bytes first */
254a997
+        UTF8 += bytes;
254a997
+
254a997
+        switch(bytes) {
254a997
+            case 4:
254a997
+                *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
254a997
+                U16 >>= 6;
254a997
+            case 3:
254a997
+                *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
254a997
+                U16 >>= 6;
254a997
+            case 2:
254a997
+                *--UTF8 = (unsigned char)((U16 | 0x80) & 0xBF);
254a997
+                U16 >>= 6;
254a997
+            case 1:
254a997
+                *--UTF8 = (unsigned char)(U16 | firstByteMark[bytes]);
254a997
+                break;
254a997
+            default:
254a997
+                return gs_note_error(gs_error_rangecheck);
254a997
+        }
254a997
+
254a997
+        /* Move to start of next set */
254a997
+        UTF8 += bytes;
254a997
+    }
254a997
+    *UTF8Start = UTF8;
254a997
+    return 0;
254a997
+}
254a997
+
254a997
+static int
254a997
+pdf_xmp_write_translated(gx_device_pdf *pdev, stream *s, const byte *data, int data_length,
254a997
+                         void(*write)(stream *s, const byte *data, int data_length))
254a997
+{
254a997
+    int i, j=0;
254a997
+    unsigned char *buf0;
254a997
+
254a997
+    buf0 = (unsigned char *)gs_alloc_bytes(pdev->memory, data_length * sizeof(unsigned char),
254a997
+                    "pdf_xmp_write_translated");
254a997
+    if (buf0 == NULL)
254a997
+        return_error(gs_error_VMerror);
254a997
+    for (i = 0; i < data_length; i++) {
254a997
+        byte c = data[i];
254a997
+
254a997
+        if (c == '\\')
254a997
+            c = decode_escape(data, data_length, &i);
254a997
+        buf0[j] = c;
254a997
+        j++;
254a997
+    }
254a997
+    if (buf0[0] != 0xfe || buf0[1] != 0xff) {
254a997
+        unsigned char *buf1;
254a997
+        /* We must assume that the information is PDFDocEncoding. In this case
254a997
+         * we need to convert it into UTF-8. If we just convert it to UTF-16
254a997
+         * then we can safely fall through to the code below.
254a997
+         */
254a997
+        /* NB the code below skips the BOM in positions 0 and 1, so we need
254a997
+         * two extra bytes, to be ignored.
254a997
+         */
254a997
+        buf1 = (unsigned char *)gs_alloc_bytes(pdev->memory, (j * sizeof(short)) + 2,
254a997
+                    "pdf_xmp_write_translated");
254a997
+        if (buf1 == NULL) {
254a997
             gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-            buf0 = buf1;
254a997
-            data_length = j = (j * 2) + 2;
254a997
+            return_error(gs_error_VMerror);
254a997
         }
254a997
-        {
254a997
-            /* Its a Unicode (UTF-16BE) string, convert to UTF-8 */
254a997
-            UTF16 *buf0b, U16;
254a997
-            UTF8 *buf1, *buf1b;
254a997
-
254a997
-            /* A single UTF-16 (2 bytes) can end up as 4 bytes in UTF-8 */
254a997
-            buf1 = (UTF8 *)gs_alloc_bytes(pdev->memory, data_length * 2 * sizeof(unsigned char),
254a997
-                        "pdf_xmp_write_translated");
254a997
-            if (buf1 == NULL) {
254a997
-                gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-                return_error(gs_error_VMerror);
254a997
-            }
254a997
-            buf1b = buf1;
254a997
-            /* Skip the Byte Order Mark (0xfe 0xff) */
254a997
-            buf0b = (UTF16 *)(buf0 + 2);
254a997
-            /* ConvertUTF16to UTF8 expects a buffer of UTF16s in the local
254a997
-             * endian-ness, but the data is big-endian. In case this is a little-endian
254a997
-             * machine, process the buffer from big-endian to whatever is right for this platform.
254a997
-             */
254a997
-            for (i = 2; i < j; i+=2) {
254a997
-                U16 = (buf0[i] << 8) + buf0[i + 1];
254a997
-                *(buf0b++) = U16;
254a997
-            }
254a997
-            buf0b = (UTF16 *)(buf0 + 2);
254a997
-            switch (ConvertUTF16toUTF8((const UTF16**)&buf0b, (UTF16 *)(buf0 + j),
254a997
-                             &buf1b, buf1 + (data_length * 2 * sizeof(unsigned char)), strictConversion)) {
254a997
-                case conversionOK:
254a997
-                    write(s, buf1, buf1b - buf1);
254a997
-                    gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
254a997
-                    break;
254a997
-                case sourceExhausted:
254a997
-                case targetExhausted:
254a997
-                case sourceIllegal:
254a997
-                default:
254a997
-                    gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-                    gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
254a997
-                    return_error(gs_error_rangecheck);
254a997
+        memset(buf1, 0x00, (j * sizeof(short)) + 2);
254a997
+        for (i = 0; i < j; i++) {
254a997
+            if (buf0[i] <= 0x7f || buf0[i] >= 0xAE) {
254a997
+                if (buf0[i] == 0x7f) {
254a997
+                    emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
254a997
+                        buf0[i]);
254a997
+                } else
254a997
+                    buf1[(i * 2) + 3] = buf0[i];
254a997
+            } else {
254a997
+                buf1[(i * 2) + 2] = PDFDocEncodingLookup[(buf0[i] - 0x80) * 2];
254a997
+                buf1[(i * 2) + 3] = PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1];
254a997
+                if (PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1] == 0x00)
254a997
+                    emprintf1(pdev->memory, "PDFDocEncoding %x cannot be represented in Unicode\n",
254a997
+                        PDFDocEncodingLookup[((buf0[i] - 0x80) * 2) + 1]);
254a997
             }
254a997
         }
254a997
         gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-        return 0;
254a997
-    } else {
254a997
-        UTF16 *buf0;
254a997
-        const UTF16 *buf0b;
254a997
-        UTF8 *buf1, *buf1b;
254a997
-        int i, j = 0;
254a997
-
254a997
-        buf0 = (UTF16 *)gs_alloc_bytes(pdev->memory, data_length * sizeof(UTF16),
254a997
-                        "pdf_xmp_write_translated");
254a997
-        if (buf0 == NULL)
254a997
-            return_error(gs_error_VMerror);
254a997
-        buf1 = (UTF8 *)gs_alloc_bytes(pdev->memory, data_length * 2,
254a997
-                        "pdf_xmp_write_translated");
254a997
+        buf0 = buf1;
254a997
+        data_length = j = (j * 2) + 2;
254a997
+    }
254a997
+    {
254a997
+        /* Its a Unicode (UTF-16BE) string, convert to UTF-8 */
254a997
+        short *buf0b;
254a997
+        char *buf1, *buf1b;
254a997
+        int code;
254a997
+
254a997
+        /* A single UTF-16 (2 bytes) can end up as 4 bytes in UTF-8 */
254a997
+        buf1 = (char *)gs_alloc_bytes(pdev->memory, data_length * 2 * sizeof(unsigned char),
254a997
+                    "pdf_xmp_write_translated");
254a997
         if (buf1 == NULL) {
254a997
             gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
             return_error(gs_error_VMerror);
254a997
         }
254a997
-        buf0b = buf0;
254a997
         buf1b = buf1;
254a997
-        for (i = 0; i < data_length; i++) {
254a997
-            byte c = data[i];
254a997
-            int v;
254a997
-
254a997
-            if (c == '\\')
254a997
-                c = decode_escape(data, data_length, &i);
254a997
-            if (c > pdev->DSCEncodingToUnicode.size) {
254a997
-                gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-                gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
254a997
-                return_error(gs_error_rangecheck);
254a997
-            }
254a997
-
254a997
-            v = pdev->DSCEncodingToUnicode.data[c];
254a997
-            if (v == -1)
254a997
-                v = '?'; /* Arbitrary. */
254a997
-            buf0[j] = v;
254a997
-            j++;
254a997
-        }
254a997
-        switch (ConvertUTF16toUTF8(&buf0b, buf0 + j,
254a997
-                             &buf1b, buf1 + data_length * 2, strictConversion)) {
254a997
-            case conversionOK:
254a997
-                write(s, buf1, buf1b - buf1);
254a997
-                break;
254a997
-            case sourceExhausted:
254a997
-            case targetExhausted:
254a997
-            case sourceIllegal:
254a997
-            default:
254a997
-                gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-                gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
254a997
-                return_error(gs_error_rangecheck);
254a997
-        }
254a997
-        gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
-        gs_free_object(pdev->memory, buf1, "pdf_xmp_write_translated");
254a997
-        return 0;
254a997
+        /* Skip the Byte Order Mark (0xfe 0xff) */
254a997
+        buf0b = (short *)(buf0 + 2);
254a997
+        code = gs_ConvertUTF16((char *)buf0b, j - 2, (unsigned char **)&buf1b, data_length * 2 * sizeof(unsigned char));
254a997
+        if (code < 0)
254a997
+            return code;
254a997
+        write(s, (const byte *)buf1, buf1b - buf1);
254a997
     }
254a997
+    gs_free_object(pdev->memory, buf0, "pdf_xmp_write_translated");
254a997
+    return 0;
254a997
 }
254a997
 
254a997
 static int
254a997
diff --git a/devices/vector/gdevpdfp.c b/devices/vector/gdevpdfp.c
254a997
index 0fa07e3..6ebcb0d 100644
254a997
--- a/devices/vector/gdevpdfp.c
254a997
+++ b/devices/vector/gdevpdfp.c
254a997
@@ -77,7 +77,6 @@ static const gs_param_item_t pdf_param_items[] = {
254a997
     pi("CompressStreams", gs_param_type_bool, CompressStreams),
254a997
     pi("PrintStatistics", gs_param_type_bool, PrintStatistics),
254a997
     pi("MaxInlineImageSize", gs_param_type_long, MaxInlineImageSize),
254a997
-    pi("DSCEncodingToUnicode", gs_param_type_int_array, DSCEncodingToUnicode),
254a997
 
254a997
         /* PDF Encryption */
254a997
     pi("OwnerPassword", gs_param_type_string, OwnerPassword),
254a997
diff --git a/devices/vector/gdevpdfx.h b/devices/vector/gdevpdfx.h
254a997
index 308900a..c436220 100644
254a997
--- a/devices/vector/gdevpdfx.h
254a997
+++ b/devices/vector/gdevpdfx.h
254a997
@@ -601,7 +601,6 @@ struct gx_device_pdf_s {
254a997
                               a bitmap representation of a shading.
254a997
                               (Bigger shadings to be downsampled). */
254a997
     long MaxInlineImageSize;
254a997
-    gs_param_int_array DSCEncodingToUnicode;
254a997
     /* Encryption parameters */
254a997
     gs_param_string OwnerPassword;
254a997
     gs_param_string UserPassword;
254a997
@@ -911,14 +910,14 @@ struct gx_device_pdf_s {
254a997
  m(28,sbstack) m(29,substream_Resources) m(30,font3)\
254a997
  m(31,accumulating_substream_resource) \
254a997
  m(32,pres_soft_mask_dict) m(33,PDFXTrimBoxToMediaBoxOffset.data)\
254a997
- m(34,PDFXBleedBoxToTrimBoxOffset.data) m(35, DSCEncodingToUnicode.data)\
254a997
- m(36,Identity_ToUnicode_CMaps[0]) m(37,Identity_ToUnicode_CMaps[1])\
254a997
- m(38,vgstack)\
254a997
- m(39, outline_levels)
254a997
- m(40, gx_device_pdf, EmbeddedFiles);
254a997
- m(41, gx_device_pdf, pdf_font_dir);
254a997
- m(42, gx_device_pdf, Extension_Metadata);*/
254a997
-#define gx_device_pdf_num_ptrs 43
254a997
+ m(34,PDFXBleedBoxToTrimBoxOffset.data)
254a997
+ m(35,Identity_ToUnicode_CMaps[0]) m(36,Identity_ToUnicode_CMaps[1])\
254a997
+ m(37,vgstack)\
254a997
+ m(38, outline_levels)
254a997
+ m(39, gx_device_pdf, EmbeddedFiles);
254a997
+ m(40, gx_device_pdf, pdf_font_dir);
254a997
+ m(41, gx_device_pdf, Extension_Metadata);*/
254a997
+#define gx_device_pdf_num_ptrs 42
254a997
 #define gx_device_pdf_do_param_strings(m)\
254a997
     m(0, OwnerPassword) m(1, UserPassword) m(2, NoEncrypt)\
254a997
     m(3, DocumentUUID) m(4, InstanceUUID)
254a997
diff --git a/windows/ghostscript.vcproj b/windows/ghostscript.vcproj
254a997
index a96d317..450cb26 100644
254a997
--- a/windows/ghostscript.vcproj
254a997
+++ b/windows/ghostscript.vcproj
254a997
@@ -1794,10 +1794,6 @@
254a997
 				>
254a997
 			</File>
254a997
 			
254a997
-				RelativePath="..\base\ConvertUTF.c"
254a997
-				>
254a997
-			</File>
254a997
-			
254a997
 				RelativePath="..\base\echogs.c"
254a997
 				>
254a997
 			</File>
254a997
@@ -3330,10 +3326,6 @@
254a997
 				>
254a997
 			</File>
254a997
 			
254a997
-				RelativePath="..\base\ConvertUTF.h"
254a997
-				>
254a997
-			</File>
254a997
-			
254a997
 				RelativePath="..\base\ctype_.h"
254a997
 				>
254a997
 			</File>
254a997
diff --git a/windows/ghostscript_rt.vcxproj b/windows/ghostscript_rt.vcxproj
254a997
index 2348f08..fae2e1f 100644
254a997
--- a/windows/ghostscript_rt.vcxproj
254a997
+++ b/windows/ghostscript_rt.vcxproj
254a997
@@ -427,7 +427,6 @@
254a997
   <ItemGroup>
254a997
     <ClCompile Include="..\base\aes.c" />
254a997
     <ClCompile Include="..\base\bench.c" />
254a997
-    <ClCompile Include="..\base\ConvertUTF.c" />
254a997
     <ClCompile Include="..\base\echogs.c" />
254a997
     <ClCompile Include="..\base\gconf.c" />
254a997
     <ClCompile Include="..\base\genarch.c" />
254a997
@@ -1689,7 +1688,6 @@
254a997
     <ClInclude Include="..\jasper\src\libjasper\ras\ras_enc.h" />
254a997
     <ClInclude Include="..\base\aes.h" />
254a997
     <ClInclude Include="..\base\assert_.h" />
254a997
-    <ClInclude Include="..\base\ConvertUTF.h" />
254a997
     <ClInclude Include="..\base\ctype_.h" />
254a997
     <ClInclude Include="..\base\dirent_.h" />
254a997
     <ClInclude Include="..\base\dos_.h" />
254a997
-- 
254a997
2.9.3
254a997