| 1 | #include "mingw-posix.h" |
| 2 | |
| 3 | struct config_context; |
| 4 | int mingw_core_config(const char *var, const char *value, |
| 5 | const struct config_context *ctx, void *cb); |
| 6 | #define platform_core_config mingw_core_config |
| 7 | |
| 8 | #ifndef NO_OPENSSL |
| 9 | #include <openssl/ssl.h> |
| 10 | static inline int mingw_SSL_set_fd(SSL *ssl, int fd) |
| 11 | { |
| 12 | return SSL_set_fd(ssl, _get_osfhandle(fd)); |
| 13 | } |
| 14 | #define SSL_set_fd mingw_SSL_set_fd |
| 15 | |
| 16 | static inline int mingw_SSL_set_rfd(SSL *ssl, int fd) |
| 17 | { |
| 18 | return SSL_set_rfd(ssl, _get_osfhandle(fd)); |
| 19 | } |
| 20 | #define SSL_set_rfd mingw_SSL_set_rfd |
| 21 | |
| 22 | static inline int mingw_SSL_set_wfd(SSL *ssl, int fd) |
| 23 | { |
| 24 | return SSL_set_wfd(ssl, _get_osfhandle(fd)); |
| 25 | } |
| 26 | #define SSL_set_wfd mingw_SSL_set_wfd |
| 27 | #endif |
| 28 | |
| 29 | /* |
| 30 | * git specific compatibility |
| 31 | */ |
| 32 | |
| 33 | static inline void convert_slashes(char *path) |
| 34 | { |
| 35 | for (; *path; path++) |
| 36 | if (*path == '\\') |
| 37 | *path = '/'; |
| 38 | } |
| 39 | #define PATH_SEP ';' |
| 40 | char *mingw_query_user_email(void); |
| 41 | #define query_user_email mingw_query_user_email |
| 42 | |
| 43 | /** |
| 44 | * Verifies that the specified path is owned by the user running the |
| 45 | * current process. |
| 46 | */ |
| 47 | int is_path_owned_by_current_sid(const char *path, struct strbuf *report); |
| 48 | #define is_path_owned_by_current_user is_path_owned_by_current_sid |
| 49 | |
| 50 | /** |
| 51 | * Verifies that the given path is a valid one on Windows. |
| 52 | * |
| 53 | * In particular, path segments are disallowed which |
| 54 | * |
| 55 | * - end in a period or a space (except the special directories `.` and `..`). |
| 56 | * |
| 57 | * - contain any of the reserved characters, e.g. `:`, `;`, `*`, etc |
| 58 | * |
| 59 | * - correspond to reserved names (such as `AUX`, `PRN`, etc) |
| 60 | * |
| 61 | * The `allow_literal_nul` parameter controls whether the path `NUL` should |
| 62 | * be considered valid (this makes sense e.g. before opening files, as it is |
| 63 | * perfectly legitimate to open `NUL` on Windows, just as it is to open |
| 64 | * `/dev/null` on Unix/Linux). |
| 65 | * |
| 66 | * Returns 1 upon success, otherwise 0. |
| 67 | */ |
| 68 | int is_valid_win32_path(const char *path, int allow_literal_nul); |
| 69 | #define is_valid_path(path) is_valid_win32_path(path, 0) |
| 70 | |
| 71 | /** |
| 72 | * Converts UTF-8 encoded string to UTF-16LE. |
| 73 | * |
| 74 | * To support repositories with legacy-encoded file names, invalid UTF-8 bytes |
| 75 | * 0xa0 - 0xff are converted to corresponding printable Unicode chars \u00a0 - |
| 76 | * \u00ff, and invalid UTF-8 bytes 0x80 - 0x9f (which would make non-printable |
| 77 | * Unicode) are converted to hex-code. |
| 78 | * |
| 79 | * Lead-bytes not followed by an appropriate number of trail-bytes, over-long |
| 80 | * encodings and 4-byte encodings > \u10ffff are detected as invalid UTF-8. |
| 81 | * |
| 82 | * Maximum space requirement for the target buffer is two wide chars per UTF-8 |
| 83 | * char (((strlen(utf) * 2) + 1) [* sizeof(wchar_t)]). |
| 84 | * |
| 85 | * The maximum space is needed only if the entire input string consists of |
| 86 | * invalid UTF-8 bytes in range 0x80-0x9f, as per the following table: |
| 87 | * |
| 88 | * | | UTF-8 | UTF-16 | |
| 89 | * Code point | UTF-8 sequence | bytes | words | ratio |
| 90 | * --------------+-------------------+-------+--------+------- |
| 91 | * 000000-00007f | 0-7f | 1 | 1 | 1 |
| 92 | * 000080-0007ff | c2-df + 80-bf | 2 | 1 | 0.5 |
| 93 | * 000800-00ffff | e0-ef + 2 * 80-bf | 3 | 1 | 0.33 |
| 94 | * 010000-10ffff | f0-f4 + 3 * 80-bf | 4 | 2 (a) | 0.5 |
| 95 | * invalid | 80-9f | 1 | 2 (b) | 2 |
| 96 | * invalid | a0-ff | 1 | 1 | 1 |
| 97 | * |
| 98 | * (a) encoded as UTF-16 surrogate pair |
| 99 | * (b) encoded as two hex digits |
| 100 | * |
| 101 | * Note that, while the UTF-8 encoding scheme can be extended to 5-byte, 6-byte |
| 102 | * or even indefinite-byte sequences, the largest valid code point \u10ffff |
| 103 | * encodes as only 4 UTF-8 bytes. |
| 104 | * |
| 105 | * Parameters: |
| 106 | * wcs: wide char target buffer |
| 107 | * utf: string to convert |
| 108 | * wcslen: size of target buffer (in wchar_t's) |
| 109 | * utflen: size of string to convert, or -1 if 0-terminated |
| 110 | * |
| 111 | * Returns: |
| 112 | * length of converted string (_wcslen(wcs)), or -1 on failure |
| 113 | * |
| 114 | * Errors: |
| 115 | * EINVAL: one of the input parameters is invalid (e.g. NULL) |
| 116 | * ERANGE: the output buffer is too small |
| 117 | */ |
| 118 | int xutftowcsn(wchar_t *wcs, const char *utf, size_t wcslen, int utflen); |
| 119 | |
| 120 | /** |
| 121 | * Simplified variant of xutftowcsn, assumes input string is \0-terminated. |
| 122 | */ |
| 123 | static inline int xutftowcs(wchar_t *wcs, const char *utf, size_t wcslen) |
| 124 | { |
| 125 | return xutftowcsn(wcs, utf, wcslen, -1); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Simplified file system specific variant of xutftowcsn, assumes output |
| 130 | * buffer size is MAX_PATH wide chars and input string is \0-terminated, |
| 131 | * fails with ENAMETOOLONG if input string is too long. |
| 132 | */ |
| 133 | static inline int xutftowcs_path(wchar_t *wcs, const char *utf) |
| 134 | { |
| 135 | int result = xutftowcsn(wcs, utf, MAX_PATH, -1); |
| 136 | if (result < 0 && errno == ERANGE) |
| 137 | errno = ENAMETOOLONG; |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Converts UTF-16LE encoded string to UTF-8. |
| 143 | * |
| 144 | * Maximum space requirement for the target buffer is three UTF-8 chars per |
| 145 | * wide char ((_wcslen(wcs) * 3) + 1). |
| 146 | * |
| 147 | * The maximum space is needed only if the entire input string consists of |
| 148 | * UTF-16 words in range 0x0800-0xd7ff or 0xe000-0xffff (i.e. \u0800-\uffff |
| 149 | * modulo surrogate pairs), as per the following table: |
| 150 | * |
| 151 | * | | UTF-16 | UTF-8 | |
| 152 | * Code point | UTF-16 sequence | words | bytes | ratio |
| 153 | * --------------+-----------------------+--------+-------+------- |
| 154 | * 000000-00007f | 0000-007f | 1 | 1 | 1 |
| 155 | * 000080-0007ff | 0080-07ff | 1 | 2 | 2 |
| 156 | * 000800-00ffff | 0800-d7ff / e000-ffff | 1 | 3 | 3 |
| 157 | * 010000-10ffff | d800-dbff + dc00-dfff | 2 | 4 | 2 |
| 158 | * |
| 159 | * Note that invalid code points > 10ffff cannot be represented in UTF-16. |
| 160 | * |
| 161 | * Parameters: |
| 162 | * utf: target buffer |
| 163 | * wcs: wide string to convert |
| 164 | * utflen: size of target buffer |
| 165 | * |
| 166 | * Returns: |
| 167 | * length of converted string, or -1 on failure |
| 168 | * |
| 169 | * Errors: |
| 170 | * EINVAL: one of the input parameters is invalid (e.g. NULL) |
| 171 | * ERANGE: the output buffer is too small |
| 172 | */ |
| 173 | int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen); |
| 174 | |
| 175 | /* |
| 176 | * A critical section used in the implementation of the spawn |
| 177 | * functions (mingw_spawnv[p]e()) and waitpid(). Initialised in |
| 178 | * the replacement main() macro below. |
| 179 | */ |
| 180 | extern CRITICAL_SECTION pinfo_cs; |
| 181 | |
| 182 | /* |
| 183 | * Git, like most portable C applications, implements a main() function. On |
| 184 | * Windows, this main() function would receive parameters encoded in the |
| 185 | * current locale, but Git for Windows would prefer UTF-8 encoded parameters. |
| 186 | * |
| 187 | * To make that happen, we still declare main() here, and then declare and |
| 188 | * implement wmain() (which is the Unicode variant of main()) and compile with |
| 189 | * -municode. This wmain() function reencodes the parameters from UTF-16 to |
| 190 | * UTF-8 format, sets up a couple of other things as required on Windows, and |
| 191 | * then hands off to the main() function. |
| 192 | */ |
| 193 | int wmain(int argc, const wchar_t **w_argv); |
| 194 | int main(int argc, const char **argv); |
| 195 | |
| 196 | /* |
| 197 | * For debugging: if a problem occurs, say, in a Git process that is spawned |
| 198 | * from another Git process which in turn is spawned from yet another Git |
| 199 | * process, it can be quite daunting to figure out what is going on. |
| 200 | * |
| 201 | * Call this function to open a new MinTTY (this assumes you are in Git for |
| 202 | * Windows' SDK) with a GDB that attaches to the current process right away. |
| 203 | */ |
| 204 | void open_in_gdb(void); |
| 205 | |
| 206 | /* |
| 207 | * Used by Pthread API implementation for Windows |
| 208 | */ |
| 209 | int err_win_to_posix(DWORD winerr); |
| 210 | |
| 211 | #ifndef NO_UNIX_SOCKETS |
| 212 | int mingw_have_unix_sockets(void); |
| 213 | #undef have_unix_sockets |
| 214 | #define have_unix_sockets mingw_have_unix_sockets |
| 215 | #endif |