1 /* src/vm/utf.c - utf functions
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
33 $Id: utf8.c 3687 2005-11-16 19:13:37Z edwin $
41 #include "mm/memory.h"
42 #include "vm/exceptions.h"
43 #include "vm/options.h"
44 #include "vm/statistics.h"
45 #include "vm/stringlocal.h"
46 #include "vm/tables.h"
49 /* global variables ***********************************************************/
51 #if defined(USE_THREADS)
52 static java_objectheader *lock_utf_hashtable;
55 hashtable utf_hash; /* hashtable for utf8-symbols */
58 /* utf-symbols for pointer comparison of frequently used strings **************/
60 utf *utf_java_lang_Object; /* java/lang/Object */
62 utf *utf_java_lang_Class;
63 utf *utf_java_lang_ClassLoader;
64 utf *utf_java_lang_Cloneable;
65 utf *utf_java_lang_SecurityManager;
66 utf *utf_java_lang_String;
67 utf *utf_java_lang_System;
68 utf *utf_java_lang_ThreadGroup;
69 utf *utf_java_io_Serializable;
71 utf *utf_java_lang_Throwable;
72 utf *utf_java_lang_VMThrowable;
73 utf *utf_java_lang_Error;
74 utf *utf_java_lang_NoClassDefFoundError;
75 utf *utf_java_lang_NoSuchMethodError;
76 utf *utf_java_lang_OutOfMemoryError;
78 utf *utf_java_lang_Exception;
79 utf *utf_java_lang_ClassNotFoundException;
80 utf *utf_java_lang_IllegalArgumentException;
81 utf *utf_java_lang_IllegalMonitorStateException;
83 utf *utf_java_lang_NullPointerException;
85 utf* utf_java_lang_Void;
86 utf* utf_java_lang_Boolean;
87 utf* utf_java_lang_Byte;
88 utf* utf_java_lang_Character;
89 utf* utf_java_lang_Short;
90 utf* utf_java_lang_Integer;
91 utf* utf_java_lang_Long;
92 utf* utf_java_lang_Float;
93 utf* utf_java_lang_Double;
95 utf *utf_java_lang_StackTraceElement;
96 utf *utf_java_lang_reflect_Constructor;
97 utf *utf_java_lang_reflect_Field;
98 utf *utf_java_lang_reflect_Method;
99 utf *utf_java_util_Vector;
101 utf *utf_InnerClasses; /* InnerClasses */
102 utf *utf_ConstantValue; /* ConstantValue */
103 utf *utf_Code; /* Code */
104 utf *utf_Exceptions; /* Exceptions */
105 utf *utf_LineNumberTable; /* LineNumberTable */
106 utf *utf_SourceFile; /* SourceFile */
108 utf *utf_init; /* <init> */
109 utf *utf_clinit; /* <clinit> */
110 utf *utf_clone; /* clone */
111 utf *utf_finalize; /* finalize */
112 utf *utf_run; /* run */
114 utf *utf_add; /* add */
115 utf *utf_remove; /* remove */
116 utf *utf_put; /* put */
117 utf *utf_get; /* get */
118 utf *utf_value; /* value */
120 utf *utf_fillInStackTrace;
121 utf *utf_getSystemClassLoader;
123 utf *utf_printStackTrace;
134 utf *utf_void__void; /* ()V */
135 utf *utf_boolean__void; /* (Z)V */
136 utf *utf_byte__void; /* (B)V */
137 utf *utf_char__void; /* (C)V */
138 utf *utf_short__void; /* (S)V */
139 utf *utf_int__void; /* (I)V */
140 utf *utf_long__void; /* (J)V */
141 utf *utf_float__void; /* (F)V */
142 utf *utf_double__void; /* (D)V */
144 utf *utf_void__java_lang_ClassLoader; /* ()Ljava/lang/ClassLoader; */
145 utf *utf_void__java_lang_Object; /* ()Ljava/lang/Object; */
146 utf *utf_void__java_lang_Throwable; /* ()Ljava/lang/Throwable; */
147 utf *utf_java_lang_Object__java_lang_Object;
148 utf *utf_java_lang_String__void; /* (Ljava/lang/String;)V */
149 utf *utf_java_lang_String__java_lang_Class;
150 utf *utf_java_lang_Throwable__void; /* (Ljava/lang/Throwable;)V */
152 utf *utf_not_named_yet; /* special name for unnamed classes */
154 utf *array_packagename;
157 /* utf_init ********************************************************************
159 Initializes the utf8 subsystem.
161 *******************************************************************************/
165 #if defined(USE_THREADS)
166 /* create utf hashtable lock object */
168 lock_utf_hashtable = NEW(java_objectheader);
170 # if defined(NATIVE_THREADS)
171 initObjectLock(lock_utf_hashtable);
175 /* create utf-symbols for pointer comparison of frequently used strings */
177 utf_java_lang_Object = utf_new_char("java/lang/Object");
179 utf_java_lang_Class = utf_new_char("java/lang/Class");
180 utf_java_lang_ClassLoader = utf_new_char("java/lang/ClassLoader");
181 utf_java_lang_Cloneable = utf_new_char("java/lang/Cloneable");
182 utf_java_lang_SecurityManager = utf_new_char("java/lang/SecurityManager");
183 utf_java_lang_String = utf_new_char("java/lang/String");
184 utf_java_lang_System = utf_new_char("java/lang/System");
185 utf_java_lang_ThreadGroup = utf_new_char("java/lang/ThreadGroup");
186 utf_java_io_Serializable = utf_new_char("java/io/Serializable");
188 utf_java_lang_Throwable = utf_new_char(string_java_lang_Throwable);
189 utf_java_lang_VMThrowable = utf_new_char(string_java_lang_VMThrowable);
190 utf_java_lang_Error = utf_new_char(string_java_lang_Error);
192 utf_java_lang_NoClassDefFoundError =
193 utf_new_char(string_java_lang_NoClassDefFoundError);
195 utf_java_lang_NoSuchMethodError =
196 utf_new_char(string_java_lang_NoSuchMethodError);
198 utf_java_lang_OutOfMemoryError =
199 utf_new_char(string_java_lang_OutOfMemoryError);
201 utf_java_lang_Exception = utf_new_char(string_java_lang_Exception);
203 utf_java_lang_ClassNotFoundException =
204 utf_new_char(string_java_lang_ClassNotFoundException);
206 utf_java_lang_IllegalArgumentException =
207 utf_new_char(string_java_lang_IllegalArgumentException);
209 utf_java_lang_IllegalMonitorStateException =
210 utf_new_char(string_java_lang_IllegalMonitorStateException);
212 utf_java_lang_NullPointerException =
213 utf_new_char(string_java_lang_NullPointerException);
215 utf_java_lang_Void = utf_new_char("java/lang/Void");
216 utf_java_lang_Boolean = utf_new_char("java/lang/Boolean");
217 utf_java_lang_Byte = utf_new_char("java/lang/Byte");
218 utf_java_lang_Character = utf_new_char("java/lang/Character");
219 utf_java_lang_Short = utf_new_char("java/lang/Short");
220 utf_java_lang_Integer = utf_new_char("java/lang/Integer");
221 utf_java_lang_Long = utf_new_char("java/lang/Long");
222 utf_java_lang_Float = utf_new_char("java/lang/Float");
223 utf_java_lang_Double = utf_new_char("java/lang/Double");
225 utf_java_lang_StackTraceElement =
226 utf_new_char("java/lang/StackTraceElement");
228 utf_java_lang_reflect_Constructor =
229 utf_new_char("java/lang/reflect/Constructor");
231 utf_java_lang_reflect_Field = utf_new_char("java/lang/reflect/Field");
232 utf_java_lang_reflect_Method = utf_new_char("java/lang/reflect/Method");
233 utf_java_util_Vector = utf_new_char("java/util/Vector");
235 utf_InnerClasses = utf_new_char("InnerClasses");
236 utf_ConstantValue = utf_new_char("ConstantValue");
237 utf_Code = utf_new_char("Code");
238 utf_Exceptions = utf_new_char("Exceptions");
239 utf_LineNumberTable = utf_new_char("LineNumberTable");
240 utf_SourceFile = utf_new_char("SourceFile");
242 utf_init = utf_new_char("<init>");
243 utf_clinit = utf_new_char("<clinit>");
244 utf_clone = utf_new_char("clone");
245 utf_finalize = utf_new_char("finalize");
246 utf_run = utf_new_char("run");
248 utf_add = utf_new_char("add");
249 utf_remove = utf_new_char("remove");
250 utf_put = utf_new_char("put");
251 utf_get = utf_new_char("get");
252 utf_value = utf_new_char("value");
254 utf_printStackTrace = utf_new_char("printStackTrace");
255 utf_fillInStackTrace = utf_new_char("fillInStackTrace");
256 utf_loadClass = utf_new_char("loadClass");
257 utf_getSystemClassLoader = utf_new_char("getSystemClassLoader");
259 utf_Z = utf_new_char("Z");
260 utf_B = utf_new_char("B");
261 utf_C = utf_new_char("C");
262 utf_S = utf_new_char("S");
263 utf_I = utf_new_char("I");
264 utf_J = utf_new_char("J");
265 utf_F = utf_new_char("F");
266 utf_D = utf_new_char("D");
268 utf_void__void = utf_new_char("()V");
269 utf_boolean__void = utf_new_char("(Z)V");
270 utf_byte__void = utf_new_char("(B)V");
271 utf_char__void = utf_new_char("(C)V");
272 utf_short__void = utf_new_char("(S)V");
273 utf_int__void = utf_new_char("(I)V");
274 utf_long__void = utf_new_char("(J)V");
275 utf_float__void = utf_new_char("(F)V");
276 utf_double__void = utf_new_char("(D)V");
277 utf_void__java_lang_Object = utf_new_char("()Ljava/lang/Object;");
278 utf_void__java_lang_Throwable = utf_new_char("()Ljava/lang/Throwable;");
280 utf_void__java_lang_ClassLoader =
281 utf_new_char("()Ljava/lang/ClassLoader;");
283 utf_java_lang_Object__java_lang_Object =
284 utf_new_char("(Ljava/lang/Object;)Ljava/lang/Object;");
286 utf_java_lang_String__void = utf_new_char("(Ljava/lang/String;)V");
288 utf_java_lang_String__java_lang_Class =
289 utf_new_char("(Ljava/lang/String;)Ljava/lang/Class;");
291 utf_java_lang_Throwable__void = utf_new_char("(Ljava/lang/Throwable;)V");
293 utf_not_named_yet = utf_new_char("\t<not_named_yet>");
295 array_packagename = utf_new_char("\t<the array package>");
297 /* everything's ok */
303 /* utf_hashkey *****************************************************************
305 The hashkey is computed from the utf-text by using up to 8
306 characters. For utf-symbols longer than 15 characters 3 characters
307 are taken from the beginning and the end, 2 characters are taken
310 *******************************************************************************/
312 #define nbs(val) ((u4) *(++text) << val) /* get next byte, left shift by val */
313 #define fbs(val) ((u4) *( text) << val) /* get first byte, left shift by val */
315 u4 utf_hashkey(const char *text, u4 length)
317 const char *start_pos = text; /* pointer to utf text */
321 case 0: /* empty string */
324 case 1: return fbs(0);
325 case 2: return fbs(0) ^ nbs(3);
326 case 3: return fbs(0) ^ nbs(3) ^ nbs(5);
327 case 4: return fbs(0) ^ nbs(2) ^ nbs(4) ^ nbs(6);
328 case 5: return fbs(0) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(6);
329 case 6: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(5) ^ nbs(6);
330 case 7: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6);
331 case 8: return fbs(0) ^ nbs(1) ^ nbs(2) ^ nbs(3) ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7);
338 return a ^ nbs(4) ^ nbs(5) ^ nbs(6) ^ nbs(7) ^ nbs(8);
347 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9);
356 return a ^ nbs(6) ^ nbs(7) ^ nbs(8) ^ nbs(9) ^ nbs(10);
368 return a ^ nbs(9) ^ nbs(10);
380 return a ^ nbs(9) ^ nbs(10);
391 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
402 return a ^ nbs(9) ^ nbs(10) ^ nbs(11);
404 default: /* 3 characters from beginning */
410 /* 2 characters from middle */
411 text = start_pos + (length / 2);
416 /* 3 characters from end */
417 text = start_pos + length - 4;
422 return a ^ nbs(10) ^ nbs(11);
427 /* utf_hashkey *****************************************************************
429 Compute the hashkey of a unicode string.
431 *******************************************************************************/
433 u4 unicode_hashkey(u2 *text, u2 len)
435 return utf_hashkey((char *) text, len);
439 /* utf_new *********************************************************************
441 Creates a new utf-symbol, the text of the symbol is passed as a
442 u1-array. The function searches the utf-hashtable for a utf-symbol
443 with this text. On success the element returned, otherwise a new
444 hashtable element is created.
446 If the number of entries in the hashtable exceeds twice the size of
447 the hashtable slots a reorganization of the hashtable is done and
448 the utf symbols are copied to a new hashtable with doubled size.
450 *******************************************************************************/
452 utf *utf_new(const char *text, u2 length)
454 u4 key; /* hashkey computed from utf-text */
455 u4 slot; /* slot in hashtable */
456 utf *u; /* hashtable element */
459 /* XXX REMOVE ME! after testing of course ;-) */
460 static int running = 0;
463 #if defined(USE_THREADS)
464 builtin_monitorenter(lock_utf_hashtable);
467 /* XXX REMOVE ME! after testing of course ;-) */
468 assert(running == 0);
477 key = utf_hashkey(text, length);
478 slot = key & (utf_hash.size - 1);
479 u = utf_hash.ptr[slot];
481 /* search external hash chain for utf-symbol */
484 if (u->blength == length) {
485 /* compare text of hashtable elements */
487 for (i = 0; i < length; i++)
488 if (text[i] != u->text[i])
491 #if defined(STATISTICS)
493 count_utf_new_found++;
496 /* symbol found in hashtable */
502 #if defined(USE_THREADS)
503 builtin_monitorexit(lock_utf_hashtable);
510 u = u->hashlink; /* next element in external chain */
513 #if defined(STATISTICS)
515 count_utf_len += sizeof(utf) + length + 1;
518 /* location in hashtable found, create new utf element */
520 u->blength = length; /* length in bytes of utfstring */
521 u->hashlink = utf_hash.ptr[slot]; /* link in external hashchain */
522 u->text = mem_alloc(length + 1);/* allocate memory for utf-text */
523 memcpy(u->text, text, length); /* copy utf-text */
524 u->text[length] = '\0';
525 utf_hash.ptr[slot] = u; /* insert symbol into table */
527 utf_hash.entries++; /* update number of entries */
529 if (utf_hash.entries > (utf_hash.size * 2)) {
531 /* reorganization of hashtable, average length of
532 the external chains is approx. 2 */
536 hashtable newhash; /* the new hashtable */
538 /* create new hashtable, double the size */
539 init_hashtable(&newhash, utf_hash.size * 2);
540 newhash.entries = utf_hash.entries;
544 count_utf_len += sizeof(utf*) * utf_hash.size;
547 /* transfer elements to new hashtable */
548 for (i = 0; i < utf_hash.size; i++) {
549 u = (utf *) utf_hash.ptr[i];
551 utf *nextu = u->hashlink;
552 u4 slot = utf_hashkey(u->text, u->blength) & (newhash.size - 1);
554 u->hashlink = (utf *) newhash.ptr[slot];
555 newhash.ptr[slot] = u;
557 /* follow link in external hash chain */
562 /* dispose old table */
563 MFREE(utf_hash.ptr, void*, utf_hash.size);
571 #if defined(USE_THREADS)
572 builtin_monitorexit(lock_utf_hashtable);
579 /* utf_new_u2 ******************************************************************
581 Make utf symbol from u2 array, if isclassname is true '.' is
584 *******************************************************************************/
586 utf *utf_new_u2(u2 *unicode_pos, u4 unicode_length, bool isclassname)
588 char *buffer; /* memory buffer for unicode characters */
589 char *pos; /* pointer to current position in buffer */
590 u4 left; /* unicode characters left */
591 u4 buflength; /* utf length in bytes of the u2 array */
592 utf *result; /* resulting utf-string */
595 /* determine utf length in bytes and allocate memory */
597 buflength = u2_utflength(unicode_pos, unicode_length);
598 buffer = MNEW(char, buflength);
603 for (i = 0; i++ < unicode_length; unicode_pos++) {
604 /* next unicode character */
607 if ((c != 0) && (c < 0x80)) {
610 if ((int) left < 0) break;
611 /* convert classname */
612 if (isclassname && c == '.')
617 } else if (c < 0x800) {
619 unsigned char high = c >> 6;
620 unsigned char low = c & 0x3F;
622 if ((int) left < 0) break;
623 *pos++ = high | 0xC0;
629 char mid = (c >> 6) & 0x3F;
632 if ((int) left < 0) break;
633 *pos++ = high | 0xE0;
639 /* insert utf-string into symbol-table */
640 result = utf_new(buffer,buflength);
642 MFREE(buffer, char, buflength);
648 /* utf_new_char ****************************************************************
650 Creates a new utf symbol, the text for this symbol is passed as a
651 c-string ( = char* ).
653 *******************************************************************************/
655 utf *utf_new_char(const char *text)
657 return utf_new(text, strlen(text));
661 /* utf_new_char_classname ******************************************************
663 Creates a new utf symbol, the text for this symbol is passed as a
664 c-string ( = char* ) "." characters are going to be replaced by
665 "/". Since the above function is used often, this is a separte
666 function, instead of an if.
668 *******************************************************************************/
670 utf *utf_new_char_classname(const char *text)
672 if (strchr(text, '.')) {
673 char *txt = strdup(text);
674 char *end = txt + strlen(txt);
678 for (c = txt; c < end; c++)
679 if (*c == '.') *c = '/';
681 tmpRes = utf_new(txt, strlen(txt));
687 return utf_new(text, strlen(text));
691 /* utf_nextu2 ******************************************************************
693 Read the next unicode character from the utf string and increment
694 the utf-string pointer accordingly.
696 *******************************************************************************/
698 u2 utf_nextu2(char **utf_ptr)
700 /* uncompressed unicode character */
702 /* current position in utf text */
703 unsigned char *utf = (unsigned char *) (*utf_ptr);
704 /* bytes representing the unicode character */
705 unsigned char ch1, ch2, ch3;
706 /* number of bytes used to represent the unicode character */
709 switch ((ch1 = utf[0]) >> 4) {
710 default: /* 1 byte */
714 case 0xD: /* 2 bytes */
715 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
716 unsigned char high = ch1 & 0x1F;
717 unsigned char low = ch2 & 0x3F;
718 unicode_char = (high << 6) + low;
723 case 0xE: /* 2 or 3 bytes */
724 if (((ch2 = utf[1]) & 0xC0) == 0x80) {
725 if (((ch3 = utf[2]) & 0xC0) == 0x80) {
726 unsigned char low = ch3 & 0x3f;
727 unsigned char mid = ch2 & 0x3f;
728 unsigned char high = ch1 & 0x0f;
729 unicode_char = (((high << 6) + mid) << 6) + low;
737 /* update position in utf-text */
738 *utf_ptr = (char *) (utf + len);
744 /* utf_strlen ******************************************************************
746 Determine number of unicode characters in the utf string.
748 *******************************************************************************/
750 u4 utf_strlen(utf *u)
752 char *endpos; /* points behind utf string */
753 char *utf_ptr; /* current position in utf text */
754 u4 len = 0; /* number of unicode characters */
757 *exceptionptr = new_nullpointerexception();
764 while (utf_ptr < endpos) {
766 /* next unicode character */
767 utf_nextu2(&utf_ptr);
770 if (utf_ptr != endpos)
771 /* string ended abruptly */
772 throw_cacao_exception_exit(string_java_lang_InternalError,
773 "Illegal utf8 string");
779 /* u2_utflength ****************************************************************
781 Returns the utf length in bytes of a u2 array.
783 *******************************************************************************/
785 u4 u2_utflength(u2 *text, u4 u2_length)
787 u4 result_len = 0; /* utf length in bytes */
788 u2 ch; /* current unicode character */
791 for (len = 0; len < u2_length; len++) {
792 /* next unicode character */
795 /* determine bytes required to store unicode character as utf */
796 if (ch && (ch < 0x80))
808 /* utf_display *****************************************************************
810 Write utf symbol to stdout (for debugging purposes).
812 *******************************************************************************/
814 void utf_display(utf *u)
816 char *endpos; /* points behind utf string */
817 char *utf_ptr; /* current position in utf text */
828 while (utf_ptr < endpos) {
829 /* read next unicode character */
830 u2 c = utf_nextu2(&utf_ptr);
831 if (c >= 32 && c <= 127) printf("%c", c);
839 /* utf_display_classname *******************************************************
841 Write utf symbol to stdout with `/' converted to `.' (for debugging
844 *******************************************************************************/
846 void utf_display_classname(utf *u)
848 char *endpos; /* points behind utf string */
849 char *utf_ptr; /* current position in utf text */
860 while (utf_ptr < endpos) {
861 /* read next unicode character */
862 u2 c = utf_nextu2(&utf_ptr);
863 if (c == '/') c = '.';
864 if (c >= 32 && c <= 127) printf("%c", c);
872 /* utf_sprint ******************************************************************
874 Write utf symbol into c-string (for debugging purposes).
876 *******************************************************************************/
878 void utf_sprint(char *buffer, utf *u)
880 char *endpos; /* points behind utf string */
881 char *utf_ptr; /* current position in utf text */
882 u2 pos = 0; /* position in c-string */
885 strcpy(buffer, "NULL");
892 while (utf_ptr < endpos)
893 /* copy next unicode character */
894 buffer[pos++] = utf_nextu2(&utf_ptr);
896 /* terminate string */
901 /* utf_sprint_classname ********************************************************
903 Write utf symbol into c-string with `/' converted to `.' (for debugging
906 *******************************************************************************/
908 void utf_sprint_classname(char *buffer, utf *u)
910 char *endpos; /* points behind utf string */
911 char *utf_ptr; /* current position in utf text */
912 u2 pos = 0; /* position in c-string */
915 strcpy(buffer, "NULL");
922 while (utf_ptr < endpos) {
923 /* copy next unicode character */
924 u2 c = utf_nextu2(&utf_ptr);
925 if (c == '/') c = '.';
929 /* terminate string */
934 /* utf_strcat ******************************************************************
936 Like libc strcat, but uses an utf8 string.
938 *******************************************************************************/
940 void utf_strcat(char *buffer, utf *u)
942 utf_sprint(buffer + strlen(buffer), u);
946 /* utf_strcat_classname ********************************************************
948 Like libc strcat, but uses an utf8 string.
950 *******************************************************************************/
952 void utf_strcat_classname(char *buffer, utf *u)
954 utf_sprint_classname(buffer + strlen(buffer), u);
958 /* utf_fprint ******************************************************************
960 Write utf symbol into file.
962 *******************************************************************************/
964 void utf_fprint(FILE *file, utf *u)
966 char *endpos; /* points behind utf string */
967 char *utf_ptr; /* current position in utf text */
975 while (utf_ptr < endpos) {
976 /* read next unicode character */
977 u2 c = utf_nextu2(&utf_ptr);
979 if (c >= 32 && c <= 127) fprintf(file, "%c", c);
980 else fprintf(file, "?");
985 /* utf_fprint_classname ********************************************************
987 Write utf symbol into file with `/' converted to `.'.
989 *******************************************************************************/
991 void utf_fprint_classname(FILE *file, utf *u)
993 char *endpos; /* points behind utf string */
994 char *utf_ptr; /* current position in utf text */
1002 while (utf_ptr < endpos) {
1003 /* read next unicode character */
1004 u2 c = utf_nextu2(&utf_ptr);
1005 if (c == '/') c = '.';
1007 if (c >= 32 && c <= 127) fprintf(file, "%c", c);
1008 else fprintf(file, "?");
1013 /* is_valid_utf ****************************************************************
1015 Return true if the given string is a valid UTF-8 string.
1017 utf_ptr...points to first character
1018 end_pos...points after last character
1020 *******************************************************************************/
1022 /* static unsigned long min_codepoint[6] = {0,1L<<7,1L<<11,1L<<16,1L<<21,1L<<26}; */
1024 bool is_valid_utf(char *utf_ptr, char *end_pos)
1031 if (end_pos < utf_ptr) return false;
1032 bytes = end_pos - utf_ptr;
1036 if (!c) return false; /* 0x00 is not allowed */
1037 if ((c & 0x80) == 0) continue; /* ASCII */
1039 if ((c & 0xe0) == 0xc0) len = 1; /* 110x xxxx */
1040 else if ((c & 0xf0) == 0xe0) len = 2; /* 1110 xxxx */
1041 else if ((c & 0xf8) == 0xf0) len = 3; /* 1111 0xxx */
1042 else if ((c & 0xfc) == 0xf8) len = 4; /* 1111 10xx */
1043 else if ((c & 0xfe) == 0xfc) len = 5; /* 1111 110x */
1044 else return false; /* invalid leading byte */
1046 if (len > 2) return false; /* Java limitation */
1048 v = (unsigned long)c & (0x3f >> len);
1050 if ((bytes -= len) < 0) return false; /* missing bytes */
1052 for (i = len; i--; ) {
1054 if ((c & 0xc0) != 0x80) /* 10xx xxxx */
1056 v = (v << 6) | (c & 0x3f);
1060 if (len != 1) return false; /* Java special */
1063 /* Sun Java seems to allow overlong UTF-8 encodings */
1065 /* if (v < min_codepoint[len]) */
1066 /* XXX throw exception? */
1069 /* surrogates in UTF-8 seem to be allowed in Java classfiles */
1070 /* if (v >= 0xd800 && v <= 0xdfff) return false; */ /* surrogates */
1072 /* even these seem to be allowed */
1073 /* if (v == 0xfffe || v == 0xffff) return false; */ /* invalid codepoints */
1080 /* is_valid_name ***************************************************************
1082 Return true if the given string may be used as a class/field/method
1083 name. (Currently this only disallows empty strings and control
1086 NOTE: The string is assumed to have passed is_valid_utf!
1088 utf_ptr...points to first character
1089 end_pos...points after last character
1091 *******************************************************************************/
1093 bool is_valid_name(char *utf_ptr, char *end_pos)
1095 if (end_pos <= utf_ptr) return false; /* disallow empty names */
1097 while (utf_ptr < end_pos) {
1098 unsigned char c = *utf_ptr++;
1100 if (c < 0x20) return false; /* disallow control characters */
1101 if (c == 0xc0 && (unsigned char) *utf_ptr == 0x80) /* disallow zero */
1108 bool is_valid_name_utf(utf *u)
1110 return is_valid_name(u->text, UTF_END(u));
1114 /* utf_show ********************************************************************
1116 Writes the utf symbols in the utfhash to stdout and displays the
1117 number of external hash chains grouped according to the chainlength
1118 (for debugging purposes).
1120 *******************************************************************************/
1125 #define CHAIN_LIMIT 20 /* limit for seperated enumeration */
1127 u4 chain_count[CHAIN_LIMIT]; /* numbers of chains */
1128 u4 max_chainlength = 0; /* maximum length of the chains */
1129 u4 sum_chainlength = 0; /* sum of the chainlengths */
1130 u4 beyond_limit = 0; /* number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
1133 printf ("UTF-HASH:\n");
1135 /* show element of utf-hashtable */
1136 for (i=0; i<utf_hash.size; i++) {
1137 utf *u = utf_hash.ptr[i];
1139 printf ("SLOT %d: ", (int) i);
1151 printf ("UTF-HASH: %d slots for %d entries\n",
1152 (int) utf_hash.size, (int) utf_hash.entries );
1155 if (utf_hash.entries == 0)
1158 printf("chains:\n chainlength number of chains %% of utfstrings\n");
1160 for (i=0;i<CHAIN_LIMIT;i++)
1163 /* count numbers of hashchains according to their length */
1164 for (i=0; i<utf_hash.size; i++) {
1166 utf *u = (utf*) utf_hash.ptr[i];
1167 u4 chain_length = 0;
1169 /* determine chainlength */
1175 /* update sum of all chainlengths */
1176 sum_chainlength+=chain_length;
1178 /* determine the maximum length of the chains */
1179 if (chain_length>max_chainlength)
1180 max_chainlength = chain_length;
1182 /* update number of utf-symbols in chains with length>=CHAIN_LIMIT-1 */
1183 if (chain_length>=CHAIN_LIMIT) {
1184 beyond_limit+=chain_length;
1185 chain_length=CHAIN_LIMIT-1;
1188 /* update number of hashchains of current length */
1189 chain_count[chain_length]++;
1192 /* display results */
1193 for (i=1;i<CHAIN_LIMIT-1;i++)
1194 printf(" %2d %17d %18.2f%%\n",i,chain_count[i],(((float) chain_count[i]*i*100)/utf_hash.entries));
1196 printf(" >=%2d %17d %18.2f%%\n",CHAIN_LIMIT-1,chain_count[CHAIN_LIMIT-1],((float) beyond_limit*100)/utf_hash.entries);
1199 printf("max. chainlength:%5d\n",max_chainlength);
1201 /* avg. chainlength = sum of chainlengths / number of chains */
1202 printf("avg. chainlength:%5.2f\n",(float) sum_chainlength / (utf_hash.size-chain_count[0]));
1207 * These are local overrides for various environment variables in Emacs.
1208 * Please do not remove this and leave it at the end of the file, where
1209 * Emacs will automagically detect them.
1210 * ---------------------------------------------------------------------
1213 * indent-tabs-mode: t