Debian Codegraph @

pTk/mTk/tclGeneric/tclEncoding.c

raw symbols SCIP
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
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tclEncoding.c,v 1.16.2.2 2003/11/06 21:47:33 hobbs Exp $
12 */
13
14 #include "Lang.h"
15 #include "tkInt.h"
16
17 typedef size_t (LengthProc)_ANSI_ARGS_((CONST char *src));
18
19 /*
20 * The following data structure represents an encoding, which describes how
21 * to convert between various character sets and UTF-8.
22 */
23
24 typedef struct Encoding {
25 char *name; /* Name of encoding. Malloced because (1)
26 * hash table entry that owns this encoding
27 * may be freed prior to this encoding being
28 * freed, (2) string passed in the
29 * Tcl_EncodingType structure may not be
30 * persistent. */
31 Tcl_EncodingConvertProc *toUtfProc;
32 /* Procedure to convert from external
33 * encoding into UTF-8. */
34 Tcl_EncodingConvertProc *fromUtfProc;
35 /* Procedure to convert from UTF-8 into
36 * external encoding. */
37 Tcl_EncodingFreeProc *freeProc;
38 /* If non-NULL, procedure to call when this
39 * encoding is deleted. */
40 int nullSize; /* Number of 0x00 bytes that signify
41 * end-of-string in this encoding. This
42 * number is used to determine the source
43 * string length when the srcLen argument is
44 * negative. This number can be 1 or 2. */
45 ClientData clientData; /* Arbitrary value associated with encoding
46 * type. Passed to conversion procedures. */
47 LengthProc *lengthProc; /* Function to compute length of
48 * null-terminated strings in this encoding.
49 * If nullSize is 1, this is strlen; if
50 * nullSize is 2, this is a function that
51 * returns the number of bytes in a 0x0000
52 * terminated string. */
53 int refCount; /* Number of uses of this structure. */
54 Tcl_HashEntry *hPtr; /* Hash table entry that owns this encoding. */
55 } Encoding;
56
57 /*
58 * The following structure is the clientData for a dynamically-loaded,
59 * table-driven encoding created by LoadTableEncoding(). It maps between
60 * Unicode and a single-byte, double-byte, or multibyte (1 or 2 bytes only)
61 * encoding.
62 */
63
64 typedef struct TableEncodingData {
65 int fallback; /* Character (in this encoding) to
66 * substitute when this encoding cannot
67 * represent a UTF-8 character. */
68 char prefixBytes[256]; /* If a byte in the input stream is a lead
69 * byte for a 2-byte sequence, the
70 * corresponding entry in this array is 1,
71 * otherwise it is 0. */
72 unsigned short **toUnicode; /* Two dimensional sparse matrix to map
73 * characters from the encoding to Unicode.
74 * Each element of the toUnicode array points
75 * to an array of 256 shorts. If there is no
76 * corresponding character in Unicode, the
77 * value in the matrix is 0x0000. malloc'd. */
78 unsigned short **fromUnicode;
79 /* Two dimensional sparse matrix to map
80 * characters from Unicode to the encoding.
81 * Each element of the fromUnicode array
82 * points to an array of 256 shorts. If there
83 * is no corresponding character the encoding,
84 * the value in the matrix is 0x0000.
85 * malloc'd. */
86 } TableEncodingData;
87
88 /*
89 * The following structures is the clientData for a dynamically-loaded,
90 * escape-driven encoding that is itself comprised of other simpler
91 * encodings. An example is "iso-2022-jp", which uses escape sequences to
92 * switch between ascii, jis0208, jis0212, gb2312, and ksc5601. Note that
93 * "escape-driven" does not necessarily mean that the ESCAPE character is
94 * the character used for switching character sets.
95 */
96
97 typedef struct EscapeSubTable {
98 unsigned int sequenceLen; /* Length of following string. */
99 char sequence[16]; /* Escape code that marks this encoding. */
100 char name[32]; /* Name for encoding. */
101 Encoding *encodingPtr; /* Encoding loaded using above name, or NULL
102 * if this sub-encoding has not been needed
103 * yet. */
104 } EscapeSubTable;
105
106 typedef struct EscapeEncodingData {
107 int fallback; /* Character (in this encoding) to
108 * substitute when this encoding cannot
109 * represent a UTF-8 character. */
110 unsigned int initLen; /* Length of following string. */
111 char init[16]; /* String to emit or expect before first char
112 * in conversion. */
113 unsigned int finalLen; /* Length of following string. */
114 char final[16]; /* String to emit or expect after last char
115 * in conversion. */
116 char prefixBytes[256]; /* If a byte in the input stream is the
117 * first character of one of the escape
118 * sequences in the following array, the
119 * corresponding entry in this array is 1,
120 * otherwise it is 0. */
121 int numSubTables; /* Length of following array. */
122 EscapeSubTable subTables[1];/* Information about each EscapeSubTable
123 * used by this encoding type. The actual
124 * size will be as large as necessary to
125 * hold all EscapeSubTables. */
126 } EscapeEncodingData;
127
128 /*
129 * Constants used when loading an encoding file to identify the type of the
130 * file.
131 */
132
133 #define ENCODING_SINGLEBYTE 0
134 #define ENCODING_DOUBLEBYTE 1
135 #define ENCODING_MULTIBYTE 2
136 #define ENCODING_ESCAPE 3
137
138 /*
139 * Initialize the default encoding directory. If this variable contains
140 * a non NULL value, it will be the first path used to locate the
141 * system encoding files.
142 */
143
144 char *tclDefaultEncodingDir = NULL;
145
146 static int encodingsInitialized = 0;
147
148 /*
149 * Hash table that keeps track of all loaded Encodings. Keys are
150 * the string names that represent the encoding, values are (Encoding *).
151 */
152
153 static Tcl_HashTable encodingTable;
154 TCL_DECLARE_MUTEX(encodingMutex)
155
156 /*
157 * The following are used to hold the default and current system encodings.
158 * If NULL is passed to one of the conversion routines, the current setting
159 * of the system encoding will be used to perform the conversion.
160 */
161
162 static Tcl_Encoding defaultEncoding;
163 static Tcl_Encoding systemEncoding;
164
165 /*
166 * The following variable is used in the sparse matrix code for a
167 * TableEncoding to represent a page in the table that has no entries.
168 */
169
170 static unsigned short emptyPage[256];
171
172 /*
173 * Procedures used only in this module.
174 */
175
176 static int BinaryProc _ANSI_ARGS_((ClientData clientData,
177 CONST char *src, int srcLen, int flags,
178 Tcl_EncodingState *statePtr, char *dst, int dstLen,
179 int *srcReadPtr, int *dstWrotePtr,
180 int *dstCharsPtr));
181 static void EscapeFreeProc _ANSI_ARGS_((ClientData clientData));
182 static int EscapeFromUtfProc _ANSI_ARGS_((ClientData clientData,
183 CONST char *src, int srcLen, int flags,
184 Tcl_EncodingState *statePtr, char *dst, int dstLen,
185 int *srcReadPtr, int *dstWrotePtr,
186 int *dstCharsPtr));
187 static int EscapeToUtfProc _ANSI_ARGS_((ClientData clientData,
188 CONST char *src, int srcLen, int flags,
189 Tcl_EncodingState *statePtr, char *dst, int dstLen,
190 int *srcReadPtr, int *dstWrotePtr,
191 int *dstCharsPtr));
192 static void FreeEncoding _ANSI_ARGS_((Tcl_Encoding encoding));
193 static Encoding * GetTableEncoding _ANSI_ARGS_((
194 EscapeEncodingData *dataPtr, int state));
195 static Tcl_Encoding LoadEncodingFile _ANSI_ARGS_((Tcl_Interp *interp,
196 CONST char *name));
197 static Tcl_Encoding LoadTableEncoding _ANSI_ARGS_((Tcl_Interp *interp,
198 CONST char *name, int type, Tcl_Channel chan));
199 static Tcl_Encoding LoadEscapeEncoding _ANSI_ARGS_((CONST char *name,
200 Tcl_Channel chan));
201 static Tcl_Channel OpenEncodingFile _ANSI_ARGS_((CONST char *dir,
202 CONST char *name));
203 static void TableFreeProc _ANSI_ARGS_((ClientData clientData));
204 static int TableFromUtfProc _ANSI_ARGS_((ClientData clientData,
205 CONST char *src, int srcLen, int flags,
206 Tcl_EncodingState *statePtr, char *dst, int dstLen,
207 int *srcReadPtr, int *dstWrotePtr,
208 int *dstCharsPtr));
209 static int TableToUtfProc _ANSI_ARGS_((ClientData clientData,
210 CONST char *src, int srcLen, int flags,
211 Tcl_EncodingState *statePtr, char *dst, int dstLen,
212 int *srcReadPtr, int *dstWrotePtr,
213 int *dstCharsPtr));
214 static size_t unilen _ANSI_ARGS_((CONST char *src));
215 static int UnicodeToUtfProc _ANSI_ARGS_((ClientData clientData,
216 CONST char *src, int srcLen, int flags,
217 Tcl_EncodingState *statePtr, char *dst, int dstLen,
218 int *srcReadPtr, int *dstWrotePtr,
219 int *dstCharsPtr));
220 static int UtfToUnicodeProc _ANSI_ARGS_((ClientData clientData,
221 CONST char *src, int srcLen, int flags,
222 Tcl_EncodingState *statePtr, char *dst, int dstLen,
223 int *srcReadPtr, int *dstWrotePtr,
224 int *dstCharsPtr));
225 static int UtfToUtfProc _ANSI_ARGS_((ClientData clientData,
226 CONST char *src, int srcLen, int flags,
227 Tcl_EncodingState *statePtr, char *dst, int dstLen,
228 int *srcReadPtr, int *dstWrotePtr,
229 int *dstCharsPtr));
230
231
232 /*
233 *---------------------------------------------------------------------------
234 *
235 * TclInitEncodingSubsystem --
236 *
237 * Initialize all resources used by this subsystem on a per-process
238 * basis.
239 *
240 * Results:
241 * None.
242 *
243 * Side effects:
244 * Depends on the memory, object, and IO subsystems.
245 *
246 *---------------------------------------------------------------------------
247 */
248
249 void
250 TclInitEncodingSubsystem()
251 {
252 Tcl_EncodingType type;
253
254 Tcl_MutexLock(&encodingMutex);
255 Tcl_InitHashTable(&encodingTable, TCL_STRING_KEYS);
256 Tcl_MutexUnlock(&encodingMutex);
257
258 /*
259 * Create a few initial encodings. Note that the UTF-8 to UTF-8
260 * translation is not a no-op, because it will turn a stream of
261 * improperly formed UTF-8 into a properly formed stream.
262 */
263
264 type.encodingName = "identity";
265 type.toUtfProc = BinaryProc;
266 type.fromUtfProc = BinaryProc;
267 type.freeProc = NULL;
268 type.nullSize = 1;
269 type.clientData = NULL;
270
271 defaultEncoding = Tcl_CreateEncoding(&type);
272 systemEncoding = Tcl_GetEncoding(NULL, type.encodingName);
273
274 type.encodingName = "utf-8";
275 type.toUtfProc = UtfToUtfProc;
276 type.fromUtfProc = UtfToUtfProc;
277 type.freeProc = NULL;
278 type.nullSize = 1;
279 type.clientData = NULL;
280 Tcl_CreateEncoding(&type);
281
282 type.encodingName = "unicode";
283 type.toUtfProc = UnicodeToUtfProc;
284 type.fromUtfProc = UtfToUnicodeProc;
285 type.freeProc = NULL;
286 type.nullSize = 2;
287 type.clientData = NULL;
288 Tcl_CreateEncoding(&type);
289 }
290
291
292 /*
293 *----------------------------------------------------------------------
294 *
295 * TclFinalizeEncodingSubsystem --
296 *
297 * Release the state associated with the encoding subsystem.
298 *
299 * Results:
300 * None.
301 *
302 * Side effects:
303 * Frees all of the encodings.
304 *
305 *----------------------------------------------------------------------
306 */
307
308 void
309 TclFinalizeEncodingSubsystem()
310 {
311 Tcl_HashSearch search;
312 Tcl_HashEntry *hPtr;
313
314 Tcl_MutexLock(&encodingMutex);
315 encodingsInitialized = 0;
316 hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
317 while (hPtr != NULL) {
318 /*
319 * Call FreeEncoding instead of doing it directly to handle refcounts
320 * like escape encodings use. [Bug #524674]
321 * Make sure to call Tcl_FirstHashEntry repeatedly so that all
322 * encodings are eventually cleaned up.
323 */
324 FreeEncoding((Tcl_Encoding) Tcl_GetHashValue(hPtr));
325 hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
326 }
327 Tcl_DeleteHashTable(&encodingTable);
328 Tcl_MutexUnlock(&encodingMutex);
329 }
330
331 /*
332 *-------------------------------------------------------------------------
333 *
334 * Tcl_GetDefaultEncodingDir --
335 *
336 *
337 * Results:
338 *
339 * Side effects:
340 *
341 *-------------------------------------------------------------------------
342 */
343
344 CONST char *
345 Tcl_GetDefaultEncodingDir()
346 {
347 return tclDefaultEncodingDir;
348 }
349
350 /*
351 *-------------------------------------------------------------------------
352 *
353 * Tcl_SetDefaultEncodingDir --
354 *
355 *
356 * Results:
357 *
358 * Side effects:
359 *
360 *-------------------------------------------------------------------------
361 */
362
363 void
364 Tcl_SetDefaultEncodingDir(path)
365 CONST char *path;
366 {
367 tclDefaultEncodingDir = (char *)ckalloc((unsigned) strlen(path) + 1);
368 strcpy(tclDefaultEncodingDir, path);
369 }
370
371 /*
372 *-------------------------------------------------------------------------
373 *
374 * Tcl_GetEncoding --
375 *
376 * Given the name of a encoding, find the corresponding Tcl_Encoding
377 * token. If the encoding did not already exist, Tcl attempts to
378 * dynamically load an encoding by that name.
379 *
380 * Results:
381 * Returns a token that represents the encoding. If the name didn't
382 * refer to any known or loadable encoding, NULL is returned. If
383 * NULL was returned, an error message is left in interp's result
384 * object, unless interp was NULL.
385 *
386 * Side effects:
387 * The new encoding type is entered into a table visible to all
388 * interpreters, keyed off the encoding's name. For each call to
389 * this procedure, there should eventually be a call to
390 * Tcl_FreeEncoding, so that the database can be cleaned up when
391 * encodings aren't needed anymore.
392 *
393 *-------------------------------------------------------------------------
394 */
395
396 Tcl_Encoding
397 Tcl_GetEncoding(interp, name)
398 Tcl_Interp *interp; /* Interp for error reporting, if not NULL. */
399 CONST char *name; /* The name of the desired encoding. */
400 {
401 Tcl_HashEntry *hPtr;
402 Encoding *encodingPtr;
403
404 Tcl_MutexLock(&encodingMutex);
405 if (name == NULL) {
406 encodingPtr = (Encoding *) systemEncoding;
407 encodingPtr->refCount++;
408 Tcl_MutexUnlock(&encodingMutex);
409 return systemEncoding;
410 }
411
412 hPtr = Tcl_FindHashEntry(&encodingTable, name);
413 if (hPtr != NULL) {
414 encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
415 encodingPtr->refCount++;
416 Tcl_MutexUnlock(&encodingMutex);
417 return (Tcl_Encoding) encodingPtr;
418 }
419 Tcl_MutexUnlock(&encodingMutex);
420 return LoadEncodingFile(interp, name);
421 }
422
423 /*
424 *---------------------------------------------------------------------------
425 *
426 * Tcl_FreeEncoding --
427 *
428 * This procedure is called to release an encoding allocated by
429 * Tcl_CreateEncoding() or Tcl_GetEncoding().
430 *
431 * Results:
432 * None.
433 *
434 * Side effects:
435 * The reference count associated with the encoding is decremented
436 * and the encoding may be deleted if nothing is using it anymore.
437 *
438 *---------------------------------------------------------------------------
439 */
440
441 void
442 Tcl_FreeEncoding(encoding)
443 Tcl_Encoding encoding;
444 {
445 Tcl_MutexLock(&encodingMutex);
446 FreeEncoding(encoding);
447 Tcl_MutexUnlock(&encodingMutex);
448 }
449
450 /*
451 *----------------------------------------------------------------------
452 *
453 * FreeEncoding --
454 *
455 * This procedure is called to release an encoding by procedures
456 * that already have the encodingMutex.
457 *
458 * Results:
459 * None.
460 *
461 * Side effects:
462 * The reference count associated with the encoding is decremented
463 * and the encoding may be deleted if nothing is using it anymore.
464 *
465 *----------------------------------------------------------------------
466 */
467
468 static void
469 FreeEncoding(encoding)
470 Tcl_Encoding encoding;
471 {
472 Encoding *encodingPtr;
473
474 encodingPtr = (Encoding *) encoding;
475 if (encodingPtr == NULL) {
476 return;
477 }
478 encodingPtr->refCount--;
479 if (encodingPtr->refCount == 0) {
480 if (encodingPtr->freeProc != NULL) {
481 (*encodingPtr->freeProc)(encodingPtr->clientData);
482 }
483 if (encodingPtr->hPtr != NULL) {
484 Tcl_DeleteHashEntry(encodingPtr->hPtr);
485 }
486 ckfree((char *) encodingPtr->name);
487 ckfree((char *) encodingPtr);
488 }
489 }
490
491 /*
492 *-------------------------------------------------------------------------
493 *
494 * Tcl_GetEncodingName --
495 *
496 * Given an encoding, return the name that was used to constuct
497 * the encoding.
498 *
499 * Results:
500 * The name of the encoding.
501 *
502 * Side effects:
503 * None.
504 *
505 *---------------------------------------------------------------------------
506 */
507
508 CONST char *
509 Tcl_GetEncodingName(encoding)
510 Tcl_Encoding encoding; /* The encoding whose name to fetch. */
511 {
512 Encoding *encodingPtr;
513
514 if (encoding == NULL) {
515 encoding = systemEncoding;
516 }
517 encodingPtr = (Encoding *) encoding;
518 return encodingPtr->name;
519 }
520
521 /*
522 *-------------------------------------------------------------------------
523 *
524 * Tcl_GetEncodingNames --
525 *
526 * Get the list of all known encodings, including the ones stored
527 * as files on disk in the encoding path.
528 *
529 * Results:
530 * Modifies interp's result object to hold a list of all the available
531 * encodings.
532 *
533 * Side effects:
534 * None.
535 *
536 *-------------------------------------------------------------------------
537 */
538
539 #ifndef _LANG
540 void
541 Tcl_GetEncodingNames(interp)
542 Tcl_Interp *interp; /* Interp to hold result. */
543 {
544 Tcl_HashSearch search;
545 Tcl_HashEntry *hPtr;
546 Tcl_Obj *pathPtr, *resultPtr;
547 int dummy;
548
549 Tcl_HashTable table;
550
551 Tcl_MutexLock(&encodingMutex);
552 Tcl_InitHashTable(&table, TCL_STRING_KEYS);
553 hPtr = Tcl_FirstHashEntry(&encodingTable, &search);
554 while (hPtr != NULL) {
555 Encoding *encodingPtr;
556
557 encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
558 Tcl_CreateHashEntry(&table, encodingPtr->name, &dummy);
559 hPtr = Tcl_NextHashEntry(&search);
560 }
561 Tcl_MutexUnlock(&encodingMutex);
562
563 pathPtr = TclGetLibraryPath();
564 if (pathPtr != NULL) {
565 int i, objc;
566 Tcl_Obj **objv;
567 char globArgString[10];
568 Tcl_Obj* encodingObj = Tcl_NewStringObj("encoding",-1);
569 Tcl_IncrRefCount(encodingObj);
570
571 objc = 0;
572 Tcl_ListObjGetElements(NULL, pathPtr, &objc, &objv);
573
574 for (i = 0; i < objc; i++) {
575 Tcl_Obj *searchIn;
576
577 /*
578 * Construct the path from the element of pathPtr,
579 * joined with 'encoding'.
580 */
581 searchIn = Tcl_FSJoinToPath(objv[i],1,&encodingObj);
582 Tcl_IncrRefCount(searchIn);
583 Tcl_ResetResult(interp);
584
585 /*
586 * TclGlob() changes the contents of globArgString, which causes
587 * a segfault if we pass in a pointer to non-writeable memory.
588 * TclGlob() puts its results directly into interp.
589 */
590
591 strcpy(globArgString, "*.enc");
592 /*
593 * The GLOBMODE_TAILS flag returns just the tail of each file
594 * which is the encoding name with a .enc extension
595 */
596 if ((TclGlob(interp, globArgString, searchIn,
597 TCL_GLOBMODE_TAILS, NULL) == TCL_OK)) {
598 int objc2 = 0;
599 Tcl_Obj **objv2;
600 int j;
601
602 Tcl_ListObjGetElements(NULL, Tcl_GetObjResult(interp), &objc2,
603 &objv2);
604
605 for (j = 0; j < objc2; j++) {
606 int length;
607 char *string;
608 string = Tcl_GetStringFromObj(objv2[j], &length);
609 length -= 4;
610 if (length > 0) {
611 string[length] = '\0';
612 Tcl_CreateHashEntry(&table, string, &dummy);
613 string[length] = '.';
614 }
615 }
616 }
617 Tcl_DecrRefCount(searchIn);
618 }
619 Tcl_DecrRefCount(encodingObj);
620 }
621
622 /*
623 * Clear any values placed in the result by globbing.
624 */
625
626 Tcl_ResetResult(interp);
627 resultPtr = Tcl_GetObjResult(interp);
628
629 hPtr = Tcl_FirstHashEntry(&table, &search);
630 while (hPtr != NULL) {
631 Tcl_Obj *strPtr;
632
633 strPtr = Tcl_NewStringObj(Tcl_GetHashKey(&table, hPtr), -1);
634 Tcl_ListObjAppendElement(NULL, resultPtr, strPtr);
635 hPtr = Tcl_NextHashEntry(&search);
636 }
637 Tcl_DeleteHashTable(&table);
638 }
639
640 #endif
641
642 /*
643 *------------------------------------------------------------------------
644 *
645 * Tcl_SetSystemEncoding --
646 *
647 * Sets the default encoding that should be used whenever the user
648 * passes a NULL value in to one of the conversion routines.
649 * If the supplied name is NULL, the system encoding is reset to the
650 * default system encoding.
651 *
652 * Results:
653 * The return value is TCL_OK if the system encoding was successfully
654 * set to the encoding specified by name, TCL_ERROR otherwise. If
655 * TCL_ERROR is returned, an error message is left in interp's result
656 * object, unless interp was NULL.
657 *
658 * Side effects:
659 * The reference count of the new system encoding is incremented.
660 * The reference count of the old system encoding is decremented and
661 * it may be freed.
662 *
663 *------------------------------------------------------------------------
664 */
665
666 int
667 Tcl_SetSystemEncoding(interp, name)
668 Tcl_Interp *interp; /* Interp for error reporting, if not NULL. */
669 CONST char *name; /* The name of the desired encoding, or NULL
670 * to reset to default encoding. */
671 {
672 Tcl_Encoding encoding;
673 Encoding *encodingPtr;
674
675 if (name == NULL) {
676 Tcl_MutexLock(&encodingMutex);
677 encoding = defaultEncoding;
678 encodingPtr = (Encoding *) encoding;
679 encodingPtr->refCount++;
680 Tcl_MutexUnlock(&encodingMutex);
681 } else {
682 encoding = Tcl_GetEncoding(interp, name);
683 if (encoding == NULL) {
684 return TCL_ERROR;
685 }
686 }
687
688 Tcl_MutexLock(&encodingMutex);
689 FreeEncoding(systemEncoding);
690 systemEncoding = encoding;
691 Tcl_MutexUnlock(&encodingMutex);
692
693 return TCL_OK;
694 }
695
696 /*
697 *---------------------------------------------------------------------------
698 *
699 * Tcl_CreateEncoding --
700 *
701 * This procedure is called to define a new encoding and the procedures
702 * that are used to convert between the specified encoding and Unicode.
703 *
704 * Results:
705 * Returns a token that represents the encoding. If an encoding with
706 * the same name already existed, the old encoding token remains
707 * valid and continues to behave as it used to, and will eventually
708 * be garbage collected when the last reference to it goes away. Any
709 * subsequent calls to Tcl_GetEncoding with the specified name will
710 * retrieve the most recent encoding token.
711 *
712 * Side effects:
713 * The new encoding type is entered into a table visible to all
714 * interpreters, keyed off the encoding's name. For each call to
715 * this procedure, there should eventually be a call to
716 * Tcl_FreeEncoding, so that the database can be cleaned up when
717 * encodings aren't needed anymore.
718 *
719 *---------------------------------------------------------------------------
720 */
721
722 Tcl_Encoding
723 Tcl_CreateEncoding(typePtr)
724 Tcl_EncodingType *typePtr; /* The encoding type. */
725 {
726 Tcl_HashEntry *hPtr;
727 int new;
728 Encoding *encodingPtr;
729 char *name;
730
731 Tcl_MutexLock(&encodingMutex);
732 hPtr = Tcl_CreateHashEntry(&encodingTable, typePtr->encodingName, &new);
733 if (new == 0) {
734 /*
735 * Remove old encoding from hash table, but don't delete it until
736 * last reference goes away.
737 */
738
739 encodingPtr = (Encoding *) Tcl_GetHashValue(hPtr);
740 encodingPtr->hPtr = NULL;
741 }
742
743 name = ckalloc((unsigned) strlen(typePtr->encodingName) + 1);
744
745 encodingPtr = (Encoding *) ckalloc(sizeof(Encoding));
746 encodingPtr->name = strcpy(name, typePtr->encodingName);
747 encodingPtr->toUtfProc = typePtr->toUtfProc;
748 encodingPtr->fromUtfProc = typePtr->fromUtfProc;
749 encodingPtr->freeProc = typePtr->freeProc;
750 encodingPtr->nullSize = typePtr->nullSize;
751 encodingPtr->clientData = typePtr->clientData;
752 if (typePtr->nullSize == 1) {
753 encodingPtr->lengthProc = (LengthProc *) strlen;
754 } else {
755 encodingPtr->lengthProc = (LengthProc *) unilen;
756 }
757 encodingPtr->refCount = 1;
758 encodingPtr->hPtr = hPtr;
759 Tcl_SetHashValue(hPtr, encodingPtr);
760
761 Tcl_MutexUnlock(&encodingMutex);
762
763 return (Tcl_Encoding) encodingPtr;
764 }
765
766 /*
767 *-------------------------------------------------------------------------
768 *
769 * Tcl_ExternalToUtfDString --
770 *
771 * Convert a source buffer from the specified encoding into UTF-8.
772 * If any of the bytes in the source buffer are invalid or cannot
773 * be represented in the target encoding, a default fallback
774 * character will be substituted.
775 *
776 * Results:
777 * The converted bytes are stored in the DString, which is then NULL
778 * terminated. The return value is a pointer to the value stored
779 * in the DString.
780 *
781 * Side effects:
782 * None.
783 *
784 *-------------------------------------------------------------------------
785 */
786
787 char *
788 Tcl_ExternalToUtfDString(encoding, src, srcLen, dstPtr)
789 Tcl_Encoding encoding; /* The encoding for the source string, or
790 * NULL for the default system encoding. */
791 CONST char *src; /* Source string in specified encoding. */
792 int srcLen; /* Source string length in bytes, or < 0 for
793 * encoding-specific string length. */
794 Tcl_DString *dstPtr; /* Uninitialized or free DString in which
795 * the converted string is stored. */
796 {
797 char *dst;
798 Tcl_EncodingState state;
799 Encoding *encodingPtr;
800 int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
801
802 Tcl_DStringInit(dstPtr);
803 dst = Tcl_DStringValue(dstPtr);
804 dstLen = dstPtr->spaceAvl - 1;
805
806 if (encoding == NULL) {
807 encoding = systemEncoding;
808 }
809 encodingPtr = (Encoding *) encoding;
810
811 if (src == NULL) {
812 srcLen = 0;
813 } else if (srcLen < 0) {
814 srcLen = (*encodingPtr->lengthProc)(src);
815 }
816 flags = TCL_ENCODING_START | TCL_ENCODING_END;
817 while (1) {
818 result = (*encodingPtr->toUtfProc)(encodingPtr->clientData, src,
819 srcLen, flags, &state, dst, dstLen, &srcRead, &dstWrote,
820 &dstChars);
821 soFar = dst + dstWrote - Tcl_DStringValue(dstPtr);
822 if (result != TCL_CONVERT_NOSPACE) {
823 Tcl_DStringSetLength(dstPtr, soFar);
824 return Tcl_DStringValue(dstPtr);
825 }
826 flags &= ~TCL_ENCODING_START;
827 src += srcRead;
828 srcLen -= srcRead;
829 if (Tcl_DStringLength(dstPtr) == 0) {
830 Tcl_DStringSetLength(dstPtr, dstLen);
831 }
832 Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1);
833 dst = Tcl_DStringValue(dstPtr) + soFar;
834 dstLen = Tcl_DStringLength(dstPtr) - soFar - 1;
835 }
836 }
837
838 /*
839 *-------------------------------------------------------------------------
840 *
841 * Tcl_ExternalToUtf --
842 *
843 * Convert a source buffer from the specified encoding into UTF-8.
844 *
845 * Results:
846 * The return value is one of TCL_OK, TCL_CONVERT_MULTIBYTE,
847 * TCL_CONVERT_SYNTAX, TCL_CONVERT_UNKNOWN, or TCL_CONVERT_NOSPACE,
848 * as documented in tcl.h.
849 *
850 * Side effects:
851 * The converted bytes are stored in the output buffer.
852 *
853 *-------------------------------------------------------------------------
854 */
855
856 int
857 Tcl_ExternalToUtf(interp, encoding, src, srcLen, flags, statePtr, dst,
858 dstLen, srcReadPtr, dstWrotePtr, dstCharsPtr)
859 Tcl_Interp *interp; /* Interp for error return, if not NULL. */
860 Tcl_Encoding encoding; /* The encoding for the source string, or
861 * NULL for the default system encoding. */
862 CONST char *src; /* Source string in specified encoding. */
863 int srcLen; /* Source string length in bytes, or < 0 for
864 * encoding-specific string length. */
865 int flags; /* Conversion control flags. */
866 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
867 * state information used during a piecewise
868 * conversion. Contents of statePtr are
869 * initialized and/or reset by conversion
870 * routine under control of flags argument. */
871 char *dst; /* Output buffer in which converted string
872 * is stored. */
873 int dstLen; /* The maximum length of output buffer in
874 * bytes. */
875 int *srcReadPtr; /* Filled with the number of bytes from the
876 * source string that were converted. This
877 * may be less than the original source length
878 * if there was a problem converting some
879 * source characters. */
880 int *dstWrotePtr; /* Filled with the number of bytes that were
881 * stored in the output buffer as a result of
882 * the conversion. */
883 int *dstCharsPtr; /* Filled with the number of characters that
884 * correspond to the bytes stored in the
885 * output buffer. */
886 {
887 Encoding *encodingPtr;
888 int result, srcRead, dstWrote, dstChars;
889 Tcl_EncodingState state;
890
891 if (encoding == NULL) {
892 encoding = systemEncoding;
893 }
894 encodingPtr = (Encoding *) encoding;
895
896 if (src == NULL) {
897 srcLen = 0;
898 } else if (srcLen < 0) {
899 srcLen = (*encodingPtr->lengthProc)(src);
900 }
901 if (statePtr == NULL) {
902 flags |= TCL_ENCODING_START | TCL_ENCODING_END;
903 statePtr = &state;
904 }
905 if (srcReadPtr == NULL) {
906 srcReadPtr = &srcRead;
907 }
908 if (dstWrotePtr == NULL) {
909 dstWrotePtr = &dstWrote;
910 }
911 if (dstCharsPtr == NULL) {
912 dstCharsPtr = &dstChars;
913 }
914
915 /*
916 * If there are any null characters in the middle of the buffer, they will
917 * converted to the UTF-8 null character (\xC080). To get the actual
918 * \0 at the end of the destination buffer, we need to append it manually.
919 */
920
921 dstLen--;
922 result = (*encodingPtr->toUtfProc)(encodingPtr->clientData, src, srcLen,
923 flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
924 dstCharsPtr);
925 dst[*dstWrotePtr] = '\0';
926 return result;
927 }
928
929 /*
930 *-------------------------------------------------------------------------
931 *
932 * Tcl_UtfToExternalDString --
933 *
934 * Convert a source buffer from UTF-8 into the specified encoding.
935 * If any of the bytes in the source buffer are invalid or cannot
936 * be represented in the target encoding, a default fallback
937 * character will be substituted.
938 *
939 * Results:
940 * The converted bytes are stored in the DString, which is then
941 * NULL terminated in an encoding-specific manner. The return value
942 * is a pointer to the value stored in the DString.
943 *
944 * Side effects:
945 * None.
946 *
947 *-------------------------------------------------------------------------
948 */
949
950 char *
951 Tcl_UtfToExternalDString(encoding, src, srcLen, dstPtr)
952 Tcl_Encoding encoding; /* The encoding for the converted string,
953 * or NULL for the default system encoding. */
954 CONST char *src; /* Source string in UTF-8. */
955 int srcLen; /* Source string length in bytes, or < 0 for
956 * strlen(). */
957 Tcl_DString *dstPtr; /* Uninitialized or free DString in which
958 * the converted string is stored. */
959 {
960 char *dst;
961 Tcl_EncodingState state;
962 Encoding *encodingPtr;
963 int flags, dstLen, result, soFar, srcRead, dstWrote, dstChars;
964
965 Tcl_DStringInit(dstPtr);
966 dst = Tcl_DStringValue(dstPtr);
967 dstLen = dstPtr->spaceAvl - 1;
968
969 if (encoding == NULL) {
970 encoding = systemEncoding;
971 }
972 encodingPtr = (Encoding *) encoding;
973
974 if (src == NULL) {
975 srcLen = 0;
976 } else if (srcLen < 0) {
977 srcLen = strlen(src);
978 }
979 flags = TCL_ENCODING_START | TCL_ENCODING_END;
980 while (1) {
981 result = (*encodingPtr->fromUtfProc)(encodingPtr->clientData, src,
982 srcLen, flags, &state, dst, dstLen, &srcRead, &dstWrote,
983 &dstChars);
984 soFar = dst + dstWrote - Tcl_DStringValue(dstPtr);
985 if (result != TCL_CONVERT_NOSPACE) {
986 if (encodingPtr->nullSize == 2) {
987 Tcl_DStringSetLength(dstPtr, soFar + 1);
988 }
989 Tcl_DStringSetLength(dstPtr, soFar);
990 return Tcl_DStringValue(dstPtr);
991 }
992 flags &= ~TCL_ENCODING_START;
993 src += srcRead;
994 srcLen -= srcRead;
995 if (Tcl_DStringLength(dstPtr) == 0) {
996 Tcl_DStringSetLength(dstPtr, dstLen);
997 }
998 Tcl_DStringSetLength(dstPtr, 2 * Tcl_DStringLength(dstPtr) + 1);
999 dst = Tcl_DStringValue(dstPtr) + soFar;
1000 dstLen = Tcl_DStringLength(dstPtr) - soFar - 1;
1001 }
1002 }
1003
1004 /*
1005 *-------------------------------------------------------------------------
1006 *
1007 * Tcl_UtfToExternal --
1008 *
1009 * Convert a buffer from UTF-8 into the specified encoding.
1010 *
1011 * Results:
1012 * The return value is one of TCL_OK, TCL_CONVERT_MULTIBYTE,
1013 * TCL_CONVERT_SYNTAX, TCL_CONVERT_UNKNOWN, or TCL_CONVERT_NOSPACE,
1014 * as documented in tcl.h.
1015 *
1016 * Side effects:
1017 * The converted bytes are stored in the output buffer.
1018 *
1019 *-------------------------------------------------------------------------
1020 */
1021
1022 int
1023 Tcl_UtfToExternal(interp, encoding, src, srcLen, flags, statePtr, dst,
1024 dstLen, srcReadPtr, dstWrotePtr, dstCharsPtr)
1025 Tcl_Interp *interp; /* Interp for error return, if not NULL. */
1026 Tcl_Encoding encoding; /* The encoding for the converted string,
1027 * or NULL for the default system encoding. */
1028 CONST char *src; /* Source string in UTF-8. */
1029 int srcLen; /* Source string length in bytes, or < 0 for
1030 * strlen(). */
1031 int flags; /* Conversion control flags. */
1032 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
1033 * state information used during a piecewise
1034 * conversion. Contents of statePtr are
1035 * initialized and/or reset by conversion
1036 * routine under control of flags argument. */
1037 char *dst; /* Output buffer in which converted string
1038 * is stored. */
1039 int dstLen; /* The maximum length of output buffer in
1040 * bytes. */
1041 int *srcReadPtr; /* Filled with the number of bytes from the
1042 * source string that were converted. This
1043 * may be less than the original source length
1044 * if there was a problem converting some
1045 * source characters. */
1046 int *dstWrotePtr; /* Filled with the number of bytes that were
1047 * stored in the output buffer as a result of
1048 * the conversion. */
1049 int *dstCharsPtr; /* Filled with the number of characters that
1050 * correspond to the bytes stored in the
1051 * output buffer. */
1052 {
1053 Encoding *encodingPtr;
1054 int result, srcRead, dstWrote, dstChars;
1055 Tcl_EncodingState state;
1056
1057 if (encoding == NULL) {
1058 encoding = systemEncoding;
1059 }
1060 encodingPtr = (Encoding *) encoding;
1061
1062 if (src == NULL) {
1063 srcLen = 0;
1064 } else if (srcLen < 0) {
1065 srcLen = strlen(src);
1066 }
1067 if (statePtr == NULL) {
1068 flags |= TCL_ENCODING_START | TCL_ENCODING_END;
1069 statePtr = &state;
1070 }
1071 if (srcReadPtr == NULL) {
1072 srcReadPtr = &srcRead;
1073 }
1074 if (dstWrotePtr == NULL) {
1075 dstWrotePtr = &dstWrote;
1076 }
1077 if (dstCharsPtr == NULL) {
1078 dstCharsPtr = &dstChars;
1079 }
1080
1081 dstLen -= encodingPtr->nullSize;
1082 result = (*encodingPtr->fromUtfProc)(encodingPtr->clientData, src, srcLen,
1083 flags, statePtr, dst, dstLen, srcReadPtr, dstWrotePtr,
1084 dstCharsPtr);
1085 if (encodingPtr->nullSize == 2) {
1086 dst[*dstWrotePtr + 1] = '\0';
1087 }
1088 dst[*dstWrotePtr] = '\0';
1089
1090 return result;
1091 }
1092
1093 /*
1094 *---------------------------------------------------------------------------
1095 *
1096 * Tcl_FindExecutable --
1097 *
1098 * This procedure computes the absolute path name of the current
1099 * application, given its argv[0] value.
1100 *
1101 * Results:
1102 * None.
1103 *
1104 * Side effects:
1105 * The variable tclExecutableName gets filled in with the file
1106 * name for the application, if we figured it out. If we couldn't
1107 * figure it out, tclExecutableName is set to NULL.
1108 *
1109 *---------------------------------------------------------------------------
1110 */
1111
1112 void
1113 Tcl_FindExecutable(argv0)
1114 CONST char *argv0; /* The value of the application's argv[0]
1115 * (native). */
1116 {
1117 CONST char *name;
1118 Tcl_DString buffer, nameString;
1119
1120 TclInitSubsystems(argv0);
1121
1122 if (argv0 == NULL) {
1123 goto done;
1124 }
1125 if (tclExecutableName != NULL) {
1126 ckfree(tclExecutableName);
1127 tclExecutableName = NULL;
1128 }
1129 if ((name = TclpFindExecutable(argv0)) == NULL) {
1130 goto done;
1131 }
1132
1133 /*
1134 * The value returned from TclpNameOfExecutable is a UTF string that
1135 * is possibly dirty depending on when it was initialized. To assure
1136 * that the UTF string is a properly encoded native string for this
1137 * system, convert the UTF string to the default native encoding
1138 * before the default encoding is initialized. Then, convert it back
1139 * to UTF after the system encoding is loaded.
1140 */
1141
1142 Tcl_UtfToExternalDString(NULL, name, -1, &buffer);
1143 TclFindEncodings(argv0);
1144
1145 /*
1146 * Now it is OK to convert the native string back to UTF and set
1147 * the value of the tclExecutableName.
1148 */
1149
1150 Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&buffer), -1, &nameString);
1151 tclExecutableName = (char *)
1152 ckalloc((unsigned) (Tcl_DStringLength(&nameString) + 1));
1153 strcpy(tclExecutableName, Tcl_DStringValue(&nameString));
1154
1155 Tcl_DStringFree(&buffer);
1156 Tcl_DStringFree(&nameString);
1157 return;
1158
1159 done:
1160 TclFindEncodings(argv0);
1161 }
1162
1163 /*
1164 *---------------------------------------------------------------------------
1165 *
1166 * LoadEncodingFile --
1167 *
1168 * Read a file that describes an encoding and create a new Encoding
1169 * from the data.
1170 *
1171 * Results:
1172 * The return value is the newly loaded Encoding, or NULL if
1173 * the file didn't exist of was in the incorrect format. If NULL was
1174 * returned, an error message is left in interp's result object,
1175 * unless interp was NULL.
1176 *
1177 * Side effects:
1178 * File read from disk.
1179 *
1180 *---------------------------------------------------------------------------
1181 */
1182
1183 static Tcl_Encoding
1184 LoadEncodingFile(interp, name)
1185 Tcl_Interp *interp; /* Interp for error reporting, if not NULL. */
1186 CONST char *name; /* The name of the encoding file on disk
1187 * and also the name for new encoding. */
1188 {
1189 int objc, i, ch;
1190 Tcl_Obj **objv;
1191 Tcl_Obj *pathPtr;
1192 Tcl_Channel chan;
1193 Tcl_Encoding encoding;
1194
1195 pathPtr = TclGetLibraryPath();
1196 if (pathPtr == NULL) {
1197 goto unknown;
1198 }
1199 objc = 0;
1200 Tcl_ListObjGetElements(NULL, pathPtr, &objc, &objv);
1201
1202 chan = NULL;
1203 for (i = 0; i < objc; i++) {
1204 chan = OpenEncodingFile(Tcl_GetString(objv[i]), name);
1205 if (chan != NULL) {
1206 break;
1207 }
1208 }
1209
1210 if (chan == NULL) {
1211 goto unknown;
1212 }
1213
1214 Tcl_SetChannelOption(NULL, chan, "-encoding", "utf-8");
1215
1216 while (1) {
1217 Tcl_DString ds;
1218
1219 Tcl_DStringInit(&ds);
1220 Tcl_Gets(chan, &ds);
1221 ch = Tcl_DStringValue(&ds)[0];
1222 Tcl_DStringFree(&ds);
1223 if (ch != '#') {
1224 break;
1225 }
1226 }
1227
1228 encoding = NULL;
1229 switch (ch) {
1230 case 'S': {
1231 encoding = LoadTableEncoding(interp, name, ENCODING_SINGLEBYTE,
1232 chan);
1233 break;
1234 }
1235 case 'D': {
1236 encoding = LoadTableEncoding(interp, name, ENCODING_DOUBLEBYTE,
1237 chan);
1238 break;
1239 }
1240 case 'M': {
1241 encoding = LoadTableEncoding(interp, name, ENCODING_MULTIBYTE,
1242 chan);
1243 break;
1244 }
1245 case 'E': {
1246 encoding = LoadEscapeEncoding(name, chan);
1247 break;
1248 }
1249 }
1250 if ((encoding == NULL) && (interp != NULL)) {
1251 Tcl_AppendResult(interp, "invalid encoding file \"", name, "\"", NULL);
1252 }
1253 Tcl_Close(NULL, chan);
1254 return encoding;
1255
1256 unknown:
1257 if (interp != NULL) {
1258 Tcl_AppendResult(interp, "unknown encoding \"", name, "\"", NULL);
1259 }
1260 return NULL;
1261 }
1262
1263 /*
1264 *----------------------------------------------------------------------
1265 *
1266 * OpenEncodingFile --
1267 *
1268 * Look for the file encoding/<name>.enc in the specified
1269 * directory.
1270 *
1271 * Results:
1272 * Returns an open file channel if the file exists.
1273 *
1274 * Side effects:
1275 * None.
1276 *
1277 *----------------------------------------------------------------------
1278 */
1279
1280 static Tcl_Channel
1281 OpenEncodingFile(dir, name)
1282 CONST char *dir;
1283 CONST char *name;
1284
1285 {
1286 CONST char *argv[3];
1287 Tcl_DString pathString;
1288 CONST char *path;
1289 Tcl_Channel chan;
1290 Tcl_Obj *pathPtr;
1291
1292 argv[0] = dir;
1293 argv[1] = "encoding";
1294 argv[2] = name;
1295
1296 Tcl_DStringInit(&pathString);
1297 Tcl_JoinPath(3, argv, &pathString);
1298 path = Tcl_DStringAppend(&pathString, ".enc", -1);
1299 pathPtr = Tcl_NewStringObj(path,-1);
1300
1301 Tcl_IncrRefCount(pathPtr);
1302 chan = Tcl_FSOpenFileChannel(NULL, pathPtr, "r", 0);
1303 Tcl_DecrRefCount(pathPtr);
1304
1305 Tcl_DStringFree(&pathString);
1306
1307 return chan;
1308 }
1309
1310 /*
1311 *-------------------------------------------------------------------------
1312 *
1313 * LoadTableEncoding --
1314 *
1315 * Helper function for LoadEncodingTable(). Loads a table to that
1316 * converts between Unicode and some other encoding and creates an
1317 * encoding (using a TableEncoding structure) from that information.
1318 *
1319 * File contains binary data, but begins with a marker to indicate
1320 * byte-ordering, so that same binary file can be read on either
1321 * endian platforms.
1322 *
1323 * Results:
1324 * The return value is the new encoding, or NULL if the encoding
1325 * could not be created (because the file contained invalid data).
1326 *
1327 * Side effects:
1328 * None.
1329 *
1330 *-------------------------------------------------------------------------
1331 */
1332
1333 static Tcl_Encoding
1334 LoadTableEncoding(interp, name, type, chan)
1335 Tcl_Interp *interp; /* Interp for temporary obj while reading. */
1336 CONST char *name; /* Name for new encoding. */
1337 int type; /* Type of encoding (ENCODING_?????). */
1338 Tcl_Channel chan; /* File containing new encoding. */
1339 {
1340 Tcl_DString lineString;
1341 Tcl_Obj *objPtr;
1342 char *line;
1343 int i, hi, lo, numPages, symbol, fallback;
1344 unsigned char used[256];
1345 unsigned int size;
1346 TableEncodingData *dataPtr;
1347 unsigned short *pageMemPtr;
1348 Tcl_EncodingType encType;
1349
1350 /*
1351 * Speed over memory. Use a full 256 character table to decode hex
1352 * sequences in the encoding files.
1353 */
1354
1355 static char staticHex[] = {
1356 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0 ... 15 */
1357 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16 ... 31 */
1358 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 32 ... 47 */
1359 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 48 ... 63 */
1360 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 64 ... 79 */
1361 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 ... 95 */
1362 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 96 ... 111 */
1363 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 112 ... 127 */
1364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 128 ... 143 */
1365 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 144 ... 159 */
1366 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 160 ... 175 */
1367 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 176 ... 191 */
1368 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 192 ... 207 */
1369 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 208 ... 223 */
1370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 224 ... 239 */
1371 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 240 ... 255 */
1372 };
1373
1374 Tcl_DStringInit(&lineString);
1375 Tcl_Gets(chan, &lineString);
1376 line = Tcl_DStringValue(&lineString);
1377
1378 fallback = (int) strtol(line, &line, 16);
1379 symbol = (int) strtol(line, &line, 10);
1380 numPages = (int) strtol(line, &line, 10);
1381 Tcl_DStringFree(&lineString);
1382
1383 if (numPages < 0) {
1384 numPages = 0;
1385 } else if (numPages > 256) {
1386 numPages = 256;
1387 }
1388
1389 memset(used, 0, sizeof(used));
1390
1391 #undef PAGESIZE
1392 #define PAGESIZE (256 * sizeof(unsigned short))
1393
1394 dataPtr = (TableEncodingData *) ckalloc(sizeof(TableEncodingData));
1395 memset(dataPtr, 0, sizeof(TableEncodingData));
1396
1397 dataPtr->fallback = fallback;
1398
1399 /*
1400 * Read the table that maps characters to Unicode. Performs a single
1401 * malloc to get the memory for the array and all the pages needed by
1402 * the array.
1403 */
1404
1405 size = 256 * sizeof(unsigned short *) + numPages * PAGESIZE;
1406 dataPtr->toUnicode = (unsigned short **) ckalloc(size);
1407 memset(dataPtr->toUnicode, 0, size);
1408 pageMemPtr = (unsigned short *) (dataPtr->toUnicode + 256);
1409
1410 if (interp == NULL) {
1411 objPtr = Tcl_NewObj();
1412 } else {
1413 objPtr = Tcl_GetObjResult(interp);
1414 }
1415 for (i = 0; i < numPages; i++) {
1416 int ch;
1417 char *p;
1418
1419 Tcl_ReadChars(chan, objPtr, 3 + 16 * (16 * 4 + 1), 0);
1420 p = Tcl_GetString(objPtr);
1421 hi = (staticHex[(unsigned int)p[0]] << 4) + staticHex[(unsigned int)p[1]];
1422 dataPtr->toUnicode[hi] = pageMemPtr;
1423 p += 2;
1424 for (lo = 0; lo < 256; lo++) {
1425 if ((lo & 0x0f) == 0) {
1426 p++;
1427 }
1428 ch = (staticHex[(unsigned int)p[0]] << 12) + (staticHex[(unsigned int)p[1]] << 8)
1429 + (staticHex[(unsigned int)p[2]] << 4) + staticHex[(unsigned int)p[3]];
1430 if (ch != 0) {
1431 used[ch >> 8] = 1;
1432 }
1433 *pageMemPtr = (unsigned short) ch;
1434 pageMemPtr++;
1435 p += 4;
1436 }
1437 }
1438 if (interp == NULL) {
1439 Tcl_DecrRefCount(objPtr);
1440 } else {
1441 Tcl_ResetResult(interp);
1442 }
1443
1444 if (type == ENCODING_DOUBLEBYTE) {
1445 memset(dataPtr->prefixBytes, 1, sizeof(dataPtr->prefixBytes));
1446 } else {
1447 for (hi = 1; hi < 256; hi++) {
1448 if (dataPtr->toUnicode[hi] != NULL) {
1449 dataPtr->prefixBytes[hi] = 1;
1450 }
1451 }
1452 }
1453
1454 /*
1455 * Invert toUnicode array to produce the fromUnicode array. Performs a
1456 * single malloc to get the memory for the array and all the pages
1457 * needed by the array. While reading in the toUnicode array, we
1458 * remembered what pages that would be needed for the fromUnicode array.
1459 */
1460
1461 if (symbol) {
1462 used[0] = 1;
1463 }
1464 numPages = 0;
1465 for (hi = 0; hi < 256; hi++) {
1466 if (used[hi]) {
1467 numPages++;
1468 }
1469 }
1470 size = 256 * sizeof(unsigned short *) + numPages * PAGESIZE;
1471 dataPtr->fromUnicode = (unsigned short **) ckalloc(size);
1472 memset(dataPtr->fromUnicode, 0, size);
1473 pageMemPtr = (unsigned short *) (dataPtr->fromUnicode + 256);
1474
1475 for (hi = 0; hi < 256; hi++) {
1476 if (dataPtr->toUnicode[hi] == NULL) {
1477 dataPtr->toUnicode[hi] = emptyPage;
1478 } else {
1479 for (lo = 0; lo < 256; lo++) {
1480 int ch;
1481
1482 ch = dataPtr->toUnicode[hi][lo];
1483 if (ch != 0) {
1484 unsigned short *page;
1485
1486 page = dataPtr->fromUnicode[ch >> 8];
1487 if (page == NULL) {
1488 page = pageMemPtr;
1489 pageMemPtr += 256;
1490 dataPtr->fromUnicode[ch >> 8] = page;
1491 }
1492 page[ch & 0xff] = (unsigned short) ((hi << 8) + lo);
1493 }
1494 }
1495 }
1496 }
1497 if (type == ENCODING_MULTIBYTE) {
1498 /*
1499 * If multibyte encodings don't have a backslash character, define
1500 * one. Otherwise, on Windows, native file names won't work because
1501 * the backslash in the file name will map to the unknown character
1502 * (question mark) when converting from UTF-8 to external encoding.
1503 */
1504
1505 if (dataPtr->fromUnicode[0] != NULL) {
1506 if (dataPtr->fromUnicode[0]['\\'] == '\0') {
1507 dataPtr->fromUnicode[0]['\\'] = '\\';
1508 }
1509 }
1510 }
1511 if (symbol) {
1512 unsigned short *page;
1513
1514 /*
1515 * Make a special symbol encoding that not only maps the symbol
1516 * characters from their Unicode code points down into page 0, but
1517 * also ensure that the characters on page 0 map to themselves.
1518 * This is so that a symbol font can be used to display a simple
1519 * string like "abcd" and have alpha, beta, chi, delta show up,
1520 * rather than have "unknown" chars show up because strictly
1521 * speaking the symbol font doesn't have glyphs for those low ascii
1522 * chars.
1523 */
1524
1525 page = dataPtr->fromUnicode[0];
1526 if (page == NULL) {
1527 page = pageMemPtr;
1528 dataPtr->fromUnicode[0] = page;
1529 }
1530 for (lo = 0; lo < 256; lo++) {
1531 if (dataPtr->toUnicode[0][lo] != 0) {
1532 page[lo] = (unsigned short) lo;
1533 }
1534 }
1535 }
1536 for (hi = 0; hi < 256; hi++) {
1537 if (dataPtr->fromUnicode[hi] == NULL) {
1538 dataPtr->fromUnicode[hi] = emptyPage;
1539 }
1540 }
1541 /*
1542 * For trailing 'R'everse encoding, see [Patch #689341]
1543 */
1544 Tcl_DStringInit(&lineString);
1545 do {
1546 int len;
1547 /* skip leading empty lines */
1548 while ((len = Tcl_Gets(chan, &lineString)) == 0)
1549 ;
1550 if (len < 0) {
1551 break;
1552 }
1553 line = Tcl_DStringValue(&lineString);
1554 if (line[0] != 'R') {
1555 break;
1556 }
1557 for (Tcl_DStringSetLength(&lineString, 0);
1558 (len = Tcl_Gets(chan, &lineString)) >= 0;
1559 Tcl_DStringSetLength(&lineString, 0)) {
1560 unsigned char* p;
1561 int to, from;
1562 if (len < 5) {
1563 continue;
1564 }
1565 p = (unsigned char*) Tcl_DStringValue(&lineString);
1566 to = (staticHex[p[0]] << 12) + (staticHex[p[1]] << 8)
1567 + (staticHex[p[2]] << 4) + staticHex[p[3]];
1568 if (to == 0) {
1569 continue;
1570 }
1571 for (p += 5, len -= 5; len >= 0 && *p; p += 5, len -= 5) {
1572 from = (staticHex[p[0]] << 12) + (staticHex[p[1]] << 8)
1573 + (staticHex[p[2]] << 4) + staticHex[p[3]];
1574 if (from == 0) {
1575 continue;
1576 }
1577 dataPtr->fromUnicode[from >> 8][from & 0xff] = to;
1578 }
1579 }
1580 } while (0);
1581 Tcl_DStringFree(&lineString);
1582
1583 encType.encodingName = name;
1584 encType.toUtfProc = TableToUtfProc;
1585 encType.fromUtfProc = TableFromUtfProc;
1586 encType.freeProc = TableFreeProc;
1587 encType.nullSize = (type == ENCODING_DOUBLEBYTE) ? 2 : 1;
1588 encType.clientData = (ClientData) dataPtr;
1589 return Tcl_CreateEncoding(&encType);
1590 }
1591
1592 /*
1593 *-------------------------------------------------------------------------
1594 *
1595 * LoadEscapeEncoding --
1596 *
1597 * Helper function for LoadEncodingTable(). Loads a state machine
1598 * that converts between Unicode and some other encoding.
1599 *
1600 * File contains text data that describes the escape sequences that
1601 * are used to choose an encoding and the associated names for the
1602 * sub-encodings.
1603 *
1604 * Results:
1605 * The return value is the new encoding, or NULL if the encoding
1606 * could not be created (because the file contained invalid data).
1607 *
1608 * Side effects:
1609 * None.
1610 *
1611 *-------------------------------------------------------------------------
1612 */
1613
1614 static Tcl_Encoding
1615 LoadEscapeEncoding(name, chan)
1616 CONST char *name; /* Name for new encoding. */
1617 Tcl_Channel chan; /* File containing new encoding. */
1618 {
1619 int i;
1620 unsigned int size;
1621 Tcl_DString escapeData;
1622 char init[16], final[16];
1623 EscapeEncodingData *dataPtr;
1624 Tcl_EncodingType type;
1625
1626 init[0] = '\0';
1627 final[0] = '\0';
1628 Tcl_DStringInit(&escapeData);
1629
1630 while (1) {
1631 int argc;
1632 CONST char **argv;
1633 char *line;
1634 Tcl_DString lineString;
1635
1636 Tcl_DStringInit(&lineString);
1637 if (Tcl_Gets(chan, &lineString) < 0) {
1638 break;
1639 }
1640 line = Tcl_DStringValue(&lineString);
1641 if (Tcl_SplitList(NULL, line, &argc, &argv) != TCL_OK) {
1642 continue;
1643 }
1644 if (argc >= 2) {
1645 if (strcmp(argv[0], "name") == 0) {
1646 ;
1647 } else if (strcmp(argv[0], "init") == 0) {
1648 strncpy(init, argv[1], sizeof(init));
1649 init[sizeof(init) - 1] = '\0';
1650 } else if (strcmp(argv[0], "final") == 0) {
1651 strncpy(final, argv[1], sizeof(final));
1652 final[sizeof(final) - 1] = '\0';
1653 } else {
1654 EscapeSubTable est;
1655
1656 strncpy(est.sequence, argv[1], sizeof(est.sequence));
1657 est.sequence[sizeof(est.sequence) - 1] = '\0';
1658 est.sequenceLen = strlen(est.sequence);
1659
1660 strncpy(est.name, argv[0], sizeof(est.name));
1661 est.name[sizeof(est.name) - 1] = '\0';
1662
1663 /* To avoid infinite recursion in [encoding system iso2022-*]*/
1664 Tcl_GetEncoding(NULL, est.name);
1665
1666 est.encodingPtr = NULL;
1667 Tcl_DStringAppend(&escapeData, (char *) &est, sizeof(est));
1668 }
1669 }
1670 ckfree((char *) argv);
1671 Tcl_DStringFree(&lineString);
1672 }
1673
1674 size = sizeof(EscapeEncodingData)
1675 - sizeof(EscapeSubTable) + Tcl_DStringLength(&escapeData);
1676 dataPtr = (EscapeEncodingData *) ckalloc(size);
1677 dataPtr->initLen = strlen(init);
1678 strcpy(dataPtr->init, init);
1679 dataPtr->finalLen = strlen(final);
1680 strcpy(dataPtr->final, final);
1681 dataPtr->numSubTables = Tcl_DStringLength(&escapeData) / sizeof(EscapeSubTable);
1682 memcpy((VOID *) dataPtr->subTables, (VOID *) Tcl_DStringValue(&escapeData),
1683 (size_t) Tcl_DStringLength(&escapeData));
1684 Tcl_DStringFree(&escapeData);
1685
1686 memset(dataPtr->prefixBytes, 0, sizeof(dataPtr->prefixBytes));
1687 for (i = 0; i < dataPtr->numSubTables; i++) {
1688 dataPtr->prefixBytes[UCHAR(dataPtr->subTables[i].sequence[0])] = 1;
1689 }
1690 if (dataPtr->init[0] != '\0') {
1691 dataPtr->prefixBytes[UCHAR(dataPtr->init[0])] = 1;
1692 }
1693 if (dataPtr->final[0] != '\0') {
1694 dataPtr->prefixBytes[UCHAR(dataPtr->final[0])] = 1;
1695 }
1696
1697 type.encodingName = name;
1698 type.toUtfProc = EscapeToUtfProc;
1699 type.fromUtfProc = EscapeFromUtfProc;
1700 type.freeProc = EscapeFreeProc;
1701 type.nullSize = 1;
1702 type.clientData = (ClientData) dataPtr;
1703
1704 return Tcl_CreateEncoding(&type);
1705 }
1706
1707 /*
1708 *-------------------------------------------------------------------------
1709 *
1710 * BinaryProc --
1711 *
1712 * The default conversion when no other conversion is specified.
1713 * No translation is done; source bytes are copied directly to
1714 * destination bytes.
1715 *
1716 * Results:
1717 * Returns TCL_OK if conversion was successful.
1718 *
1719 * Side effects:
1720 * None.
1721 *
1722 *-------------------------------------------------------------------------
1723 */
1724
1725 static int
1726 BinaryProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
1727 srcReadPtr, dstWrotePtr, dstCharsPtr)
1728 ClientData clientData; /* Not used. */
1729 CONST char *src; /* Source string (unknown encoding). */
1730 int srcLen; /* Source string length in bytes. */
1731 int flags; /* Conversion control flags. */
1732 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
1733 * state information used during a piecewise
1734 * conversion. Contents of statePtr are
1735 * initialized and/or reset by conversion
1736 * routine under control of flags argument. */
1737 char *dst; /* Output buffer in which converted string
1738 * is stored. */
1739 int dstLen; /* The maximum length of output buffer in
1740 * bytes. */
1741 int *srcReadPtr; /* Filled with the number of bytes from the
1742 * source string that were converted. */
1743 int *dstWrotePtr; /* Filled with the number of bytes that were
1744 * stored in the output buffer as a result of
1745 * the conversion. */
1746 int *dstCharsPtr; /* Filled with the number of characters that
1747 * correspond to the bytes stored in the
1748 * output buffer. */
1749 {
1750 int result;
1751
1752 result = TCL_OK;
1753 dstLen -= TCL_UTF_MAX - 1;
1754 if (dstLen < 0) {
1755 dstLen = 0;
1756 }
1757 if (srcLen > dstLen) {
1758 srcLen = dstLen;
1759 result = TCL_CONVERT_NOSPACE;
1760 }
1761
1762 *srcReadPtr = srcLen;
1763 *dstWrotePtr = srcLen;
1764 *dstCharsPtr = srcLen;
1765 for ( ; --srcLen >= 0; ) {
1766 *dst++ = *src++;
1767 }
1768 return result;
1769 }
1770
1771 /*
1772 *-------------------------------------------------------------------------
1773 *
1774 * UtfToUtfProc --
1775 *
1776 * Convert from UTF-8 to UTF-8. Note that the UTF-8 to UTF-8
1777 * translation is not a no-op, because it will turn a stream of
1778 * improperly formed UTF-8 into a properly formed stream.
1779 *
1780 * Results:
1781 * Returns TCL_OK if conversion was successful.
1782 *
1783 * Side effects:
1784 * None.
1785 *
1786 *-------------------------------------------------------------------------
1787 */
1788
1789 static int
1790 UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
1791 srcReadPtr, dstWrotePtr, dstCharsPtr)
1792 ClientData clientData; /* Not used. */
1793 CONST char *src; /* Source string in UTF-8. */
1794 int srcLen; /* Source string length in bytes. */
1795 int flags; /* Conversion control flags. */
1796 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
1797 * state information used during a piecewise
1798 * conversion. Contents of statePtr are
1799 * initialized and/or reset by conversion
1800 * routine under control of flags argument. */
1801 char *dst; /* Output buffer in which converted string
1802 * is stored. */
1803 int dstLen; /* The maximum length of output buffer in
1804 * bytes. */
1805 int *srcReadPtr; /* Filled with the number of bytes from the
1806 * source string that were converted. This
1807 * may be less than the original source length
1808 * if there was a problem converting some
1809 * source characters. */
1810 int *dstWrotePtr; /* Filled with the number of bytes that were
1811 * stored in the output buffer as a result of
1812 * the conversion. */
1813 int *dstCharsPtr; /* Filled with the number of characters that
1814 * correspond to the bytes stored in the
1815 * output buffer. */
1816 {
1817 CONST char *srcStart, *srcEnd, *srcClose;
1818 char *dstStart, *dstEnd;
1819 int result, numChars;
1820 Tcl_UniChar ch;
1821
1822 result = TCL_OK;
1823
1824 srcStart = src;
1825 srcEnd = src + srcLen;
1826 srcClose = srcEnd;
1827 if ((flags & TCL_ENCODING_END) == 0) {
1828 srcClose -= TCL_UTF_MAX;
1829 }
1830
1831 dstStart = dst;
1832 dstEnd = dst + dstLen - TCL_UTF_MAX;
1833
1834 for (numChars = 0; src < srcEnd; numChars++) {
1835 if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
1836 /*
1837 * If there is more string to follow, this will ensure that the
1838 * last UTF-8 character in the source buffer hasn't been cut off.
1839 */
1840
1841 result = TCL_CONVERT_MULTIBYTE;
1842 break;
1843 }
1844 if (dst > dstEnd) {
1845 result = TCL_CONVERT_NOSPACE;
1846 break;
1847 }
1848 if (UCHAR(*src) < 0x80) {
1849 *dst++ = *src++;
1850 } else {
1851 src += Tcl_UtfToUniChar(src, &ch);
1852 dst += Tcl_UniCharToUtf(ch, dst);
1853 }
1854 }
1855
1856 *srcReadPtr = src - srcStart;
1857 *dstWrotePtr = dst - dstStart;
1858 *dstCharsPtr = numChars;
1859 return result;
1860 }
1861
1862 /*
1863 *-------------------------------------------------------------------------
1864 *
1865 * UnicodeToUtfProc --
1866 *
1867 * Convert from Unicode to UTF-8.
1868 *
1869 * Results:
1870 * Returns TCL_OK if conversion was successful.
1871 *
1872 * Side effects:
1873 * None.
1874 *
1875 *-------------------------------------------------------------------------
1876 */
1877
1878 static int
1879 UnicodeToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
1880 srcReadPtr, dstWrotePtr, dstCharsPtr)
1881 ClientData clientData; /* Not used. */
1882 CONST char *src; /* Source string in Unicode. */
1883 int srcLen; /* Source string length in bytes. */
1884 int flags; /* Conversion control flags. */
1885 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
1886 * state information used during a piecewise
1887 * conversion. Contents of statePtr are
1888 * initialized and/or reset by conversion
1889 * routine under control of flags argument. */
1890 char *dst; /* Output buffer in which converted string
1891 * is stored. */
1892 int dstLen; /* The maximum length of output buffer in
1893 * bytes. */
1894 int *srcReadPtr; /* Filled with the number of bytes from the
1895 * source string that were converted. This
1896 * may be less than the original source length
1897 * if there was a problem converting some
1898 * source characters. */
1899 int *dstWrotePtr; /* Filled with the number of bytes that were
1900 * stored in the output buffer as a result of
1901 * the conversion. */
1902 int *dstCharsPtr; /* Filled with the number of characters that
1903 * correspond to the bytes stored in the
1904 * output buffer. */
1905 {
1906 CONST Tcl_UniChar *wSrc, *wSrcStart, *wSrcEnd;
1907 char *dstEnd, *dstStart;
1908 int result, numChars;
1909
1910 result = TCL_OK;
1911 if ((srcLen % sizeof(Tcl_UniChar)) != 0) {
1912 result = TCL_CONVERT_MULTIBYTE;
1913 srcLen /= sizeof(Tcl_UniChar);
1914 srcLen *= sizeof(Tcl_UniChar);
1915 }
1916
1917 wSrc = (Tcl_UniChar *) src;
1918
1919 wSrcStart = (Tcl_UniChar *) src;
1920 wSrcEnd = (Tcl_UniChar *) (src + srcLen);
1921
1922 dstStart = dst;
1923 dstEnd = dst + dstLen - TCL_UTF_MAX;
1924
1925 for (numChars = 0; wSrc < wSrcEnd; numChars++) {
1926 if (dst > dstEnd) {
1927 result = TCL_CONVERT_NOSPACE;
1928 break;
1929 }
1930 /*
1931 * Special case for 1-byte utf chars for speed.
1932 */
1933 if (*wSrc && *wSrc < 0x80) {
1934 *dst++ = (char) *wSrc;
1935 } else {
1936 dst += Tcl_UniCharToUtf(*wSrc, dst);
1937 }
1938 wSrc++;
1939 }
1940
1941 *srcReadPtr = (char *) wSrc - (char *) wSrcStart;
1942 *dstWrotePtr = dst - dstStart;
1943 *dstCharsPtr = numChars;
1944 return result;
1945 }
1946
1947 /*
1948 *-------------------------------------------------------------------------
1949 *
1950 * UtfToUnicodeProc --
1951 *
1952 * Convert from UTF-8 to Unicode.
1953 *
1954 * Results:
1955 * Returns TCL_OK if conversion was successful.
1956 *
1957 * Side effects:
1958 * None.
1959 *
1960 *-------------------------------------------------------------------------
1961 */
1962
1963 static int
1964 UtfToUnicodeProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
1965 srcReadPtr, dstWrotePtr, dstCharsPtr)
1966 ClientData clientData; /* TableEncodingData that specifies encoding. */
1967 CONST char *src; /* Source string in UTF-8. */
1968 int srcLen; /* Source string length in bytes. */
1969 int flags; /* Conversion control flags. */
1970 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
1971 * state information used during a piecewise
1972 * conversion. Contents of statePtr are
1973 * initialized and/or reset by conversion
1974 * routine under control of flags argument. */
1975 char *dst; /* Output buffer in which converted string
1976 * is stored. */
1977 int dstLen; /* The maximum length of output buffer in
1978 * bytes. */
1979 int *srcReadPtr; /* Filled with the number of bytes from the
1980 * source string that were converted. This
1981 * may be less than the original source length
1982 * if there was a problem converting some
1983 * source characters. */
1984 int *dstWrotePtr; /* Filled with the number of bytes that were
1985 * stored in the output buffer as a result of
1986 * the conversion. */
1987 int *dstCharsPtr; /* Filled with the number of characters that
1988 * correspond to the bytes stored in the
1989 * output buffer. */
1990 {
1991 CONST char *srcStart, *srcEnd, *srcClose;
1992 Tcl_UniChar *wDst, *wDstStart, *wDstEnd;
1993 int result, numChars;
1994
1995 srcStart = src;
1996 srcEnd = src + srcLen;
1997 srcClose = srcEnd;
1998 if ((flags & TCL_ENCODING_END) == 0) {
1999 srcClose -= TCL_UTF_MAX;
2000 }
2001
2002 wDst = (Tcl_UniChar *) dst;
2003 wDstStart = (Tcl_UniChar *) dst;
2004 wDstEnd = (Tcl_UniChar *) (dst + dstLen - sizeof(Tcl_UniChar));
2005
2006 result = TCL_OK;
2007 for (numChars = 0; src < srcEnd; numChars++) {
2008 if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
2009 /*
2010 * If there is more string to follow, this will ensure that the
2011 * last UTF-8 character in the source buffer hasn't been cut off.
2012 */
2013
2014 result = TCL_CONVERT_MULTIBYTE;
2015 break;
2016 }
2017 if (wDst > wDstEnd) {
2018 result = TCL_CONVERT_NOSPACE;
2019 break;
2020 }
2021 src += TclUtfToUniChar(src, wDst);
2022 wDst++;
2023 }
2024 *srcReadPtr = src - srcStart;
2025 *dstWrotePtr = (char *) wDst - (char *) wDstStart;
2026 *dstCharsPtr = numChars;
2027 return result;
2028 }
2029
2030 /*
2031 *-------------------------------------------------------------------------
2032 *
2033 * TableToUtfProc --
2034 *
2035 * Convert from the encoding specified by the TableEncodingData into
2036 * UTF-8.
2037 *
2038 * Results:
2039 * Returns TCL_OK if conversion was successful.
2040 *
2041 * Side effects:
2042 * None.
2043 *
2044 *-------------------------------------------------------------------------
2045 */
2046
2047 static int
2048 TableToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
2049 srcReadPtr, dstWrotePtr, dstCharsPtr)
2050 ClientData clientData; /* TableEncodingData that specifies
2051 * encoding. */
2052 CONST char *src; /* Source string in specified encoding. */
2053 int srcLen; /* Source string length in bytes. */
2054 int flags; /* Conversion control flags. */
2055 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
2056 * state information used during a piecewise
2057 * conversion. Contents of statePtr are
2058 * initialized and/or reset by conversion
2059 * routine under control of flags argument. */
2060 char *dst; /* Output buffer in which converted string
2061 * is stored. */
2062 int dstLen; /* The maximum length of output buffer in
2063 * bytes. */
2064 int *srcReadPtr; /* Filled with the number of bytes from the
2065 * source string that were converted. This
2066 * may be less than the original source length
2067 * if there was a problem converting some
2068 * source characters. */
2069 int *dstWrotePtr; /* Filled with the number of bytes that were
2070 * stored in the output buffer as a result of
2071 * the conversion. */
2072 int *dstCharsPtr; /* Filled with the number of characters that
2073 * correspond to the bytes stored in the
2074 * output buffer. */
2075 {
2076 CONST char *srcStart, *srcEnd;
2077 char *dstEnd, *dstStart, *prefixBytes;
2078 int result, byte, numChars;
2079 Tcl_UniChar ch;
2080 unsigned short **toUnicode;
2081 unsigned short *pageZero;
2082 TableEncodingData *dataPtr;
2083
2084 srcStart = src;
2085 srcEnd = src + srcLen;
2086
2087 dstStart = dst;
2088 dstEnd = dst + dstLen - TCL_UTF_MAX;
2089
2090 dataPtr = (TableEncodingData *) clientData;
2091 toUnicode = dataPtr->toUnicode;
2092 prefixBytes = dataPtr->prefixBytes;
2093 pageZero = toUnicode[0];
2094
2095 result = TCL_OK;
2096 for (numChars = 0; src < srcEnd; numChars++) {
2097 if (dst > dstEnd) {
2098 result = TCL_CONVERT_NOSPACE;
2099 break;
2100 }
2101 byte = *((unsigned char *) src);
2102 if (prefixBytes[byte]) {
2103 src++;
2104 if (src >= srcEnd) {
2105 src--;
2106 result = TCL_CONVERT_MULTIBYTE;
2107 break;
2108 }
2109 ch = toUnicode[byte][*((unsigned char *) src)];
2110 } else {
2111 ch = pageZero[byte];
2112 }
2113 if ((ch == 0) && (byte != 0)) {
2114 if (flags & TCL_ENCODING_STOPONERROR) {
2115 result = TCL_CONVERT_SYNTAX;
2116 break;
2117 }
2118 if (prefixBytes[byte]) {
2119 src--;
2120 }
2121 ch = (Tcl_UniChar) byte;
2122 }
2123 /*
2124 * Special case for 1-byte utf chars for speed.
2125 */
2126 if (ch && ch < 0x80) {
2127 *dst++ = (char) ch;
2128 } else {
2129 dst += Tcl_UniCharToUtf(ch, dst);
2130 }
2131 src++;
2132 }
2133 *srcReadPtr = src - srcStart;
2134 *dstWrotePtr = dst - dstStart;
2135 *dstCharsPtr = numChars;
2136 return result;
2137 }
2138
2139 /*
2140 *-------------------------------------------------------------------------
2141 *
2142 * TableFromUtfProc --
2143 *
2144 * Convert from UTF-8 into the encoding specified by the
2145 * TableEncodingData.
2146 *
2147 * Results:
2148 * Returns TCL_OK if conversion was successful.
2149 *
2150 * Side effects:
2151 * None.
2152 *
2153 *-------------------------------------------------------------------------
2154 */
2155
2156 static int
2157 TableFromUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
2158 srcReadPtr, dstWrotePtr, dstCharsPtr)
2159 ClientData clientData; /* TableEncodingData that specifies
2160 * encoding. */
2161 CONST char *src; /* Source string in UTF-8. */
2162 int srcLen; /* Source string length in bytes. */
2163 int flags; /* Conversion control flags. */
2164 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
2165 * state information used during a piecewise
2166 * conversion. Contents of statePtr are
2167 * initialized and/or reset by conversion
2168 * routine under control of flags argument. */
2169 char *dst; /* Output buffer in which converted string
2170 * is stored. */
2171 int dstLen; /* The maximum length of output buffer in
2172 * bytes. */
2173 int *srcReadPtr; /* Filled with the number of bytes from the
2174 * source string that were converted. This
2175 * may be less than the original source length
2176 * if there was a problem converting some
2177 * source characters. */
2178 int *dstWrotePtr; /* Filled with the number of bytes that were
2179 * stored in the output buffer as a result of
2180 * the conversion. */
2181 int *dstCharsPtr; /* Filled with the number of characters that
2182 * correspond to the bytes stored in the
2183 * output buffer. */
2184 {
2185 CONST char *srcStart, *srcEnd, *srcClose;
2186 char *dstStart, *dstEnd, *prefixBytes;
2187 Tcl_UniChar ch;
2188 int result, len, word, numChars;
2189 TableEncodingData *dataPtr;
2190 unsigned short **fromUnicode;
2191
2192 result = TCL_OK;
2193
2194 dataPtr = (TableEncodingData *) clientData;
2195 prefixBytes = dataPtr->prefixBytes;
2196 fromUnicode = dataPtr->fromUnicode;
2197
2198 srcStart = src;
2199 srcEnd = src + srcLen;
2200 srcClose = srcEnd;
2201 if ((flags & TCL_ENCODING_END) == 0) {
2202 srcClose -= TCL_UTF_MAX;
2203 }
2204
2205 dstStart = dst;
2206 dstEnd = dst + dstLen - 1;
2207
2208 for (numChars = 0; src < srcEnd; numChars++) {
2209 if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
2210 /*
2211 * If there is more string to follow, this will ensure that the
2212 * last UTF-8 character in the source buffer hasn't been cut off.
2213 */
2214
2215 result = TCL_CONVERT_MULTIBYTE;
2216 break;
2217 }
2218 len = TclUtfToUniChar(src, &ch);
2219 word = fromUnicode[(ch >> 8)][ch & 0xff];
2220 if ((word == 0) && (ch != 0)) {
2221 if (flags & TCL_ENCODING_STOPONERROR) {
2222 result = TCL_CONVERT_UNKNOWN;
2223 break;
2224 }
2225 word = dataPtr->fallback;
2226 }
2227 if (prefixBytes[(word >> 8)] != 0) {
2228 if (dst + 1 > dstEnd) {
2229 result = TCL_CONVERT_NOSPACE;
2230 break;
2231 }
2232 dst[0] = (char) (word >> 8);
2233 dst[1] = (char) word;
2234 dst += 2;
2235 } else {
2236 if (dst > dstEnd) {
2237 result = TCL_CONVERT_NOSPACE;
2238 break;
2239 }
2240 dst[0] = (char) word;
2241 dst++;
2242 }
2243 src += len;
2244 }
2245 *srcReadPtr = src - srcStart;
2246 *dstWrotePtr = dst - dstStart;
2247 *dstCharsPtr = numChars;
2248 return result;
2249 }
2250
2251 /*
2252 *---------------------------------------------------------------------------
2253 *
2254 * TableFreeProc --
2255 *
2256 * This procedure is invoked when an encoding is deleted. It deletes
2257 * the memory used by the TableEncodingData.
2258 *
2259 * Results:
2260 * None.
2261 *
2262 * Side effects:
2263 * Memory freed.
2264 *
2265 *---------------------------------------------------------------------------
2266 */
2267
2268 static void
2269 TableFreeProc(clientData)
2270 ClientData clientData; /* TableEncodingData that specifies
2271 * encoding. */
2272 {
2273 TableEncodingData *dataPtr;
2274
2275 /*
2276 * Make sure we aren't freeing twice on shutdown. [Bug #219314]
2277 */
2278
2279 dataPtr = (TableEncodingData *) clientData;
2280 ckfree((char *) dataPtr->toUnicode);
2281 ckfree((char *) dataPtr->fromUnicode);
2282 ckfree((char *) dataPtr);
2283 }
2284
2285 /*
2286 *-------------------------------------------------------------------------
2287 *
2288 * EscapeToUtfProc --
2289 *
2290 * Convert from the encoding specified by the EscapeEncodingData into
2291 * UTF-8.
2292 *
2293 * Results:
2294 * Returns TCL_OK if conversion was successful.
2295 *
2296 * Side effects:
2297 * None.
2298 *
2299 *-------------------------------------------------------------------------
2300 */
2301
2302 static int
2303 EscapeToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
2304 srcReadPtr, dstWrotePtr, dstCharsPtr)
2305 ClientData clientData; /* EscapeEncodingData that specifies
2306 * encoding. */
2307 CONST char *src; /* Source string in specified encoding. */
2308 int srcLen; /* Source string length in bytes. */
2309 int flags; /* Conversion control flags. */
2310 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
2311 * state information used during a piecewise
2312 * conversion. Contents of statePtr are
2313 * initialized and/or reset by conversion
2314 * routine under control of flags argument. */
2315 char *dst; /* Output buffer in which converted string
2316 * is stored. */
2317 int dstLen; /* The maximum length of output buffer in
2318 * bytes. */
2319 int *srcReadPtr; /* Filled with the number of bytes from the
2320 * source string that were converted. This
2321 * may be less than the original source length
2322 * if there was a problem converting some
2323 * source characters. */
2324 int *dstWrotePtr; /* Filled with the number of bytes that were
2325 * stored in the output buffer as a result of
2326 * the conversion. */
2327 int *dstCharsPtr; /* Filled with the number of characters that
2328 * correspond to the bytes stored in the
2329 * output buffer. */
2330 {
2331 EscapeEncodingData *dataPtr;
2332 char *prefixBytes, *tablePrefixBytes;
2333 unsigned short **tableToUnicode;
2334 Encoding *encodingPtr;
2335 int state, result, numChars;
2336 CONST char *srcStart, *srcEnd;
2337 char *dstStart, *dstEnd;
2338
2339 result = TCL_OK;
2340
2341 tablePrefixBytes = NULL; /* lint. */
2342 tableToUnicode = NULL; /* lint. */
2343
2344 dataPtr = (EscapeEncodingData *) clientData;
2345 prefixBytes = dataPtr->prefixBytes;
2346 encodingPtr = NULL;
2347
2348 srcStart = src;
2349 srcEnd = src + srcLen;
2350
2351 dstStart = dst;
2352 dstEnd = dst + dstLen - TCL_UTF_MAX;
2353
2354 state = (int) *statePtr;
2355 if (flags & TCL_ENCODING_START) {
2356 state = 0;
2357 }
2358
2359 for (numChars = 0; src < srcEnd; ) {
2360 int byte, hi, lo, ch;
2361
2362 if (dst > dstEnd) {
2363 result = TCL_CONVERT_NOSPACE;
2364 break;
2365 }
2366 byte = *((unsigned char *) src);
2367 if (prefixBytes[byte]) {
2368 unsigned int left, len, longest;
2369 int checked, i;
2370 EscapeSubTable *subTablePtr;
2371
2372 /*
2373 * Saw the beginning of an escape sequence.
2374 */
2375
2376 left = srcEnd - src;
2377 len = dataPtr->initLen;
2378 longest = len;
2379 checked = 0;
2380 if (len <= left) {
2381 checked++;
2382 if ((len > 0) &&
2383 (memcmp(src, dataPtr->init, len) == 0)) {
2384 /*
2385 * If we see initialization string, skip it, even if we're
2386 * not at the beginning of the buffer.
2387 */
2388
2389 src += len;
2390 continue;
2391 }
2392 }
2393 len = dataPtr->finalLen;
2394 if (len > longest) {
2395 longest = len;
2396 }
2397 if (len <= left) {
2398 checked++;
2399 if ((len > 0) &&
2400 (memcmp(src, dataPtr->final, len) == 0)) {
2401 /*
2402 * If we see finalization string, skip it, even if we're
2403 * not at the end of the buffer.
2404 */
2405
2406 src += len;
2407 continue;
2408 }
2409 }
2410 subTablePtr = dataPtr->subTables;
2411 for (i = 0; i < dataPtr->numSubTables; i++) {
2412 len = subTablePtr->sequenceLen;
2413 if (len > longest) {
2414 longest = len;
2415 }
2416 if (len <= left) {
2417 checked++;
2418 if ((len > 0) &&
2419 (memcmp(src, subTablePtr->sequence, len) == 0)) {
2420 state = i;
2421 encodingPtr = NULL;
2422 subTablePtr = NULL;
2423 src += len;
2424 break;
2425 }
2426 }
2427 subTablePtr++;
2428 }
2429 if (subTablePtr == NULL) {
2430 /*
2431 * A match was found, the escape sequence was consumed, and
2432 * the state was updated.
2433 */
2434
2435 continue;
2436 }
2437
2438 /*
2439 * We have a split-up or unrecognized escape sequence. If we
2440 * checked all the sequences, then it's a syntax error,
2441 * otherwise we need more bytes to determine a match.
2442 */
2443
2444 if ((checked == dataPtr->numSubTables + 2)
2445 || (flags & TCL_ENCODING_END)) {
2446 if ((flags & TCL_ENCODING_STOPONERROR) == 0) {
2447 /*
2448 * Skip the unknown escape sequence.
2449 */
2450
2451 src += longest;
2452 continue;
2453 }
2454 result = TCL_CONVERT_SYNTAX;
2455 } else {
2456 result = TCL_CONVERT_MULTIBYTE;
2457 }
2458 break;
2459 }
2460
2461 if (encodingPtr == NULL) {
2462 TableEncodingData *tableDataPtr;
2463
2464 encodingPtr = GetTableEncoding(dataPtr, state);
2465 tableDataPtr = (TableEncodingData *) encodingPtr->clientData;
2466 tablePrefixBytes = tableDataPtr->prefixBytes;
2467 tableToUnicode = tableDataPtr->toUnicode;
2468 }
2469 if (tablePrefixBytes[byte]) {
2470 src++;
2471 if (src >= srcEnd) {
2472 src--;
2473 result = TCL_CONVERT_MULTIBYTE;
2474 break;
2475 }
2476 hi = byte;
2477 lo = *((unsigned char *) src);
2478 } else {
2479 hi = 0;
2480 lo = byte;
2481 }
2482 ch = tableToUnicode[hi][lo];
2483 dst += Tcl_UniCharToUtf(ch, dst);
2484 src++;
2485 numChars++;
2486 }
2487
2488 *statePtr = (Tcl_EncodingState) state;
2489 *srcReadPtr = src - srcStart;
2490 *dstWrotePtr = dst - dstStart;
2491 *dstCharsPtr = numChars;
2492 return result;
2493 }
2494
2495 /*
2496 *-------------------------------------------------------------------------
2497 *
2498 * EscapeFromUtfProc --
2499 *
2500 * Convert from UTF-8 into the encoding specified by the
2501 * EscapeEncodingData.
2502 *
2503 * Results:
2504 * Returns TCL_OK if conversion was successful.
2505 *
2506 * Side effects:
2507 * None.
2508 *
2509 *-------------------------------------------------------------------------
2510 */
2511
2512 static int
2513 EscapeFromUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
2514 srcReadPtr, dstWrotePtr, dstCharsPtr)
2515 ClientData clientData; /* EscapeEncodingData that specifies
2516 * encoding. */
2517 CONST char *src; /* Source string in UTF-8. */
2518 int srcLen; /* Source string length in bytes. */
2519 int flags; /* Conversion control flags. */
2520 Tcl_EncodingState *statePtr;/* Place for conversion routine to store
2521 * state information used during a piecewise
2522 * conversion. Contents of statePtr are
2523 * initialized and/or reset by conversion
2524 * routine under control of flags argument. */
2525 char *dst; /* Output buffer in which converted string
2526 * is stored. */
2527 int dstLen; /* The maximum length of output buffer in
2528 * bytes. */
2529 int *srcReadPtr; /* Filled with the number of bytes from the
2530 * source string that were converted. This
2531 * may be less than the original source length
2532 * if there was a problem converting some
2533 * source characters. */
2534 int *dstWrotePtr; /* Filled with the number of bytes that were
2535 * stored in the output buffer as a result of
2536 * the conversion. */
2537 int *dstCharsPtr; /* Filled with the number of characters that
2538 * correspond to the bytes stored in the
2539 * output buffer. */
2540 {
2541 EscapeEncodingData *dataPtr;
2542 Encoding *encodingPtr;
2543 CONST char *srcStart, *srcEnd, *srcClose;
2544 char *dstStart, *dstEnd;
2545 int state, result, numChars;
2546 TableEncodingData *tableDataPtr;
2547 char *tablePrefixBytes;
2548 unsigned short **tableFromUnicode;
2549
2550 result = TCL_OK;
2551
2552 dataPtr = (EscapeEncodingData *) clientData;
2553
2554 srcStart = src;
2555 srcEnd = src + srcLen;
2556 srcClose = srcEnd;
2557 if ((flags & TCL_ENCODING_END) == 0) {
2558 srcClose -= TCL_UTF_MAX;
2559 }
2560
2561 dstStart = dst;
2562 dstEnd = dst + dstLen - 1;
2563
2564 /*
2565 * RFC1468 states that the text starts in ASCII, and switches to Japanese
2566 * characters, and that the text must end in ASCII. [Patch #474358]
2567 */
2568
2569 if (flags & TCL_ENCODING_START) {
2570 state = 0;
2571 if (dst + dataPtr->initLen > dstEnd) {
2572 *srcReadPtr = 0;
2573 *dstWrotePtr = 0;
2574 return TCL_CONVERT_NOSPACE;
2575 }
2576 memcpy((VOID *) dst, (VOID *) dataPtr->init,
2577 (size_t) dataPtr->initLen);
2578 dst += dataPtr->initLen;
2579 } else {
2580 state = (int) *statePtr;
2581 }
2582
2583 encodingPtr = GetTableEncoding(dataPtr, state);
2584 tableDataPtr = (TableEncodingData *) encodingPtr->clientData;
2585 tablePrefixBytes = tableDataPtr->prefixBytes;
2586 tableFromUnicode = tableDataPtr->fromUnicode;
2587
2588 for (numChars = 0; src < srcEnd; numChars++) {
2589 unsigned int len;
2590 int word;
2591 Tcl_UniChar ch;
2592
2593 if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
2594 /*
2595 * If there is more string to follow, this will ensure that the
2596 * last UTF-8 character in the source buffer hasn't been cut off.
2597 */
2598
2599 result = TCL_CONVERT_MULTIBYTE;
2600 break;
2601 }
2602 len = TclUtfToUniChar(src, &ch);
2603 word = tableFromUnicode[(ch >> 8)][ch & 0xff];
2604
2605 if ((word == 0) && (ch != 0)) {
2606 int oldState;
2607 EscapeSubTable *subTablePtr;
2608
2609 oldState = state;
2610 for (state = 0; state < dataPtr->numSubTables; state++) {
2611 encodingPtr = GetTableEncoding(dataPtr, state);
2612 tableDataPtr = (TableEncodingData *) encodingPtr->clientData;
2613 word = tableDataPtr->fromUnicode[(ch >> 8)][ch & 0xff];
2614 if (word != 0) {
2615 break;
2616 }
2617 }
2618
2619 if (word == 0) {
2620 state = oldState;
2621 if (flags & TCL_ENCODING_STOPONERROR) {
2622 result = TCL_CONVERT_UNKNOWN;
2623 break;
2624 }
2625 encodingPtr = GetTableEncoding(dataPtr, state);
2626 tableDataPtr = (TableEncodingData *) encodingPtr->clientData;
2627 word = tableDataPtr->fallback;
2628 }
2629
2630 tablePrefixBytes = tableDataPtr->prefixBytes;
2631 tableFromUnicode = tableDataPtr->fromUnicode;
2632
2633 /*
2634 * The state variable has the value of oldState when word is 0.
2635 * In this case, the escape sequense should not be copied to dst
2636 * because the current character set is not changed.
2637 */
2638 if (state != oldState) {
2639 subTablePtr = &dataPtr->subTables[state];
2640 if ((dst + subTablePtr->sequenceLen) > dstEnd) {
2641 /*
2642 * If there is no space to write the escape sequence, the
2643 * state variable must be changed to the value of oldState
2644 * variable because this escape sequence must be written
2645 * in the next conversion.
2646 */
2647 state = oldState;
2648 result = TCL_CONVERT_NOSPACE;
2649 break;
2650 }
2651 memcpy((VOID *) dst, (VOID *) subTablePtr->sequence,
2652 (size_t) subTablePtr->sequenceLen);
2653 dst += subTablePtr->sequenceLen;
2654 }
2655 }
2656
2657 if (tablePrefixBytes[(word >> 8)] != 0) {
2658 if (dst + 1 > dstEnd) {
2659 result = TCL_CONVERT_NOSPACE;
2660 break;
2661 }
2662 dst[0] = (char) (word >> 8);
2663 dst[1] = (char) word;
2664 dst += 2;
2665 } else {
2666 if (dst > dstEnd) {
2667 result = TCL_CONVERT_NOSPACE;
2668 break;
2669 }
2670 dst[0] = (char) word;
2671 dst++;
2672 }
2673 src += len;
2674 }
2675
2676 if ((result == TCL_OK) && (flags & TCL_ENCODING_END)) {
2677 unsigned int len = dataPtr->subTables[0].sequenceLen;
2678 if (dst + dataPtr->finalLen + (state?len:0) > dstEnd) {
2679 result = TCL_CONVERT_NOSPACE;
2680 } else {
2681 if (state) {
2682 memcpy((VOID *) dst, (VOID *) dataPtr->subTables[0].sequence,
2683 (size_t) len);
2684 dst += len;
2685 }
2686 memcpy((VOID *) dst, (VOID *) dataPtr->final,
2687 (size_t) dataPtr->finalLen);
2688 dst += dataPtr->finalLen;
2689 }
2690 }
2691
2692 *statePtr = (Tcl_EncodingState) state;
2693 *srcReadPtr = src - srcStart;
2694 *dstWrotePtr = dst - dstStart;
2695 *dstCharsPtr = numChars;
2696 return result;
2697 }
2698
2699 /*
2700 *---------------------------------------------------------------------------
2701 *
2702 * EscapeFreeProc --
2703 *
2704 * This procedure is invoked when an EscapeEncodingData encoding is
2705 * deleted. It deletes the memory used by the encoding.
2706 *
2707 * Results:
2708 * None.
2709 *
2710 * Side effects:
2711 * Memory freed.
2712 *
2713 *---------------------------------------------------------------------------
2714 */
2715
2716 static void
2717 EscapeFreeProc(clientData)
2718 ClientData clientData; /* EscapeEncodingData that specifies encoding. */
2719 {
2720 EscapeEncodingData *dataPtr;
2721 EscapeSubTable *subTablePtr;
2722 int i;
2723
2724 dataPtr = (EscapeEncodingData *) clientData;
2725 if (dataPtr == NULL) {
2726 return;
2727 }
2728 subTablePtr = dataPtr->subTables;
2729 for (i = 0; i < dataPtr->numSubTables; i++) {
2730 FreeEncoding((Tcl_Encoding) subTablePtr->encodingPtr);
2731 subTablePtr++;
2732 }
2733 ckfree((char *) dataPtr);
2734 }
2735
2736 /*
2737 *---------------------------------------------------------------------------
2738 *
2739 * GetTableEncoding --
2740 *
2741 * Helper function for the EscapeEncodingData conversions. Gets the
2742 * encoding (of type TextEncodingData) that represents the specified
2743 * state.
2744 *
2745 * Results:
2746 * The return value is the encoding.
2747 *
2748 * Side effects:
2749 * If the encoding that represents the specified state has not
2750 * already been used by this EscapeEncoding, it will be loaded
2751 * and cached in the dataPtr.
2752 *
2753 *---------------------------------------------------------------------------
2754 */
2755
2756 static Encoding *
2757 GetTableEncoding(dataPtr, state)
2758 EscapeEncodingData *dataPtr;/* Contains names of encodings. */
2759 int state; /* Index in dataPtr of desired Encoding. */
2760 {
2761 EscapeSubTable *subTablePtr;
2762 Encoding *encodingPtr;
2763
2764 subTablePtr = &dataPtr->subTables[state];
2765 encodingPtr = subTablePtr->encodingPtr;
2766 if (encodingPtr == NULL) {
2767 encodingPtr = (Encoding *) Tcl_GetEncoding(NULL, subTablePtr->name);
2768 if ((encodingPtr == NULL)
2769 || (encodingPtr->toUtfProc != TableToUtfProc)) {
2770 panic("EscapeToUtfProc: invalid sub table");
2771 }
2772 subTablePtr->encodingPtr = encodingPtr;
2773 }
2774 return encodingPtr;
2775 }
2776
2777 /*
2778 *---------------------------------------------------------------------------
2779 *
2780 * unilen --
2781 *
2782 * A helper function for the Tcl_ExternalToUtf functions. This
2783 * function is similar to strlen for double-byte characters: it
2784 * returns the number of bytes in a 0x0000 terminated string.
2785 *
2786 * Results:
2787 * As above.
2788 *
2789 * Side effects:
2790 * None.
2791 *
2792 *---------------------------------------------------------------------------
2793 */
2794
2795 static size_t
2796 unilen(src)
2797 CONST char *src;
2798 {
2799 unsigned short *p;
2800
2801 p = (unsigned short *) src;
2802 while (*p != 0x0000) {
2803 p++;
2804 }
2805 return (char *) p - src;
2806 }
2807
2808 /*
2809 *-------------------------------------------------------------------------
2810 *
2811 * TclFindEncodings --
2812 *
2813 * Find and load the encoding file for this operating system.
2814 * Before this is called, Tcl makes assumptions about the
2815 * native string representation, but the true encoding is not
2816 * assured.
2817 *
2818 * Results:
2819 * None.
2820 *
2821 * Side effects:
2822 * Varied, see the respective initialization routines.
2823 *
2824 *-------------------------------------------------------------------------
2825 */
2826
2827 void
2828 TclFindEncodings(argv0)
2829 CONST char *argv0; /* Name of executable from argv[0] to main()
2830 * in native multi-byte encoding. */
2831 {
2832 if (encodingsInitialized == 0) {
2833 /*
2834 * Double check inside the mutex. There may be calls
2835 * back into this routine from some of the procedures below.
2836 */
2837
2838 TclpInitLock();
2839 if (encodingsInitialized == 0) {
2840 char *native;
2841 Tcl_Obj *pathPtr;
2842 Tcl_DString libPath, buffer;
2843
2844 /*
2845 * Have to set this bit here to avoid deadlock with the
2846 * routines below us that call into TclInitSubsystems.
2847 */
2848
2849 encodingsInitialized = 1;
2850
2851 native = TclpFindExecutable(argv0);
2852 TclpInitLibraryPath(native);
2853
2854 /*
2855 * The library path was set in the TclpInitLibraryPath routine.
2856 * The string set is a dirty UTF string. To preserve the value
2857 * convert the UTF string back to native before setting the new
2858 * default encoding.
2859 */
2860
2861 pathPtr = TclGetLibraryPath();
2862 if (pathPtr != NULL) {
2863 Tcl_UtfToExternalDString(NULL, Tcl_GetString(pathPtr), -1,
2864 &libPath);
2865 }
2866
2867 TclpSetInitialEncodings();
2868
2869 /*
2870 * Now convert the native string back to UTF.
2871 */
2872
2873 if (pathPtr != NULL) {
2874 Tcl_ExternalToUtfDString(NULL, Tcl_DStringValue(&libPath), -1,
2875 &buffer);
2876 pathPtr = Tcl_NewStringObj(Tcl_DStringValue(&buffer), -1);
2877 TclSetLibraryPath(pathPtr);
2878
2879 Tcl_DStringFree(&libPath);
2880 Tcl_DStringFree(&buffer);
2881 }
2882 }
2883 TclpInitUnlock();
2884 }
2885 }
2886