| 1 |
/* |
| 2 |
* tclEncoding.c -- |
| 3 |
* |
| 4 |
* Contains the implementation of the encoding conversion package. |
| 5 |
* |
| 6 |
* Copyright (c) 1996-1998 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution of |
| 9 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "tclInt.h" |
| 13 |
|
| 14 |
typedef size_t (LengthProc)(const char *src); |
| 15 |
|
| 16 |
/* |
| 17 |
* The following data structure represents an encoding, which describes how to |
| 18 |
* convert between various character sets and UTF-8. |
| 19 |
*/ |
| 20 |
|
| 21 |
typedef struct { |
| 22 |
char *name; /* Name of encoding. Malloced because (1) hash |
| 23 |
* table entry that owns this encoding may be |
| 24 |
* freed prior to this encoding being freed, |
| 25 |
* (2) string passed in the Tcl_EncodingType |
| 26 |
* structure may not be persistent. */ |
| 27 |
Tcl_EncodingConvertProc *toUtfProc; |
| 28 |
/* Function to convert from external encoding |
| 29 |
* into UTF-8. */ |
| 30 |
Tcl_EncodingConvertProc *fromUtfProc; |
| 31 |
/* Function to convert from UTF-8 into |
| 32 |
* external encoding. */ |
| 33 |
Tcl_EncodingFreeProc *freeProc; |
| 34 |
/* If non-NULL, function to call when this |
| 35 |
* encoding is deleted. */ |
| 36 |
int nullSize; /* Number of 0x00 bytes that signify |
| 37 |
* end-of-string in this encoding. This number |
| 38 |
* is used to determine the source string |
| 39 |
* length when the srcLen argument is |
| 40 |
* negative. This number can be 1 or 2. */ |
| 41 |
ClientData clientData; /* Arbitrary value associated with encoding |
| 42 |
* type. Passed to conversion functions. */ |
| 43 |
LengthProc *lengthProc; /* Function to compute length of |
| 44 |
* null-terminated strings in this encoding. |
| 45 |
* If nullSize is 1, this is strlen; if |
| 46 |
* nullSize is 2, this is a function that |
| 47 |
* returns the number of bytes in a 0x0000 |
| 48 |
* terminated string. */ |
| 49 |
int refCount; /* Number of uses of this structure. */ |
| 50 |
Tcl_HashEntry *hPtr; /* Hash table entry that owns this encoding. */ |
| 51 |
} Encoding; |
| 52 |
|
| 53 |
/* |
| 54 |
* The following structure is the clientData for a dynamically-loaded, |
| 55 |
* table-driven encoding created by LoadTableEncoding(). It maps between |
| 56 |
* Unicode and a single-byte, double-byte, or multibyte (1 or 2 bytes only) |
| 57 |
* encoding. |
| 58 |
*/ |
| 59 |
|
| 60 |
typedef struct { |
| 61 |
int fallback; /* Character (in this encoding) to substitute |
| 62 |
* when this encoding cannot represent a UTF-8 |
| 63 |
* character. */ |
| 64 |
char prefixBytes[256]; /* If a byte in the input stream is a lead |
| 65 |
* byte for a 2-byte sequence, the |
| 66 |
* corresponding entry in this array is 1, |
| 67 |
* otherwise it is 0. */ |
| 68 |
unsigned short **toUnicode; /* Two dimensional sparse matrix to map |
| 69 |
* characters from the encoding to Unicode. |
| 70 |
* Each element of the toUnicode array points |
| 71 |
* to an array of 256 shorts. If there is no |
| 72 |
* corresponding character in Unicode, the |
| 73 |
* value in the matrix is 0x0000. |
| 74 |
* malloc'd. */ |
| 75 |
unsigned short **fromUnicode; |
| 76 |
/* Two dimensional sparse matrix to map |
| 77 |
* characters from Unicode to the encoding. |
| 78 |
* Each element of the fromUnicode array |
| 79 |
* points to an array of 256 shorts. If there |
| 80 |
* is no corresponding character the encoding, |
| 81 |
* the value in the matrix is 0x0000. |
| 82 |
* malloc'd. */ |
| 83 |
} TableEncodingData; |
| 84 |
|
| 85 |
/* |
| 86 |
* Each of the following structures is the clientData for a dynamically-loaded |
| 87 |
* escape-driven encoding that is itself comprised of other simpler encodings. |
| 88 |
* An example is "iso-2022-jp", which uses escape sequences to switch between |
| 89 |
* ascii, jis0208, jis0212, gb2312, and ksc5601. Note that "escape-driven" |
| 90 |
* does not necessarily mean that the ESCAPE character is the character used |
| 91 |
* for switching character sets. |
| 92 |
*/ |
| 93 |
|
| 94 |
typedef struct { |
| 95 |
unsigned sequenceLen; /* Length of following string. */ |
| 96 |
char sequence[16]; /* Escape code that marks this encoding. */ |
| 97 |
char name[32]; /* Name for encoding. */ |
| 98 |
Encoding *encodingPtr; /* Encoding loaded using above name, or NULL |
| 99 |
* if this sub-encoding has not been needed |
| 100 |
* yet. */ |
| 101 |
} EscapeSubTable; |
| 102 |
|
| 103 |
typedef struct { |
| 104 |
int fallback; /* Character (in this encoding) to substitute |
| 105 |
* when this encoding cannot represent a UTF-8 |
| 106 |
* character. */ |
| 107 |
unsigned initLen; /* Length of following string. */ |
| 108 |
char init[16]; /* String to emit or expect before first char |
| 109 |
* in conversion. */ |
| 110 |
unsigned finalLen; /* Length of following string. */ |
| 111 |
char final[16]; /* String to emit or expect after last char in |
| 112 |
* conversion. */ |
| 113 |
char prefixBytes[256]; /* If a byte in the input stream is the first |
| 114 |
* character of one of the escape sequences in |
| 115 |
* the following array, the corresponding |
| 116 |
* entry in this array is 1, otherwise it is |
| 117 |
* 0. */ |
| 118 |
int numSubTables; /* Length of following array. */ |
| 119 |
EscapeSubTable subTables[TCLFLEXARRAY];/* Information about each EscapeSubTable used |
| 120 |
* by this encoding type. The actual size is |
| 121 |
* as large as necessary to hold all |
| 122 |
* EscapeSubTables. */ |
| 123 |
} EscapeEncodingData; |
| 124 |
|
| 125 |
/* |
| 126 |
* Constants used when loading an encoding file to identify the type of the |
| 127 |
* file. |
| 128 |
*/ |
| 129 |
|
| 130 |
#define ENCODING_SINGLEBYTE 0 |
| 131 |
#define ENCODING_DOUBLEBYTE 1 |
| 132 |
#define ENCODING_MULTIBYTE 2 |
| 133 |
#define ENCODING_ESCAPE 3 |
| 134 |
|
| 135 |
/* |
| 136 |
* A list of directories in which Tcl should look for *.enc files. This list |
| 137 |
* is shared by all threads. Access is governed by a mutex lock. |
| 138 |
*/ |
| 139 |
|
| 140 |
static TclInitProcessGlobalValueProc InitializeEncodingSearchPath; |
| 141 |
static ProcessGlobalValue encodingSearchPath = { |
| 142 |
0, 0, NULL, NULL, InitializeEncodingSearchPath, NULL, NULL |
| 143 |
}; |
| 144 |
|
| 145 |
/* |
| 146 |
* A map from encoding names to the directories in which their data files have |
| 147 |
* been seen. The string value of the map is shared by all threads. Access to |
| 148 |
* the shared string is governed by a mutex lock. |
| 149 |
*/ |
| 150 |
|
| 151 |
static ProcessGlobalValue encodingFileMap = { |
| 152 |
0, 0, NULL, NULL, NULL, NULL, NULL |
| 153 |
}; |
| 154 |
|
| 155 |
/* |
| 156 |
* A list of directories making up the "library path". Historically this |
| 157 |
* search path has served many uses, but the only one remaining is a base for |
| 158 |
* the encodingSearchPath above. If the application does not explicitly set |
| 159 |
* the encodingSearchPath, then it is initialized by appending /encoding |
| 160 |
* to each directory in this "libraryPath". |
| 161 |
*/ |
| 162 |
|
| 163 |
static ProcessGlobalValue libraryPath = { |
| 164 |
0, 0, NULL, NULL, TclpInitLibraryPath, NULL, NULL |
| 165 |
}; |
| 166 |
|
| 167 |
static int encodingsInitialized = 0; |
| 168 |
|
| 169 |
/* |
| 170 |
* Hash table that keeps track of all loaded Encodings. Keys are the string |
| 171 |
* names that represent the encoding, values are (Encoding *). |
| 172 |
*/ |
| 173 |
|
| 174 |
static Tcl_HashTable encodingTable; |
| 175 |
TCL_DECLARE_MUTEX(encodingMutex) |
| 176 |
|
| 177 |
/* |
| 178 |
* The following are used to hold the default and current system encodings. |
| 179 |
* If NULL is passed to one of the conversion routines, the current setting of |
| 180 |
* the system encoding is used to perform the conversion. |
| 181 |
*/ |
| 182 |
|
| 183 |
static Tcl_Encoding defaultEncoding = NULL; |
| 184 |
static Tcl_Encoding systemEncoding = NULL; |
| 185 |
Tcl_Encoding tclIdentityEncoding = NULL; |
| 186 |
|
| 187 |
/* |
| 188 |
* The following variable is used in the sparse matrix code for a |
| 189 |
* TableEncoding to represent a page in the table that has no entries. |
| 190 |
*/ |
| 191 |
|
| 192 |
static unsigned short emptyPage[256]; |
| 193 |
|
| 194 |
/* |
| 195 |
* Functions used only in this module. |
| 196 |
*/ |
| 197 |
|
| 198 |
static Tcl_EncodingConvertProc BinaryProc; |
| 199 |
static Tcl_DupInternalRepProc DupEncodingInternalRep; |
| 200 |
static Tcl_EncodingFreeProc EscapeFreeProc; |
| 201 |
static Tcl_EncodingConvertProc EscapeFromUtfProc; |
| 202 |
static Tcl_EncodingConvertProc EscapeToUtfProc; |
| 203 |
static void FillEncodingFileMap(void); |
| 204 |
static void FreeEncoding(Tcl_Encoding encoding); |
| 205 |
static Tcl_FreeInternalRepProc FreeEncodingInternalRep; |
| 206 |
static Encoding * GetTableEncoding(EscapeEncodingData *dataPtr, |
| 207 |
int state); |
| 208 |
static Tcl_Encoding LoadEncodingFile(Tcl_Interp *interp, |
| 209 |
const char *name); |
| 210 |
static Tcl_Encoding LoadTableEncoding(const char *name, int type, |
| 211 |
Tcl_Channel chan); |
| 212 |
static Tcl_Encoding LoadEscapeEncoding(const char *name, |
| 213 |
Tcl_Channel chan); |
| 214 |
static Tcl_Channel OpenEncodingFileChannel(Tcl_Interp *interp, |
| 215 |
const char *name); |
| 216 |
static Tcl_EncodingFreeProc TableFreeProc; |
| 217 |
static int TableFromUtfProc(ClientData clientData, |
| 218 |
const char *src, int srcLen, int flags, |
| 219 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 220 |
int *srcReadPtr, int *dstWrotePtr, |
| 221 |
int *dstCharsPtr); |
| 222 |
static int TableToUtfProc(ClientData clientData, const char *src, |
| 223 |
int srcLen, int flags, Tcl_EncodingState *statePtr, |
| 224 |
char *dst, int dstLen, int *srcReadPtr, |
| 225 |
int *dstWrotePtr, int *dstCharsPtr); |
| 226 |
static size_t unilen(const char *src); |
| 227 |
static int UnicodeToUtfProc(ClientData clientData, |
| 228 |
const char *src, int srcLen, int flags, |
| 229 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 230 |
int *srcReadPtr, int *dstWrotePtr, |
| 231 |
int *dstCharsPtr); |
| 232 |
static int UtfToUnicodeProc(ClientData clientData, |
| 233 |
const char *src, int srcLen, int flags, |
| 234 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 235 |
int *srcReadPtr, int *dstWrotePtr, |
| 236 |
int *dstCharsPtr); |
| 237 |
static int UtfToUtfProc(ClientData clientData, |
| 238 |
const char *src, int srcLen, int flags, |
| 239 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 240 |
int *srcReadPtr, int *dstWrotePtr, |
| 241 |
int *dstCharsPtr, int pureNullMode); |
| 242 |
static int UtfIntToUtfExtProc(ClientData clientData, |
| 243 |
const char *src, int srcLen, int flags, |
| 244 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 245 |
int *srcReadPtr, int *dstWrotePtr, |
| 246 |
int *dstCharsPtr); |
| 247 |
static int UtfExtToUtfIntProc(ClientData clientData, |
| 248 |
const char *src, int srcLen, int flags, |
| 249 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 250 |
int *srcReadPtr, int *dstWrotePtr, |
| 251 |
int *dstCharsPtr); |
| 252 |
static int Iso88591FromUtfProc(ClientData clientData, |
| 253 |
const char *src, int srcLen, int flags, |
| 254 |
Tcl_EncodingState *statePtr, char *dst, int dstLen, |
| 255 |
int *srcReadPtr, int *dstWrotePtr, |
| 256 |
int *dstCharsPtr); |
| 257 |
static int Iso88591ToUtfProc(ClientData clientData, |
| 258 |
const char *src, int srcLen, int flags, |
| 259 |
Tcl_EncodingState *statePtr, char *dst, |
| 260 |
int dstLen, int *srcReadPtr, int *dstWrotePtr, |
| 261 |
int *dstCharsPtr); |
| 262 |
|
| 263 |
/* |
| 264 |
* A Tcl_ObjType for holding a cached Tcl_Encoding in the twoPtrValue.ptr1 field |
| 265 |
* of the internalrep. This should help the lifetime of encodings be more useful. |
| 266 |
* See concerns raised in [Bug 1077262]. |
| 267 |
*/ |
| 268 |
|
| 269 |
static const Tcl_ObjType encodingType = { |
| 270 |
"encoding", FreeEncodingInternalRep, DupEncodingInternalRep, NULL, NULL |
| 271 |
}; |
| 272 |
|
| 273 |
/* |
| 274 |
*---------------------------------------------------------------------- |
| 275 |
* |
| 276 |
* Tcl_GetEncodingFromObj -- |
| 277 |
* |
| 278 |
* Writes to (*encodingPtr) the Tcl_Encoding value of (*objPtr), if |
| 279 |
* possible, and returns TCL_OK. If no such encoding exists, TCL_ERROR is |
| 280 |
* returned, and if interp is non-NULL, an error message is written |
| 281 |
* there. |
| 282 |
* |
| 283 |
* Results: |
| 284 |
* Standard Tcl return code. |
| 285 |
* |
| 286 |
* Side effects: |
| 287 |
* Caches the Tcl_Encoding value as the internal rep of (*objPtr). |
| 288 |
* |
| 289 |
*---------------------------------------------------------------------- |
| 290 |
*/ |
| 291 |
|
| 292 |
int |
| 293 |
Tcl_GetEncodingFromObj( |
| 294 |
Tcl_Interp *interp, |
| 295 |
Tcl_Obj *objPtr, |
| 296 |
Tcl_Encoding *encodingPtr) |
| 297 |
{ |
| 298 |
const char *name = TclGetString(objPtr); |
| 299 |
|
| 300 |
if (objPtr->typePtr != &encodingType) { |
| 301 |
Tcl_Encoding encoding = Tcl_GetEncoding(interp, name); |
| 302 |
|
| 303 |
if (encoding == NULL) { |
| 304 |
return TCL_ERROR; |
| 305 |
} |
| 306 |
TclFreeIntRep(objPtr); |
| 307 |
objPtr->internalRep.twoPtrValue.ptr1 = encoding; |
| 308 |
objPtr->typePtr = &encodingType; |
| 309 |
} |
| 310 |
*encodingPtr = Tcl_GetEncoding(NULL, name); |
| 311 |
return TCL_OK; |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
*---------------------------------------------------------------------- |
| 316 |
* |
| 317 |
* FreeEncodingInternalRep -- |
| 318 |
* |
| 319 |
* The Tcl_FreeInternalRepProc for the "encoding" Tcl_ObjType. |
| 320 |
* |
| 321 |
*---------------------------------------------------------------------- |
| 322 |
*/ |
| 323 |
|
| 324 |
static void |
| 325 |
FreeEncodingInternalRep( |
| 326 |
Tcl_Obj *objPtr) |
| 327 |
{ |
| 328 |
Tcl_FreeEncoding((Tcl_Encoding)objPtr->internalRep.twoPtrValue.ptr1); |
| 329 |
objPtr->typePtr = NULL; |
| 330 |
} |
| 331 |
|
| 332 |
/* |
| 333 |
*---------------------------------------------------------------------- |
| 334 |
* |
| 335 |
* DupEncodingInternalRep -- |
| 336 |
* |
| 337 |
* The Tcl_DupInternalRepProc for the "encoding" Tcl_ObjType. |
| 338 |
* |
| 339 |
*---------------------------------------------------------------------- |
| 340 |
*/ |
| 341 |
|
| 342 |
static void |
| 343 |
DupEncodingInternalRep( |
| 344 |
Tcl_Obj *srcPtr, |
| 345 |
Tcl_Obj *dupPtr) |
| 346 |
{ |
| 347 |
dupPtr->internalRep.twoPtrValue.ptr1 = Tcl_GetEncoding(NULL, srcPtr->bytes); |
| 348 |
} |
| 349 |
|
| 350 |
/* |
| 351 |
*---------------------------------------------------------------------- |
| 352 |
* |
| 353 |
* Tcl_GetEncodingSearchPath -- |
| 354 |
* |
| 355 |
* Keeps the per-thread copy of the encoding search path current with |
| 356 |
* changes to the global copy. |
| 357 |
* |
| 358 |
* Results: |
| 359 |
* Returns a "list" (Tcl_Obj *) that contains the encoding search path. |
| 360 |
* |
| 361 |
*---------------------------------------------------------------------- |
| 362 |
*/ |
| 363 |
|
| 364 |
Tcl_Obj * |
| 365 |
Tcl_GetEncodingSearchPath(void) |
| 366 |
{ |
| 367 |
return TclGetProcessGlobalValue(&encodingSearchPath); |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
*---------------------------------------------------------------------- |
| 372 |
* |
| 373 |
* Tcl_SetEncodingSearchPath -- |
| 374 |
* |
| 375 |
* Keeps the per-thread copy of the encoding search path current with |
| 376 |
* changes to the global copy. |
| 377 |
* |
| 378 |
*---------------------------------------------------------------------- |
| 379 |
*/ |
| 380 |
|
| 381 |
int |
| 382 |
Tcl_SetEncodingSearchPath( |
| 383 |
Tcl_Obj *searchPath) |
| 384 |
{ |
| 385 |
int dummy; |
| 386 |
|
| 387 |
if (TCL_ERROR == TclListObjLength(NULL, searchPath, &dummy)) { |
| 388 |
return TCL_ERROR; |
| 389 |
} |
| 390 |
TclSetProcessGlobalValue(&encodingSearchPath, searchPath, NULL); |
| 391 |
return TCL_OK; |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
*---------------------------------------------------------------------- |
| 396 |
* |
| 397 |
* TclGetLibraryPath -- |
| 398 |
* |
| 399 |
* Keeps the per-thread copy of the library path current with changes to |
| 400 |
* the global copy. |
| 401 |
* |
| 402 |
* Results: |
| 403 |
* Returns a "list" (Tcl_Obj *) that contains the library path. |
| 404 |
* |
| 405 |
*---------------------------------------------------------------------- |
| 406 |
*/ |
| 407 |
|
| 408 |
Tcl_Obj * |
| 409 |
TclGetLibraryPath(void) |
| 410 |
{ |
| 411 |
return TclGetProcessGlobalValue(&libraryPath); |
| 412 |
} |
| 413 |
|
| 414 |
/* |
| 415 |
*---------------------------------------------------------------------- |
| 416 |
* |
| 417 |
* TclSetLibraryPath -- |
| 418 |
* |
| 419 |
* Keeps the per-thread copy of the library path current with changes to |
| 420 |
* the global copy. |
| 421 |
* |
| 422 |
* Since the result of this routine is void, if searchPath is not a valid |
| 423 |
* list this routine silently does nothing. |
| 424 |
* |
| 425 |
*---------------------------------------------------------------------- |
| 426 |
*/ |
| 427 |
|
| 428 |
void |
| 429 |
TclSetLibraryPath( |
| 430 |
Tcl_Obj *path) |
| 431 |
{ |
| 432 |
int dummy; |
| 433 |
|
| 434 |
if (TCL_ERROR == TclListObjLength(NULL, path, &dummy)) { |
| 435 |
return; |
| 436 |
} |
| 437 |
TclSetProcessGlobalValue(&libraryPath, path, NULL); |
| 438 |
} |
| 439 |
|
| 440 |
/* |
| 441 |
*--------------------------------------------------------------------------- |
| 442 |
* |
| 443 |
* FillEncodingFileMap -- |
| 444 |
* |
| 445 |
* Called to update the encoding file map with the current value |
| 446 |
* of the encoding search path. |
| 447 |
* |
| 448 |
* Finds *.end files in the directories on the encoding search path and |
| 449 |
* stores the found pathnames in a map associated with the encoding name. |
| 450 |
* |
| 451 |
* If $dir is on the encoding search path and the file $dir/foo.enc is |
| 452 |
* found, stores a "foo" -> $dir entry in the map. if the "foo" encoding |
| 453 |
* is needed later, the $dir/foo.enc name can be quickly constructed in |
| 454 |
* order to read the encoding data. |
| 455 |
* |
| 456 |
* Results: |
| 457 |
* None. |
| 458 |
* |
| 459 |
* Side effects: |
| 460 |
* Entries are added to the encoding file map. |
| 461 |
* |
| 462 |
*--------------------------------------------------------------------------- |
| 463 |
*/ |
| 464 |
|
| 465 |
static void |
| 466 |
FillEncodingFileMap(void) |
| 467 |
{ |
| 468 |
int i, numDirs = 0; |
| 469 |
Tcl_Obj *map, *searchPath; |
| 470 |
|
| 471 |
searchPath = Tcl_GetEncodingSearchPath(); |
| 472 |
Tcl_IncrRefCount(searchPath); |
| 473 |
TclListObjLength(NULL, searchPath, &numDirs); |
| 474 |
map = Tcl_NewDictObj(); |
| 475 |
Tcl_IncrRefCount(map); |
| 476 |
|
| 477 |
for (i = numDirs-1; i >= 0; i--) { |
| 478 |
/* |
| 479 |
* Iterate backwards through the search path so as we overwrite |
| 480 |
* entries found, we favor files earlier on the search path. |
| 481 |
*/ |
| 482 |
|
| 483 |
int j, numFiles; |
| 484 |
Tcl_Obj *directory, *matchFileList; |
| 485 |
Tcl_Obj **filev; |
| 486 |
Tcl_GlobTypeData readableFiles = { |
| 487 |
TCL_GLOB_TYPE_FILE, TCL_GLOB_PERM_R, NULL, NULL |
| 488 |
}; |
| 489 |
|
| 490 |
TclNewObj(matchFileList); |
| 491 |
Tcl_ListObjIndex(NULL, searchPath, i, &directory); |
| 492 |
Tcl_IncrRefCount(directory); |
| 493 |
Tcl_IncrRefCount(matchFileList); |
| 494 |
Tcl_FSMatchInDirectory(NULL, matchFileList, directory, "*.enc", |
| 495 |
&readableFiles); |
| 496 |
|
| 497 |
TclListObjGetElements(NULL, matchFileList, &numFiles, &filev); |
| 498 |
for (j=0; j<numFiles; j++) { |
| 499 |
Tcl_Obj *encodingName, *fileObj; |
| 500 |
|
| 501 |
fileObj = TclPathPart(NULL, filev[j], TCL_PATH_TAIL); |
| 502 |
encodingName = TclPathPart(NULL, fileObj, TCL_PATH_ROOT); |
| 503 |
Tcl_DictObjPut(NULL, map, encodingName, directory); |
| 504 |
Tcl_DecrRefCount(fileObj); |
| 505 |
Tcl_DecrRefCount(encodingName); |
| 506 |
} |
| 507 |
Tcl_DecrRefCount(matchFileList); |
| 508 |
Tcl_DecrRefCount(directory); |
| 509 |
} |
| 510 |
Tcl_DecrRefCount(searchPath); |
| 511 |
TclSetProcessGlobalValue(&encodingFileMap, map, NULL); |
| 512 |
Tcl_DecrRefCount(map); |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
*--------------------------------------------------------------------------- |
| 517 |
* |
| 518 |
* TclInitEncodingSubsystem -- |
| 519 |
* |
| 520 |
* Initialize all resources used by this subsystem on a per-process |
| 521 |
* basis. |
| 522 |
* |
| 523 |
* Results: |
| 524 |
* None. |
| 525 |
* |
| 526 |
* Side effects: |
| 527 |
* Depends on the memory, object, and IO subsystems. |
| 528 |
* |
| 529 |
*--------------------------------------------------------------------------- |
| 530 |
*/ |
| 531 |
|
| 532 |
void |
| 533 |
TclInitEncodingSubsystem(void) |
| 534 |
{ |
| 535 |
Tcl_EncodingType type; |
| 536 |
TableEncodingData *dataPtr; |
| 537 |
unsigned size; |
| 538 |
unsigned short i; |
| 539 |
union { |
| 540 |
char c; |
| 541 |
short s; |
| 542 |
} isLe; |
| 543 |
|
| 544 |
if (encodingsInitialized) { |
| 545 |
return; |
| 546 |
} |
| 547 |
|
| 548 |
isLe.s = 1; |
| 549 |
Tcl_MutexLock(&encodingMutex); |
| 550 |
Tcl_InitHashTable(&encodingTable, TCL_STRING_KEYS); |
| 551 |
Tcl_MutexUnlock(&encodingMutex); |
| 552 |
|
| 553 |
/* |
| 554 |
* Create a few initial encodings. UTF-8 to UTF-8 translation is not a |
| 555 |
* no-op because it turns a stream of improperly formed UTF-8 into a |
| 556 |
* properly formed stream. |
| 557 |
*/ |
| 558 |
|
| 559 |
type.encodingName = "identity"; |
| 560 |
type.toUtfProc = BinaryProc; |
| 561 |
type.fromUtfProc = BinaryProc; |
| 562 |
type.freeProc = NULL; |
| 563 |
type.nullSize = 1; |
| 564 |
type.clientData = NULL; |
| 565 |
tclIdentityEncoding = Tcl_CreateEncoding(&type); |
| 566 |
|
| 567 |
type.encodingName = "utf-8"; |
| 568 |
type.toUtfProc = UtfExtToUtfIntProc; |
| 569 |
type.fromUtfProc = UtfIntToUtfExtProc; |
| 570 |
type.freeProc = NULL; |
| 571 |
type.nullSize = 1; |
| 572 |
type.clientData = NULL; |
| 573 |
Tcl_CreateEncoding(&type); |
| 574 |
|
| 575 |
type.encodingName = "unicode"; |
| 576 |
type.toUtfProc = UnicodeToUtfProc; |
| 577 |
type.fromUtfProc = UtfToUnicodeProc; |
| 578 |
type.freeProc = NULL; |
| 579 |
type.nullSize = 2; |
| 580 |
type.clientData = INT2PTR(isLe.c); |
| 581 |
Tcl_CreateEncoding(&type); |
| 582 |
|
| 583 |
/* |
| 584 |
* Need the iso8859-1 encoding in order to process binary data, so force |
| 585 |
* it to always be embedded. Note that this encoding *must* be a proper |
| 586 |
* table encoding or some of the escape encodings crash! Hence the ugly |
| 587 |
* code to duplicate the structure of a table encoding here. |
| 588 |
*/ |
| 589 |
|
| 590 |
dataPtr = (TableEncodingData *)ckalloc(sizeof(TableEncodingData)); |
| 591 |
memset(dataPtr, 0, sizeof(TableEncodingData)); |
| 592 |
dataPtr->fallback = '?'; |
| 593 |
|
| 594 |
size = 256*(sizeof(unsigned short *) + sizeof(unsigned short)); |
| 595 |
dataPtr->toUnicode = (unsigned short **)ckalloc(size); |
| 596 |
memset(dataPtr->toUnicode, 0, size); |
| 597 |
dataPtr->fromUnicode = (unsigned short **)ckalloc(size); |
| 598 |
memset(dataPtr->fromUnicode, 0, size); |
| 599 |
|
| 600 |
dataPtr->toUnicode[0] = (unsigned short *) (dataPtr->toUnicode + 256); |
| 601 |
dataPtr->fromUnicode[0] = (unsigned short *) (dataPtr->fromUnicode + 256); |
| 602 |
for (i=1 ; i<256 ; i++) { |
| 603 |
dataPtr->toUnicode[i] = emptyPage; |
| 604 |
dataPtr->fromUnicode[i] = emptyPage; |
| 605 |
} |
| 606 |
|
| 607 |
for (i=0 ; i<256 ; i++) { |
| 608 |
dataPtr->toUnicode[0][i] = i; |
| 609 |
dataPtr->fromUnicode[0][i] = i; |
| 610 |
} |
| 611 |
|
| 612 |
type.encodingName = "iso8859-1"; |
| 613 |
type.toUtfProc = Iso88591ToUtfProc; |
| 614 |
type.fromUtfProc = Iso88591FromUtfProc; |
| 615 |
type.freeProc = TableFreeProc; |
| 616 |
type.nullSize = 1; |
| 617 |
type.clientData = dataPtr; |
| 618 |
defaultEncoding = Tcl_CreateEncoding(&type); |
| 619 |
systemEncoding = Tcl_GetEncoding(NULL, type.encodingName); |
| 620 |
|
| 621 |
encodingsInitialized = 1; |
| 622 |
} |
| 623 |
|
| 624 |
/* |
| 625 |
*---------------------------------------------------------------------- |
| 626 |
* |
| 627 |
* TclFinalizeEncodingSubsystem -- |
| 628 |
* |
| 629 |
* Release the state associated with the encoding subsystem. |
| 630 |
* |
| 631 |
* Results: |
| 632 |
* None. |
| 633 |
* |
| 634 |
* Side effects: |
| 635 |
* Frees all of the encodings. |
| 636 |
* |
| 637 |
*---------------------------------------------------------------------- |
| 638 |
*/ |
| 639 |
|
| 640 |
void |
| 641 |
TclFinalizeEncodingSubsystem(void) |
| 642 |
{ |
| 643 |
Tcl_HashSearch search; |
| 644 |
Tcl_HashEntry *hPtr; |
| 645 |
|
| 646 |
Tcl_MutexLock(&encodingMutex); |
| 647 |
encodingsInitialized = 0; |
| 648 |
FreeEncoding(systemEncoding); |
| 649 |
systemEncoding = NULL; |
| 650 |
defaultEncoding = NULL; |
| 651 |
FreeEncoding(tclIdentityEncoding); |
| 652 |
tclIdentityEncoding = NULL; |
| 653 |
|
| 654 |
hPtr = Tcl_FirstHashEntry(&encodingTable, &search); |
| 655 |
while (hPtr != NULL) { |
| 656 |
/* |
| 657 |
* Call FreeEncoding instead of doing it directly to handle refcounts |
| 658 |
* like escape encodings use. [Bug 524674] Make sure to call |
| 659 |
* Tcl_FirstHashEntry repeatedly so that all encodings are eventually |
| 660 |
* cleaned up. |
| 661 |
*/ |
| 662 |
|
| 663 |
FreeEncoding((Tcl_Encoding)Tcl_GetHashValue(hPtr)); |
| 664 |
hPtr = Tcl_FirstHashEntry(&encodingTable, &search); |
| 665 |
} |
| 666 |
|
| 667 |
Tcl_DeleteHashTable(&encodingTable); |
| 668 |
Tcl_MutexUnlock(&encodingMutex); |
| 669 |
} |
| 670 |
|
| 671 |
/* |
| 672 |
*------------------------------------------------------------------------- |
| 673 |
* |
| 674 |
* Tcl_GetDefaultEncodingDir -- |
| 675 |
* |
| 676 |
* Legacy public interface to retrieve first directory in the encoding |
| 677 |
* searchPath. |
| 678 |
* |
| 679 |
* Results: |
| 680 |
* The directory pathname, as a string, or NULL for an empty encoding |
| 681 |
* search path. |
| 682 |
* |
| 683 |
* Side effects: |
| 684 |
* None. |
| 685 |
* |
| 686 |
*------------------------------------------------------------------------- |
| 687 |
*/ |
| 688 |
|
| 689 |
const char * |
| 690 |
Tcl_GetDefaultEncodingDir(void) |
| 691 |
{ |
| 692 |
int numDirs; |
| 693 |
Tcl_Obj *first, *searchPath = Tcl_GetEncodingSearchPath(); |
| 694 |
|
| 695 |
TclListObjLength(NULL, searchPath, &numDirs); |
| 696 |
if (numDirs == 0) { |
| 697 |
return NULL; |
| 698 |
} |
| 699 |
Tcl_ListObjIndex(NULL, searchPath, 0, &first); |
| 700 |
|
| 701 |
return TclGetString(first); |
| 702 |
} |
| 703 |
|
| 704 |
/* |
| 705 |
*------------------------------------------------------------------------- |
| 706 |
* |
| 707 |
* Tcl_SetDefaultEncodingDir -- |
| 708 |
* |
| 709 |
* Legacy public interface to set the first directory in the encoding |
| 710 |
* search path. |
| 711 |
* |
| 712 |
* Results: |
| 713 |
* None. |
| 714 |
* |
| 715 |
* Side effects: |
| 716 |
* Modifies the encoding search path. |
| 717 |
* |
| 718 |
*------------------------------------------------------------------------- |
| 719 |
*/ |
| 720 |
|
| 721 |
void |
| 722 |
Tcl_SetDefaultEncodingDir( |
| 723 |
const char *path) |
| 724 |
{ |
| 725 |
Tcl_Obj *searchPath = Tcl_GetEncodingSearchPath(); |
| 726 |
Tcl_Obj *directory = Tcl_NewStringObj(path, -1); |
| 727 |
|
| 728 |
searchPath = Tcl_DuplicateObj(searchPath); |
| 729 |
Tcl_ListObjReplace(NULL, searchPath, 0, 0, 1, &directory); |
| 730 |
Tcl_SetEncodingSearchPath(searchPath); |
| 731 |
} |
| 732 |
|
| 733 |
/* |
| 734 |
*------------------------------------------------------------------------- |
| 735 |
* |
| 736 |
* Tcl_GetEncoding -- |
| 737 |
* |
| 738 |
* Given the name of a encoding, find the corresponding Tcl_Encoding |
| 739 |
* token. If the encoding did not already exist, Tcl attempts to |
| 740 |
* dynamically load an encoding by that name. |
| 741 |
* |
| 742 |
* Results: |
| 743 |
* Returns a token that represents the encoding. If the name didn't refer |
| 744 |
* to any known or loadable encoding, NULL is returned. If NULL was |
| 745 |
* returned, an error message is left in interp's result object, unless |
| 746 |
* interp was NULL. |
| 747 |
* |
| 748 |
* Side effects: |
| 749 |
* LoadEncodingFile is called if necessary. |
| 750 |
* |
| 751 |
*------------------------------------------------------------------------- |
| 752 |
*/ |
| 753 |
|
| 754 |
Tcl_Encoding |
| 755 |
Tcl_GetEncoding( |
| 756 |
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */ |
| 757 |
const char *name) /* The name of the desired encoding. */ |
| 758 |
{ |
| 759 |
Tcl_HashEntry *hPtr; |
| 760 |
Encoding *encodingPtr; |
| 761 |
|
| 762 |
Tcl_MutexLock(&encodingMutex); |
| 763 |
if (name == NULL) { |
| 764 |
encodingPtr = (Encoding *) systemEncoding; |
| 765 |
encodingPtr->refCount++; |
| 766 |
Tcl_MutexUnlock(&encodingMutex); |
| 767 |
return systemEncoding; |
| 768 |
} |
| 769 |
|
| 770 |
hPtr = Tcl_FindHashEntry(&encodingTable, name); |
| 771 |
if (hPtr != NULL) { |
| 772 |
encodingPtr = (Encoding *)Tcl_GetHashValue(hPtr); |
| 773 |
encodingPtr->refCount++; |
| 774 |
Tcl_MutexUnlock(&encodingMutex); |
| 775 |
return (Tcl_Encoding) encodingPtr; |
| 776 |
} |
| 777 |
Tcl_MutexUnlock(&encodingMutex); |
| 778 |
|
| 779 |
return LoadEncodingFile(interp, name); |
| 780 |
} |
| 781 |
|
| 782 |
/* |
| 783 |
*--------------------------------------------------------------------------- |
| 784 |
* |
| 785 |
* Tcl_FreeEncoding -- |
| 786 |
* |
| 787 |
* Releases an encoding allocated by Tcl_CreateEncoding() or |
| 788 |
* Tcl_GetEncoding(). |
| 789 |
* |
| 790 |
* Results: |
| 791 |
* None. |
| 792 |
* |
| 793 |
* Side effects: |
| 794 |
* The reference count associated with the encoding is decremented and |
| 795 |
* the encoding is deleted if nothing is using it anymore. |
| 796 |
* |
| 797 |
*--------------------------------------------------------------------------- |
| 798 |
*/ |
| 799 |
|
| 800 |
void |
| 801 |
Tcl_FreeEncoding( |
| 802 |
Tcl_Encoding encoding) |
| 803 |
{ |
| 804 |
Tcl_MutexLock(&encodingMutex); |
| 805 |
FreeEncoding(encoding); |
| 806 |
Tcl_MutexUnlock(&encodingMutex); |
| 807 |
} |
| 808 |
|
| 809 |
/* |
| 810 |
*---------------------------------------------------------------------- |
| 811 |
* |
| 812 |
* FreeEncoding -- |
| 813 |
* |
| 814 |
* Decrements the reference count of an encoding. The caller must hold |
| 815 |
* encodingMutes. |
| 816 |
* |
| 817 |
* Results: |
| 818 |
* None. |
| 819 |
* |
| 820 |
* Side effects: |
| 821 |
* Releases the resource for an encoding if it is now unused. |
| 822 |
* The reference count associated with the encoding is decremented and |
| 823 |
* the encoding may be deleted if nothing is using it anymore. |
| 824 |
* |
| 825 |
*---------------------------------------------------------------------- |
| 826 |
*/ |
| 827 |
|
| 828 |
static void |
| 829 |
FreeEncoding( |
| 830 |
Tcl_Encoding encoding) |
| 831 |
{ |
| 832 |
Encoding *encodingPtr = (Encoding *) encoding; |
| 833 |
|
| 834 |
if (encodingPtr == NULL) { |
| 835 |
return; |
| 836 |
} |
| 837 |
if (encodingPtr->refCount<=0) { |
| 838 |
Tcl_Panic("FreeEncoding: refcount problem !!!"); |
| 839 |
} |
| 840 |
if (encodingPtr->refCount-- <= 1) { |
| 841 |
if (encodingPtr->freeProc != NULL) { |
| 842 |
encodingPtr->freeProc(encodingPtr->clientData); |
| 843 |
} |
| 844 |
if (encodingPtr->hPtr != NULL) { |
| 845 |
Tcl_DeleteHashEntry(encodingPtr->hPtr); |
| 846 |
} |
| 847 |
if (encodingPtr->name) { |
| 848 |
ckfree(encodingPtr->name); |
| 849 |
} |
| 850 |
ckfree(encodingPtr); |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
/* |
| 855 |
*------------------------------------------------------------------------- |
| 856 |
* |
| 857 |
* Tcl_GetEncodingName -- |
| 858 |
* |
| 859 |
* Given an encoding, return the name that was used to construct the |
| 860 |
* encoding. |
| 861 |
* |
| 862 |
* Results: |
| 863 |
* The name of the encoding. |
| 864 |
* |
| 865 |
* Side effects: |
| 866 |
* None. |
| 867 |
* |
| 868 |
*--------------------------------------------------------------------------- |
| 869 |
*/ |
| 870 |
|
| 871 |
const char * |
| 872 |
Tcl_GetEncodingName( |
| 873 |
Tcl_Encoding encoding) /* The encoding whose name to fetch. */ |
| 874 |
{ |
| 875 |
if (encoding == NULL) { |
| 876 |
encoding = systemEncoding; |
| 877 |
} |
| 878 |
|
| 879 |
return ((Encoding *) encoding)->name; |
| 880 |
} |
| 881 |
|
| 882 |
/* |
| 883 |
*------------------------------------------------------------------------- |
| 884 |
* |
| 885 |
* Tcl_GetEncodingNames -- |
| 886 |
* |
| 887 |
* Get the list of all known encodings, including the ones stored as |
| 888 |
* files on disk in the encoding path. |
| 889 |
* |
| 890 |
* Results: |
| 891 |
* Modifies interp's result object to hold a list of all the available |
| 892 |
* encodings. |
| 893 |
* |
| 894 |
* Side effects: |
| 895 |
* None. |
| 896 |
* |
| 897 |
*------------------------------------------------------------------------- |
| 898 |
*/ |
| 899 |
|
| 900 |
void |
| 901 |
Tcl_GetEncodingNames( |
| 902 |
Tcl_Interp *interp) /* Interp to hold result. */ |
| 903 |
{ |
| 904 |
Tcl_HashTable table; |
| 905 |
Tcl_HashSearch search; |
| 906 |
Tcl_HashEntry *hPtr; |
| 907 |
Tcl_Obj *map, *name, *result; |
| 908 |
Tcl_DictSearch mapSearch; |
| 909 |
int dummy, done = 0; |
| 910 |
|
| 911 |
TclNewObj(result); |
| 912 |
Tcl_InitObjHashTable(&table); |
| 913 |
|
| 914 |
/* |
| 915 |
* Copy encoding names from loaded encoding table to table. |
| 916 |
*/ |
| 917 |
|
| 918 |
Tcl_MutexLock(&encodingMutex); |
| 919 |
for (hPtr = Tcl_FirstHashEntry(&encodingTable, &search); hPtr != NULL; |
| 920 |
hPtr = Tcl_NextHashEntry(&search)) { |
| 921 |
Encoding *encodingPtr = (Encoding *)Tcl_GetHashValue(hPtr); |
| 922 |
|
| 923 |
Tcl_CreateHashEntry(&table, |
| 924 |
Tcl_NewStringObj(encodingPtr->name, -1), &dummy); |
| 925 |
} |
| 926 |
Tcl_MutexUnlock(&encodingMutex); |
| 927 |
|
| 928 |
FillEncodingFileMap(); |
| 929 |
map = TclGetProcessGlobalValue(&encodingFileMap); |
| 930 |
|
| 931 |
/* |
| 932 |
* Copy encoding names from encoding file map to table. |
| 933 |
*/ |
| 934 |
|
| 935 |
Tcl_DictObjFirst(NULL, map, &mapSearch, &name, NULL, &done); |
| 936 |
for (; !done; Tcl_DictObjNext(&mapSearch, &name, NULL, &done)) { |
| 937 |
Tcl_CreateHashEntry(&table, name, &dummy); |
| 938 |
} |
| 939 |
|
| 940 |
/* |
| 941 |
* Pull all encoding names from table into the result list. |
| 942 |
*/ |
| 943 |
|
| 944 |
for (hPtr = Tcl_FirstHashEntry(&table, &search); hPtr != NULL; |
| 945 |
hPtr = Tcl_NextHashEntry(&search)) { |
| 946 |
Tcl_ListObjAppendElement(NULL, result, |
| 947 |
(Tcl_Obj *) Tcl_GetHashKey(&table, hPtr)); |
| 948 |
} |
| 949 |
Tcl_SetObjResult(interp, result); |
| 950 |
Tcl_DeleteHashTable(&table); |
| 951 |
} |
| 952 |
|
| 953 |
/* |
| 954 |
*------------------------------------------------------------------------ |
| 955 |
* |
| 956 |
* Tcl_SetSystemEncoding -- |
| 957 |
* |
| 958 |
* Sets the default encoding that should be used whenever the user passes |
| 959 |
* a NULL value in to one of the conversion routines. If the supplied |
| 960 |
* name is NULL, the system encoding is reset to the default system |
| 961 |
* encoding. |
| 962 |
* |
| 963 |
* Results: |
| 964 |
* The return value is TCL_OK if the system encoding was successfully set |
| 965 |
* to the encoding specified by name, TCL_ERROR otherwise. If TCL_ERROR |
| 966 |
* is returned, an error message is left in interp's result object, |
| 967 |
* unless interp was NULL. |
| 968 |
* |
| 969 |
* Side effects: |
| 970 |
* The reference count of the new system encoding is incremented. The |
| 971 |
* reference count of the old system encoding is decremented and it may |
| 972 |
* be freed. All VFS cached information is invalidated. |
| 973 |
* |
| 974 |
*------------------------------------------------------------------------ |
| 975 |
*/ |
| 976 |
|
| 977 |
int |
| 978 |
Tcl_SetSystemEncoding( |
| 979 |
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */ |
| 980 |
const char *name) /* The name of the desired encoding, or NULL/"" |
| 981 |
* to reset to default encoding. */ |
| 982 |
{ |
| 983 |
Tcl_Encoding encoding; |
| 984 |
Encoding *encodingPtr; |
| 985 |
|
| 986 |
if (!name || !*name) { |
| 987 |
Tcl_MutexLock(&encodingMutex); |
| 988 |
encoding = defaultEncoding; |
| 989 |
encodingPtr = (Encoding *) encoding; |
| 990 |
encodingPtr->refCount++; |
| 991 |
Tcl_MutexUnlock(&encodingMutex); |
| 992 |
} else { |
| 993 |
encoding = Tcl_GetEncoding(interp, name); |
| 994 |
if (encoding == NULL) { |
| 995 |
return TCL_ERROR; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
Tcl_MutexLock(&encodingMutex); |
| 1000 |
FreeEncoding(systemEncoding); |
| 1001 |
systemEncoding = encoding; |
| 1002 |
Tcl_MutexUnlock(&encodingMutex); |
| 1003 |
Tcl_FSMountsChanged(NULL); |
| 1004 |
|
| 1005 |
return TCL_OK; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
*--------------------------------------------------------------------------- |
| 1010 |
* |
| 1011 |
* Tcl_CreateEncoding -- |
| 1012 |
* |
| 1013 |
* Defines a new encoding, along with the functions that are used to |
| 1014 |
* convert to and from Unicode. |
| 1015 |
* |
| 1016 |
* Results: |
| 1017 |
* Returns a token that represents the encoding. If an encoding with the |
| 1018 |
* same name already existed, the old encoding token remains valid and |
| 1019 |
* continues to behave as it used to, and is eventually garbage collected |
| 1020 |
* when the last reference to it goes away. Any subsequent calls to |
| 1021 |
* Tcl_GetEncoding with the specified name retrieve the most recent |
| 1022 |
* encoding token. |
| 1023 |
* |
| 1024 |
* Side effects: |
| 1025 |
* A new record having the name of the encoding is entered into a table of |
| 1026 |
* encodings visible to all interpreters. For each call to this function, |
| 1027 |
* there should eventually be a call to Tcl_FreeEncoding, which cleans |
| 1028 |
* deletes the record in the table when an encoding is no longer needed. |
| 1029 |
* |
| 1030 |
*--------------------------------------------------------------------------- |
| 1031 |
*/ |
| 1032 |
|
| 1033 |
Tcl_Encoding |
| 1034 |
Tcl_CreateEncoding( |
| 1035 |
const Tcl_EncodingType *typePtr) |
| 1036 |
/* The encoding type. */ |
| 1037 |
{ |
| 1038 |
Tcl_HashEntry *hPtr; |
| 1039 |
int isNew; |
| 1040 |
Encoding *encodingPtr; |
| 1041 |
char *name; |
| 1042 |
|
| 1043 |
Tcl_MutexLock(&encodingMutex); |
| 1044 |
hPtr = Tcl_CreateHashEntry(&encodingTable, typePtr->encodingName, &isNew); |
| 1045 |
if (isNew == 0) { |
| 1046 |
/* |
| 1047 |
* Remove old encoding from hash table, but don't delete it until last |
| 1048 |
* reference goes away. |
| 1049 |
*/ |
| 1050 |
|
| 1051 |
encodingPtr = (Encoding *)Tcl_GetHashValue(hPtr); |
| 1052 |
encodingPtr->hPtr = NULL; |
| 1053 |
} |
| 1054 |
|
| 1055 |
name = (char *)ckalloc(strlen(typePtr->encodingName) + 1); |
| 1056 |
|
| 1057 |
encodingPtr = (Encoding *)ckalloc(sizeof(Encoding)); |
| 1058 |
encodingPtr->name = strcpy(name, typePtr->encodingName); |
| 1059 |
encodingPtr->toUtfProc = typePtr->toUtfProc; |
| 1060 |
encodingPtr->fromUtfProc = typePtr->fromUtfProc; |
| 1061 |
encodingPtr->freeProc = typePtr->freeProc; |
| 1062 |
encodingPtr->nullSize = typePtr->nullSize; |
| 1063 |
encodingPtr->clientData = typePtr->clientData; |
| 1064 |
if (typePtr->nullSize == 1) { |
| 1065 |
encodingPtr->lengthProc = (LengthProc *) strlen; |
| 1066 |
} else { |
| 1067 |
encodingPtr->lengthProc = (LengthProc *) unilen; |
| 1068 |
} |
| 1069 |
encodingPtr->refCount = 1; |
| 1070 |
encodingPtr->hPtr = hPtr; |
| 1071 |
Tcl_SetHashValue(hPtr, encodingPtr); |
| 1072 |
|
| 1073 |
Tcl_MutexUnlock(&encodingMutex); |
| 1074 |
|
| 1075 |
return (Tcl_Encoding) encodingPtr; |
| 1076 |
} |
| 1077 |
|
| 1078 |
/* |
| 1079 |
*------------------------------------------------------------------------- |
| 1080 |
* |
| 1081 |
* Tcl_ExternalToUtfDString -- |
| 1082 |
* |
| 1083 |
* Convert a source buffer from the specified encoding into UTF-8. If any |
| 1084 |
* of the bytes in the source buffer are invalid or cannot be represented |
| 1085 |
* in the target encoding, a default fallback character will be |
| 1086 |
* substituted. |
| 1087 |
* |
| 1088 |
* Results: |
| 1089 |
* The converted bytes are stored in the DString, which is then NULL |
| 1090 |
* terminated. The return value is a pointer to the value stored in the |
| 1091 |
* DString. |
| 1092 |
* |
| 1093 |
* Side effects: |
| 1094 |
* None. |
| 1095 |
* |
| 1096 |
*------------------------------------------------------------------------- |
| 1097 |
*/ |
| 1098 |
|
| 1099 |
char * |
| 1100 |
Tcl_ExternalToUtfDString( |
| 1101 |
Tcl_Encoding encoding, /* The encoding for the source string, or NULL |
| 1102 |
* for the default system encoding. */ |
| 1103 |
const char *src, /* Source string in specified encoding. */ |
| 1104 |
int srcLen, /* Source string length in bytes, or < 0 for |
| 1105 |
* encoding-specific string length. */ |
| 1106 |
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the |
| 1107 |
* converted string is stored. */ |
| 1108 |
{ |
| 1109 |
char *dst; |
| 1110 |
Tcl_EncodingState state; |
| 1111 |
const Encoding *encodingPtr; |
| 1112 |
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars; |
| 1113 |
|
| 1114 |
Tcl_DStringInit(dstPtr); |
| 1115 |
dst = Tcl_DStringValue(dstPtr); |
| 1116 |
dstLen = dstPtr->spaceAvl - 1; |
| 1117 |
|
| 1118 |
if (encoding == NULL) { |
| 1119 |
encoding = systemEncoding; |
| 1120 |
} |
| 1121 |
encodingPtr = (Encoding *) encoding; |
| 1122 |
|
| 1123 |
if (src == NULL) { |
| 1124 |
srcLen = 0; |
| 1125 |
src = ""; |
| 1126 |
} else if (srcLen < 0) { |
| 1127 |
srcLen = encodingPtr->lengthProc(src); |
| 1128 |
} |
| 1129 |
|
| 1130 |
flags = TCL_ENCODING_START | TCL_ENCODING_END; |
| 1131 |
|
| 1132 |
while (1) { |
| 1133 |
result = encodingPtr->toUtfProc(encodingPtr->clientData, src, srcLen, |
| 1134 |
flags, &state, dst, dstLen, &srcRead, &dstWrote, &dstChars); |
| 1135 |
soFar = dst + dstWrote - Tcl_DStringValue(dstPtr); |
| 1136 |
|
| 1137 |
if (result != TCL_CONVERT_NOSPACE) { |
| 1138 |
Tcl_DStringSetLength(dstPtr, soFar); |
| 1139 |
return Tcl_DStringValue(dstPtr); |
| 1140 |
} |
| 1141 |
|
| 1142 |
flags &= ~TCL_ENCODING_START; |
| 1143 |
src += srcRead; |
| 1144 |
srcLen -= srcRead; |
| 1145 |
if (Tcl_DStringLength(dstPtr) == 0) { |
| 1146 |
Tcl_DStringSetLength(dstPtr, dstLen); |
| 1147 |
} |
| 1148 |
Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1); |
| 1149 |
dst = Tcl_DStringValue(dstPtr) + soFar; |
| 1150 |
dstLen = Tcl_DStringLength(dstPtr) - soFar - 1; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
/* |
| 1155 |
*------------------------------------------------------------------------- |
| 1156 |
* |
| 1157 |
* Tcl_ExternalToUtf -- |
| 1158 |
* |
| 1159 |
* Convert a source buffer from the specified encoding into UTF-8. |
| 1160 |
* |
| 1161 |
* Results: |
| 1162 |
* The return value is one of TCL_OK, TCL_CONVERT_MULTIBYTE, |
| 1163 |
* TCL_CONVERT_SYNTAX, TCL_CONVERT_UNKNOWN, or TCL_CONVERT_NOSPACE, as |
| 1164 |
* documented in tcl.h. |
| 1165 |
* |
| 1166 |
* Side effects: |
| 1167 |
* The converted bytes are stored in the output buffer. |
| 1168 |
* |
| 1169 |
*------------------------------------------------------------------------- |
| 1170 |
*/ |
| 1171 |
|
| 1172 |
int |
| 1173 |
Tcl_ExternalToUtf( |
| 1174 |
Tcl_Interp *interp, /* Interp for error return, if not NULL. */ |
| 1175 |
Tcl_Encoding encoding, /* The encoding for the source string, or NULL |
| 1176 |
* for the default system encoding. */ |
| 1177 |
const char *src, /* Source string in specified encoding. */ |
| 1178 |
int srcLen, /* Source string length in bytes, or < 0 for |
| 1179 |
* encoding-specific string length. */ |
| 1180 |
int flags, /* Conversion control flags. */ |
| 1181 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 1182 |
* information used during a piecewise |
| 1183 |
* conversion. Contents of statePtr are |
| 1184 |
* initialized and/or reset by conversion |
| 1185 |
* routine under control of flags argument. */ |
| 1186 |
char *dst, /* Output buffer in which converted string is |
| 1187 |
* stored. */ |
| 1188 |
int dstLen, /* The maximum length of output buffer in |
| 1189 |
* bytes. */ |
| 1190 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 1191 |
* source string that were converted. This may |
| 1192 |
* be less than the original source length if |
| 1193 |
* there was a problem converting some source |
| 1194 |
* characters. */ |
| 1195 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 1196 |
* stored in the output buffer as a result of |
| 1197 |
* the conversion. */ |
| 1198 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 1199 |
* correspond to the bytes stored in the |
| 1200 |
* output buffer. */ |
| 1201 |
{ |
| 1202 |
const Encoding *encodingPtr; |
| 1203 |
int result, srcRead, dstWrote, dstChars = 0; |
| 1204 |
int noTerminate = flags & TCL_ENCODING_NO_TERMINATE; |
| 1205 |
int charLimited = (flags & TCL_ENCODING_CHAR_LIMIT) && dstCharsPtr; |
| 1206 |
int maxChars = INT_MAX; |
| 1207 |
Tcl_EncodingState state; |
| 1208 |
|
| 1209 |
if (encoding == NULL) { |
| 1210 |
encoding = systemEncoding; |
| 1211 |
} |
| 1212 |
encodingPtr = (Encoding *) encoding; |
| 1213 |
|
| 1214 |
if (src == NULL) { |
| 1215 |
srcLen = 0; |
| 1216 |
src = ""; |
| 1217 |
} else if (srcLen < 0) { |
| 1218 |
srcLen = encodingPtr->lengthProc(src); |
| 1219 |
} |
| 1220 |
if (statePtr == NULL) { |
| 1221 |
flags |= TCL_ENCODING_START | TCL_ENCODING_END; |
| 1222 |
statePtr = &state; |
| 1223 |
} |
| 1224 |
if (srcReadPtr == NULL) { |
| 1225 |
srcReadPtr = &srcRead; |
| 1226 |
} |
| 1227 |
if (dstWrotePtr == NULL) { |
| 1228 |
dstWrotePtr = &dstWrote; |
| 1229 |
} |
| 1230 |
if (dstCharsPtr == NULL) { |
| 1231 |
dstCharsPtr = &dstChars; |
| 1232 |
flags &= ~TCL_ENCODING_CHAR_LIMIT; |
| 1233 |
} else if (charLimited) { |
| 1234 |
maxChars = *dstCharsPtr; |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (!noTerminate) { |
| 1238 |
if (dstLen < 1) { |
| 1239 |
return TCL_CONVERT_NOSPACE; |
| 1240 |
} |
| 1241 |
/* |
| 1242 |
* If there are any null characters in the middle of the buffer, |
| 1243 |
* they will converted to the UTF-8 null character (\xC080). To get |
| 1244 |
* the actual \0 at the end of the destination buffer, we need to |
| 1245 |
* append it manually. First make room for it... |
| 1246 |
*/ |
| 1247 |
|
| 1248 |
dstLen--; |
| 1249 |
} else { |
| 1250 |
if (dstLen < 0) { |
| 1251 |
return TCL_CONVERT_NOSPACE; |
| 1252 |
} |
| 1253 |
} |
| 1254 |
do { |
| 1255 |
Tcl_EncodingState savedState = *statePtr; |
| 1256 |
|
| 1257 |
result = encodingPtr->toUtfProc(encodingPtr->clientData, src, srcLen, |
| 1258 |
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr, |
| 1259 |
dstCharsPtr); |
| 1260 |
if (*dstCharsPtr <= maxChars) { |
| 1261 |
break; |
| 1262 |
} |
| 1263 |
dstLen = Tcl_UtfAtIndex(dst, maxChars) - dst + (TCL_UTF_MAX - 1); |
| 1264 |
*statePtr = savedState; |
| 1265 |
} while (1); |
| 1266 |
if (!noTerminate) { |
| 1267 |
/* ...and then append it */ |
| 1268 |
|
| 1269 |
dst[*dstWrotePtr] = '\0'; |
| 1270 |
} |
| 1271 |
|
| 1272 |
return result; |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* |
| 1276 |
*------------------------------------------------------------------------- |
| 1277 |
* |
| 1278 |
* Tcl_UtfToExternalDString -- |
| 1279 |
* |
| 1280 |
* Convert a source buffer from UTF-8 to the specified encoding. If any |
| 1281 |
* of the bytes in the source buffer are invalid or cannot be represented |
| 1282 |
* in the target encoding, a default fallback character is substituted. |
| 1283 |
* |
| 1284 |
* Results: |
| 1285 |
* The converted bytes are stored in the DString, which is then NULL |
| 1286 |
* terminated in an encoding-specific manner. The return value is a |
| 1287 |
* pointer to the value stored in the DString. |
| 1288 |
* |
| 1289 |
* Side effects: |
| 1290 |
* None. |
| 1291 |
* |
| 1292 |
*------------------------------------------------------------------------- |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
char * |
| 1296 |
Tcl_UtfToExternalDString( |
| 1297 |
Tcl_Encoding encoding, /* The encoding for the converted string, or |
| 1298 |
* NULL for the default system encoding. */ |
| 1299 |
const char *src, /* Source string in UTF-8. */ |
| 1300 |
int srcLen, /* Source string length in bytes, or < 0 for |
| 1301 |
* strlen(). */ |
| 1302 |
Tcl_DString *dstPtr) /* Uninitialized or free DString in which the |
| 1303 |
* converted string is stored. */ |
| 1304 |
{ |
| 1305 |
char *dst; |
| 1306 |
Tcl_EncodingState state; |
| 1307 |
const Encoding *encodingPtr; |
| 1308 |
int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars; |
| 1309 |
|
| 1310 |
Tcl_DStringInit(dstPtr); |
| 1311 |
dst = Tcl_DStringValue(dstPtr); |
| 1312 |
dstLen = dstPtr->spaceAvl - 1; |
| 1313 |
|
| 1314 |
if (encoding == NULL) { |
| 1315 |
encoding = systemEncoding; |
| 1316 |
} |
| 1317 |
encodingPtr = (Encoding *) encoding; |
| 1318 |
|
| 1319 |
if (src == NULL) { |
| 1320 |
srcLen = 0; |
| 1321 |
src = ""; |
| 1322 |
} else if (srcLen < 0) { |
| 1323 |
srcLen = strlen(src); |
| 1324 |
} |
| 1325 |
flags = TCL_ENCODING_START | TCL_ENCODING_END; |
| 1326 |
while (1) { |
| 1327 |
result = encodingPtr->fromUtfProc(encodingPtr->clientData, src, |
| 1328 |
srcLen, flags, &state, dst, dstLen, &srcRead, &dstWrote, |
| 1329 |
&dstChars); |
| 1330 |
soFar = dst + dstWrote - Tcl_DStringValue(dstPtr); |
| 1331 |
|
| 1332 |
if (result != TCL_CONVERT_NOSPACE) { |
| 1333 |
if (encodingPtr->nullSize == 2) { |
| 1334 |
Tcl_DStringSetLength(dstPtr, soFar + 1); |
| 1335 |
} |
| 1336 |
Tcl_DStringSetLength(dstPtr, soFar); |
| 1337 |
return Tcl_DStringValue(dstPtr); |
| 1338 |
} |
| 1339 |
|
| 1340 |
flags &= ~TCL_ENCODING_START; |
| 1341 |
src += srcRead; |
| 1342 |
srcLen -= srcRead; |
| 1343 |
if (Tcl_DStringLength(dstPtr) == 0) { |
| 1344 |
Tcl_DStringSetLength(dstPtr, dstLen); |
| 1345 |
} |
| 1346 |
Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1); |
| 1347 |
dst = Tcl_DStringValue(dstPtr) + soFar; |
| 1348 |
dstLen = Tcl_DStringLength(dstPtr) - soFar - 1; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* |
| 1353 |
*------------------------------------------------------------------------- |
| 1354 |
* |
| 1355 |
* Tcl_UtfToExternal -- |
| 1356 |
* |
| 1357 |
* Convert a buffer from UTF-8 into the specified encoding. |
| 1358 |
* |
| 1359 |
* Results: |
| 1360 |
* The return value is one of TCL_OK, TCL_CONVERT_MULTIBYTE, |
| 1361 |
* TCL_CONVERT_SYNTAX, TCL_CONVERT_UNKNOWN, or TCL_CONVERT_NOSPACE, as |
| 1362 |
* documented in tcl.h. |
| 1363 |
* |
| 1364 |
* Side effects: |
| 1365 |
* The converted bytes are stored in the output buffer. |
| 1366 |
* |
| 1367 |
*------------------------------------------------------------------------- |
| 1368 |
*/ |
| 1369 |
|
| 1370 |
int |
| 1371 |
Tcl_UtfToExternal( |
| 1372 |
Tcl_Interp *interp, /* Interp for error return, if not NULL. */ |
| 1373 |
Tcl_Encoding encoding, /* The encoding for the converted string, or |
| 1374 |
* NULL for the default system encoding. */ |
| 1375 |
const char *src, /* Source string in UTF-8. */ |
| 1376 |
int srcLen, /* Source string length in bytes, or < 0 for |
| 1377 |
* strlen(). */ |
| 1378 |
int flags, /* Conversion control flags. */ |
| 1379 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 1380 |
* information used during a piecewise |
| 1381 |
* conversion. Contents of statePtr are |
| 1382 |
* initialized and/or reset by conversion |
| 1383 |
* routine under control of flags argument. */ |
| 1384 |
char *dst, /* Output buffer in which converted string |
| 1385 |
* is stored. */ |
| 1386 |
int dstLen, /* The maximum length of output buffer in |
| 1387 |
* bytes. */ |
| 1388 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 1389 |
* source string that were converted. This may |
| 1390 |
* be less than the original source length if |
| 1391 |
* there was a problem converting some source |
| 1392 |
* characters. */ |
| 1393 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 1394 |
* stored in the output buffer as a result of |
| 1395 |
* the conversion. */ |
| 1396 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 1397 |
* correspond to the bytes stored in the |
| 1398 |
* output buffer. */ |
| 1399 |
{ |
| 1400 |
const Encoding *encodingPtr; |
| 1401 |
int result, srcRead, dstWrote, dstChars; |
| 1402 |
Tcl_EncodingState state; |
| 1403 |
|
| 1404 |
if (encoding == NULL) { |
| 1405 |
encoding = systemEncoding; |
| 1406 |
} |
| 1407 |
encodingPtr = (Encoding *) encoding; |
| 1408 |
|
| 1409 |
if (src == NULL) { |
| 1410 |
srcLen = 0; |
| 1411 |
src = ""; |
| 1412 |
} else if (srcLen < 0) { |
| 1413 |
srcLen = strlen(src); |
| 1414 |
} |
| 1415 |
if (statePtr == NULL) { |
| 1416 |
flags |= TCL_ENCODING_START | TCL_ENCODING_END; |
| 1417 |
statePtr = &state; |
| 1418 |
} |
| 1419 |
if (srcReadPtr == NULL) { |
| 1420 |
srcReadPtr = &srcRead; |
| 1421 |
} |
| 1422 |
if (dstWrotePtr == NULL) { |
| 1423 |
dstWrotePtr = &dstWrote; |
| 1424 |
} |
| 1425 |
if (dstCharsPtr == NULL) { |
| 1426 |
dstCharsPtr = &dstChars; |
| 1427 |
} |
| 1428 |
|
| 1429 |
if (dstLen < encodingPtr->nullSize) { |
| 1430 |
return TCL_CONVERT_NOSPACE; |
| 1431 |
} |
| 1432 |
dstLen -= encodingPtr->nullSize; |
| 1433 |
result = encodingPtr->fromUtfProc(encodingPtr->clientData, src, srcLen, |
| 1434 |
flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr, |
| 1435 |
dstCharsPtr); |
| 1436 |
/* |
| 1437 |
* Buffer is terminated irrespective of result. Not sure this is |
| 1438 |
* reasonable but keep for historical/compatibility reasons. |
| 1439 |
*/ |
| 1440 |
if (encodingPtr->nullSize == 2) { |
| 1441 |
dst[*dstWrotePtr + 1] = '\0'; |
| 1442 |
} |
| 1443 |
dst[*dstWrotePtr] = '\0'; |
| 1444 |
|
| 1445 |
return result; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/* |
| 1449 |
*--------------------------------------------------------------------------- |
| 1450 |
* |
| 1451 |
* Tcl_FindExecutable -- |
| 1452 |
* |
| 1453 |
* This function computes the absolute path name of the current |
| 1454 |
* application, given its argv[0] value. |
| 1455 |
* |
| 1456 |
* Results: |
| 1457 |
* None. |
| 1458 |
* |
| 1459 |
* Side effects: |
| 1460 |
* The absolute pathname for the application is computed and stored to be |
| 1461 |
* returned later by [info nameofexecutable]. |
| 1462 |
* |
| 1463 |
*--------------------------------------------------------------------------- |
| 1464 |
*/ |
| 1465 |
#undef Tcl_FindExecutable |
| 1466 |
void |
| 1467 |
Tcl_FindExecutable( |
| 1468 |
const char *argv0) /* The value of the application's argv[0] |
| 1469 |
* (native). */ |
| 1470 |
{ |
| 1471 |
TclInitSubsystems(); |
| 1472 |
TclpSetInitialEncodings(); |
| 1473 |
TclpFindExecutable(argv0); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* |
| 1477 |
*--------------------------------------------------------------------------- |
| 1478 |
* |
| 1479 |
* OpenEncodingFileChannel -- |
| 1480 |
* |
| 1481 |
* Open the file believed to hold data for the encoding, "name". |
| 1482 |
* |
| 1483 |
* Results: |
| 1484 |
* Returns the readable Tcl_Channel from opening the file, or NULL if the |
| 1485 |
* file could not be successfully opened. If NULL was returned, an error |
| 1486 |
* message is left in interp's result object, unless interp was NULL. |
| 1487 |
* |
| 1488 |
* Side effects: |
| 1489 |
* Channel may be opened. Information about the filesystem may be cached |
| 1490 |
* to speed later calls. |
| 1491 |
* |
| 1492 |
*--------------------------------------------------------------------------- |
| 1493 |
*/ |
| 1494 |
|
| 1495 |
static Tcl_Channel |
| 1496 |
OpenEncodingFileChannel( |
| 1497 |
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */ |
| 1498 |
const char *name) /* The name of the encoding file on disk and |
| 1499 |
* also the name for new encoding. */ |
| 1500 |
{ |
| 1501 |
Tcl_Obj *fileNameObj = Tcl_NewStringObj(name, -1); |
| 1502 |
Tcl_Obj *searchPath = Tcl_DuplicateObj(Tcl_GetEncodingSearchPath()); |
| 1503 |
Tcl_Obj *map = TclGetProcessGlobalValue(&encodingFileMap); |
| 1504 |
Tcl_Obj **dir, *path, *directory = NULL; |
| 1505 |
Tcl_Channel chan = NULL; |
| 1506 |
int i, numDirs; |
| 1507 |
|
| 1508 |
TclListObjGetElements(NULL, searchPath, &numDirs, &dir); |
| 1509 |
Tcl_AppendToObj(fileNameObj, ".enc", -1); |
| 1510 |
Tcl_IncrRefCount(fileNameObj); |
| 1511 |
TclDictGet(NULL, map, name, &directory); |
| 1512 |
|
| 1513 |
/* |
| 1514 |
* Check that any cached directory is still on the encoding search path. |
| 1515 |
*/ |
| 1516 |
|
| 1517 |
if (NULL != directory) { |
| 1518 |
int verified = 0; |
| 1519 |
|
| 1520 |
for (i=0; i<numDirs && !verified; i++) { |
| 1521 |
if (dir[i] == directory) { |
| 1522 |
verified = 1; |
| 1523 |
} |
| 1524 |
} |
| 1525 |
if (!verified) { |
| 1526 |
const char *dirString = TclGetString(directory); |
| 1527 |
|
| 1528 |
for (i=0; i<numDirs && !verified; i++) { |
| 1529 |
if (strcmp(dirString, TclGetString(dir[i])) == 0) { |
| 1530 |
verified = 1; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
} |
| 1534 |
if (!verified) { |
| 1535 |
/* |
| 1536 |
* Directory no longer on the search path. Remove from cache. |
| 1537 |
*/ |
| 1538 |
|
| 1539 |
map = Tcl_DuplicateObj(map); |
| 1540 |
TclDictRemove(NULL, map, name); |
| 1541 |
TclSetProcessGlobalValue(&encodingFileMap, map, NULL); |
| 1542 |
directory = NULL; |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (NULL != directory) { |
| 1547 |
/* |
| 1548 |
* Got a directory from the cache. Try to use it first. |
| 1549 |
*/ |
| 1550 |
|
| 1551 |
Tcl_IncrRefCount(directory); |
| 1552 |
path = Tcl_FSJoinToPath(directory, 1, &fileNameObj); |
| 1553 |
Tcl_IncrRefCount(path); |
| 1554 |
Tcl_DecrRefCount(directory); |
| 1555 |
chan = Tcl_FSOpenFileChannel(NULL, path, "r", 0); |
| 1556 |
Tcl_DecrRefCount(path); |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* |
| 1560 |
* Scan the search path until we find it. |
| 1561 |
*/ |
| 1562 |
|
| 1563 |
for (i=0; i<numDirs && (chan == NULL); i++) { |
| 1564 |
path = Tcl_FSJoinToPath(dir[i], 1, &fileNameObj); |
| 1565 |
Tcl_IncrRefCount(path); |
| 1566 |
chan = Tcl_FSOpenFileChannel(NULL, path, "r", 0); |
| 1567 |
Tcl_DecrRefCount(path); |
| 1568 |
if (chan != NULL) { |
| 1569 |
/* |
| 1570 |
* Save directory in the cache. |
| 1571 |
*/ |
| 1572 |
|
| 1573 |
map = Tcl_DuplicateObj(TclGetProcessGlobalValue(&encodingFileMap)); |
| 1574 |
TclDictPut(NULL, map, name, dir[i]); |
| 1575 |
TclSetProcessGlobalValue(&encodingFileMap, map, NULL); |
| 1576 |
} |
| 1577 |
} |
| 1578 |
|
| 1579 |
if ((NULL == chan) && (interp != NULL)) { |
| 1580 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1581 |
"unknown encoding \"%s\"", name)); |
| 1582 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ENCODING", name, (char *)NULL); |
| 1583 |
} |
| 1584 |
Tcl_DecrRefCount(fileNameObj); |
| 1585 |
Tcl_DecrRefCount(searchPath); |
| 1586 |
|
| 1587 |
return chan; |
| 1588 |
} |
| 1589 |
|
| 1590 |
/* |
| 1591 |
*--------------------------------------------------------------------------- |
| 1592 |
* |
| 1593 |
* LoadEncodingFile -- |
| 1594 |
* |
| 1595 |
* Read a file that describes an encoding and create a new Encoding from |
| 1596 |
* the data. |
| 1597 |
* |
| 1598 |
* Results: |
| 1599 |
* The return value is the newly loaded Tcl_Encoding or NULL if the file |
| 1600 |
* didn't exist or could not be processed. If NULL is returned and interp |
| 1601 |
* is not NULL, an error message is left in interp's result object. |
| 1602 |
* |
| 1603 |
* Side effects: |
| 1604 |
* A corresponding encoding file might be read from persistent storage, in |
| 1605 |
* which case LoadTableEncoding is called. |
| 1606 |
* |
| 1607 |
*--------------------------------------------------------------------------- |
| 1608 |
*/ |
| 1609 |
|
| 1610 |
static Tcl_Encoding |
| 1611 |
LoadEncodingFile( |
| 1612 |
Tcl_Interp *interp, /* Interp for error reporting, if not NULL. */ |
| 1613 |
const char *name) /* The name of both the encoding file |
| 1614 |
* and the new encoding. */ |
| 1615 |
{ |
| 1616 |
Tcl_Channel chan = NULL; |
| 1617 |
Tcl_Encoding encoding = NULL; |
| 1618 |
int ch; |
| 1619 |
|
| 1620 |
chan = OpenEncodingFileChannel(interp, name); |
| 1621 |
if (chan == NULL) { |
| 1622 |
return NULL; |
| 1623 |
} |
| 1624 |
|
| 1625 |
Tcl_SetChannelOption(NULL, chan, "-encoding", "utf-8"); |
| 1626 |
|
| 1627 |
while (1) { |
| 1628 |
Tcl_DString ds; |
| 1629 |
|
| 1630 |
Tcl_DStringInit(&ds); |
| 1631 |
Tcl_Gets(chan, &ds); |
| 1632 |
ch = Tcl_DStringValue(&ds)[0]; |
| 1633 |
Tcl_DStringFree(&ds); |
| 1634 |
if (ch != '#') { |
| 1635 |
break; |
| 1636 |
} |
| 1637 |
} |
| 1638 |
|
| 1639 |
switch (ch) { |
| 1640 |
case 'S': |
| 1641 |
encoding = LoadTableEncoding(name, ENCODING_SINGLEBYTE, chan); |
| 1642 |
break; |
| 1643 |
case 'D': |
| 1644 |
encoding = LoadTableEncoding(name, ENCODING_DOUBLEBYTE, chan); |
| 1645 |
break; |
| 1646 |
case 'M': |
| 1647 |
encoding = LoadTableEncoding(name, ENCODING_MULTIBYTE, chan); |
| 1648 |
break; |
| 1649 |
case 'E': |
| 1650 |
encoding = LoadEscapeEncoding(name, chan); |
| 1651 |
break; |
| 1652 |
} |
| 1653 |
if ((encoding == NULL) && (interp != NULL)) { |
| 1654 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1655 |
"invalid encoding file \"%s\"", name)); |
| 1656 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ENCODING", name, (char *)NULL); |
| 1657 |
} |
| 1658 |
Tcl_Close(NULL, chan); |
| 1659 |
|
| 1660 |
return encoding; |
| 1661 |
} |
| 1662 |
|
| 1663 |
/* |
| 1664 |
*------------------------------------------------------------------------- |
| 1665 |
* |
| 1666 |
* LoadTableEncoding -- |
| 1667 |
* |
| 1668 |
* Helper function for LoadEncodingFile(). Creates a Tcl_EncodingType |
| 1669 |
* structure along with its corresponding TableEncodingData structure, and |
| 1670 |
* passes it to Tcl_Createncoding. |
| 1671 |
* |
| 1672 |
* The file contains binary data but begins with a marker to indicate |
| 1673 |
* byte-ordering so a single binary file can be read on big or |
| 1674 |
* little-endian systems. |
| 1675 |
* |
| 1676 |
* Results: |
| 1677 |
* Returns the new Tcl_Encoding, or NULL if it could could |
| 1678 |
* not be created because the file contained invalid data. |
| 1679 |
* |
| 1680 |
* Side effects: |
| 1681 |
* See Tcl_CreateEncoding(). |
| 1682 |
* |
| 1683 |
*------------------------------------------------------------------------- |
| 1684 |
*/ |
| 1685 |
|
| 1686 |
static Tcl_Encoding |
| 1687 |
LoadTableEncoding( |
| 1688 |
const char *name, /* Name of the new encoding. */ |
| 1689 |
int type, /* Type of encoding (ENCODING_?????). */ |
| 1690 |
Tcl_Channel chan) /* File containing new encoding. */ |
| 1691 |
{ |
| 1692 |
Tcl_DString lineString; |
| 1693 |
Tcl_Obj *objPtr; |
| 1694 |
char *line; |
| 1695 |
int i, hi, lo, numPages, symbol, fallback, len; |
| 1696 |
unsigned char used[256]; |
| 1697 |
unsigned size; |
| 1698 |
TableEncodingData *dataPtr; |
| 1699 |
unsigned short *pageMemPtr, *page; |
| 1700 |
Tcl_EncodingType encType; |
| 1701 |
|
| 1702 |
/* |
| 1703 |
* Speed over memory. Use a full 256 character table to decode hex |
| 1704 |
* sequences in the encoding files. |
| 1705 |
*/ |
| 1706 |
|
| 1707 |
static const char staticHex[] = { |
| 1708 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 ... 15 */ |
| 1709 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 ... 31 */ |
| 1710 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32 ... 47 */ |
| 1711 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 48 ... 63 */ |
| 1712 |
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 64 ... 79 */ |
| 1713 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 ... 95 */ |
| 1714 |
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 96 ... 111 */ |
| 1715 |
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 112 ... 127 */ |
| 1716 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128 ... 143 */ |
| 1717 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 144 ... 159 */ |
| 1718 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 160 ... 175 */ |
| 1719 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 176 ... 191 */ |
| 1720 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 192 ... 207 */ |
| 1721 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 208 ... 223 */ |
| 1722 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 224 ... 239 */ |
| 1723 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 240 ... 255 */ |
| 1724 |
}; |
| 1725 |
|
| 1726 |
Tcl_DStringInit(&lineString); |
| 1727 |
if (Tcl_Gets(chan, &lineString) == -1) { |
| 1728 |
return NULL; |
| 1729 |
} |
| 1730 |
line = Tcl_DStringValue(&lineString); |
| 1731 |
|
| 1732 |
fallback = (int) strtol(line, &line, 16); |
| 1733 |
symbol = (int) strtol(line, &line, 10); |
| 1734 |
numPages = (int) strtol(line, &line, 10); |
| 1735 |
Tcl_DStringFree(&lineString); |
| 1736 |
|
| 1737 |
if (numPages < 0) { |
| 1738 |
numPages = 0; |
| 1739 |
} else if (numPages > 256) { |
| 1740 |
numPages = 256; |
| 1741 |
} |
| 1742 |
|
| 1743 |
memset(used, 0, sizeof(used)); |
| 1744 |
|
| 1745 |
#undef PAGESIZE |
| 1746 |
#define PAGESIZE (256 * sizeof(unsigned short)) |
| 1747 |
|
| 1748 |
dataPtr = (TableEncodingData *)ckalloc(sizeof(TableEncodingData)); |
| 1749 |
memset(dataPtr, 0, sizeof(TableEncodingData)); |
| 1750 |
|
| 1751 |
dataPtr->fallback = fallback; |
| 1752 |
|
| 1753 |
/* |
| 1754 |
* Read the table that maps characters to Unicode. Performs a single |
| 1755 |
* malloc to get the memory for the array and all the pages needed by the |
| 1756 |
* array. |
| 1757 |
*/ |
| 1758 |
|
| 1759 |
size = 256 * sizeof(unsigned short *) + numPages * PAGESIZE; |
| 1760 |
dataPtr->toUnicode = (unsigned short **)ckalloc(size); |
| 1761 |
memset(dataPtr->toUnicode, 0, size); |
| 1762 |
pageMemPtr = (unsigned short *) (dataPtr->toUnicode + 256); |
| 1763 |
|
| 1764 |
TclNewObj(objPtr); |
| 1765 |
Tcl_IncrRefCount(objPtr); |
| 1766 |
for (i = 0; i < numPages; i++) { |
| 1767 |
int ch; |
| 1768 |
const char *p; |
| 1769 |
int expected = 3 + 16 * (16 * 4 + 1); |
| 1770 |
|
| 1771 |
if (Tcl_ReadChars(chan, objPtr, expected, 0) != expected) { |
| 1772 |
return NULL; |
| 1773 |
} |
| 1774 |
p = TclGetString(objPtr); |
| 1775 |
hi = (staticHex[UCHAR(p[0])] << 4) + staticHex[UCHAR(p[1])]; |
| 1776 |
dataPtr->toUnicode[hi] = pageMemPtr; |
| 1777 |
p += 2; |
| 1778 |
for (lo = 0; lo < 256; lo++) { |
| 1779 |
if ((lo & 0x0F) == 0) { |
| 1780 |
p++; |
| 1781 |
} |
| 1782 |
ch = (staticHex[UCHAR(p[0])] << 12) + (staticHex[UCHAR(p[1])] << 8) |
| 1783 |
+ (staticHex[UCHAR(p[2])] << 4) + staticHex[UCHAR(p[3])]; |
| 1784 |
if (ch != 0) { |
| 1785 |
used[ch >> 8] = 1; |
| 1786 |
} |
| 1787 |
*pageMemPtr = (unsigned short) ch; |
| 1788 |
pageMemPtr++; |
| 1789 |
p += 4; |
| 1790 |
} |
| 1791 |
} |
| 1792 |
TclDecrRefCount(objPtr); |
| 1793 |
|
| 1794 |
if (type == ENCODING_DOUBLEBYTE) { |
| 1795 |
memset(dataPtr->prefixBytes, 1, sizeof(dataPtr->prefixBytes)); |
| 1796 |
} else { |
| 1797 |
for (hi = 1; hi < 256; hi++) { |
| 1798 |
if (dataPtr->toUnicode[hi] != NULL) { |
| 1799 |
dataPtr->prefixBytes[hi] = 1; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
} |
| 1803 |
|
| 1804 |
/* |
| 1805 |
* Invert the toUnicode array to produce the fromUnicode array. Performs a |
| 1806 |
* single malloc to get the memory for the array and all the pages needed |
| 1807 |
* by the array. While reading in the toUnicode array remember what |
| 1808 |
* pages are needed for the fromUnicode array. |
| 1809 |
*/ |
| 1810 |
|
| 1811 |
if (symbol) { |
| 1812 |
used[0] = 1; |
| 1813 |
} |
| 1814 |
numPages = 0; |
| 1815 |
for (hi = 0; hi < 256; hi++) { |
| 1816 |
if (used[hi]) { |
| 1817 |
numPages++; |
| 1818 |
} |
| 1819 |
} |
| 1820 |
size = 256 * sizeof(unsigned short *) + numPages * PAGESIZE; |
| 1821 |
dataPtr->fromUnicode = (unsigned short **)ckalloc(size); |
| 1822 |
memset(dataPtr->fromUnicode, 0, size); |
| 1823 |
pageMemPtr = (unsigned short *) (dataPtr->fromUnicode + 256); |
| 1824 |
|
| 1825 |
for (hi = 0; hi < 256; hi++) { |
| 1826 |
if (dataPtr->toUnicode[hi] == NULL) { |
| 1827 |
dataPtr->toUnicode[hi] = emptyPage; |
| 1828 |
continue; |
| 1829 |
} |
| 1830 |
for (lo = 0; lo < 256; lo++) { |
| 1831 |
int ch = dataPtr->toUnicode[hi][lo]; |
| 1832 |
|
| 1833 |
if (ch != 0) { |
| 1834 |
page = dataPtr->fromUnicode[ch >> 8]; |
| 1835 |
if (page == NULL) { |
| 1836 |
page = pageMemPtr; |
| 1837 |
pageMemPtr += 256; |
| 1838 |
dataPtr->fromUnicode[ch >> 8] = page; |
| 1839 |
} |
| 1840 |
page[ch & 0xFF] = (unsigned short) ((hi << 8) + lo); |
| 1841 |
} |
| 1842 |
} |
| 1843 |
} |
| 1844 |
if (type == ENCODING_MULTIBYTE) { |
| 1845 |
/* |
| 1846 |
* If multibyte encodings don't have a backslash character, define |
| 1847 |
* one. Otherwise, on Windows, native file names don't work because |
| 1848 |
* the backslash in the file name maps to the unknown character |
| 1849 |
* (question mark) when converting from UTF-8 to external encoding. |
| 1850 |
*/ |
| 1851 |
|
| 1852 |
if (dataPtr->fromUnicode[0] != NULL) { |
| 1853 |
if (dataPtr->fromUnicode[0]['\\'] == '\0') { |
| 1854 |
dataPtr->fromUnicode[0]['\\'] = '\\'; |
| 1855 |
} |
| 1856 |
} |
| 1857 |
} |
| 1858 |
if (symbol) { |
| 1859 |
/* |
| 1860 |
* Make a special symbol encoding that maps each symbol character from |
| 1861 |
* its Unicode code point down into page 0, and also ensure that each |
| 1862 |
* characters on page 0 maps to itself so that a symbol font can be |
| 1863 |
* used to display a simple string like "abcd" and have alpha, beta, |
| 1864 |
* chi, delta show up, rather than have "unknown" chars show up because |
| 1865 |
* strictly speaking the symbol font doesn't have glyphs for those low |
| 1866 |
* ASCII chars. |
| 1867 |
*/ |
| 1868 |
|
| 1869 |
page = dataPtr->fromUnicode[0]; |
| 1870 |
if (page == NULL) { |
| 1871 |
page = pageMemPtr; |
| 1872 |
dataPtr->fromUnicode[0] = page; |
| 1873 |
} |
| 1874 |
for (lo = 0; lo < 256; lo++) { |
| 1875 |
if (dataPtr->toUnicode[0][lo] != 0) { |
| 1876 |
page[lo] = (unsigned short) lo; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
} |
| 1880 |
for (hi = 0; hi < 256; hi++) { |
| 1881 |
if (dataPtr->fromUnicode[hi] == NULL) { |
| 1882 |
dataPtr->fromUnicode[hi] = emptyPage; |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* |
| 1887 |
* For trailing 'R'everse encoding, see [Patch 689341] |
| 1888 |
*/ |
| 1889 |
|
| 1890 |
Tcl_DStringInit(&lineString); |
| 1891 |
|
| 1892 |
/* |
| 1893 |
* Skip leading empty lines. |
| 1894 |
*/ |
| 1895 |
|
| 1896 |
while ((len = Tcl_Gets(chan, &lineString)) == 0) { |
| 1897 |
/* empty body */ |
| 1898 |
} |
| 1899 |
if (len < 0) { |
| 1900 |
goto doneParse; |
| 1901 |
} |
| 1902 |
|
| 1903 |
/* |
| 1904 |
* Require that it starts with an 'R'. |
| 1905 |
*/ |
| 1906 |
|
| 1907 |
line = Tcl_DStringValue(&lineString); |
| 1908 |
if (line[0] != 'R') { |
| 1909 |
goto doneParse; |
| 1910 |
} |
| 1911 |
|
| 1912 |
/* |
| 1913 |
* Read lines until EOF. |
| 1914 |
*/ |
| 1915 |
|
| 1916 |
for (TclDStringClear(&lineString); |
| 1917 |
(len = Tcl_Gets(chan, &lineString)) >= 0; |
| 1918 |
TclDStringClear(&lineString)) { |
| 1919 |
const unsigned char *p; |
| 1920 |
int to, from; |
| 1921 |
|
| 1922 |
/* |
| 1923 |
* Skip short lines. |
| 1924 |
*/ |
| 1925 |
|
| 1926 |
if (len < 5) { |
| 1927 |
continue; |
| 1928 |
} |
| 1929 |
|
| 1930 |
/* |
| 1931 |
* Parse the line as a sequence of hex digits. |
| 1932 |
*/ |
| 1933 |
|
| 1934 |
p = (const unsigned char *) Tcl_DStringValue(&lineString); |
| 1935 |
to = (staticHex[p[0]] << 12) + (staticHex[p[1]] << 8) |
| 1936 |
+ (staticHex[p[2]] << 4) + staticHex[p[3]]; |
| 1937 |
if (to == 0) { |
| 1938 |
continue; |
| 1939 |
} |
| 1940 |
for (p += 5, len -= 5; len >= 0 && *p; p += 5, len -= 5) { |
| 1941 |
from = (staticHex[p[0]] << 12) + (staticHex[p[1]] << 8) |
| 1942 |
+ (staticHex[p[2]] << 4) + staticHex[p[3]]; |
| 1943 |
if (from == 0) { |
| 1944 |
continue; |
| 1945 |
} |
| 1946 |
dataPtr->fromUnicode[from >> 8][from & 0xFF] = to; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
doneParse: |
| 1950 |
Tcl_DStringFree(&lineString); |
| 1951 |
|
| 1952 |
/* |
| 1953 |
* Package everything into an encoding structure. |
| 1954 |
*/ |
| 1955 |
|
| 1956 |
encType.encodingName = name; |
| 1957 |
encType.toUtfProc = TableToUtfProc; |
| 1958 |
encType.fromUtfProc = TableFromUtfProc; |
| 1959 |
encType.freeProc = TableFreeProc; |
| 1960 |
encType.nullSize = (type == ENCODING_DOUBLEBYTE) ? 2 : 1; |
| 1961 |
encType.clientData = dataPtr; |
| 1962 |
|
| 1963 |
return Tcl_CreateEncoding(&encType); |
| 1964 |
} |
| 1965 |
|
| 1966 |
/* |
| 1967 |
*------------------------------------------------------------------------- |
| 1968 |
* |
| 1969 |
* LoadEscapeEncoding -- |
| 1970 |
* |
| 1971 |
* Helper function for LoadEncodingTable(). Loads a state machine that |
| 1972 |
* converts between Unicode and some other encoding. |
| 1973 |
* |
| 1974 |
* File contains text data that describes the escape sequences that are |
| 1975 |
* used to choose an encoding and the associated names for the |
| 1976 |
* sub-encodings. |
| 1977 |
* |
| 1978 |
* Results: |
| 1979 |
* The return value is the new encoding, or NULL if the encoding could |
| 1980 |
* not be created (because the file contained invalid data). |
| 1981 |
* |
| 1982 |
* Side effects: |
| 1983 |
* None. |
| 1984 |
* |
| 1985 |
*------------------------------------------------------------------------- |
| 1986 |
*/ |
| 1987 |
|
| 1988 |
static Tcl_Encoding |
| 1989 |
LoadEscapeEncoding( |
| 1990 |
const char *name, /* Name of the new encoding. */ |
| 1991 |
Tcl_Channel chan) /* File containing new encoding. */ |
| 1992 |
{ |
| 1993 |
int i; |
| 1994 |
unsigned size; |
| 1995 |
Tcl_DString escapeData; |
| 1996 |
char init[16], final[16]; |
| 1997 |
EscapeEncodingData *dataPtr; |
| 1998 |
Tcl_EncodingType type; |
| 1999 |
|
| 2000 |
init[0] = '\0'; |
| 2001 |
final[0] = '\0'; |
| 2002 |
Tcl_DStringInit(&escapeData); |
| 2003 |
|
| 2004 |
while (1) { |
| 2005 |
int argc; |
| 2006 |
const char **argv; |
| 2007 |
char *line; |
| 2008 |
Tcl_DString lineString; |
| 2009 |
|
| 2010 |
Tcl_DStringInit(&lineString); |
| 2011 |
if (Tcl_Gets(chan, &lineString) < 0) { |
| 2012 |
break; |
| 2013 |
} |
| 2014 |
line = Tcl_DStringValue(&lineString); |
| 2015 |
if (Tcl_SplitList(NULL, line, &argc, &argv) != TCL_OK) { |
| 2016 |
Tcl_DStringFree(&lineString); |
| 2017 |
continue; |
| 2018 |
} |
| 2019 |
if (argc >= 2) { |
| 2020 |
if (strcmp(argv[0], "name") == 0) { |
| 2021 |
/* do nothing */ |
| 2022 |
} else if (strcmp(argv[0], "init") == 0) { |
| 2023 |
strncpy(init, argv[1], sizeof(init)); |
| 2024 |
init[sizeof(init) - 1] = '\0'; |
| 2025 |
} else if (strcmp(argv[0], "final") == 0) { |
| 2026 |
strncpy(final, argv[1], sizeof(final)); |
| 2027 |
final[sizeof(final) - 1] = '\0'; |
| 2028 |
} else { |
| 2029 |
EscapeSubTable est; |
| 2030 |
Encoding *e; |
| 2031 |
|
| 2032 |
strncpy(est.sequence, argv[1], sizeof(est.sequence)); |
| 2033 |
est.sequence[sizeof(est.sequence) - 1] = '\0'; |
| 2034 |
est.sequenceLen = strlen(est.sequence); |
| 2035 |
|
| 2036 |
strncpy(est.name, argv[0], sizeof(est.name)); |
| 2037 |
est.name[sizeof(est.name) - 1] = '\0'; |
| 2038 |
|
| 2039 |
/* |
| 2040 |
* To avoid infinite recursion in [encoding system iso2022-*] |
| 2041 |
*/ |
| 2042 |
|
| 2043 |
e = (Encoding *) Tcl_GetEncoding(NULL, est.name); |
| 2044 |
if ((e != NULL) && (e->toUtfProc != TableToUtfProc) |
| 2045 |
&& (e->toUtfProc != Iso88591ToUtfProc)) { |
| 2046 |
Tcl_FreeEncoding((Tcl_Encoding) e); |
| 2047 |
e = NULL; |
| 2048 |
} |
| 2049 |
est.encodingPtr = e; |
| 2050 |
Tcl_DStringAppend(&escapeData, (char *) &est, sizeof(est)); |
| 2051 |
} |
| 2052 |
} |
| 2053 |
ckfree(argv); |
| 2054 |
Tcl_DStringFree(&lineString); |
| 2055 |
} |
| 2056 |
|
| 2057 |
size = TclOffset(EscapeEncodingData, subTables) |
| 2058 |
+ Tcl_DStringLength(&escapeData); |
| 2059 |
dataPtr = (EscapeEncodingData *)ckalloc(size); |
| 2060 |
dataPtr->initLen = strlen(init); |
| 2061 |
memcpy(dataPtr->init, init, dataPtr->initLen + 1); |
| 2062 |
dataPtr->finalLen = strlen(final); |
| 2063 |
memcpy(dataPtr->final, final, dataPtr->finalLen + 1); |
| 2064 |
dataPtr->numSubTables = |
| 2065 |
Tcl_DStringLength(&escapeData) / sizeof(EscapeSubTable); |
| 2066 |
memcpy(dataPtr->subTables, Tcl_DStringValue(&escapeData), |
| 2067 |
Tcl_DStringLength(&escapeData)); |
| 2068 |
Tcl_DStringFree(&escapeData); |
| 2069 |
|
| 2070 |
memset(dataPtr->prefixBytes, 0, sizeof(dataPtr->prefixBytes)); |
| 2071 |
for (i = 0; i < dataPtr->numSubTables; i++) { |
| 2072 |
dataPtr->prefixBytes[UCHAR(dataPtr->subTables[i].sequence[0])] = 1; |
| 2073 |
} |
| 2074 |
if (dataPtr->init[0] != '\0') { |
| 2075 |
dataPtr->prefixBytes[UCHAR(dataPtr->init[0])] = 1; |
| 2076 |
} |
| 2077 |
if (dataPtr->final[0] != '\0') { |
| 2078 |
dataPtr->prefixBytes[UCHAR(dataPtr->final[0])] = 1; |
| 2079 |
} |
| 2080 |
|
| 2081 |
/* |
| 2082 |
* Package everything into an encoding structure. |
| 2083 |
*/ |
| 2084 |
|
| 2085 |
type.encodingName = name; |
| 2086 |
type.toUtfProc = EscapeToUtfProc; |
| 2087 |
type.fromUtfProc = EscapeFromUtfProc; |
| 2088 |
type.freeProc = EscapeFreeProc; |
| 2089 |
type.nullSize = 1; |
| 2090 |
type.clientData = dataPtr; |
| 2091 |
|
| 2092 |
return Tcl_CreateEncoding(&type); |
| 2093 |
} |
| 2094 |
|
| 2095 |
/* |
| 2096 |
*------------------------------------------------------------------------- |
| 2097 |
* |
| 2098 |
* BinaryProc -- |
| 2099 |
* |
| 2100 |
* The default conversion when no other conversion is specified. No |
| 2101 |
* translation is done; source bytes are copied directly to destination |
| 2102 |
* bytes. |
| 2103 |
* |
| 2104 |
* Results: |
| 2105 |
* Returns TCL_OK if conversion was successful. |
| 2106 |
* |
| 2107 |
* Side effects: |
| 2108 |
* None. |
| 2109 |
* |
| 2110 |
*------------------------------------------------------------------------- |
| 2111 |
*/ |
| 2112 |
|
| 2113 |
static int |
| 2114 |
BinaryProc( |
| 2115 |
ClientData clientData, /* Not used. */ |
| 2116 |
const char *src, /* Source string (unknown encoding). */ |
| 2117 |
int srcLen, /* Source string length in bytes. */ |
| 2118 |
int flags, /* Conversion control flags. */ |
| 2119 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2120 |
* information used during a piecewise |
| 2121 |
* conversion. Contents of statePtr are |
| 2122 |
* initialized and/or reset by conversion |
| 2123 |
* routine under control of flags argument. */ |
| 2124 |
char *dst, /* Output buffer in which converted string is |
| 2125 |
* stored. */ |
| 2126 |
int dstLen, /* The maximum length of output buffer in |
| 2127 |
* bytes. */ |
| 2128 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2129 |
* source string that were converted. */ |
| 2130 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2131 |
* stored in the output buffer as a result of |
| 2132 |
* the conversion. */ |
| 2133 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2134 |
* correspond to the bytes stored in the |
| 2135 |
* output buffer. */ |
| 2136 |
{ |
| 2137 |
int result; |
| 2138 |
|
| 2139 |
result = TCL_OK; |
| 2140 |
dstLen -= TCL_UTF_MAX - 1; |
| 2141 |
if (dstLen < 0) { |
| 2142 |
dstLen = 0; |
| 2143 |
} |
| 2144 |
if ((flags & TCL_ENCODING_CHAR_LIMIT) && srcLen > *dstCharsPtr) { |
| 2145 |
srcLen = *dstCharsPtr; |
| 2146 |
} |
| 2147 |
if (srcLen > dstLen) { |
| 2148 |
srcLen = dstLen; |
| 2149 |
result = TCL_CONVERT_NOSPACE; |
| 2150 |
} |
| 2151 |
|
| 2152 |
*srcReadPtr = srcLen; |
| 2153 |
*dstWrotePtr = srcLen; |
| 2154 |
*dstCharsPtr = srcLen; |
| 2155 |
memcpy(dst, src, srcLen); |
| 2156 |
return result; |
| 2157 |
} |
| 2158 |
|
| 2159 |
/* |
| 2160 |
*------------------------------------------------------------------------- |
| 2161 |
* |
| 2162 |
* UtfIntToUtfExtProc -- |
| 2163 |
* |
| 2164 |
* Convert from UTF-8 to UTF-8. While converting null-bytes from the |
| 2165 |
* Tcl's internal representation (0xC0, 0x80) to the official |
| 2166 |
* representation (0x00). See UtfToUtfProc for details. |
| 2167 |
* |
| 2168 |
* Results: |
| 2169 |
* Returns TCL_OK if conversion was successful. |
| 2170 |
* |
| 2171 |
* Side effects: |
| 2172 |
* None. |
| 2173 |
* |
| 2174 |
*------------------------------------------------------------------------- |
| 2175 |
*/ |
| 2176 |
|
| 2177 |
static int |
| 2178 |
UtfIntToUtfExtProc( |
| 2179 |
ClientData clientData, /* Not used. */ |
| 2180 |
const char *src, /* Source string in UTF-8. */ |
| 2181 |
int srcLen, /* Source string length in bytes. */ |
| 2182 |
int flags, /* Conversion control flags. */ |
| 2183 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2184 |
* information used during a piecewise |
| 2185 |
* conversion. Contents of statePtr are |
| 2186 |
* initialized and/or reset by conversion |
| 2187 |
* routine under control of flags argument. */ |
| 2188 |
char *dst, /* Output buffer in which converted string |
| 2189 |
* is stored. */ |
| 2190 |
int dstLen, /* The maximum length of output buffer in |
| 2191 |
* bytes. */ |
| 2192 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2193 |
* source string that were converted. This may |
| 2194 |
* be less than the original source length if |
| 2195 |
* there was a problem converting some source |
| 2196 |
* characters. */ |
| 2197 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2198 |
* stored in the output buffer as a result of |
| 2199 |
* the conversion. */ |
| 2200 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2201 |
* correspond to the bytes stored in the |
| 2202 |
* output buffer. */ |
| 2203 |
{ |
| 2204 |
return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen, |
| 2205 |
srcReadPtr, dstWrotePtr, dstCharsPtr, 1); |
| 2206 |
} |
| 2207 |
|
| 2208 |
/* |
| 2209 |
*------------------------------------------------------------------------- |
| 2210 |
* |
| 2211 |
* UtfExtToUtfIntProc -- |
| 2212 |
* |
| 2213 |
* Convert from UTF-8 to UTF-8 while converting null-bytes from the |
| 2214 |
* official representation (0x00) to Tcl's internal representation (0xC0, |
| 2215 |
* 0x80). See UtfToUtfProc for details. |
| 2216 |
* |
| 2217 |
* Results: |
| 2218 |
* Returns TCL_OK if conversion was successful. |
| 2219 |
* |
| 2220 |
* Side effects: |
| 2221 |
* None. |
| 2222 |
* |
| 2223 |
*------------------------------------------------------------------------- |
| 2224 |
*/ |
| 2225 |
|
| 2226 |
static int |
| 2227 |
UtfExtToUtfIntProc( |
| 2228 |
ClientData clientData, /* Not used. */ |
| 2229 |
const char *src, /* Source string in UTF-8. */ |
| 2230 |
int srcLen, /* Source string length in bytes. */ |
| 2231 |
int flags, /* Conversion control flags. */ |
| 2232 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2233 |
* information used during a piecewise |
| 2234 |
* conversion. Contents of statePtr are |
| 2235 |
* initialized and/or reset by conversion |
| 2236 |
* routine under control of flags argument. */ |
| 2237 |
char *dst, /* Output buffer in which converted string is |
| 2238 |
* stored. */ |
| 2239 |
int dstLen, /* The maximum length of output buffer in |
| 2240 |
* bytes. */ |
| 2241 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2242 |
* source string that were converted. This may |
| 2243 |
* be less than the original source length if |
| 2244 |
* there was a problem converting some source |
| 2245 |
* characters. */ |
| 2246 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2247 |
* stored in the output buffer as a result of |
| 2248 |
* the conversion. */ |
| 2249 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2250 |
* correspond to the bytes stored in the |
| 2251 |
* output buffer. */ |
| 2252 |
{ |
| 2253 |
return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen, |
| 2254 |
srcReadPtr, dstWrotePtr, dstCharsPtr, 0); |
| 2255 |
} |
| 2256 |
|
| 2257 |
/* |
| 2258 |
*------------------------------------------------------------------------- |
| 2259 |
* |
| 2260 |
* UtfToUtfProc -- |
| 2261 |
* |
| 2262 |
* Converts from UTF-8 to UTF-8. Note that the UTF-8 to UTF-8 translation |
| 2263 |
* is not a no-op, because it turns a stream of improperly formed |
| 2264 |
* UTF-8 into a properly-formed stream. |
| 2265 |
* |
| 2266 |
* Results: |
| 2267 |
* Returns TCL_OK if conversion was successful. |
| 2268 |
* |
| 2269 |
* Side effects: |
| 2270 |
* None. |
| 2271 |
* |
| 2272 |
*------------------------------------------------------------------------- |
| 2273 |
*/ |
| 2274 |
|
| 2275 |
static int |
| 2276 |
UtfToUtfProc( |
| 2277 |
ClientData clientData, /* Not used. */ |
| 2278 |
const char *src, /* Source string in UTF-8. */ |
| 2279 |
int srcLen, /* Source string length in bytes. */ |
| 2280 |
int flags, /* Conversion control flags. */ |
| 2281 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2282 |
* information used during a piecewise |
| 2283 |
* conversion. Contents of statePtr are |
| 2284 |
* initialized and/or reset by conversion |
| 2285 |
* routine under control of flags argument. */ |
| 2286 |
char *dst, /* Output buffer in which converted string is |
| 2287 |
* stored. */ |
| 2288 |
int dstLen, /* The maximum length of output buffer in |
| 2289 |
* bytes. */ |
| 2290 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2291 |
* source string that were converted. This may |
| 2292 |
* be less than the original source length if |
| 2293 |
* there was a problem converting some source |
| 2294 |
* characters. */ |
| 2295 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2296 |
* stored in the output buffer as a result of |
| 2297 |
* the conversion. */ |
| 2298 |
int *dstCharsPtr, /* Filled with the number of characters that |
| 2299 |
* correspond to the bytes stored in the |
| 2300 |
* output buffer. */ |
| 2301 |
int pureNullMode) /* Convert embedded nulls from internal |
| 2302 |
* representation to real null-bytes or vice |
| 2303 |
* versa. Also combine or separate surrogate pairs */ |
| 2304 |
{ |
| 2305 |
const char *srcStart, *srcEnd, *srcClose; |
| 2306 |
const char *dstStart, *dstEnd; |
| 2307 |
int result, numChars, charLimit = INT_MAX; |
| 2308 |
Tcl_UniChar *chPtr = (Tcl_UniChar *) statePtr; |
| 2309 |
|
| 2310 |
if (flags & TCL_ENCODING_START) { |
| 2311 |
*statePtr = 0; |
| 2312 |
} |
| 2313 |
result = TCL_OK; |
| 2314 |
|
| 2315 |
srcStart = src; |
| 2316 |
srcEnd = src + srcLen; |
| 2317 |
srcClose = srcEnd; |
| 2318 |
if ((flags & TCL_ENCODING_END) == 0) { |
| 2319 |
srcClose -= 6; |
| 2320 |
} |
| 2321 |
if (flags & TCL_ENCODING_CHAR_LIMIT) { |
| 2322 |
charLimit = *dstCharsPtr; |
| 2323 |
} |
| 2324 |
|
| 2325 |
dstStart = dst; |
| 2326 |
dstEnd = dst + dstLen - ((pureNullMode == 1) ? 4 : TCL_UTF_MAX); |
| 2327 |
|
| 2328 |
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) { |
| 2329 |
if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) { |
| 2330 |
/* |
| 2331 |
* If there is more string to follow, this will ensure that the |
| 2332 |
* last UTF-8 character in the source buffer hasn't been cut off. |
| 2333 |
*/ |
| 2334 |
|
| 2335 |
result = TCL_CONVERT_MULTIBYTE; |
| 2336 |
break; |
| 2337 |
} |
| 2338 |
if (dst > dstEnd) { |
| 2339 |
result = TCL_CONVERT_NOSPACE; |
| 2340 |
break; |
| 2341 |
} |
| 2342 |
if (UCHAR(*src) < 0x80 && !((UCHAR(*src) == 0) && (pureNullMode == 0))) { |
| 2343 |
/* |
| 2344 |
* Copy 7bit characters, but skip null-bytes when we are in input |
| 2345 |
* mode, so that they get converted to 0xC080. |
| 2346 |
*/ |
| 2347 |
|
| 2348 |
*dst++ = *src++; |
| 2349 |
*chPtr = 0; /* reset surrogate handling */ |
| 2350 |
} else if ((UCHAR(*src) == 0xC0) && (src + 1 < srcEnd) |
| 2351 |
&& (UCHAR(src[1]) == 0x80) && (pureNullMode == 1)) { |
| 2352 |
/* |
| 2353 |
* Convert 0xC080 to real nulls when we are in output mode. |
| 2354 |
*/ |
| 2355 |
|
| 2356 |
*dst++ = 0; |
| 2357 |
*chPtr = 0; /* reset surrogate handling */ |
| 2358 |
src += 2; |
| 2359 |
} else if (!Tcl_UtfCharComplete(src, srcEnd - src)) { |
| 2360 |
/* |
| 2361 |
* Always check before using TclUtfToUniChar. Not doing can so |
| 2362 |
* cause it run beyond the end of the buffer! If we happen such an |
| 2363 |
* incomplete char its bytes are made to represent themselves |
| 2364 |
* unless the user has explicitly asked to be told. |
| 2365 |
*/ |
| 2366 |
|
| 2367 |
if ((flags & TCL_ENCODING_STOPONERROR) && (pureNullMode == 0)) { |
| 2368 |
result = TCL_CONVERT_MULTIBYTE; |
| 2369 |
break; |
| 2370 |
} |
| 2371 |
*chPtr = UCHAR(*src); |
| 2372 |
src += 1; |
| 2373 |
dst += Tcl_UniCharToUtf(*chPtr, dst); |
| 2374 |
} else { |
| 2375 |
size_t len = TclUtfToUniChar(src, chPtr); |
| 2376 |
if ((len < 2) && (*chPtr != 0) && (flags & TCL_ENCODING_STOPONERROR) |
| 2377 |
&& ((*chPtr & ~0x7FF) != 0xD800) && (pureNullMode == 0)) { |
| 2378 |
result = TCL_CONVERT_SYNTAX; |
| 2379 |
break; |
| 2380 |
} |
| 2381 |
src += len; |
| 2382 |
if ((*chPtr & ~0x7FF) == 0xD800) { |
| 2383 |
Tcl_UniChar low; |
| 2384 |
/* A surrogate character is detected, handle especially */ |
| 2385 |
#if TCL_UTF_MAX <= 4 |
| 2386 |
if ((len < 3) && ((src[3 - len] & 0xC0) != 0x80)) { |
| 2387 |
/* It's invalid. See [ed29806ba] */ |
| 2388 |
*chPtr = UCHAR(src[-1]); |
| 2389 |
dst += Tcl_UniCharToUtf(*chPtr, dst); |
| 2390 |
continue; |
| 2391 |
} |
| 2392 |
#endif |
| 2393 |
low = *chPtr; |
| 2394 |
len = (src <= srcEnd-3) ? Tcl_UtfToUniChar(src, &low) : 0; |
| 2395 |
if (((low & ~0x3FF) != 0xDC00) || (*chPtr & 0x400)) { |
| 2396 |
*dst++ = (char) (((*chPtr >> 12) | 0xE0) & 0xEF); |
| 2397 |
*dst++ = (char) (((*chPtr >> 6) | 0x80) & 0xBF); |
| 2398 |
*dst++ = (char) ((*chPtr | 0x80) & 0xBF); |
| 2399 |
*chPtr = 0; /* reset surrogate handling */ |
| 2400 |
continue; |
| 2401 |
} else if ((TCL_UTF_MAX > 3) || (pureNullMode == 1)) { |
| 2402 |
int full = (((*chPtr & 0x3FF) << 10) | (low & 0x3FF)) + 0x10000; |
| 2403 |
*dst++ = (char) (((full >> 18) | 0xF0) & 0xF7); |
| 2404 |
*dst++ = (char) (((full >> 12) | 0x80) & 0xBF); |
| 2405 |
*dst++ = (char) (((full >> 6) | 0x80) & 0xBF); |
| 2406 |
*dst++ = (char) ((full | 0x80) & 0xBF); |
| 2407 |
*chPtr = 0; /* reset surrogate handling */ |
| 2408 |
src += len; |
| 2409 |
continue; |
| 2410 |
} |
| 2411 |
} |
| 2412 |
dst += Tcl_UniCharToUtf(*chPtr, dst); |
| 2413 |
} |
| 2414 |
} |
| 2415 |
|
| 2416 |
*srcReadPtr = src - srcStart; |
| 2417 |
*dstWrotePtr = dst - dstStart; |
| 2418 |
*dstCharsPtr = numChars; |
| 2419 |
return result; |
| 2420 |
} |
| 2421 |
|
| 2422 |
/* |
| 2423 |
*------------------------------------------------------------------------- |
| 2424 |
* |
| 2425 |
* UnicodeToUtfProc -- |
| 2426 |
* |
| 2427 |
* Convert from UTF-16 to UTF-8. |
| 2428 |
* |
| 2429 |
* Results: |
| 2430 |
* Returns TCL_OK if conversion was successful. |
| 2431 |
* |
| 2432 |
* Side effects: |
| 2433 |
* None. |
| 2434 |
* |
| 2435 |
*------------------------------------------------------------------------- |
| 2436 |
*/ |
| 2437 |
|
| 2438 |
static int |
| 2439 |
UnicodeToUtfProc( |
| 2440 |
ClientData clientData, /* != NULL means LE, == NUL means BE */ |
| 2441 |
const char *src, /* Source string in Unicode. */ |
| 2442 |
int srcLen, /* Source string length in bytes. */ |
| 2443 |
int flags, /* Conversion control flags. */ |
| 2444 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2445 |
* information used during a piecewise |
| 2446 |
* conversion. Contents of statePtr are |
| 2447 |
* initialized and/or reset by conversion |
| 2448 |
* routine under control of flags argument. */ |
| 2449 |
char *dst, /* Output buffer in which converted string is |
| 2450 |
* stored. */ |
| 2451 |
int dstLen, /* The maximum length of output buffer in |
| 2452 |
* bytes. */ |
| 2453 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2454 |
* source string that were converted. This may |
| 2455 |
* be less than the original source length if |
| 2456 |
* there was a problem converting some source |
| 2457 |
* characters. */ |
| 2458 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2459 |
* stored in the output buffer as a result of |
| 2460 |
* the conversion. */ |
| 2461 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2462 |
* correspond to the bytes stored in the |
| 2463 |
* output buffer. */ |
| 2464 |
{ |
| 2465 |
const char *srcStart, *srcEnd; |
| 2466 |
const char *dstEnd, *dstStart; |
| 2467 |
int result, numChars, charLimit = INT_MAX; |
| 2468 |
unsigned short ch; |
| 2469 |
|
| 2470 |
if (flags & TCL_ENCODING_CHAR_LIMIT) { |
| 2471 |
charLimit = *dstCharsPtr; |
| 2472 |
} |
| 2473 |
result = TCL_OK; |
| 2474 |
|
| 2475 |
/* |
| 2476 |
* Check alignment with utf-16 (2 == sizeof(UTF-16)) |
| 2477 |
*/ |
| 2478 |
|
| 2479 |
if ((srcLen % 2) != 0) { |
| 2480 |
result = TCL_CONVERT_MULTIBYTE; |
| 2481 |
srcLen--; |
| 2482 |
} |
| 2483 |
|
| 2484 |
#if TCL_UTF_MAX > 3 |
| 2485 |
/* |
| 2486 |
* If last code point is a high surrogate, we cannot handle that yet, |
| 2487 |
* unless we are at the end. |
| 2488 |
*/ |
| 2489 |
|
| 2490 |
if (!(flags & TCL_ENCODING_END) && (srcLen >= 2) && |
| 2491 |
((src[srcLen - (clientData?1:2)] & 0xFC) == 0xD8)) { |
| 2492 |
result = TCL_CONVERT_MULTIBYTE; |
| 2493 |
srcLen-= 2; |
| 2494 |
} |
| 2495 |
#endif |
| 2496 |
|
| 2497 |
srcStart = src; |
| 2498 |
srcEnd = src + srcLen; |
| 2499 |
|
| 2500 |
dstStart = dst; |
| 2501 |
dstEnd = dst + dstLen - TCL_UTF_MAX; |
| 2502 |
|
| 2503 |
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) { |
| 2504 |
if (dst > dstEnd) { |
| 2505 |
result = TCL_CONVERT_NOSPACE; |
| 2506 |
break; |
| 2507 |
} |
| 2508 |
|
| 2509 |
if (clientData) { |
| 2510 |
ch = (src[1] & 0xFF) << 8 | (src[0] & 0xFF); |
| 2511 |
} else { |
| 2512 |
ch = (src[0] & 0xFF) << 8 | (src[1] & 0xFF); |
| 2513 |
} |
| 2514 |
|
| 2515 |
/* |
| 2516 |
* Special case for 1-byte utf chars for speed. Make sure we work with |
| 2517 |
* unsigned short-size data. |
| 2518 |
*/ |
| 2519 |
|
| 2520 |
if (ch && ch < 0x80) { |
| 2521 |
*dst++ = (ch & 0xFF); |
| 2522 |
} else { |
| 2523 |
dst += Tcl_UniCharToUtf(ch, dst); |
| 2524 |
} |
| 2525 |
src += sizeof(unsigned short); |
| 2526 |
} |
| 2527 |
|
| 2528 |
if ((flags & TCL_ENCODING_END) && (result == TCL_CONVERT_MULTIBYTE)) { |
| 2529 |
/* We have a single byte left-over at the end */ |
| 2530 |
if (dst > dstEnd) { |
| 2531 |
result = TCL_CONVERT_NOSPACE; |
| 2532 |
} else { |
| 2533 |
/* destination is not full, so we really are at the end now */ |
| 2534 |
result = TCL_OK; |
| 2535 |
dst += Tcl_UniCharToUtf(0xFFFD, dst); |
| 2536 |
numChars++; |
| 2537 |
src++; |
| 2538 |
} |
| 2539 |
} |
| 2540 |
*srcReadPtr = src - srcStart; |
| 2541 |
*dstWrotePtr = dst - dstStart; |
| 2542 |
*dstCharsPtr = numChars; |
| 2543 |
return result; |
| 2544 |
} |
| 2545 |
|
| 2546 |
/* |
| 2547 |
*------------------------------------------------------------------------- |
| 2548 |
* |
| 2549 |
* UtfToUnicodeProc -- |
| 2550 |
* |
| 2551 |
* Convert from UTF-8 to UTF-16. |
| 2552 |
* |
| 2553 |
* Results: |
| 2554 |
* Returns TCL_OK if conversion was successful. |
| 2555 |
* |
| 2556 |
* Side effects: |
| 2557 |
* None. |
| 2558 |
* |
| 2559 |
*------------------------------------------------------------------------- |
| 2560 |
*/ |
| 2561 |
|
| 2562 |
static int |
| 2563 |
UtfToUnicodeProc( |
| 2564 |
ClientData clientData, /* != NULL means LE, == NUL means BE */ |
| 2565 |
const char *src, /* Source string in UTF-8. */ |
| 2566 |
int srcLen, /* Source string length in bytes. */ |
| 2567 |
int flags, /* Conversion control flags. */ |
| 2568 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2569 |
* information used during a piecewise |
| 2570 |
* conversion. Contents of statePtr are |
| 2571 |
* initialized and/or reset by conversion |
| 2572 |
* routine under control of flags argument. */ |
| 2573 |
char *dst, /* Output buffer in which converted string is |
| 2574 |
* stored. */ |
| 2575 |
int dstLen, /* The maximum length of output buffer in |
| 2576 |
* bytes. */ |
| 2577 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2578 |
* source string that were converted. This may |
| 2579 |
* be less than the original source length if |
| 2580 |
* there was a problem converting some source |
| 2581 |
* characters. */ |
| 2582 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2583 |
* stored in the output buffer as a result of |
| 2584 |
* the conversion. */ |
| 2585 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2586 |
* correspond to the bytes stored in the |
| 2587 |
* output buffer. */ |
| 2588 |
{ |
| 2589 |
const char *srcStart, *srcEnd, *srcClose, *dstStart, *dstEnd; |
| 2590 |
int result, numChars; |
| 2591 |
Tcl_UniChar *chPtr = (Tcl_UniChar *) statePtr; |
| 2592 |
|
| 2593 |
if (flags & TCL_ENCODING_START) { |
| 2594 |
*statePtr = 0; |
| 2595 |
} |
| 2596 |
srcStart = src; |
| 2597 |
srcEnd = src + srcLen; |
| 2598 |
srcClose = srcEnd; |
| 2599 |
if ((flags & TCL_ENCODING_END) == 0) { |
| 2600 |
srcClose -= TCL_UTF_MAX; |
| 2601 |
} |
| 2602 |
|
| 2603 |
dstStart = dst; |
| 2604 |
dstEnd = dst + dstLen - sizeof(Tcl_UniChar); |
| 2605 |
|
| 2606 |
result = TCL_OK; |
| 2607 |
for (numChars = 0; src < srcEnd; numChars++) { |
| 2608 |
if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) { |
| 2609 |
/* |
| 2610 |
* If there is more string to follow, this will ensure that the |
| 2611 |
* last UTF-8 character in the source buffer hasn't been cut off. |
| 2612 |
*/ |
| 2613 |
|
| 2614 |
result = TCL_CONVERT_MULTIBYTE; |
| 2615 |
break; |
| 2616 |
} |
| 2617 |
if (dst > dstEnd) { |
| 2618 |
result = TCL_CONVERT_NOSPACE; |
| 2619 |
break; |
| 2620 |
} |
| 2621 |
src += TclUtfToUniChar(src, chPtr); |
| 2622 |
|
| 2623 |
if (clientData) { |
| 2624 |
#if TCL_UTF_MAX > 4 |
| 2625 |
if (*chPtr <= 0xFFFF) { |
| 2626 |
*dst++ = (*chPtr & 0xFF); |
| 2627 |
*dst++ = (*chPtr >> 8); |
| 2628 |
} else { |
| 2629 |
*dst++ = (((*chPtr - 0x10000) >> 10) & 0xFF); |
| 2630 |
*dst++ = (((*chPtr - 0x10000) >> 18) & 0x3) | 0xD8; |
| 2631 |
*dst++ = (*chPtr & 0xFF); |
| 2632 |
*dst++ = ((*chPtr >> 8) & 0x3) | 0xDC; |
| 2633 |
} |
| 2634 |
#else |
| 2635 |
*dst++ = (*chPtr & 0xFF); |
| 2636 |
*dst++ = (*chPtr >> 8); |
| 2637 |
#endif |
| 2638 |
} else { |
| 2639 |
#if TCL_UTF_MAX > 4 |
| 2640 |
if (*chPtr <= 0xFFFF) { |
| 2641 |
*dst++ = (*chPtr >> 8); |
| 2642 |
*dst++ = (*chPtr & 0xFF); |
| 2643 |
} else { |
| 2644 |
*dst++ = (((*chPtr - 0x10000) >> 18) & 0x3) | 0xD8; |
| 2645 |
*dst++ = (((*chPtr - 0x10000) >> 10) & 0xFF); |
| 2646 |
*dst++ = ((*chPtr >> 8) & 0x3) | 0xDC; |
| 2647 |
*dst++ = (*chPtr & 0xFF); |
| 2648 |
} |
| 2649 |
#else |
| 2650 |
*dst++ = (*chPtr >> 8); |
| 2651 |
*dst++ = (*chPtr & 0xFF); |
| 2652 |
#endif |
| 2653 |
} |
| 2654 |
} |
| 2655 |
*srcReadPtr = src - srcStart; |
| 2656 |
*dstWrotePtr = dst - dstStart; |
| 2657 |
*dstCharsPtr = numChars; |
| 2658 |
return result; |
| 2659 |
} |
| 2660 |
|
| 2661 |
/* |
| 2662 |
*------------------------------------------------------------------------- |
| 2663 |
* |
| 2664 |
* TableToUtfProc -- |
| 2665 |
* |
| 2666 |
* Convert from the encoding specified by the TableEncodingData into |
| 2667 |
* UTF-8. |
| 2668 |
* |
| 2669 |
* Results: |
| 2670 |
* Returns TCL_OK if conversion was successful. |
| 2671 |
* |
| 2672 |
* Side effects: |
| 2673 |
* None. |
| 2674 |
* |
| 2675 |
*------------------------------------------------------------------------- |
| 2676 |
*/ |
| 2677 |
|
| 2678 |
static int |
| 2679 |
TableToUtfProc( |
| 2680 |
ClientData clientData, /* TableEncodingData that specifies |
| 2681 |
* encoding. */ |
| 2682 |
const char *src, /* Source string in specified encoding. */ |
| 2683 |
int srcLen, /* Source string length in bytes. */ |
| 2684 |
int flags, /* Conversion control flags. */ |
| 2685 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2686 |
* information used during a piecewise |
| 2687 |
* conversion. Contents of statePtr are |
| 2688 |
* initialized and/or reset by conversion |
| 2689 |
* routine under control of flags argument. */ |
| 2690 |
char *dst, /* Output buffer in which converted string is |
| 2691 |
* stored. */ |
| 2692 |
int dstLen, /* The maximum length of output buffer in |
| 2693 |
* bytes. */ |
| 2694 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2695 |
* source string that were converted. This may |
| 2696 |
* be less than the original source length if |
| 2697 |
* there was a problem converting some source |
| 2698 |
* characters. */ |
| 2699 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2700 |
* stored in the output buffer as a result of |
| 2701 |
* the conversion. */ |
| 2702 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2703 |
* correspond to the bytes stored in the |
| 2704 |
* output buffer. */ |
| 2705 |
{ |
| 2706 |
const char *srcStart, *srcEnd; |
| 2707 |
const char *dstEnd, *dstStart, *prefixBytes; |
| 2708 |
int result, byte, numChars, charLimit = INT_MAX; |
| 2709 |
Tcl_UniChar ch = 0; |
| 2710 |
const unsigned short *const *toUnicode; |
| 2711 |
const unsigned short *pageZero; |
| 2712 |
TableEncodingData *dataPtr = (TableEncodingData *)clientData; |
| 2713 |
|
| 2714 |
if (flags & TCL_ENCODING_CHAR_LIMIT) { |
| 2715 |
charLimit = *dstCharsPtr; |
| 2716 |
} |
| 2717 |
srcStart = src; |
| 2718 |
srcEnd = src + srcLen; |
| 2719 |
|
| 2720 |
dstStart = dst; |
| 2721 |
dstEnd = dst + dstLen - TCL_UTF_MAX; |
| 2722 |
|
| 2723 |
toUnicode = (const unsigned short *const *) dataPtr->toUnicode; |
| 2724 |
prefixBytes = dataPtr->prefixBytes; |
| 2725 |
pageZero = toUnicode[0]; |
| 2726 |
|
| 2727 |
result = TCL_OK; |
| 2728 |
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) { |
| 2729 |
if (dst > dstEnd) { |
| 2730 |
result = TCL_CONVERT_NOSPACE; |
| 2731 |
break; |
| 2732 |
} |
| 2733 |
byte = *((unsigned char *) src); |
| 2734 |
if (prefixBytes[byte]) { |
| 2735 |
if (src >= srcEnd-1) { |
| 2736 |
/* Prefix byte but nothing after it */ |
| 2737 |
if (!(flags & TCL_ENCODING_END)) { |
| 2738 |
/* More data to come */ |
| 2739 |
result = TCL_CONVERT_MULTIBYTE; |
| 2740 |
break; |
| 2741 |
} else if (flags & TCL_ENCODING_STOPONERROR) { |
| 2742 |
result = TCL_CONVERT_SYNTAX; |
| 2743 |
break; |
| 2744 |
} else { |
| 2745 |
ch = (Tcl_UniChar)byte; |
| 2746 |
} |
| 2747 |
} else { |
| 2748 |
ch = toUnicode[byte][*((unsigned char *)++src)]; |
| 2749 |
} |
| 2750 |
} else { |
| 2751 |
ch = pageZero[byte]; |
| 2752 |
} |
| 2753 |
if ((ch == 0) && (byte != 0)) { |
| 2754 |
/* Prefix+suffix pair is invalid */ |
| 2755 |
if (flags & TCL_ENCODING_STOPONERROR) { |
| 2756 |
result = TCL_CONVERT_SYNTAX; |
| 2757 |
break; |
| 2758 |
} |
| 2759 |
if (prefixBytes[byte]) { |
| 2760 |
src--; |
| 2761 |
} |
| 2762 |
ch = (Tcl_UniChar)byte; |
| 2763 |
} |
| 2764 |
|
| 2765 |
/* |
| 2766 |
* Special case for 1-byte Utf chars for speed. |
| 2767 |
*/ |
| 2768 |
|
| 2769 |
if ((unsigned)ch - 1 < 0x7F) { |
| 2770 |
*dst++ = (char) ch; |
| 2771 |
} else { |
| 2772 |
dst += Tcl_UniCharToUtf(ch, dst); |
| 2773 |
} |
| 2774 |
src++; |
| 2775 |
} |
| 2776 |
|
| 2777 |
*srcReadPtr = src - srcStart; |
| 2778 |
*dstWrotePtr = dst - dstStart; |
| 2779 |
*dstCharsPtr = numChars; |
| 2780 |
return result; |
| 2781 |
} |
| 2782 |
|
| 2783 |
/* |
| 2784 |
*------------------------------------------------------------------------- |
| 2785 |
* |
| 2786 |
* TableFromUtfProc -- |
| 2787 |
* |
| 2788 |
* Convert from UTF-8 into the encoding specified by the |
| 2789 |
* TableEncodingData. |
| 2790 |
* |
| 2791 |
* Results: |
| 2792 |
* Returns TCL_OK if conversion was successful. |
| 2793 |
* |
| 2794 |
* Side effects: |
| 2795 |
* None. |
| 2796 |
* |
| 2797 |
*------------------------------------------------------------------------- |
| 2798 |
*/ |
| 2799 |
|
| 2800 |
static int |
| 2801 |
TableFromUtfProc( |
| 2802 |
ClientData clientData, /* TableEncodingData that specifies |
| 2803 |
* encoding. */ |
| 2804 |
const char *src, /* Source string in UTF-8. */ |
| 2805 |
int srcLen, /* Source string length in bytes. */ |
| 2806 |
int flags, /* Conversion control flags. */ |
| 2807 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2808 |
* information used during a piecewise |
| 2809 |
* conversion. Contents of statePtr are |
| 2810 |
* initialized and/or reset by conversion |
| 2811 |
* routine under control of flags argument. */ |
| 2812 |
char *dst, /* Output buffer in which converted string is |
| 2813 |
* stored. */ |
| 2814 |
int dstLen, /* The maximum length of output buffer in |
| 2815 |
* bytes. */ |
| 2816 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2817 |
* source string that were converted. This may |
| 2818 |
* be less than the original source length if |
| 2819 |
* there was a problem converting some source |
| 2820 |
* characters. */ |
| 2821 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2822 |
* stored in the output buffer as a result of |
| 2823 |
* the conversion. */ |
| 2824 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2825 |
* correspond to the bytes stored in the |
| 2826 |
* output buffer. */ |
| 2827 |
{ |
| 2828 |
const char *srcStart, *srcEnd, *srcClose; |
| 2829 |
const char *dstStart, *dstEnd, *prefixBytes; |
| 2830 |
Tcl_UniChar ch = 0; |
| 2831 |
int result, len, word, numChars; |
| 2832 |
TableEncodingData *dataPtr = (TableEncodingData *)clientData; |
| 2833 |
const unsigned short *const *fromUnicode; |
| 2834 |
|
| 2835 |
result = TCL_OK; |
| 2836 |
|
| 2837 |
prefixBytes = dataPtr->prefixBytes; |
| 2838 |
fromUnicode = (const unsigned short *const *) dataPtr->fromUnicode; |
| 2839 |
|
| 2840 |
srcStart = src; |
| 2841 |
srcEnd = src + srcLen; |
| 2842 |
srcClose = srcEnd; |
| 2843 |
if ((flags & TCL_ENCODING_END) == 0) { |
| 2844 |
srcClose -= TCL_UTF_MAX; |
| 2845 |
} |
| 2846 |
|
| 2847 |
dstStart = dst; |
| 2848 |
dstEnd = dst + dstLen - 1; |
| 2849 |
|
| 2850 |
for (numChars = 0; src < srcEnd; numChars++) { |
| 2851 |
if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) { |
| 2852 |
/* |
| 2853 |
* If there is more string to follow, this will ensure that the |
| 2854 |
* last UTF-8 character in the source buffer hasn't been cut off. |
| 2855 |
*/ |
| 2856 |
|
| 2857 |
result = TCL_CONVERT_MULTIBYTE; |
| 2858 |
break; |
| 2859 |
} |
| 2860 |
len = TclUtfToUniChar(src, &ch); |
| 2861 |
|
| 2862 |
#if TCL_UTF_MAX > 4 |
| 2863 |
/* Unicode chars > +U0FFFF cannot be represented in any table encoding */ |
| 2864 |
if (ch & 0xFFFF0000) { |
| 2865 |
word = 0; |
| 2866 |
} else |
| 2867 |
#elif TCL_UTF_MAX == 4 |
| 2868 |
if (!len) { |
| 2869 |
word = 0; |
| 2870 |
} else |
| 2871 |
#endif |
| 2872 |
word = fromUnicode[(ch >> 8)][ch & 0xFF]; |
| 2873 |
|
| 2874 |
if ((word == 0) && (ch != 0)) { |
| 2875 |
if (flags & TCL_ENCODING_STOPONERROR) { |
| 2876 |
result = TCL_CONVERT_UNKNOWN; |
| 2877 |
break; |
| 2878 |
} |
| 2879 |
word = dataPtr->fallback; |
| 2880 |
} |
| 2881 |
if (prefixBytes[(word >> 8)] != 0) { |
| 2882 |
if (dst + 1 > dstEnd) { |
| 2883 |
result = TCL_CONVERT_NOSPACE; |
| 2884 |
break; |
| 2885 |
} |
| 2886 |
dst[0] = (char) (word >> 8); |
| 2887 |
dst[1] = (char) word; |
| 2888 |
dst += 2; |
| 2889 |
} else { |
| 2890 |
if (dst > dstEnd) { |
| 2891 |
result = TCL_CONVERT_NOSPACE; |
| 2892 |
break; |
| 2893 |
} |
| 2894 |
dst[0] = (char) word; |
| 2895 |
dst++; |
| 2896 |
} |
| 2897 |
src += len; |
| 2898 |
} |
| 2899 |
|
| 2900 |
*srcReadPtr = src - srcStart; |
| 2901 |
*dstWrotePtr = dst - dstStart; |
| 2902 |
*dstCharsPtr = numChars; |
| 2903 |
return result; |
| 2904 |
} |
| 2905 |
|
| 2906 |
/* |
| 2907 |
*------------------------------------------------------------------------- |
| 2908 |
* |
| 2909 |
* Iso88591ToUtfProc -- |
| 2910 |
* |
| 2911 |
* Convert from the "iso8859-1" encoding into UTF-8. |
| 2912 |
* |
| 2913 |
* Results: |
| 2914 |
* Returns TCL_OK if conversion was successful. |
| 2915 |
* |
| 2916 |
* Side effects: |
| 2917 |
* None. |
| 2918 |
* |
| 2919 |
*------------------------------------------------------------------------- |
| 2920 |
*/ |
| 2921 |
|
| 2922 |
static int |
| 2923 |
Iso88591ToUtfProc( |
| 2924 |
ClientData clientData, /* Ignored. */ |
| 2925 |
const char *src, /* Source string in specified encoding. */ |
| 2926 |
int srcLen, /* Source string length in bytes. */ |
| 2927 |
int flags, /* Conversion control flags. */ |
| 2928 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 2929 |
* information used during a piecewise |
| 2930 |
* conversion. Contents of statePtr are |
| 2931 |
* initialized and/or reset by conversion |
| 2932 |
* routine under control of flags argument. */ |
| 2933 |
char *dst, /* Output buffer in which converted string is |
| 2934 |
* stored. */ |
| 2935 |
int dstLen, /* The maximum length of output buffer in |
| 2936 |
* bytes. */ |
| 2937 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 2938 |
* source string that were converted. This may |
| 2939 |
* be less than the original source length if |
| 2940 |
* there was a problem converting some source |
| 2941 |
* characters. */ |
| 2942 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 2943 |
* stored in the output buffer as a result of |
| 2944 |
* the conversion. */ |
| 2945 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 2946 |
* correspond to the bytes stored in the |
| 2947 |
* output buffer. */ |
| 2948 |
{ |
| 2949 |
const char *srcStart, *srcEnd; |
| 2950 |
const char *dstEnd, *dstStart; |
| 2951 |
int result, numChars, charLimit = INT_MAX; |
| 2952 |
|
| 2953 |
if (flags & TCL_ENCODING_CHAR_LIMIT) { |
| 2954 |
charLimit = *dstCharsPtr; |
| 2955 |
} |
| 2956 |
srcStart = src; |
| 2957 |
srcEnd = src + srcLen; |
| 2958 |
|
| 2959 |
dstStart = dst; |
| 2960 |
dstEnd = dst + dstLen - TCL_UTF_MAX; |
| 2961 |
|
| 2962 |
result = TCL_OK; |
| 2963 |
for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) { |
| 2964 |
Tcl_UniChar ch = 0; |
| 2965 |
|
| 2966 |
if (dst > dstEnd) { |
| 2967 |
result = TCL_CONVERT_NOSPACE; |
| 2968 |
break; |
| 2969 |
} |
| 2970 |
ch = (Tcl_UniChar) *((unsigned char *) src); |
| 2971 |
|
| 2972 |
/* |
| 2973 |
* Special case for 1-byte utf chars for speed. |
| 2974 |
*/ |
| 2975 |
|
| 2976 |
if ((unsigned)ch - 1 < 0x7F) { |
| 2977 |
*dst++ = (char) ch; |
| 2978 |
} else { |
| 2979 |
dst += Tcl_UniCharToUtf(ch, dst); |
| 2980 |
} |
| 2981 |
src++; |
| 2982 |
} |
| 2983 |
|
| 2984 |
*srcReadPtr = src - srcStart; |
| 2985 |
*dstWrotePtr = dst - dstStart; |
| 2986 |
*dstCharsPtr = numChars; |
| 2987 |
return result; |
| 2988 |
} |
| 2989 |
|
| 2990 |
/* |
| 2991 |
*------------------------------------------------------------------------- |
| 2992 |
* |
| 2993 |
* Iso88591FromUtfProc -- |
| 2994 |
* |
| 2995 |
* Convert from UTF-8 into the encoding "iso8859-1". |
| 2996 |
* |
| 2997 |
* Results: |
| 2998 |
* Returns TCL_OK if conversion was successful. |
| 2999 |
* |
| 3000 |
* Side effects: |
| 3001 |
* None. |
| 3002 |
* |
| 3003 |
*------------------------------------------------------------------------- |
| 3004 |
*/ |
| 3005 |
|
| 3006 |
static int |
| 3007 |
Iso88591FromUtfProc( |
| 3008 |
ClientData clientData, /* Ignored. */ |
| 3009 |
const char *src, /* Source string in UTF-8. */ |
| 3010 |
int srcLen, /* Source string length in bytes. */ |
| 3011 |
int flags, /* Conversion control flags. */ |
| 3012 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 3013 |
* information used during a piecewise |
| 3014 |
* conversion. Contents of statePtr are |
| 3015 |
* initialized and/or reset by conversion |
| 3016 |
* routine under control of flags argument. */ |
| 3017 |
char *dst, /* Output buffer in which converted string is |
| 3018 |
* stored. */ |
| 3019 |
int dstLen, /* The maximum length of output buffer in |
| 3020 |
* bytes. */ |
| 3021 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 3022 |
* source string that were converted. This may |
| 3023 |
* be less than the original source length if |
| 3024 |
* there was a problem converting some source |
| 3025 |
* characters. */ |
| 3026 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 3027 |
* stored in the output buffer as a result of |
| 3028 |
* the conversion. */ |
| 3029 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 3030 |
* correspond to the bytes stored in the |
| 3031 |
* output buffer. */ |
| 3032 |
{ |
| 3033 |
const char *srcStart, *srcEnd, *srcClose; |
| 3034 |
const char *dstStart, *dstEnd; |
| 3035 |
int result = TCL_OK, numChars; |
| 3036 |
Tcl_UniChar ch = 0; |
| 3037 |
|
| 3038 |
srcStart = src; |
| 3039 |
srcEnd = src + srcLen; |
| 3040 |
srcClose = srcEnd; |
| 3041 |
if ((flags & TCL_ENCODING_END) == 0) { |
| 3042 |
srcClose -= TCL_UTF_MAX; |
| 3043 |
} |
| 3044 |
|
| 3045 |
dstStart = dst; |
| 3046 |
dstEnd = dst + dstLen - 1; |
| 3047 |
|
| 3048 |
for (numChars = 0; src < srcEnd; numChars++) { |
| 3049 |
int len; |
| 3050 |
|
| 3051 |
if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) { |
| 3052 |
/* |
| 3053 |
* If there is more string to follow, this will ensure that the |
| 3054 |
* last UTF-8 character in the source buffer hasn't been cut off. |
| 3055 |
*/ |
| 3056 |
|
| 3057 |
result = TCL_CONVERT_MULTIBYTE; |
| 3058 |
break; |
| 3059 |
} |
| 3060 |
len = TclUtfToUniChar(src, &ch); |
| 3061 |
|
| 3062 |
/* |
| 3063 |
* Check for illegal characters. |
| 3064 |
*/ |
| 3065 |
|
| 3066 |
if (ch > 0xFF |
| 3067 |
#if TCL_UTF_MAX == 4 |
| 3068 |
|| ((ch >= 0xD800) && (len < 3)) |
| 3069 |
#endif |
| 3070 |
) { |
| 3071 |
if (flags & TCL_ENCODING_STOPONERROR) { |
| 3072 |
result = TCL_CONVERT_UNKNOWN; |
| 3073 |
break; |
| 3074 |
} |
| 3075 |
#if TCL_UTF_MAX == 4 |
| 3076 |
if ((ch >= 0xD800) && (len < 3)) { |
| 3077 |
len = 4; |
| 3078 |
} |
| 3079 |
#endif |
| 3080 |
/* |
| 3081 |
* Plunge on, using '?' as a fallback character. |
| 3082 |
*/ |
| 3083 |
|
| 3084 |
ch = (Tcl_UniChar) '?'; |
| 3085 |
} |
| 3086 |
|
| 3087 |
if (dst > dstEnd) { |
| 3088 |
result = TCL_CONVERT_NOSPACE; |
| 3089 |
break; |
| 3090 |
} |
| 3091 |
*(dst++) = (char) ch; |
| 3092 |
src += len; |
| 3093 |
} |
| 3094 |
|
| 3095 |
*srcReadPtr = src - srcStart; |
| 3096 |
*dstWrotePtr = dst - dstStart; |
| 3097 |
*dstCharsPtr = numChars; |
| 3098 |
return result; |
| 3099 |
} |
| 3100 |
|
| 3101 |
/* |
| 3102 |
*--------------------------------------------------------------------------- |
| 3103 |
* |
| 3104 |
* TableFreeProc -- |
| 3105 |
* |
| 3106 |
* This function is invoked when an encoding is deleted. It deletes the |
| 3107 |
* memory used by the TableEncodingData. |
| 3108 |
* |
| 3109 |
* Results: |
| 3110 |
* None. |
| 3111 |
* |
| 3112 |
* Side effects: |
| 3113 |
* Memory freed. |
| 3114 |
* |
| 3115 |
*--------------------------------------------------------------------------- |
| 3116 |
*/ |
| 3117 |
|
| 3118 |
static void |
| 3119 |
TableFreeProc( |
| 3120 |
ClientData clientData) /* TableEncodingData that specifies |
| 3121 |
* encoding. */ |
| 3122 |
{ |
| 3123 |
TableEncodingData *dataPtr = (TableEncodingData *) clientData; |
| 3124 |
|
| 3125 |
/* |
| 3126 |
* Make sure we aren't freeing twice on shutdown. [Bug 219314] |
| 3127 |
*/ |
| 3128 |
|
| 3129 |
ckfree(dataPtr->toUnicode); |
| 3130 |
dataPtr->toUnicode = NULL; |
| 3131 |
ckfree(dataPtr->fromUnicode); |
| 3132 |
dataPtr->fromUnicode = NULL; |
| 3133 |
ckfree(dataPtr); |
| 3134 |
} |
| 3135 |
|
| 3136 |
/* |
| 3137 |
*------------------------------------------------------------------------- |
| 3138 |
* |
| 3139 |
* EscapeToUtfProc -- |
| 3140 |
* |
| 3141 |
* Convert from the encoding specified by the EscapeEncodingData into |
| 3142 |
* UTF-8. |
| 3143 |
* |
| 3144 |
* Results: |
| 3145 |
* Returns TCL_OK if conversion was successful. |
| 3146 |
* |
| 3147 |
* Side effects: |
| 3148 |
* None. |
| 3149 |
* |
| 3150 |
*------------------------------------------------------------------------- |
| 3151 |
*/ |
| 3152 |
|
| 3153 |
static int |
| 3154 |
EscapeToUtfProc( |
| 3155 |
ClientData clientData, /* EscapeEncodingData that specifies |
| 3156 |
* encoding. */ |
| 3157 |
const char *src, /* Source string in specified encoding. */ |
| 3158 |
int srcLen, /* Source string length in bytes. */ |
| 3159 |
int flags, /* Conversion control flags. */ |
| 3160 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 3161 |
* information used during a piecewise |
| 3162 |
* conversion. Contents of statePtr are |
| 3163 |
* initialized and/or reset by conversion |
| 3164 |
* routine under control of flags argument. */ |
| 3165 |
char *dst, /* Output buffer in which converted string is |
| 3166 |
* stored. */ |
| 3167 |
int dstLen, /* The maximum length of output buffer in |
| 3168 |
* bytes. */ |
| 3169 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 3170 |
* source string that were converted. This may |
| 3171 |
* be less than the original source length if |
| 3172 |
* there was a problem converting some source |
| 3173 |
* characters. */ |
| 3174 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 3175 |
* stored in the output buffer as a result of |
| 3176 |
* the conversion. */ |
| 3177 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 3178 |
* correspond to the bytes stored in the |
| 3179 |
* output buffer. */ |
| 3180 |
{ |
| 3181 |
EscapeEncodingData *dataPtr = (EscapeEncodingData *) clientData; |
| 3182 |
const char *prefixBytes, *tablePrefixBytes, *srcStart, *srcEnd; |
| 3183 |
const unsigned short *const *tableToUnicode; |
| 3184 |
const Encoding *encodingPtr; |
| 3185 |
int state, result, numChars, charLimit = INT_MAX; |
| 3186 |
const char *dstStart, *dstEnd; |
| 3187 |
|
| 3188 |
if (flags & TCL_ENCODING_CHAR_LIMIT) { |
| 3189 |
charLimit = *dstCharsPtr; |
| 3190 |
} |
| 3191 |
result = TCL_OK; |
| 3192 |
tablePrefixBytes = NULL; |
| 3193 |
tableToUnicode = NULL; |
| 3194 |
prefixBytes = dataPtr->prefixBytes; |
| 3195 |
encodingPtr = NULL; |
| 3196 |
|
| 3197 |
srcStart = src; |
| 3198 |
srcEnd = src + srcLen; |
| 3199 |
|
| 3200 |
dstStart = dst; |
| 3201 |
dstEnd = dst + dstLen - TCL_UTF_MAX; |
| 3202 |
|
| 3203 |
state = PTR2INT(*statePtr); |
| 3204 |
if (flags & TCL_ENCODING_START) { |
| 3205 |
state = 0; |
| 3206 |
} |
| 3207 |
|
| 3208 |
for (numChars = 0; src < srcEnd && numChars <= charLimit; ) { |
| 3209 |
int byte, hi, lo, ch; |
| 3210 |
|
| 3211 |
if (dst > dstEnd) { |
| 3212 |
result = TCL_CONVERT_NOSPACE; |
| 3213 |
break; |
| 3214 |
} |
| 3215 |
byte = *((unsigned char *) src); |
| 3216 |
if (prefixBytes[byte]) { |
| 3217 |
unsigned left, len, longest; |
| 3218 |
int checked, i; |
| 3219 |
const EscapeSubTable *subTablePtr; |
| 3220 |
|
| 3221 |
/* |
| 3222 |
* Saw the beginning of an escape sequence. |
| 3223 |
*/ |
| 3224 |
|
| 3225 |
left = srcEnd - src; |
| 3226 |
len = dataPtr->initLen; |
| 3227 |
longest = len; |
| 3228 |
checked = 0; |
| 3229 |
|
| 3230 |
if (len <= left) { |
| 3231 |
checked++; |
| 3232 |
if ((len > 0) && (memcmp(src, dataPtr->init, len) == 0)) { |
| 3233 |
/* |
| 3234 |
* If we see initialization string, skip it, even if we're |
| 3235 |
* not at the beginning of the buffer. |
| 3236 |
*/ |
| 3237 |
|
| 3238 |
src += len; |
| 3239 |
continue; |
| 3240 |
} |
| 3241 |
} |
| 3242 |
|
| 3243 |
len = dataPtr->finalLen; |
| 3244 |
if (len > longest) { |
| 3245 |
longest = len; |
| 3246 |
} |
| 3247 |
|
| 3248 |
if (len <= left) { |
| 3249 |
checked++; |
| 3250 |
if ((len > 0) && (memcmp(src, dataPtr->final, len) == 0)) { |
| 3251 |
/* |
| 3252 |
* If we see finalization string, skip it, even if we're |
| 3253 |
* not at the end of the buffer. |
| 3254 |
*/ |
| 3255 |
|
| 3256 |
src += len; |
| 3257 |
continue; |
| 3258 |
} |
| 3259 |
} |
| 3260 |
|
| 3261 |
subTablePtr = dataPtr->subTables; |
| 3262 |
for (i = 0; i < dataPtr->numSubTables; i++) { |
| 3263 |
len = subTablePtr->sequenceLen; |
| 3264 |
if (len > longest) { |
| 3265 |
longest = len; |
| 3266 |
} |
| 3267 |
if (len <= left) { |
| 3268 |
checked++; |
| 3269 |
if ((len > 0) && |
| 3270 |
(memcmp(src, subTablePtr->sequence, len) == 0)) { |
| 3271 |
state = i; |
| 3272 |
encodingPtr = NULL; |
| 3273 |
subTablePtr = NULL; |
| 3274 |
src += len; |
| 3275 |
break; |
| 3276 |
} |
| 3277 |
} |
| 3278 |
subTablePtr++; |
| 3279 |
} |
| 3280 |
|
| 3281 |
if (subTablePtr == NULL) { |
| 3282 |
/* |
| 3283 |
* A match was found, the escape sequence was consumed, and |
| 3284 |
* the state was updated. |
| 3285 |
*/ |
| 3286 |
|
| 3287 |
continue; |
| 3288 |
} |
| 3289 |
|
| 3290 |
/* |
| 3291 |
* We have a split-up or unrecognized escape sequence. If we |
| 3292 |
* checked all the sequences, then it's a syntax error, otherwise |
| 3293 |
* we need more bytes to determine a match. |
| 3294 |
*/ |
| 3295 |
|
| 3296 |
if ((checked == dataPtr->numSubTables + 2) |
| 3297 |
|| (flags & TCL_ENCODING_END)) { |
| 3298 |
if ((flags & TCL_ENCODING_STOPONERROR) == 0) { |
| 3299 |
/* |
| 3300 |
* Skip the unknown escape sequence. |
| 3301 |
*/ |
| 3302 |
|
| 3303 |
src += longest; |
| 3304 |
continue; |
| 3305 |
} |
| 3306 |
result = TCL_CONVERT_SYNTAX; |
| 3307 |
} else { |
| 3308 |
result = TCL_CONVERT_MULTIBYTE; |
| 3309 |
} |
| 3310 |
break; |
| 3311 |
} |
| 3312 |
|
| 3313 |
if (encodingPtr == NULL) { |
| 3314 |
TableEncodingData *tableDataPtr; |
| 3315 |
|
| 3316 |
encodingPtr = GetTableEncoding(dataPtr, state); |
| 3317 |
tableDataPtr = (TableEncodingData *)encodingPtr->clientData; |
| 3318 |
tablePrefixBytes = tableDataPtr->prefixBytes; |
| 3319 |
tableToUnicode = (const unsigned short *const*) |
| 3320 |
tableDataPtr->toUnicode; |
| 3321 |
} |
| 3322 |
|
| 3323 |
if (tablePrefixBytes[byte]) { |
| 3324 |
src++; |
| 3325 |
if (src >= srcEnd) { |
| 3326 |
src--; |
| 3327 |
result = TCL_CONVERT_MULTIBYTE; |
| 3328 |
break; |
| 3329 |
} |
| 3330 |
hi = byte; |
| 3331 |
lo = *((unsigned char *) src); |
| 3332 |
} else { |
| 3333 |
hi = 0; |
| 3334 |
lo = byte; |
| 3335 |
} |
| 3336 |
|
| 3337 |
ch = tableToUnicode[hi][lo]; |
| 3338 |
dst += Tcl_UniCharToUtf(ch, dst); |
| 3339 |
src++; |
| 3340 |
numChars++; |
| 3341 |
} |
| 3342 |
|
| 3343 |
*statePtr = (Tcl_EncodingState) INT2PTR(state); |
| 3344 |
*srcReadPtr = src - srcStart; |
| 3345 |
*dstWrotePtr = dst - dstStart; |
| 3346 |
*dstCharsPtr = numChars; |
| 3347 |
return result; |
| 3348 |
} |
| 3349 |
|
| 3350 |
/* |
| 3351 |
*------------------------------------------------------------------------- |
| 3352 |
* |
| 3353 |
* EscapeFromUtfProc -- |
| 3354 |
* |
| 3355 |
* Convert from UTF-8 into the encoding specified by the |
| 3356 |
* EscapeEncodingData. |
| 3357 |
* |
| 3358 |
* Results: |
| 3359 |
* Returns TCL_OK if conversion was successful. |
| 3360 |
* |
| 3361 |
* Side effects: |
| 3362 |
* None. |
| 3363 |
* |
| 3364 |
*------------------------------------------------------------------------- |
| 3365 |
*/ |
| 3366 |
|
| 3367 |
static int |
| 3368 |
EscapeFromUtfProc( |
| 3369 |
ClientData clientData, /* EscapeEncodingData that specifies |
| 3370 |
* encoding. */ |
| 3371 |
const char *src, /* Source string in UTF-8. */ |
| 3372 |
int srcLen, /* Source string length in bytes. */ |
| 3373 |
int flags, /* Conversion control flags. */ |
| 3374 |
Tcl_EncodingState *statePtr,/* Place for conversion routine to store state |
| 3375 |
* information used during a piecewise |
| 3376 |
* conversion. Contents of statePtr are |
| 3377 |
* initialized and/or reset by conversion |
| 3378 |
* routine under control of flags argument. */ |
| 3379 |
char *dst, /* Output buffer in which converted string is |
| 3380 |
* stored. */ |
| 3381 |
int dstLen, /* The maximum length of output buffer in |
| 3382 |
* bytes. */ |
| 3383 |
int *srcReadPtr, /* Filled with the number of bytes from the |
| 3384 |
* source string that were converted. This may |
| 3385 |
* be less than the original source length if |
| 3386 |
* there was a problem converting some source |
| 3387 |
* characters. */ |
| 3388 |
int *dstWrotePtr, /* Filled with the number of bytes that were |
| 3389 |
* stored in the output buffer as a result of |
| 3390 |
* the conversion. */ |
| 3391 |
int *dstCharsPtr) /* Filled with the number of characters that |
| 3392 |
* correspond to the bytes stored in the |
| 3393 |
* output buffer. */ |
| 3394 |
{ |
| 3395 |
EscapeEncodingData *dataPtr = (EscapeEncodingData *)clientData; |
| 3396 |
const Encoding *encodingPtr; |
| 3397 |
const char *srcStart, *srcEnd, *srcClose; |
| 3398 |
const char *dstStart, *dstEnd; |
| 3399 |
int state, result, numChars; |
| 3400 |
const TableEncodingData *tableDataPtr; |
| 3401 |
const char *tablePrefixBytes; |
| 3402 |
const unsigned short *const *tableFromUnicode; |
| 3403 |
Tcl_UniChar ch = 0; |
| 3404 |
|
| 3405 |
result = TCL_OK; |
| 3406 |
|
| 3407 |
srcStart = src; |
| 3408 |
srcEnd = src + srcLen; |
| 3409 |
srcClose = srcEnd; |
| 3410 |
if ((flags & TCL_ENCODING_END) == 0) { |
| 3411 |
srcClose -= TCL_UTF_MAX; |
| 3412 |
} |
| 3413 |
|
| 3414 |
dstStart = dst; |
| 3415 |
dstEnd = dst + dstLen - 1; |
| 3416 |
|
| 3417 |
/* |
| 3418 |
* RFC 1468 states that the text starts in ASCII, and switches to Japanese |
| 3419 |
* characters, and that the text must end in ASCII. [Patch 474358] |
| 3420 |
*/ |
| 3421 |
|
| 3422 |
if (flags & TCL_ENCODING_START) { |
| 3423 |
state = 0; |
| 3424 |
if ((dst + dataPtr->initLen) > dstEnd) { |
| 3425 |
*srcReadPtr = 0; |
| 3426 |
*dstWrotePtr = 0; |
| 3427 |
return TCL_CONVERT_NOSPACE; |
| 3428 |
} |
| 3429 |
memcpy(dst, dataPtr->init, dataPtr->initLen); |
| 3430 |
dst += dataPtr->initLen; |
| 3431 |
} else { |
| 3432 |
state = PTR2INT(*statePtr); |
| 3433 |
} |
| 3434 |
|
| 3435 |
encodingPtr = GetTableEncoding(dataPtr, state); |
| 3436 |
tableDataPtr = (const TableEncodingData *)encodingPtr->clientData; |
| 3437 |
tablePrefixBytes = tableDataPtr->prefixBytes; |
| 3438 |
tableFromUnicode = (const unsigned short *const *) |
| 3439 |
tableDataPtr->fromUnicode; |
| 3440 |
|
| 3441 |
for (numChars = 0; src < srcEnd; numChars++) { |
| 3442 |
unsigned len; |
| 3443 |
int word; |
| 3444 |
|
| 3445 |
if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) { |
| 3446 |
/* |
| 3447 |
* If there is more string to follow, this will ensure that the |
| 3448 |
* last UTF-8 character in the source buffer hasn't been cut off. |
| 3449 |
*/ |
| 3450 |
|
| 3451 |
result = TCL_CONVERT_MULTIBYTE; |
| 3452 |
break; |
| 3453 |
} |
| 3454 |
len = TclUtfToUniChar(src, &ch); |
| 3455 |
word = tableFromUnicode[(ch >> 8)][ch & 0xFF]; |
| 3456 |
|
| 3457 |
if ((word == 0) && (ch != 0)) { |
| 3458 |
int oldState; |
| 3459 |
const EscapeSubTable *subTablePtr; |
| 3460 |
|
| 3461 |
oldState = state; |
| 3462 |
for (state = 0; state < dataPtr->numSubTables; state++) { |
| 3463 |
encodingPtr = GetTableEncoding(dataPtr, state); |
| 3464 |
tableDataPtr = (const TableEncodingData *)encodingPtr->clientData; |
| 3465 |
word = tableDataPtr->fromUnicode[(ch >> 8)][ch & 0xFF]; |
| 3466 |
if (word != 0) { |
| 3467 |
break; |
| 3468 |
} |
| 3469 |
} |
| 3470 |
|
| 3471 |
if (word == 0) { |
| 3472 |
state = oldState; |
| 3473 |
if (flags & TCL_ENCODING_STOPONERROR) { |
| 3474 |
result = TCL_CONVERT_UNKNOWN; |
| 3475 |
break; |
| 3476 |
} |
| 3477 |
encodingPtr = GetTableEncoding(dataPtr, state); |
| 3478 |
tableDataPtr = (const TableEncodingData *)encodingPtr->clientData; |
| 3479 |
word = tableDataPtr->fallback; |
| 3480 |
} |
| 3481 |
|
| 3482 |
tablePrefixBytes = (const char *) tableDataPtr->prefixBytes; |
| 3483 |
tableFromUnicode = (const unsigned short *const *) |
| 3484 |
tableDataPtr->fromUnicode; |
| 3485 |
|
| 3486 |
/* |
| 3487 |
* The state variable has the value of oldState when word is 0. |
| 3488 |
* In this case, the escape sequence should not be copied to dst |
| 3489 |
* because the current character set is not changed. |
| 3490 |
*/ |
| 3491 |
|
| 3492 |
if (state != oldState) { |
| 3493 |
subTablePtr = &dataPtr->subTables[state]; |
| 3494 |
if ((dst + subTablePtr->sequenceLen) > dstEnd) { |
| 3495 |
/* |
| 3496 |
* If there is no space to write the escape sequence, the |
| 3497 |
* state variable must be changed to the value of oldState |
| 3498 |
* variable because this escape sequence must be written |
| 3499 |
* in the next conversion. |
| 3500 |
*/ |
| 3501 |
|
| 3502 |
state = oldState; |
| 3503 |
result = TCL_CONVERT_NOSPACE; |
| 3504 |
break; |
| 3505 |
} |
| 3506 |
memcpy(dst, subTablePtr->sequence, |
| 3507 |
subTablePtr->sequenceLen); |
| 3508 |
dst += subTablePtr->sequenceLen; |
| 3509 |
} |
| 3510 |
} |
| 3511 |
|
| 3512 |
if (tablePrefixBytes[(word >> 8)] != 0) { |
| 3513 |
if (dst + 1 > dstEnd) { |
| 3514 |
result = TCL_CONVERT_NOSPACE; |
| 3515 |
break; |
| 3516 |
} |
| 3517 |
dst[0] = (char) (word >> 8); |
| 3518 |
dst[1] = (char) word; |
| 3519 |
dst += 2; |
| 3520 |
} else { |
| 3521 |
if (dst > dstEnd) { |
| 3522 |
result = TCL_CONVERT_NOSPACE; |
| 3523 |
break; |
| 3524 |
} |
| 3525 |
dst[0] = (char) word; |
| 3526 |
dst++; |
| 3527 |
} |
| 3528 |
src += len; |
| 3529 |
} |
| 3530 |
|
| 3531 |
if ((result == TCL_OK) && (flags & TCL_ENCODING_END)) { |
| 3532 |
unsigned len = dataPtr->subTables[0].sequenceLen; |
| 3533 |
|
| 3534 |
/* |
| 3535 |
* Certain encodings like iso2022-jp need to write an escape sequence |
| 3536 |
* after all characters have been converted. This logic checks that |
| 3537 |
* enough room is available in the buffer for the escape bytes. The |
| 3538 |
* TCL_ENCODING_END flag is cleared after a final escape sequence has |
| 3539 |
* been added to the buffer so that another call to this method does |
| 3540 |
* not attempt to append escape bytes a second time. |
| 3541 |
*/ |
| 3542 |
|
| 3543 |
if ((dst + dataPtr->finalLen + (state?len:0)) > dstEnd) { |
| 3544 |
result = TCL_CONVERT_NOSPACE; |
| 3545 |
} else { |
| 3546 |
if (state) { |
| 3547 |
memcpy(dst, dataPtr->subTables[0].sequence, len); |
| 3548 |
dst += len; |
| 3549 |
} |
| 3550 |
memcpy(dst, dataPtr->final, dataPtr->finalLen); |
| 3551 |
dst += dataPtr->finalLen; |
| 3552 |
state &= ~TCL_ENCODING_END; |
| 3553 |
} |
| 3554 |
} |
| 3555 |
|
| 3556 |
*statePtr = (Tcl_EncodingState) INT2PTR(state); |
| 3557 |
*srcReadPtr = src - srcStart; |
| 3558 |
*dstWrotePtr = dst - dstStart; |
| 3559 |
*dstCharsPtr = numChars; |
| 3560 |
return result; |
| 3561 |
} |
| 3562 |
|
| 3563 |
/* |
| 3564 |
*--------------------------------------------------------------------------- |
| 3565 |
* |
| 3566 |
* EscapeFreeProc -- |
| 3567 |
* |
| 3568 |
* Frees resources used by the encoding. |
| 3569 |
* |
| 3570 |
* Results: |
| 3571 |
* None. |
| 3572 |
* |
| 3573 |
* Side effects: |
| 3574 |
* Memory is freed. |
| 3575 |
* |
| 3576 |
*--------------------------------------------------------------------------- |
| 3577 |
*/ |
| 3578 |
|
| 3579 |
static void |
| 3580 |
EscapeFreeProc( |
| 3581 |
ClientData clientData) /* EscapeEncodingData that specifies |
| 3582 |
* encoding. */ |
| 3583 |
{ |
| 3584 |
EscapeEncodingData *dataPtr = (EscapeEncodingData *)clientData; |
| 3585 |
EscapeSubTable *subTablePtr; |
| 3586 |
int i; |
| 3587 |
|
| 3588 |
if (dataPtr == NULL) { |
| 3589 |
return; |
| 3590 |
} |
| 3591 |
|
| 3592 |
/* |
| 3593 |
* The subTables should be freed recursively in normal operation but not |
| 3594 |
* during TclFinalizeEncodingSubsystem because they are also present as a |
| 3595 |
* weak reference in the toplevel encodingTable (i.e., they don't have a |
| 3596 |
* +1 refcount for this), and unpredictable nuking order could remove them |
| 3597 |
* from under the following loop's feet. [Bug 2891556] |
| 3598 |
* |
| 3599 |
* The encodingsInitialized flag, being reset on entry to TFES, can serve |
| 3600 |
* as a "not in finalization" test. |
| 3601 |
*/ |
| 3602 |
|
| 3603 |
if (encodingsInitialized) { |
| 3604 |
subTablePtr = dataPtr->subTables; |
| 3605 |
for (i = 0; i < dataPtr->numSubTables; i++) { |
| 3606 |
FreeEncoding((Tcl_Encoding) subTablePtr->encodingPtr); |
| 3607 |
subTablePtr->encodingPtr = NULL; |
| 3608 |
subTablePtr++; |
| 3609 |
} |
| 3610 |
} |
| 3611 |
ckfree(dataPtr); |
| 3612 |
} |
| 3613 |
|
| 3614 |
/* |
| 3615 |
*--------------------------------------------------------------------------- |
| 3616 |
* |
| 3617 |
* GetTableEncoding -- |
| 3618 |
* |
| 3619 |
* Helper function for the EscapeEncodingData conversions. Gets the |
| 3620 |
* encoding (of type TextEncodingData) that represents the specified |
| 3621 |
* state. |
| 3622 |
* |
| 3623 |
* Results: |
| 3624 |
* The return value is the encoding. |
| 3625 |
* |
| 3626 |
* Side effects: |
| 3627 |
* If the encoding that represents the specified state has not already |
| 3628 |
* been used by this EscapeEncoding, it will be loaded and cached in the |
| 3629 |
* dataPtr. |
| 3630 |
* |
| 3631 |
*--------------------------------------------------------------------------- |
| 3632 |
*/ |
| 3633 |
|
| 3634 |
static Encoding * |
| 3635 |
GetTableEncoding( |
| 3636 |
EscapeEncodingData *dataPtr,/* Contains names of encodings. */ |
| 3637 |
int state) /* Index in dataPtr of desired Encoding. */ |
| 3638 |
{ |
| 3639 |
EscapeSubTable *subTablePtr = &dataPtr->subTables[state]; |
| 3640 |
Encoding *encodingPtr = subTablePtr->encodingPtr; |
| 3641 |
|
| 3642 |
if (encodingPtr == NULL) { |
| 3643 |
encodingPtr = (Encoding *) Tcl_GetEncoding(NULL, subTablePtr->name); |
| 3644 |
if ((encodingPtr == NULL) |
| 3645 |
|| (encodingPtr->toUtfProc != TableToUtfProc |
| 3646 |
&& encodingPtr->toUtfProc != Iso88591ToUtfProc)) { |
| 3647 |
Tcl_Panic("EscapeToUtfProc: invalid sub table"); |
| 3648 |
} |
| 3649 |
subTablePtr->encodingPtr = encodingPtr; |
| 3650 |
} |
| 3651 |
|
| 3652 |
return encodingPtr; |
| 3653 |
} |
| 3654 |
|
| 3655 |
/* |
| 3656 |
*--------------------------------------------------------------------------- |
| 3657 |
* |
| 3658 |
* unilen -- |
| 3659 |
* |
| 3660 |
* A helper function for the Tcl_ExternalToUtf functions. This function |
| 3661 |
* is similar to strlen for double-byte characters: it returns the number |
| 3662 |
* of bytes in a 0x0000 terminated string. |
| 3663 |
* |
| 3664 |
* Results: |
| 3665 |
* As above. |
| 3666 |
* |
| 3667 |
* Side effects: |
| 3668 |
* None. |
| 3669 |
* |
| 3670 |
*--------------------------------------------------------------------------- |
| 3671 |
*/ |
| 3672 |
|
| 3673 |
static size_t |
| 3674 |
unilen( |
| 3675 |
const char *src) |
| 3676 |
{ |
| 3677 |
unsigned short *p; |
| 3678 |
|
| 3679 |
p = (unsigned short *) src; |
| 3680 |
while (*p != 0x0000) { |
| 3681 |
p++; |
| 3682 |
} |
| 3683 |
return (char *) p - src; |
| 3684 |
} |
| 3685 |
|
| 3686 |
/* |
| 3687 |
*------------------------------------------------------------------------- |
| 3688 |
* |
| 3689 |
* InitializeEncodingSearchPath -- |
| 3690 |
* |
| 3691 |
* This is the fallback routine that sets the default value of the |
| 3692 |
* encoding search path if the application has not set one via a call to |
| 3693 |
* Tcl_SetEncodingSearchPath() by the first time the search path is needed |
| 3694 |
* to load encoding data. |
| 3695 |
* |
| 3696 |
* The default encoding search path is produced by taking each directory |
| 3697 |
* in the library path, appending a subdirectory named "encoding", and if |
| 3698 |
* the resulting directory exists, adding it to the encoding search path. |
| 3699 |
* |
| 3700 |
* Results: |
| 3701 |
* None. |
| 3702 |
* |
| 3703 |
* Side effects: |
| 3704 |
* Sets the encoding search path to an initial value. |
| 3705 |
* |
| 3706 |
*------------------------------------------------------------------------- |
| 3707 |
*/ |
| 3708 |
|
| 3709 |
static void |
| 3710 |
InitializeEncodingSearchPath( |
| 3711 |
char **valuePtr, |
| 3712 |
int *lengthPtr, |
| 3713 |
Tcl_Encoding *encodingPtr) |
| 3714 |
{ |
| 3715 |
const char *bytes; |
| 3716 |
int i, numDirs, numBytes; |
| 3717 |
Tcl_Obj *libPathObj, *encodingObj, *searchPathObj; |
| 3718 |
|
| 3719 |
TclNewLiteralStringObj(encodingObj, "encoding"); |
| 3720 |
TclNewObj(searchPathObj); |
| 3721 |
Tcl_IncrRefCount(encodingObj); |
| 3722 |
Tcl_IncrRefCount(searchPathObj); |
| 3723 |
libPathObj = TclGetLibraryPath(); |
| 3724 |
Tcl_IncrRefCount(libPathObj); |
| 3725 |
TclListObjLength(NULL, libPathObj, &numDirs); |
| 3726 |
|
| 3727 |
for (i = 0; i < numDirs; i++) { |
| 3728 |
Tcl_Obj *directoryObj, *pathObj; |
| 3729 |
Tcl_StatBuf stat; |
| 3730 |
|
| 3731 |
Tcl_ListObjIndex(NULL, libPathObj, i, &directoryObj); |
| 3732 |
pathObj = Tcl_FSJoinToPath(directoryObj, 1, &encodingObj); |
| 3733 |
Tcl_IncrRefCount(pathObj); |
| 3734 |
if ((0 == Tcl_FSStat(pathObj, &stat)) && S_ISDIR(stat.st_mode)) { |
| 3735 |
Tcl_ListObjAppendElement(NULL, searchPathObj, pathObj); |
| 3736 |
} |
| 3737 |
Tcl_DecrRefCount(pathObj); |
| 3738 |
} |
| 3739 |
|
| 3740 |
Tcl_DecrRefCount(libPathObj); |
| 3741 |
Tcl_DecrRefCount(encodingObj); |
| 3742 |
*encodingPtr = libraryPath.encoding; |
| 3743 |
if (*encodingPtr) { |
| 3744 |
((Encoding *)(*encodingPtr))->refCount++; |
| 3745 |
} |
| 3746 |
bytes = Tcl_GetStringFromObj(searchPathObj, &numBytes); |
| 3747 |
|
| 3748 |
*lengthPtr = numBytes; |
| 3749 |
*valuePtr = (char *)ckalloc(numBytes + 1); |
| 3750 |
memcpy(*valuePtr, bytes, numBytes + 1); |
| 3751 |
Tcl_DecrRefCount(searchPathObj); |
| 3752 |
} |
| 3753 |
|
| 3754 |
/* |
| 3755 |
* Local Variables: |
| 3756 |
* mode: c |
| 3757 |
* c-basic-offset: 4 |
| 3758 |
* fill-column: 78 |
| 3759 |
* End: |
| 3760 |
*/ |