o_fopen.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. newname = OPENSSL_malloc(strlen(filename) + 1);
  67. if (newname == NULL)
  68. return NULL;
  69. for (iterator = newname, lastchar = '\0';
  70. *filename; filename++, iterator++) {
  71. if (lastchar == '/' && filename[0] == '.'
  72. && filename[1] != '.' && filename[1] != '/') {
  73. /* Leading dots are not permitted in plain DOS. */
  74. *iterator = '_';
  75. } else {
  76. *iterator = *filename;
  77. }
  78. lastchar = *filename;
  79. }
  80. *iterator = '\0';
  81. filename = newname;
  82. }
  83. file = fopen(filename, mode);
  84. OPENSSL_free(newname);
  85. }
  86. # else
  87. file = fopen(filename, mode);
  88. # endif
  89. return file;
  90. }
  91. #else
  92. void *openssl_fopen(const char *filename, const char *mode)
  93. {
  94. return NULL;
  95. }
  96. #endif