OLD | NEW |
1 /* crypto/bio/bss_file.c */ | 1 /* crypto/bio/bss_file.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 file_puts, | 111 file_puts, |
112 file_gets, | 112 file_gets, |
113 file_ctrl, | 113 file_ctrl, |
114 file_new, | 114 file_new, |
115 file_free, | 115 file_free, |
116 NULL, | 116 NULL, |
117 }; | 117 }; |
118 | 118 |
119 BIO *BIO_new_file(const char *filename, const char *mode) | 119 BIO *BIO_new_file(const char *filename, const char *mode) |
120 { | 120 { |
121 » BIO *ret; | 121 » BIO *ret; |
122 » FILE *file; | 122 » FILE *file=NULL; |
123 | 123 |
124 » if ((file=fopen(filename,mode)) == NULL) | 124 #if defined(_WIN32) && defined(CP_UTF8) |
| 125 » int sz, len_0 = (int)strlen(filename)+1; |
| 126 » DWORD flags; |
| 127 |
| 128 » /* |
| 129 » * Basically there are three cases to cover: a) filename is |
| 130 » * pure ASCII string; b) actual UTF-8 encoded string and |
| 131 » * c) locale-ized string, i.e. one containing 8-bit |
| 132 » * characters that are meaningful in current system locale. |
| 133 » * If filename is pure ASCII or real UTF-8 encoded string, |
| 134 » * MultiByteToWideChar succeeds and _wfopen works. If |
| 135 » * filename is locale-ized string, chances are that |
| 136 » * MultiByteToWideChar fails reporting |
| 137 » * ERROR_NO_UNICODE_TRANSLATION, in which case we fall |
| 138 » * back to fopen... |
| 139 » */ |
| 140 » if ((sz=MultiByteToWideChar(CP_UTF8,(flags=MB_ERR_INVALID_CHARS), |
| 141 » » » » » filename,len_0,NULL,0))>0 || |
| 142 » (GetLastError()==ERROR_INVALID_FLAGS && |
| 143 » (sz=MultiByteToWideChar(CP_UTF8,(flags=0), |
| 144 » » » » » filename,len_0,NULL,0))>0) |
| 145 » ) |
| 146 » » { |
| 147 » » WCHAR wmode[8]; |
| 148 » » WCHAR *wfilename = _alloca(sz*sizeof(WCHAR)); |
| 149 |
| 150 » » if (MultiByteToWideChar(CP_UTF8,flags, |
| 151 » » » » » filename,len_0,wfilename,sz) && |
| 152 » » MultiByteToWideChar(CP_UTF8,0,mode,strlen(mode)+1, |
| 153 » » » » » wmode,sizeof(wmode)/sizeof(wmode[0])) && |
| 154 » » (file=_wfopen(wfilename,wmode))==NULL && |
| 155 » » (errno==ENOENT || errno==EBADF) |
| 156 » » )» /* UTF-8 decode succeeded, but no file, filename |
| 157 » » » * could still have been locale-ized... */ |
| 158 » » » file = fopen(filename,mode); |
| 159 » » } |
| 160 » else if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) |
| 161 » » { |
| 162 » » file = fopen(filename,mode); |
| 163 » » } |
| 164 #else |
| 165 » file=fopen(filename,mode);» |
| 166 #endif |
| 167 » if (file == NULL) |
125 { | 168 { |
126 SYSerr(SYS_F_FOPEN,get_last_sys_error()); | 169 SYSerr(SYS_F_FOPEN,get_last_sys_error()); |
127 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); | 170 ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); |
128 if (errno == ENOENT) | 171 if (errno == ENOENT) |
129 BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); | 172 BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); |
130 else | 173 else |
131 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); | 174 BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); |
132 return(NULL); | 175 return(NULL); |
133 } | 176 } |
134 » if ((ret=BIO_new(BIO_s_file_internal())) == NULL) | 177 » if ((ret=BIO_new(BIO_s_file())) == NULL) |
135 { | 178 { |
136 fclose(file); | 179 fclose(file); |
137 return(NULL); | 180 return(NULL); |
138 } | 181 } |
139 | 182 |
140 BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage U
PLINK */ | 183 BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage U
PLINK */ |
141 BIO_set_fp(ret,file,BIO_CLOSE); | 184 BIO_set_fp(ret,file,BIO_CLOSE); |
142 return(ret); | 185 return(ret); |
143 } | 186 } |
144 | 187 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 #endif | 322 #endif |
280 { | 323 { |
281 #if defined(OPENSSL_SYS_WINDOWS) | 324 #if defined(OPENSSL_SYS_WINDOWS) |
282 int fd = _fileno((FILE*)ptr); | 325 int fd = _fileno((FILE*)ptr); |
283 if (num & BIO_FP_TEXT) | 326 if (num & BIO_FP_TEXT) |
284 _setmode(fd,_O_TEXT); | 327 _setmode(fd,_O_TEXT); |
285 else | 328 else |
286 _setmode(fd,_O_BINARY); | 329 _setmode(fd,_O_BINARY); |
287 #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) | 330 #elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) |
288 int fd = fileno((FILE*)ptr); | 331 int fd = fileno((FILE*)ptr); |
289 /* Under CLib there are differences in file modes | 332 » » /* Under CLib there are differences in file modes */ |
290 */ | |
291 if (num & BIO_FP_TEXT) | 333 if (num & BIO_FP_TEXT) |
292 setmode(fd,O_TEXT); | 334 setmode(fd,O_TEXT); |
293 else | 335 else |
294 setmode(fd,O_BINARY); | 336 setmode(fd,O_BINARY); |
295 #elif defined(OPENSSL_SYS_MSDOS) | 337 #elif defined(OPENSSL_SYS_MSDOS) |
296 int fd = fileno((FILE*)ptr); | 338 int fd = fileno((FILE*)ptr); |
297 /* Set correct text/binary mode */ | 339 /* Set correct text/binary mode */ |
298 if (num & BIO_FP_TEXT) | 340 if (num & BIO_FP_TEXT) |
299 _setmode(fd,_O_TEXT); | 341 _setmode(fd,_O_TEXT); |
300 /* Dangerous to set stdin/stdout to raw (unless redirected) */ | 342 /* Dangerous to set stdin/stdout to raw (unless redirected) */ |
301 else | 343 else |
302 { | 344 { |
303 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) | 345 if (fd == STDIN_FILENO || fd == STDOUT_FILENO) |
304 { | 346 { |
305 if (isatty(fd) <= 0) | 347 if (isatty(fd) <= 0) |
306 _setmode(fd,_O_BINARY); | 348 _setmode(fd,_O_BINARY); |
307 } | 349 } |
308 else | 350 else |
309 _setmode(fd,_O_BINARY); | 351 _setmode(fd,_O_BINARY); |
310 } | 352 } |
311 #elif defined(OPENSSL_SYS_OS2) | 353 #elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN) |
312 int fd = fileno((FILE*)ptr); | 354 int fd = fileno((FILE*)ptr); |
313 if (num & BIO_FP_TEXT) | 355 if (num & BIO_FP_TEXT) |
314 setmode(fd, O_TEXT); | 356 setmode(fd, O_TEXT); |
315 else | 357 else |
316 setmode(fd, O_BINARY); | 358 setmode(fd, O_BINARY); |
317 #endif | 359 #endif |
318 } | 360 } |
319 break; | 361 break; |
320 case BIO_C_SET_FILENAME: | 362 case BIO_C_SET_FILENAME: |
321 file_free(b); | 363 file_free(b); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 n=strlen(str); | 468 n=strlen(str); |
427 ret=file_write(bp,str,n); | 469 ret=file_write(bp,str,n); |
428 return(ret); | 470 return(ret); |
429 } | 471 } |
430 | 472 |
431 #endif /* OPENSSL_NO_STDIO */ | 473 #endif /* OPENSSL_NO_STDIO */ |
432 | 474 |
433 #endif /* HEADER_BSS_FILE_C */ | 475 #endif /* HEADER_BSS_FILE_C */ |
434 | 476 |
435 | 477 |
OLD | NEW |