| 1 |
// ================================================================================================= |
| 2 |
// Copyright 2009 Adobe |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms |
| 6 |
// of the Adobe license agreement accompanying it. If you have received this file from a source other |
| 7 |
// than Adobe, then your use, modification, or distribution of it requires the prior written permission |
| 8 |
// of Adobe. |
| 9 |
// ================================================================================================= |
| 10 |
|
| 11 |
#if AdobePrivate |
| 12 |
// ================================================================================================= |
| 13 |
// Change history |
| 14 |
// ============== |
| 15 |
// |
| 16 |
// Writers: |
| 17 |
// AWL Alan Lillich |
| 18 |
// ADC Amandeep Chawla |
| 19 |
// IJS Inder Jeet Singh |
| 20 |
// AB Amit Bhatti |
| 21 |
// |
| 22 |
// mm-dd-yy who M.m-bbb Description of changes, most recent on top. |
| 23 |
// |
| 24 |
// 01-05-15 AB 5.6-f122 Provide more functionalities to Plugin( Existing XMP packet, PacketInfo, OpenFlags, Error Callback and progress notification), |
| 25 |
// more standard handler access API getFileModDate,IsMetadataWritable,putXMP,getAssociatedResources. |
| 26 |
// New plugin handler for MPEG4 with Exif support. |
| 27 |
// 03-19-14 IJS 5.6-f098 [3709896] P2 Handler Modified to support spanned clip. |
| 28 |
// 02-13-13 ADC 5.6-f035 [3498686, 3497304] XMP GetFileModDate() is extremely slow. |
| 29 |
// |
| 30 |
// 10-10-12 ADC 5.5-c012 Changed internal implementation of common error notification infrastructure. |
| 31 |
// 08-08-12 AWL 5.5-c007 XMPCore error notifications for one case of XML parsing, no existing test failures. |
| 32 |
// |
| 33 |
// 06-23-09 AWL 5.0-c040-f049 Add hack to revert to global DLL locking. |
| 34 |
// 06-23-09 AWL 5.0-c039-f048 Fix atomic increment and decrement to be optional, not on Solaris. |
| 35 |
// 06-22-09 AWL 5.0-c038-f047 Fix locking code to use atomic increment and decrement. |
| 36 |
// 06-18-09 AWL 5.0-c037-f046 Minor tweaks from code review. |
| 37 |
// 06-12-09 AWL 5.0-c035-f045 Simplify the multiple lock permutations. |
| 38 |
// 06-11-09 AWL 5.0-c034-f043 Finish threading revamp, implement friendly reader/writer locking. |
| 39 |
// |
| 40 |
// ================================================================================================= |
| 41 |
#endif // AdobePrivate |
| 42 |
|
| 43 |
#include "public/include/XMP_Environment.h" |
| 44 |
|
| 45 |
#include "source/XMP_LibUtils.hpp" |
| 46 |
|
| 47 |
#include "source/UnicodeInlines.incl_cpp" |
| 48 |
|
| 49 |
#include <cstdio> |
| 50 |
#include <cstring> |
| 51 |
|
| 52 |
// ================================================================================================= |
| 53 |
|
| 54 |
#ifndef TraceThreadLocks |
| 55 |
#define TraceThreadLocks 0 |
| 56 |
#endif |
| 57 |
|
| 58 |
// ------------------------------------------------------------------------------------------------- |
| 59 |
|
| 60 |
extern "C" bool Initialize_LibUtils() |
| 61 |
{ |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
// ------------------------------------------------------------------------------------------------- |
| 66 |
|
| 67 |
extern "C" void Terminate_LibUtils(){ |
| 68 |
// Nothing to do. |
| 69 |
} |
| 70 |
|
| 71 |
// ================================================================================================= |
| 72 |
// Thread synchronization locks |
| 73 |
// ================================================================================================= |
| 74 |
|
| 75 |
XMP_ReadWriteLock::XMP_ReadWriteLock() : beingWritten(false) |
| 76 |
{ |
| 77 |
#if XMP_DebugBuild && HaveAtomicIncrDecr |
| 78 |
this->lockCount = 0; |
| 79 |
|
| 80 |
#if 0 //changing type of XMP_AtomicCounter from int32_t to std::atomic<int32_t> |
| 81 |
|
| 82 |
// Atomic counter must be 32 or 64 bits and naturally aligned. |
| 83 |
size_t counterSize = sizeof ( XMP_AtomicCounter ); |
| 84 |
size_t counterOffset = XMP_OffsetOf ( XMP_ReadWriteLock, lockCount ); |
| 85 |
XMP_Assert ( (counterSize == 4) || (counterSize == 8) ); // Counter must be 32 or 64 bits. |
| 86 |
XMP_Assert ( (counterOffset & (counterSize-1)) == 0 ); // Counter must be naturally aligned. |
| 87 |
#endif |
| 88 |
|
| 89 |
#endif |
| 90 |
XMP_BasicRWLock_Initialize ( this->lock ); |
| 91 |
#if TraceThreadLocks |
| 92 |
fprintf ( stderr, "Created lock %.8X\n", this ); |
| 93 |
#endif |
| 94 |
} |
| 95 |
|
| 96 |
// --------------------------------------------------------------------------------------------- |
| 97 |
|
| 98 |
XMP_ReadWriteLock::~XMP_ReadWriteLock() |
| 99 |
{ |
| 100 |
#if TraceThreadLocks |
| 101 |
fprintf ( stderr, "Deleting lock %.8X\n", this ); |
| 102 |
#endif |
| 103 |
#if XMP_DebugBuild && HaveAtomicIncrDecr |
| 104 |
XMP_Assert ( this->lockCount == 0 ); |
| 105 |
#endif |
| 106 |
XMP_BasicRWLock_Terminate ( this->lock ); |
| 107 |
} |
| 108 |
|
| 109 |
// --------------------------------------------------------------------------------------------- |
| 110 |
|
| 111 |
void XMP_ReadWriteLock::Acquire ( bool forWriting ) |
| 112 |
{ |
| 113 |
#if TraceThreadLocks |
| 114 |
fprintf ( stderr, "Acquiring lock %.8X for %s, count %d%s\n", |
| 115 |
this, (forWriting ? "writing" : "reading"), this->lockCount, (this->beingWritten ? ", being written" : "") ); |
| 116 |
#endif |
| 117 |
|
| 118 |
if ( forWriting ) { |
| 119 |
XMP_BasicRWLock_AcquireForWrite ( this->lock ); |
| 120 |
this->beingWritten = forWriting; |
| 121 |
#if XMP_DebugBuild && HaveAtomicIncrDecr |
| 122 |
XMP_Assert ( this->lockCount == 0 ); |
| 123 |
#endif |
| 124 |
} else { |
| 125 |
XMP_BasicRWLock_AcquireForRead ( this->lock ); |
| 126 |
XMP_Assert ( ! this->beingWritten ); |
| 127 |
} |
| 128 |
#if XMP_DebugBuild && HaveAtomicIncrDecr |
| 129 |
XMP_AtomicIncrement ( this->lockCount ); |
| 130 |
#endif |
| 131 |
|
| 132 |
|
| 133 |
#if TraceThreadLocks |
| 134 |
fprintf ( stderr, "Acquired lock %.8X for %s, count %d%s\n", |
| 135 |
this, (forWriting ? "writing" : "reading"), this->lockCount, (this->beingWritten ? ", being written" : "") ); |
| 136 |
#endif |
| 137 |
} |
| 138 |
|
| 139 |
// --------------------------------------------------------------------------------------------- |
| 140 |
|
| 141 |
void XMP_ReadWriteLock::Release() |
| 142 |
{ |
| 143 |
#if TraceThreadLocks |
| 144 |
fprintf ( stderr, "Releasing lock %.8X, count %d%s\n", this, this->lockCount, (this->beingWritten ? ", being written" : "") ); |
| 145 |
#endif |
| 146 |
|
| 147 |
#if XMP_DebugBuild && HaveAtomicIncrDecr |
| 148 |
XMP_Assert ( this->lockCount > 0 ); |
| 149 |
XMP_AtomicDecrement ( this->lockCount ); // ! Do these before unlocking, that might release a waiting thread. |
| 150 |
#endif |
| 151 |
bool forWriting = this->beingWritten; |
| 152 |
|
| 153 |
if ( forWriting ) { |
| 154 |
this->beingWritten = false; |
| 155 |
XMP_BasicRWLock_ReleaseFromWrite ( this->lock ); |
| 156 |
} else { |
| 157 |
XMP_BasicRWLock_ReleaseFromRead ( this->lock ); |
| 158 |
} |
| 159 |
|
| 160 |
#if TraceThreadLocks |
| 161 |
fprintf ( stderr, "Released lock %.8X, count %d%s\n", this, this->lockCount, (this->beingWritten ? ", being written" : "") ); |
| 162 |
#endif |
| 163 |
} |
| 164 |
|
| 165 |
// ================================================================================================= |
| 166 |
|
| 167 |
#if UseHomeGrownLock |
| 168 |
|
| 169 |
#if XMP_MacBuild | XMP_UNIXBuild | XMP_iOSBuild | XMP_AndroidBuild |
| 170 |
|
| 171 |
// ----------------------------------------------------------------------------------------- |
| 172 |
|
| 173 |
// About pthread mutexes and conditions: |
| 174 |
// |
| 175 |
// The mutex protecting the condition must be locked before waiting for the condition. A |
| 176 |
// thread can wait for a condition to be signaled by calling the pthread_cond_wait |
| 177 |
// subroutine. The subroutine atomically unlocks the mutex and blocks the calling thread |
| 178 |
// until the condition is signaled. When the call returns, the mutex is locked again. |
| 179 |
|
| 180 |
#define InitializeBasicMutex(mutex) { int err = pthread_mutex_init ( &mutex, 0 ); XMP_Enforce ( err == 0 ); } |
| 181 |
#define TerminateBasicMutex(mutex) { int err = pthread_mutex_destroy ( &mutex ); XMP_Enforce_NoThrow ( err == 0 ); } |
| 182 |
|
| 183 |
#define AcquireBasicMutex(mutex) { int err = pthread_mutex_lock ( &mutex ); XMP_Enforce ( err == 0 ); } |
| 184 |
#define ReleaseBasicMutex(mutex) { int err = pthread_mutex_unlock ( &mutex ); XMP_Enforce ( err == 0 ); } |
| 185 |
|
| 186 |
#define InitializeBasicQueue(queue) { int err = pthread_cond_init ( &queue, 0 ); XMP_Enforce ( err == 0 ); } |
| 187 |
#define TerminateBasicQueue(queue) { int err = pthread_cond_destroy ( &queue ); XMP_Enforce_NoThrow ( err == 0 ); } |
| 188 |
|
| 189 |
#define WaitOnBasicQueue(queue,mutex) { int err = pthread_cond_wait ( &queue, &mutex ); XMP_Enforce ( err == 0 ); } |
| 190 |
#define ReleaseOneBasicQueue(queue) { int err = pthread_cond_signal ( &queue ); XMP_Enforce ( err == 0 ); } |
| 191 |
#define ReleaseAllBasicQueue(queue) { int err = pthread_cond_broadcast ( &queue ); XMP_Enforce ( err == 0 ); } |
| 192 |
|
| 193 |
// ----------------------------------------------------------------------------------------- |
| 194 |
|
| 195 |
#elif XMP_WinBuild |
| 196 |
|
| 197 |
// ----------------------------------------------------------------------------------------- |
| 198 |
|
| 199 |
#define InitializeBasicMutex(mutex) { InitializeCriticalSection ( &mutex ); } |
| 200 |
#define TerminateBasicMutex(mutex) { DeleteCriticalSection ( &mutex ); } |
| 201 |
|
| 202 |
#define AcquireBasicMutex(mutex) { EnterCriticalSection ( &mutex ); } |
| 203 |
#define ReleaseBasicMutex(mutex) { LeaveCriticalSection ( &mutex ); } |
| 204 |
|
| 205 |
#if ! BuildLocksForWinXP |
| 206 |
|
| 207 |
// About Win32 condition variables (not on XP): |
| 208 |
// |
| 209 |
// Condition variables enable threads to atomically release a lock and enter the |
| 210 |
// sleeping state. They can be used with critical sections or slim reader/writer (SRW) |
| 211 |
// locks. Condition variables support operations that "wake one" or "wake all" waiting |
| 212 |
// threads. After a thread is woken, it re-acquires the lock it released when the thread |
| 213 |
// entered the sleeping state. |
| 214 |
|
| 215 |
#define InitializeBasicQueue(queue) { InitializeConditionVariable ( &queue ); } |
| 216 |
#define TerminateBasicQueue(queue) /* Do nothing. */ |
| 217 |
|
| 218 |
#define WaitOnBasicQueue(queue,mutex) \ |
| 219 |
{ BOOL ok = SleepConditionVariableCS ( &queue, &mutex, INFINITE /* timeout */ ); XMP_Enforce ( ok ); } |
| 220 |
|
| 221 |
#define ReleaseOneBasicQueue(queue) { WakeConditionVariable ( &queue ); } |
| 222 |
#define ReleaseAllBasicQueue(queue) { WakeAllConditionVariable ( &queue ); } |
| 223 |
|
| 224 |
#else |
| 225 |
|
| 226 |
// Need to create our own queue for Windows XP. This is not a general queue, it depends |
| 227 |
// on the usage inside XMP_HomeGrownLock where the queueMutex guarantees that the |
| 228 |
// queueing operations are done single threaded. |
| 229 |
|
| 230 |
#define InitializeBasicQueue(queue) /* Do nothing. */ |
| 231 |
#define TerminateBasicQueue(queue) /* Do nothing. */ |
| 232 |
|
| 233 |
#define WaitOnBasicQueue(queue,mutex) { queue.Wait ( mutex ); } |
| 234 |
#define ReleaseOneBasicQueue(queue) { queue.ReleaseOne(); } |
| 235 |
#define ReleaseAllBasicQueue(queue) { queue.ReleaseAll(); } |
| 236 |
|
| 237 |
// ------------------------------------------------------------------------------------- |
| 238 |
|
| 239 |
XMP_WinXP_HGQueue::XMP_WinXP_HGQueue() : queueEvent(0), waitCount(0), releaseAll(false) |
| 240 |
{ |
| 241 |
this->queueEvent = CreateEvent ( NULL, FALSE, TRUE, NULL ); // Auto reset, initially clear. |
| 242 |
XMP_Enforce ( this->queueEvent != 0 ); |
| 243 |
} |
| 244 |
|
| 245 |
// ------------------------------------------------------------------------------------- |
| 246 |
|
| 247 |
XMP_WinXP_HGQueue::~XMP_WinXP_HGQueue() |
| 248 |
{ |
| 249 |
CloseHandle ( this->queueEvent ); |
| 250 |
} |
| 251 |
|
| 252 |
// ------------------------------------------------------------------------------------- |
| 253 |
|
| 254 |
void XMP_WinXP_HGQueue::Wait ( XMP_BasicMutex & queueMutex ) |
| 255 |
{ |
| 256 |
++this->waitCount; // ! Does not need atomic increment, protected by queue mutex. |
| 257 |
ReleaseBasicMutex ( queueMutex ); |
| 258 |
DWORD status = WaitForSingleObject ( this->queueEvent, INFINITE ); |
| 259 |
if ( status != WAIT_OBJECT_0 ) XMP_Throw ( "Failure from WaitForSingleObject", kXMPErr_ExternalFailure ); |
| 260 |
AcquireBasicMutex ( queueMutex ); |
| 261 |
--this->waitCount; // ! Does not need atomic decrement, protected by queue mutex. |
| 262 |
|
| 263 |
if ( this->releaseAll ) { |
| 264 |
if ( this->waitCount == 0 ) { |
| 265 |
this->releaseAll = false; |
| 266 |
} else { |
| 267 |
BOOL ok = SetEvent ( this->queueEvent ); |
| 268 |
if ( ! ok ) XMP_Throw ( "Failure from SetEvent", kXMPErr_ExternalFailure ); |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
// ------------------------------------------------------------------------------------- |
| 274 |
|
| 275 |
void XMP_WinXP_HGQueue::ReleaseOne() |
| 276 |
{ |
| 277 |
XMP_Assert ( ! this->releaseAll ); |
| 278 |
BOOL ok = SetEvent ( this->queueEvent ); |
| 279 |
if ( ! ok ) XMP_Throw ( "Failure from SetEvent", kXMPErr_ExternalFailure ); |
| 280 |
} |
| 281 |
|
| 282 |
// ------------------------------------------------------------------------------------- |
| 283 |
|
| 284 |
void XMP_WinXP_HGQueue::ReleaseAll() |
| 285 |
{ |
| 286 |
this->releaseAll = true; |
| 287 |
BOOL ok = SetEvent ( this->queueEvent ); |
| 288 |
if ( ! ok ) XMP_Throw ( "Failure from SetEvent", kXMPErr_ExternalFailure ); |
| 289 |
} |
| 290 |
|
| 291 |
#endif |
| 292 |
|
| 293 |
// ----------------------------------------------------------------------------------------- |
| 294 |
|
| 295 |
#endif |
| 296 |
|
| 297 |
// ============================================================================================= |
| 298 |
|
| 299 |
XMP_HomeGrownLock::XMP_HomeGrownLock() : lockCount(0), readersWaiting(0), writersWaiting(0), beingWritten(false) |
| 300 |
{ |
| 301 |
InitializeBasicMutex ( this->queueMutex ); |
| 302 |
InitializeBasicQueue ( this->writerQueue ); |
| 303 |
InitializeBasicQueue ( this->readerQueue ); |
| 304 |
} |
| 305 |
|
| 306 |
// ============================================================================================= |
| 307 |
|
| 308 |
XMP_HomeGrownLock::~XMP_HomeGrownLock() |
| 309 |
{ |
| 310 |
TerminateBasicMutex ( this->queueMutex ); |
| 311 |
TerminateBasicQueue ( this->writerQueue ); |
| 312 |
TerminateBasicQueue ( this->readerQueue ); |
| 313 |
} |
| 314 |
|
| 315 |
// ============================================================================================= |
| 316 |
|
| 317 |
void XMP_HomeGrownLock::AcquireForRead() |
| 318 |
{ |
| 319 |
XMP_AutoMutex autoMutex ( &this->queueMutex ); |
| 320 |
|
| 321 |
++this->readersWaiting; // ! Does not need atomic increment, protected by queue mutex. |
| 322 |
while ( (this->beingWritten) || (this->writersWaiting > 0) ) { |
| 323 |
// Don't allow more readers if writers are waiting. |
| 324 |
WaitOnBasicQueue ( this->readerQueue, this->queueMutex ); |
| 325 |
} |
| 326 |
--this->readersWaiting; // ! Does not need atomic decrement, protected by queue mutex. |
| 327 |
XMP_Assert ( ! this->beingWritten ); |
| 328 |
|
| 329 |
++this->lockCount; // ! Does not need atomic increment, protected by queue mutex. |
| 330 |
} |
| 331 |
|
| 332 |
// ============================================================================================= |
| 333 |
|
| 334 |
void XMP_HomeGrownLock::AcquireForWrite() |
| 335 |
{ |
| 336 |
XMP_AutoMutex autoMutex ( &this->queueMutex ); |
| 337 |
|
| 338 |
++this->writersWaiting; // ! Does not need atomic increment, protected by queue mutex. |
| 339 |
while ( this->lockCount > 0 ) { |
| 340 |
WaitOnBasicQueue ( this->writerQueue, this->queueMutex ); |
| 341 |
} |
| 342 |
--this->writersWaiting; // ! Does not need atomic decrement, protected by queue mutex. |
| 343 |
XMP_Assert ( (! this->beingWritten) && (this->lockCount == 0) ); |
| 344 |
|
| 345 |
++this->lockCount; // ! Does not need atomic increment, protected by queue mutex. |
| 346 |
this->beingWritten = true; |
| 347 |
} |
| 348 |
|
| 349 |
// ============================================================================================= |
| 350 |
|
| 351 |
void XMP_HomeGrownLock::ReleaseFromRead() |
| 352 |
{ |
| 353 |
XMP_AutoMutex autoMutex ( &this->queueMutex ); |
| 354 |
|
| 355 |
XMP_Assert ( (! this->beingWritten) && (this->lockCount > 0) ); |
| 356 |
--this->lockCount; // ! Does not need atomic decrement, protected by queue mutex. |
| 357 |
|
| 358 |
if ( this->writersWaiting > 0 ) { |
| 359 |
ReleaseOneBasicQueue ( this->writerQueue ); |
| 360 |
} else if ( this->readersWaiting > 0 ) { |
| 361 |
ReleaseAllBasicQueue ( this->readerQueue ); |
| 362 |
} |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
// ============================================================================================= |
| 367 |
|
| 368 |
void XMP_HomeGrownLock::ReleaseFromWrite() |
| 369 |
{ |
| 370 |
XMP_AutoMutex autoMutex ( &this->queueMutex ); |
| 371 |
|
| 372 |
XMP_Assert ( this->beingWritten && (this->lockCount == 1) ); |
| 373 |
--this->lockCount; // ! Does not need atomic decrement, protected by queue mutex. |
| 374 |
this->beingWritten = false; |
| 375 |
|
| 376 |
if ( this->writersWaiting > 0 ) { |
| 377 |
ReleaseOneBasicQueue ( this->writerQueue ); |
| 378 |
} else if ( this->readersWaiting > 0 ) { |
| 379 |
ReleaseAllBasicQueue ( this->readerQueue ); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
// ============================================================================================= |
| 384 |
|
| 385 |
#endif |
| 386 |
|
| 387 |
// ================================================================================================= |
| 388 |
// Data structure dumping utilities |
| 389 |
// ================================ |
| 390 |
|
| 391 |
void |
| 392 |
DumpClearString ( const XMP_VarString & value, XMP_TextOutputProc outProc, void * refCon ) |
| 393 |
{ |
| 394 |
|
| 395 |
char buffer [20]; |
| 396 |
bool prevNormal; |
| 397 |
XMP_Status status = 0; |
| 398 |
|
| 399 |
XMP_StringPtr spanStart, spanEnd; |
| 400 |
XMP_StringPtr valueEnd = &value[0] + value.size(); |
| 401 |
|
| 402 |
spanStart = &value[0]; |
| 403 |
while ( spanStart < valueEnd ) { |
| 404 |
|
| 405 |
// Output the next span of regular characters. |
| 406 |
for ( spanEnd = spanStart; spanEnd < valueEnd; ++spanEnd ) { |
| 407 |
if ( *spanEnd > 0x7F ) break; |
| 408 |
if ( (*spanEnd < 0x20) && (*spanEnd != kTab) && (*spanEnd != kLF) ) break; |
| 409 |
} |
| 410 |
if ( spanStart != spanEnd ) status = (*outProc) ( refCon, spanStart, (XMP_StringLen)(spanEnd-spanStart) ); |
| 411 |
if ( status != 0 ) break; |
| 412 |
spanStart = spanEnd; |
| 413 |
|
| 414 |
// Output the next span of irregular characters. |
| 415 |
prevNormal = true; |
| 416 |
for ( spanEnd = spanStart; spanEnd < valueEnd; ++spanEnd ) { |
| 417 |
if ( ((0x20 <= *spanEnd) && (*spanEnd <= 0x7F)) || (*spanEnd == kTab) || (*spanEnd == kLF) ) break; |
| 418 |
char space = ' '; |
| 419 |
if ( prevNormal ) space = '<'; |
| 420 |
status = (*outProc) ( refCon, &space, 1 ); |
| 421 |
if ( status != 0 ) break; |
| 422 |
OutProcHexByte ( *spanEnd ); |
| 423 |
prevNormal = false; |
| 424 |
} |
| 425 |
if ( ! prevNormal ) { |
| 426 |
status = (*outProc) ( refCon, ">", 1 ); |
| 427 |
if ( status != 0 ) return; |
| 428 |
} |
| 429 |
spanStart = spanEnd; |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
} // DumpClearString |
| 434 |
|
| 435 |
// ------------------------------------------------------------------------------------------------- |
| 436 |
|
| 437 |
static void |
| 438 |
DumpStringMap ( const XMP_StringMap & map, XMP_StringPtr label, XMP_TextOutputProc outProc, void * refCon ) |
| 439 |
{ |
| 440 |
XMP_cStringMapPos currPos; |
| 441 |
XMP_cStringMapPos endPos = map.end(); |
| 442 |
|
| 443 |
size_t maxLen = 0; |
| 444 |
for ( currPos = map.begin(); currPos != endPos; ++currPos ) { |
| 445 |
size_t currLen = currPos->first.size(); |
| 446 |
if ( currLen > maxLen ) maxLen = currLen; |
| 447 |
} |
| 448 |
|
| 449 |
OutProcNewline(); |
| 450 |
OutProcLiteral ( label ); |
| 451 |
OutProcNewline(); |
| 452 |
|
| 453 |
for ( currPos = map.begin(); currPos != endPos; ++currPos ) { |
| 454 |
OutProcNChars ( " ", 2 ); |
| 455 |
DumpClearString ( currPos->first, outProc, refCon ); |
| 456 |
OutProcPadding ( maxLen - currPos->first.size() ); |
| 457 |
OutProcNChars ( " => ", 4 ); |
| 458 |
DumpClearString ( currPos->second, outProc, refCon ); |
| 459 |
OutProcNewline(); |
| 460 |
} |
| 461 |
|
| 462 |
} // DumpStringMap |
| 463 |
|
| 464 |
// ================================================================================================= |
| 465 |
// Namespace Tables |
| 466 |
// ================================================================================================= |
| 467 |
|
| 468 |
XMP_NamespaceTable::XMP_NamespaceTable ( const XMP_NamespaceTable & presets ) |
| 469 |
{ |
| 470 |
XMP_AutoLock presetLock ( &presets.lock, kXMP_ReadLock ); |
| 471 |
|
| 472 |
this->uriToPrefixMap = presets.uriToPrefixMap; |
| 473 |
this->prefixToURIMap = presets.prefixToURIMap; |
| 474 |
|
| 475 |
} // XMP_NamespaceTable::XMP_NamespaceTable |
| 476 |
|
| 477 |
// ================================================================================================= |
| 478 |
|
| 479 |
bool XMP_NamespaceTable::Define ( XMP_StringPtr _uri, XMP_StringPtr _suggPrefix, |
| 480 |
XMP_StringPtr * prefixPtr, XMP_StringLen * prefixLen ) |
| 481 |
{ |
| 482 |
XMP_AutoLock tableLock ( &this->lock, kXMP_WriteLock ); |
| 483 |
bool prefixMatches = false; |
| 484 |
|
| 485 |
XMP_Assert ( (_uri != 0) && (*_uri != 0) && (_suggPrefix != 0) && (*_suggPrefix != 0) ); |
| 486 |
|
| 487 |
XMP_VarString uri ( _uri ); |
| 488 |
XMP_VarString suggPrefix ( _suggPrefix ); |
| 489 |
if ( suggPrefix[suggPrefix.size()-1] != ':' ) suggPrefix += ':'; |
| 490 |
VerifySimpleXMLName ( _suggPrefix, _suggPrefix+suggPrefix.size()-1 ); // Exclude the colon. |
| 491 |
|
| 492 |
XMP_StringMapPos uriPos = this->uriToPrefixMap.find ( uri ); |
| 493 |
|
| 494 |
if ( uriPos == this->uriToPrefixMap.end() ) { |
| 495 |
|
| 496 |
// The URI is not yet registered, make sure we use a unique prefix. |
| 497 |
|
| 498 |
XMP_VarString uniqPrefix ( suggPrefix ); |
| 499 |
int suffix = 0; |
| 500 |
char buffer [32]; // AUDIT: Plenty of room for the "_%d_" suffix. |
| 501 |
|
| 502 |
while ( true ) { |
| 503 |
if ( this->prefixToURIMap.find ( uniqPrefix ) == this->prefixToURIMap.end() ) break; |
| 504 |
++suffix; |
| 505 |
snprintf ( buffer, sizeof(buffer), "_%d_:", suffix ); // AUDIT: Using sizeof for snprintf length is safe. |
| 506 |
uniqPrefix = suggPrefix; |
| 507 |
uniqPrefix.erase ( uniqPrefix.size()-1 ); // ! Remove the trailing ':'. |
| 508 |
uniqPrefix += buffer; |
| 509 |
} |
| 510 |
|
| 511 |
// Add the new namespace to both maps. |
| 512 |
|
| 513 |
XMP_StringPair newNS ( uri, uniqPrefix ); |
| 514 |
uriPos = this->uriToPrefixMap.insert ( this->uriToPrefixMap.end(), newNS ); |
| 515 |
|
| 516 |
newNS.first.swap ( newNS.second ); |
| 517 |
(void) this->prefixToURIMap.insert ( this->prefixToURIMap.end(), newNS ); |
| 518 |
|
| 519 |
} |
| 520 |
|
| 521 |
// Return the actual prefix and see if it matches the suggested prefix. |
| 522 |
|
| 523 |
if ( prefixPtr != 0 ) *prefixPtr = uriPos->second.c_str(); |
| 524 |
if ( prefixLen != 0 ) *prefixLen = (XMP_StringLen)uriPos->second.size(); |
| 525 |
|
| 526 |
prefixMatches = ( uriPos->second == suggPrefix ); |
| 527 |
return prefixMatches; |
| 528 |
|
| 529 |
} // XMP_NamespaceTable::Define |
| 530 |
|
| 531 |
// ================================================================================================= |
| 532 |
|
| 533 |
bool XMP_NamespaceTable::GetPrefix ( XMP_StringPtr _uri, XMP_StringPtr * prefixPtr, XMP_StringLen * prefixLen ) const |
| 534 |
{ |
| 535 |
XMP_AutoLock tableLock ( &this->lock, kXMP_ReadLock ); |
| 536 |
bool found = false; |
| 537 |
|
| 538 |
XMP_Assert ( (_uri != 0) && (*_uri != 0) ); |
| 539 |
|
| 540 |
XMP_VarString uri ( _uri ); |
| 541 |
XMP_cStringMapPos uriPos = this->uriToPrefixMap.find ( uri ); |
| 542 |
|
| 543 |
if ( uriPos != this->uriToPrefixMap.end() ) { |
| 544 |
if ( prefixPtr != 0 ) *prefixPtr = uriPos->second.c_str(); |
| 545 |
if ( prefixLen != 0 ) *prefixLen = (XMP_StringLen)uriPos->second.size(); |
| 546 |
found = true; |
| 547 |
} |
| 548 |
|
| 549 |
return found; |
| 550 |
|
| 551 |
} // XMP_NamespaceTable::GetPrefix |
| 552 |
|
| 553 |
// ================================================================================================= |
| 554 |
|
| 555 |
bool XMP_NamespaceTable::GetURI ( XMP_StringPtr _prefix, XMP_StringPtr * uriPtr, XMP_StringLen * uriLen ) const |
| 556 |
{ |
| 557 |
XMP_AutoLock tableLock ( &this->lock, kXMP_ReadLock ); |
| 558 |
|
| 559 |
bool found = false; |
| 560 |
|
| 561 |
XMP_Assert ( (_prefix != 0) && (*_prefix != 0) ); |
| 562 |
|
| 563 |
XMP_VarString prefix ( _prefix ); |
| 564 |
if ( prefix[prefix.size()-1] != ':' ) prefix += ':'; |
| 565 |
XMP_cStringMapPos prefixPos = this->prefixToURIMap.find ( prefix ); |
| 566 |
|
| 567 |
if ( prefixPos != this->prefixToURIMap.end() ) { |
| 568 |
if ( uriPtr != 0 ) *uriPtr = prefixPos->second.c_str(); |
| 569 |
if ( uriLen != 0 ) *uriLen = (XMP_StringLen)prefixPos->second.size(); |
| 570 |
found = true; |
| 571 |
} |
| 572 |
|
| 573 |
return found; |
| 574 |
|
| 575 |
} // XMP_NamespaceTable::GetURI |
| 576 |
|
| 577 |
// ================================================================================================= |
| 578 |
|
| 579 |
void XMP_NamespaceTable::Dump ( XMP_TextOutputProc outProc, void * refCon ) const |
| 580 |
{ |
| 581 |
XMP_AutoLock tableLock ( &this->lock, kXMP_ReadLock ); |
| 582 |
|
| 583 |
XMP_cStringMapPos p2uEnd = this->prefixToURIMap.end(); // ! Move up to avoid gcc complaints. |
| 584 |
XMP_cStringMapPos u2pEnd = this->uriToPrefixMap.end(); |
| 585 |
|
| 586 |
DumpStringMap ( this->prefixToURIMap, "Dumping namespace prefix to URI map", outProc, refCon ); |
| 587 |
|
| 588 |
if ( this->prefixToURIMap.size() != this->uriToPrefixMap.size() ) { |
| 589 |
OutProcLiteral ( "** bad namespace map sizes **" ); |
| 590 |
XMP_Throw ( "Fatal namespace map problem", kXMPErr_InternalFailure ); |
| 591 |
} |
| 592 |
|
| 593 |
for ( XMP_cStringMapPos nsLeft = this->prefixToURIMap.begin(); nsLeft != p2uEnd; ++nsLeft ) { |
| 594 |
|
| 595 |
XMP_cStringMapPos nsOther = this->uriToPrefixMap.find ( nsLeft->second ); |
| 596 |
if ( (nsOther == u2pEnd) || (nsLeft != this->prefixToURIMap.find ( nsOther->second )) ) { |
| 597 |
OutProcLiteral ( " ** bad namespace URI ** " ); |
| 598 |
DumpClearString ( nsLeft->second, outProc, refCon ); |
| 599 |
break; |
| 600 |
} |
| 601 |
|
| 602 |
for ( XMP_cStringMapPos nsRight = nsLeft; nsRight != p2uEnd; ++nsRight ) { |
| 603 |
if ( nsRight == nsLeft ) continue; // ! Can't start at nsLeft+1, no operator+! |
| 604 |
if ( nsLeft->second == nsRight->second ) { |
| 605 |
OutProcLiteral ( " ** duplicate namespace URI ** " ); |
| 606 |
DumpClearString ( nsLeft->second, outProc, refCon ); |
| 607 |
break; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
for ( XMP_cStringMapPos nsLeft = this->uriToPrefixMap.begin(); nsLeft != u2pEnd; ++nsLeft ) { |
| 614 |
|
| 615 |
XMP_cStringMapPos nsOther = this->prefixToURIMap.find ( nsLeft->second ); |
| 616 |
if ( (nsOther == p2uEnd) || (nsLeft != this->uriToPrefixMap.find ( nsOther->second )) ) { |
| 617 |
OutProcLiteral ( " ** bad namespace prefix ** " ); |
| 618 |
DumpClearString ( nsLeft->second, outProc, refCon ); |
| 619 |
break; |
| 620 |
} |
| 621 |
|
| 622 |
for ( XMP_cStringMapPos nsRight = nsLeft; nsRight != u2pEnd; ++nsRight ) { |
| 623 |
if ( nsRight == nsLeft ) continue; // ! Can't start at nsLeft+1, no operator+! |
| 624 |
if ( nsLeft->second == nsRight->second ) { |
| 625 |
OutProcLiteral ( " ** duplicate namespace prefix ** " ); |
| 626 |
DumpClearString ( nsLeft->second, outProc, refCon ); |
| 627 |
break; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
} |
| 632 |
|
| 633 |
} // XMP_NamespaceTable::Dump |
| 634 |
|
| 635 |
// ================================================================================================= |
| 636 |
static XMP_Bool matchdigit ( XMP_StringPtr text ) { |
| 637 |
if ( *text >= '0' && *text <= '9' ) |
| 638 |
return true; |
| 639 |
return false; |
| 640 |
} |
| 641 |
|
| 642 |
static XMP_Bool matchUpperCase ( XMP_StringPtr text ) { |
| 643 |
if ( *text >= 'A' && *text <= 'Z' ) |
| 644 |
return true; |
| 645 |
return false; |
| 646 |
} |
| 647 |
|
| 648 |
static XMP_Bool matchLowerCase ( XMP_StringPtr text ) { |
| 649 |
if ( *text >= 'a' && *text <= 'z' ) |
| 650 |
return true; |
| 651 |
return false; |
| 652 |
} |
| 653 |
|
| 654 |
/* matchhere: search for regexp at beginning of text */ |
| 655 |
static XMP_Bool matchhere ( XMP_StringPtr regexp, XMP_StringPtr text ) { |
| 656 |
if ( regexp[0] == '\0' ) |
| 657 |
return true; |
| 658 |
if ( regexp[0] == '\\' ) { |
| 659 |
if ( regexp[1] == 'd' ) { |
| 660 |
if ( matchdigit(text) ) |
| 661 |
return matchhere ( regexp+2, text+1 ); |
| 662 |
else |
| 663 |
return false; |
| 664 |
} |
| 665 |
else if ( regexp[1] == 'W' ) { |
| 666 |
if ( matchUpperCase(text) ) |
| 667 |
return matchhere ( regexp+2, text+1 ); |
| 668 |
else |
| 669 |
return false; |
| 670 |
} |
| 671 |
else if ( regexp[1] == 'w' ) { |
| 672 |
if ( matchLowerCase(text) ) |
| 673 |
return matchhere ( regexp+2, text+1 ); |
| 674 |
else |
| 675 |
return false; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
if ( regexp[0] == '$' && regexp[1] == '\0' ) |
| 680 |
return *text == '\0'; |
| 681 |
|
| 682 |
if ( *text != '\0' && regexp[0] == *text ) |
| 683 |
return matchhere ( regexp+1, text+1 ); |
| 684 |
return 0; |
| 685 |
} |
| 686 |
|
| 687 |
/* match: search for regexp anywhere in text */ |
| 688 |
static XMP_Bool match ( XMP_StringPtr regexp, XMP_StringPtr text ) { |
| 689 |
if ( regexp[0] == '^' ) |
| 690 |
return matchhere ( regexp+1, text ); |
| 691 |
do { /* must look even if string is empty */ |
| 692 |
if ( matchhere ( regexp, text ) ) |
| 693 |
return true; |
| 694 |
} while ( *text++ != '\0' ); |
| 695 |
return false; |
| 696 |
} |
| 697 |
|
| 698 |
XMP_Bool XMP_RegExp::Match ( XMP_StringPtr s ) |
| 699 |
{ |
| 700 |
if ( regExpStr.size() == 0 ) |
| 701 |
return true; |
| 702 |
if ( s == NULL ) |
| 703 |
return false; |
| 704 |
return match ( this->regExpStr.c_str(), s ); |
| 705 |
} |
| 706 |
// ================================================================================================= |