Changeset 250 for trunk/src/kernel32/unicode.cpp
- Timestamp:
- Jun 30, 1999, 3:25:28 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kernel32/unicode.cpp
r238 r250 1 /* $Id: unicode.cpp,v 1. 9 1999-06-28 16:59:17sandervl Exp $ */1 /* $Id: unicode.cpp,v 1.10 1999-06-30 13:25:01 sandervl Exp $ */ 2 2 3 3 /* … … 8 8 * Copyright 1999 Achim Hasenmueller 9 9 * Copyright 1999 Christoph Bratschi 10 * 11 * Read comments about the implementation before using these functions 12 * Attention: length values include terminating 0 10 13 */ 11 14 #include <os2win.h> … … 107 110 } 108 111 //****************************************************************************** 112 // unilen: length of astring buffer (including 0 terminator) 113 // returns string length 109 114 //****************************************************************************** 110 115 int WIN32API UnicodeToAsciiN(WCHAR *ustring, char *astring, int unilen) … … 118 123 char * out_buf; 119 124 120 if(ustring == NULL) 121 return(NULL); 125 if (ustring == NULL) 126 { 127 if (astring != NULL && unilen > 0) astring[0] = 0; 128 return 0; 129 } 130 131 if (astring == NULL || unilen <= 0) return 0; 122 132 123 133 // dprintf(("KERNEL32: UnicodeToAsciiN\n")); 124 134 if (getUconvObject()) 125 135 { 126 uni_chars_left = unilen + 1; 127 out_bytes_left = uni_chars_left; 136 if (unilen == 1) 137 { 138 astring[0] = 0; 139 return 0; //no data 140 } 141 142 uni_chars_left = unilen; //elements 143 out_bytes_left = unilen; //size in bytes 128 144 in_buf = (UniChar*)ustring; 129 145 out_buf = astring; … … 132 148 (void**)&out_buf, &out_bytes_left, 133 149 &num_subs); 150 134 151 // dprintf(("KERNEL32: UnicodeToAsciiN(%d) '%s'\n", rc, astring )); 135 } 136 else 137 /* idiots unicode conversion :) */ 138 for(i = 0; i < unilen; i++) 139 astring[i] = (char)ustring[i]; 140 141 astring[unilen] = 0; // @@@PH: 1999/06/09 fix - always terminate string 142 143 return(unilen); 144 } 145 //****************************************************************************** 152 } else 153 { 154 /* idiots unicode conversion :) */ 155 for(i = 0; i < unilen-1; i++) 156 { 157 astring[i] = (ustring[i] > 255) ? (char)20 : (char)ustring[i]; //CB: handle invalid characters as space 158 if (ustring[i] == 0) return i; //asta la vista, baby 159 } 160 } 161 162 astring[unilen-1] = 0; // @@@PH: 1999/06/09 fix - always terminate string 163 164 return(unilen-1); 165 } 166 //****************************************************************************** 167 // Converts unicode string to ascii string 168 // returns length of ascii string 146 169 //****************************************************************************** 147 170 int WIN32API UnicodeToAscii(WCHAR *ustring, char *astring) 148 171 { 149 172 /* forward to function with len parameter */ 150 return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring)); 151 } 152 //****************************************************************************** 173 return UnicodeToAsciiN(ustring, astring, UniStrlen((UniChar*)ustring)+1); //end included 174 } 175 //****************************************************************************** 176 // Converts unicode string to ascii string 177 // returns pointer to ascii string 153 178 //****************************************************************************** 154 179 char * WIN32API UnicodeToAsciiString(WCHAR *ustring) … … 171 196 } 172 197 //****************************************************************************** 198 // asciilen: max length of unicode buffer (including end 0) 173 199 //****************************************************************************** 174 200 void WIN32API AsciiToUnicodeN(char *ascii, WCHAR *unicode, int asciilen) … … 187 213 unicode)); 188 214 189 //CB: no input, set at least 0215 //CB: no input, set at least terminator 190 216 if (ascii == NULL) 191 217 { … … 199 225 if (getUconvObject()) 200 226 { 227 if (asciilen == 1) 228 { 229 unicode[0] = 0; 230 return; 231 } 232 201 233 in_buf = ascii; 202 in_bytes_left = asciilen; 234 in_bytes_left = asciilen; //buffer size in bytes 203 235 out_buf = (UniChar*)unicode; 204 236 205 uni_chars_left = in_bytes_left +1;237 uni_chars_left = asciilen; //elements 206 238 dprintf(("KERNEL32: AsciiToUnicode %d\n", in_bytes_left)); 207 239 … … 210 242 &out_buf, &uni_chars_left, 211 243 &num_subs ); 244 245 //if (rc != ULS_SUCCESS && in_bytes_left > 0) //CB: never the case during my tests 246 // dprintf(("KERNEL32: AsciiToUnicode failed, %d bytes left!\n",in_bytes_left)); 247 212 248 } else 213 { 214 for(i=0; 215 i < asciilen;216 i++)249 { //poor man's conversion 250 251 for(i = 0;i < asciilen-1;i++) 252 { 217 253 unicode[i] = ascii[i]; 218 } 219 220 unicode[asciilen] = 0; 221 //SvL: Messes up the heap 222 // unicode[len+1] = 0; /* @@@PH 98/06/07 */ 223 } 224 //****************************************************************************** 254 if (ascii[i] == 0) return; //work done 255 } 256 } 257 258 unicode[asciilen-1] = 0; 259 } 260 //****************************************************************************** 261 // Copies the full string from ascii to unicode 225 262 //****************************************************************************** 226 263 void WIN32API AsciiToUnicode(char *ascii, WCHAR *unicode) … … 228 265 /* achimha for security, strlen might trap if garbage in */ 229 266 /* @@@PH 98/06/07 */ 230 if ( (ascii == NULL) || /* garbage in, garbage out ! */ 231 (unicode == NULL) ) 267 if (ascii == NULL) 268 { 269 if (unicode != NULL) unicode[0] = 0; //CB: set at least end 232 270 return; 271 } 272 273 if (unicode == NULL) return; /* garbage in, garbage out ! */ 274 233 275 /* forward to call with length parameter */ 234 AsciiToUnicodeN(ascii, unicode, strlen(ascii)); 235 } 236 237 276 AsciiToUnicodeN(ascii, unicode, strlen(ascii)+1); //end included 277 } 278 279 280
Note:
See TracChangeset
for help on using the changeset viewer.