filename.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Implementation of Filename for Windows.
  3. */
  4. #include <wchar.h>
  5. #include "putty.h"
  6. #ifdef MPEXT
  7. #include <assert.h>
  8. #endif
  9. Filename *filename_from_str(const char *str)
  10. {
  11. Filename *fn = snew(Filename);
  12. fn->cpath = dupstr(str);
  13. fn->wpath = dup_mb_to_wc(DEFAULT_CODEPAGE, fn->cpath);
  14. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  15. return fn;
  16. }
  17. Filename *filename_from_wstr(const wchar_t *str)
  18. {
  19. Filename *fn = snew(Filename);
  20. fn->wpath = dupwcs(str);
  21. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  22. fn->utf8path = encode_wide_string_as_utf8(fn->wpath);
  23. return fn;
  24. }
  25. Filename *filename_from_utf8(const char *ustr)
  26. {
  27. Filename *fn = snew(Filename);
  28. fn->utf8path = dupstr(ustr);
  29. fn->wpath = decode_utf8_to_wide_string(fn->utf8path);
  30. fn->cpath = dup_wc_to_mb(DEFAULT_CODEPAGE, fn->wpath, "?");
  31. return fn;
  32. }
  33. Filename *filename_copy(const Filename *fn)
  34. {
  35. Filename *newfn = snew(Filename);
  36. newfn->cpath = dupstr(fn->cpath);
  37. newfn->wpath = dupwcs(fn->wpath);
  38. newfn->utf8path = dupstr(fn->utf8path);
  39. return newfn;
  40. }
  41. #ifdef WINSCP
  42. const char* in_memory_key_data(const Filename *fn)
  43. {
  44. const char* result = fn->path;
  45. if (result[0] != '@')
  46. {
  47. result = NULL;
  48. }
  49. else
  50. {
  51. int len;
  52. result++;
  53. len = strlen(result);
  54. if (((len % 2) != 0) ||
  55. ((len / 2) < MAX_PATH))
  56. {
  57. result = NULL;
  58. }
  59. else
  60. {
  61. int i;
  62. for (i = 0; (result != NULL) && (i < len); i++)
  63. {
  64. if (!isxdigit(result[i]))
  65. {
  66. result = NULL;
  67. }
  68. }
  69. }
  70. }
  71. return result;
  72. }
  73. #endif
  74. const char *filename_to_str(const Filename *fn)
  75. {
  76. #ifdef WINSCP
  77. if (in_memory_key_data(fn) != NULL) return "in-memory";
  78. #endif
  79. return fn->cpath; /* FIXME */
  80. }
  81. bool filename_equal(const Filename *f1, const Filename *f2)
  82. {
  83. /* wpath is primary: two filenames refer to the same file if they
  84. * have the same wpath */
  85. return !wcscmp(f1->wpath, f2->wpath);
  86. }
  87. bool filename_is_null(const Filename *fn)
  88. {
  89. return !*fn->wpath;
  90. }
  91. void filename_free(Filename *fn)
  92. {
  93. sfree(fn->wpath);
  94. sfree(fn->cpath);
  95. sfree(fn->utf8path);
  96. sfree(fn);
  97. }
  98. void filename_serialise(BinarySink *bs, const Filename *f)
  99. {
  100. put_asciz(bs, f->utf8path);
  101. }
  102. Filename *filename_deserialise(BinarySource *src)
  103. {
  104. const char *utf8 = get_asciz(src);
  105. return filename_from_utf8(utf8);
  106. }
  107. char filename_char_sanitise(char c)
  108. {
  109. if (strchr("<>:\"/\\|?*", c))
  110. return '.';
  111. return c;
  112. }
  113. FILE *f_open(const Filename *fn, const char *mode, bool isprivate)
  114. {
  115. #ifdef LEGACY_WINDOWS
  116. /* Fallback for legacy pre-NT windows, where as far as I can see
  117. * _wfopen just doesn't work at all */
  118. init_winver();
  119. if (osPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
  120. osPlatformId == VER_PLATFORM_WIN32s)
  121. return fopen(fn->cpath, mode);
  122. #endif
  123. wchar_t *wmode = dup_mb_to_wc(DEFAULT_CODEPAGE, mode);
  124. FILE *fp = _wfopen(fn->wpath, wmode);
  125. sfree(wmode);
  126. return fp;
  127. }
  128. #ifdef WINSCP
  129. FILE * mp_wfopen(const char *filename, const char *mode)
  130. {
  131. size_t len = strlen(filename);
  132. wchar_t * wfilename = snewn(len * 10, wchar_t);
  133. size_t wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, wfilename, len * 10);
  134. FILE * file;
  135. if (wlen <= 0)
  136. {
  137. file = NULL;
  138. }
  139. else
  140. {
  141. wchar_t wmode[3];
  142. memset(wmode, 0, sizeof(wmode));
  143. wmode[0] = (wchar_t)mode[0];
  144. if (mode[0] != '\0')
  145. {
  146. wmode[1] = (wchar_t)mode[1];
  147. if (mode[1] != '\0')
  148. {
  149. assert(mode[2] == '\0');
  150. }
  151. }
  152. file = _wfopen(wfilename, wmode);
  153. }
  154. sfree(wfilename);
  155. return file;
  156. }
  157. #endif