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