o_fopen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #if !defined(OPENSSL_NO_STDIO)
  11. # include <stdio.h>
  12. # ifdef _WIN32
  13. # include <windows.h>
  14. # endif
  15. # ifdef __DJGPP__
  16. # include <unistd.h>
  17. # endif
  18. FILE *openssl_fopen(const char *filename, const char *mode)
  19. {
  20. FILE *file = NULL;
  21. # if defined(_WIN32) && defined(CP_UTF8)
  22. int sz, len_0 = (int)strlen(filename) + 1;
  23. DWORD flags;
  24. /*
  25. * Basically there are three cases to cover: a) filename is
  26. * pure ASCII string; b) actual UTF-8 encoded string and
  27. * c) locale-ized string, i.e. one containing 8-bit
  28. * characters that are meaningful in current system locale.
  29. * If filename is pure ASCII or real UTF-8 encoded string,
  30. * MultiByteToWideChar succeeds and _wfopen works. If
  31. * filename is locale-ized string, chances are that
  32. * MultiByteToWideChar fails reporting
  33. * ERROR_NO_UNICODE_TRANSLATION, in which case we fall
  34. * back to fopen...
  35. */
  36. if ((sz = MultiByteToWideChar(CP_UTF8, (flags = MB_ERR_INVALID_CHARS),
  37. filename, len_0, NULL, 0)) > 0 ||
  38. (GetLastError() == ERROR_INVALID_FLAGS &&
  39. (sz = MultiByteToWideChar(CP_UTF8, (flags = 0),
  40. filename, len_0, NULL, 0)) > 0)
  41. ) {
  42. WCHAR wmode[8];
  43. WCHAR *wfilename = _alloca(sz * sizeof(WCHAR));
  44. if (MultiByteToWideChar(CP_UTF8, flags,
  45. filename, len_0, wfilename, sz) &&
  46. MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode) + 1,
  47. wmode, OSSL_NELEM(wmode)) &&
  48. (file = _wfopen(wfilename, wmode)) == NULL &&
  49. (errno == ENOENT || errno == EBADF)
  50. ) {
  51. /*
  52. * UTF-8 decode succeeded, but no file, filename
  53. * could still have been locale-ized...
  54. */
  55. file = fopen(filename, mode);
  56. }
  57. } else if (GetLastError() == ERROR_NO_UNICODE_TRANSLATION) {
  58. file = fopen(filename, mode);
  59. }
  60. # elif defined(__DJGPP__)
  61. {
  62. char *newname = NULL;
  63. if (pathconf(filename, _PC_NAME_MAX) <= 12) { /* 8.3 file system? */
  64. char *iterator;
  65. char lastchar;
  66. if ((newname = OPENSSL_malloc(strlen(filename) + 1)) == NULL) {
  67. CRYPTOerr(CRYPTO_F_OPENSSL_FOPEN, ERR_R_MALLOC_FAILURE);
  68. return NULL;
  69. }
  70. for (iterator = newname, lastchar = '\0';
  71. *filename; filename++, iterator++) {
  72. if (lastchar == '/' && filename[0] == '.'
  73. && filename[1] != '.' && filename[1] != '/') {
  74. /* Leading dots are not permitted in plain DOS. */
  75. *iterator = '_';
  76. } else {
  77. *iterator = *filename;
  78. }
  79. lastchar = *filename;
  80. }
  81. *iterator = '\0';
  82. filename = newname;
  83. }
  84. file = fopen(filename, mode);
  85. OPENSSL_free(newname);
  86. }
  87. # else
  88. file = fopen(filename, mode);
  89. # endif
  90. return file;
  91. }
  92. #else
  93. void *openssl_fopen(const char *filename, const char *mode)
  94. {
  95. return NULL;
  96. }
  97. #endif