| 1 |
#ifndef __UnicodeInlines_incl_cpp__ |
| 2 |
#define __UnicodeInlines_incl_cpp__ |
| 3 |
|
| 4 |
// ================================================================================================= |
| 5 |
// Copyright 2004 Adobe Systems Incorporated |
| 6 |
// All Rights Reserved. |
| 7 |
// |
| 8 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms |
| 9 |
// of the Adobe license agreement accompanying it. |
| 10 |
// ================================================================================================= |
| 11 |
|
| 12 |
#if AdobePrivate |
| 13 |
// ================================================================================================= |
| 14 |
// Change history |
| 15 |
// ============== |
| 16 |
// |
| 17 |
// Writers: |
| 18 |
// AWL Alan Lillich |
| 19 |
// |
| 20 |
// mm-dd-yy who Description of changes, most recent on top. |
| 21 |
// |
| 22 |
// 03-24-06 AWL 4.0-c001 Adapt for move to ham-perforce, integrate XMPFiles, bump version to 4. |
| 23 |
// |
| 24 |
// 01-28-05 AWL 3.2-001 Remove BIB. |
| 25 |
// 01-07-05 AWL 3.1.1-104 [1129616] Fix SetObjectName to verify that the string is UTF-8. |
| 26 |
// 12-09-04 AWl 3.1.1-099 [1041438] Add parsing and serialization support for UTF-16 and UTF-32. |
| 27 |
// 10-12-04 AWL 3.1.1-084 [616293] Add value and name checking to SetProperty. |
| 28 |
// ================================================================================================= |
| 29 |
#endif // AdobePrivate |
| 30 |
|
| 31 |
#include "source/UnicodeConversions.hpp" |
| 32 |
|
| 33 |
// ================================================================================================= |
| 34 |
// Inner loop utilities that need to be inlined. |
| 35 |
// ================================================================================================= |
| 36 |
|
| 37 |
static inline XMP_Uns32 GetCodePoint ( const XMP_Uns8 ** utf8Str_io ) |
| 38 |
{ |
| 39 |
const XMP_Uns8 * u8Ptr = *utf8Str_io; |
| 40 |
XMP_Uns32 cp; |
| 41 |
size_t u8Len; |
| 42 |
CodePoint_from_UTF8 ( u8Ptr, 4, &cp, &u8Len ); // Throws an exception for errors. |
| 43 |
*utf8Str_io = u8Ptr + u8Len; |
| 44 |
return cp; |
| 45 |
} |
| 46 |
|
| 47 |
// ================================================================================================= |
| 48 |
|
| 49 |
static inline bool IsStartChar_ASCII ( XMP_Uns32 cp ) |
| 50 |
{ |
| 51 |
// ASCII starting characters for an XML name. |
| 52 |
if ( (('a' <= cp) && (cp <= 'z')) || (('A' <= cp) && (cp <= 'Z')) || (cp == '_') ) return true; |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
// ------------------------------------------------------------------------------------------------- |
| 57 |
|
| 58 |
static inline bool IsStartChar_NonASCII ( XMP_Uns32 cp ) |
| 59 |
{ |
| 60 |
// Non-ASCII starting characters for an XML name. |
| 61 |
|
| 62 |
if ( ((0xC0 <= cp) && (cp <= 0xD6)) || ((0xD8 <= cp) && (cp <= 0xF6)) ) return true; |
| 63 |
if ( ((0xF8 <= cp) && (cp <= 0x2FF)) || ((0x370 <= cp) && (cp <= 0x37D)) ) return true; |
| 64 |
|
| 65 |
if ( ((0x37F <= cp) && (cp <= 0x1FFF)) || ((0x200C <= cp) && (cp <= 0x200D)) ) return true; |
| 66 |
if ( ((0x2070 <= cp) && (cp <= 0x218F)) || ((0x2C00 <= cp) && (cp <= 0x2FEF)) ) return true; |
| 67 |
if ( ((0x3001 <= cp) && (cp <= 0xD7FF)) || ((0xF900 <= cp) && (cp <= 0xFDCF)) ) return true; |
| 68 |
if ( ((0xFDF0 <= cp) && (cp <= 0xFFFD)) || ((0x10000 <= cp) && (cp <= 0xEFFFF)) ) return true; |
| 69 |
|
| 70 |
return false; |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
// ------------------------------------------------------------------------------------------------- |
| 75 |
|
| 76 |
static inline bool IsOtherChar_ASCII ( XMP_Uns32 cp ) |
| 77 |
{ |
| 78 |
// ASCII following characters for an XML name. |
| 79 |
if ( (('0' <= cp) && (cp <= '9')) || (cp == '-') || (cp == '.') ) return true; |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
// ------------------------------------------------------------------------------------------------- |
| 84 |
|
| 85 |
static inline bool IsOtherChar_NonASCII ( XMP_Uns32 cp ) |
| 86 |
{ |
| 87 |
// Non-ASCII following characters for an XML name. |
| 88 |
if ( (cp == 0xB7) || ((0x300 <= cp) && (cp <= 0x36F)) || ((0x203F <= cp) && (cp <= 0x2040)) ) return true; |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
// ------------------------------------------------------------------------------------------------- |
| 93 |
|
| 94 |
static inline void VerifyUTF8 ( XMP_StringPtr str ) |
| 95 |
{ |
| 96 |
const XMP_Uns8 * utf8Str = (XMP_Uns8*)str; |
| 97 |
while ( *utf8Str != 0 ) { |
| 98 |
while ( (*utf8Str != 0) && (*utf8Str < 0x80) ) ++utf8Str; |
| 99 |
if ( *utf8Str >= 0x80 ) (void) GetCodePoint ( &utf8Str ); // Throws for bad UTF-8. |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// ------------------------------------------------------------------------------------------------- |
| 104 |
|
| 105 |
static inline void VerifySimpleXMLName ( XMP_StringPtr _nameStart, XMP_StringPtr _nameEnd ) |
| 106 |
{ |
| 107 |
|
| 108 |
const XMP_Uns8 * nameStart = (const XMP_Uns8 *) _nameStart; |
| 109 |
const XMP_Uns8 * nameEnd = (const XMP_Uns8 *) _nameEnd; |
| 110 |
const XMP_Uns8 * namePos = nameStart; |
| 111 |
XMP_Uns32 cp; |
| 112 |
|
| 113 |
// The first character is more restricted. |
| 114 |
|
| 115 |
if ( nameStart >= nameEnd ) XMP_Throw ( "Empty XML name", kXMPErr_BadXPath ); |
| 116 |
|
| 117 |
cp = *namePos; |
| 118 |
if ( cp < 0x80 ) { |
| 119 |
++namePos; |
| 120 |
if ( ! IsStartChar_ASCII(cp) ) goto NameError; |
| 121 |
} else { |
| 122 |
cp = GetCodePoint ( &namePos ); |
| 123 |
if ( ! IsStartChar_NonASCII(cp) ) goto NameError; |
| 124 |
} |
| 125 |
|
| 126 |
// Check the rest of the name. |
| 127 |
|
| 128 |
while ( namePos < nameEnd ) { |
| 129 |
cp = *namePos; |
| 130 |
if ( cp < 0x80 ) { |
| 131 |
++namePos; |
| 132 |
if ( (! IsStartChar_ASCII(cp)) && (! IsOtherChar_ASCII(cp)) ) goto NameError; |
| 133 |
} else { |
| 134 |
cp = GetCodePoint ( &namePos ); |
| 135 |
if ( (! IsStartChar_NonASCII(cp)) && (! IsOtherChar_NonASCII(cp)) ) goto NameError; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return; |
| 140 |
|
| 141 |
NameError: |
| 142 |
XMP_Throw ( "Bad XML name", kXMPErr_BadXPath ); |
| 143 |
|
| 144 |
} // VerifySimpleXMLName |
| 145 |
|
| 146 |
// ================================================================================================= |
| 147 |
|
| 148 |
#endif // __UnicodeInlines_incl_cpp__ |