Skip to content

Commit 0d7f3f5

Browse files
ethomsonEdward Thomson
authored andcommitted
utf8: add conversion with size and refactor names
Add functions to use convert a string with length, instead of assuming NUL termination. In addition, move the utf8 to 16 conversion routines into the `git_utf8` namespace instead of using namespaceless `git__` prefixed names.
1 parent cff0d9b commit 0d7f3f5

File tree

12 files changed

+185
-120
lines changed

12 files changed

+185
-120
lines changed

src/libgit2/streams/schannel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ static int schannel_stream_wrap(
661661
st->io = in;
662662
st->owned = owned;
663663

664-
if (git__utf8_to_16_alloc(&st->host_w, host) < 0) {
664+
if (git_utf8_to_16_alloc(&st->host_w, host) < 0) {
665665
git__free(st);
666666
return -1;
667667
}

src/libgit2/transports/winhttp.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ static int apply_userpass_credentials(HINTERNET request, DWORD target, int mecha
158158
goto done;
159159
}
160160

161-
if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0)
161+
if ((error = user_len = git_utf8_to_16_alloc(&user, c->username)) < 0)
162162
goto done;
163163

164-
if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0)
164+
if ((error = pass_len = git_utf8_to_16_alloc(&pass, c->password)) < 0)
165165
goto done;
166166

167167
if (!WinHttpSetCredentials(request, target, native_scheme, user, pass, NULL)) {
@@ -242,7 +242,7 @@ static int acquire_fallback_cred(
242242
HRESULT hCoInitResult;
243243

244244
/* Convert URL to wide characters */
245-
if (git__utf8_to_16_alloc(&wide_url, url) < 0) {
245+
if (git_utf8_to_16_alloc(&wide_url, url) < 0) {
246246
git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
247247
return -1;
248248
}
@@ -397,7 +397,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
397397
return -1;
398398

399399
/* Convert URL to wide characters */
400-
if (git__utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) {
400+
if (git_utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) {
401401
git_error_set(GIT_ERROR_OS, "failed to convert string to wide form");
402402
goto on_error;
403403
}
@@ -473,7 +473,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
473473
}
474474

475475
/* Convert URL to wide characters */
476-
error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
476+
error = git_utf8_to_16_alloc(&proxy_wide, processed_url.ptr);
477477
git_str_dispose(&processed_url);
478478
if (error < 0)
479479
goto on_error;
@@ -531,7 +531,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
531531
s->service) < 0)
532532
goto on_error;
533533

534-
if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) {
534+
if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) {
535535
git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters");
536536
goto on_error;
537537
}
@@ -548,7 +548,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
548548
s->service) < 0)
549549
goto on_error;
550550

551-
if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) {
551+
if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) {
552552
git_error_set(GIT_ERROR_OS, "failed to convert accept header to wide characters");
553553
goto on_error;
554554
}
@@ -568,7 +568,7 @@ static int winhttp_stream_connect(winhttp_stream *s)
568568
git_str_puts(&buf, t->owner->connect_opts.custom_headers.strings[i]);
569569

570570
/* Convert header to wide characters */
571-
if ((error = git__utf8_to_16_alloc(&custom_header_wide, git_str_cstr(&buf))) < 0)
571+
if ((error = git_utf8_to_16_alloc(&custom_header_wide, git_str_cstr(&buf))) < 0)
572572
goto on_error;
573573

574574
if (!WinHttpAddRequestHeaders(s->request, custom_header_wide, (ULONG)-1L,
@@ -783,7 +783,7 @@ static int winhttp_connect(
783783
}
784784

785785
/* Prepare host */
786-
if (git__utf8_to_16_alloc(&wide_host, host) < 0) {
786+
if (git_utf8_to_16_alloc(&wide_host, host) < 0) {
787787
git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
788788
goto on_error;
789789
}
@@ -792,7 +792,7 @@ static int winhttp_connect(
792792
if (git_http__user_agent(&ua) < 0)
793793
goto on_error;
794794

795-
if (git__utf8_to_16_alloc(&wide_ua, git_str_cstr(&ua)) < 0) {
795+
if (git_utf8_to_16_alloc(&wide_ua, git_str_cstr(&ua)) < 0) {
796796
git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters");
797797
goto on_error;
798798
}
@@ -1182,7 +1182,7 @@ static int winhttp_stream_read(
11821182
}
11831183

11841184
/* Convert the Location header to UTF-8 */
1185-
if (git__utf16_to_8_alloc(&location8, location) < 0) {
1185+
if (git_utf8_from_16_alloc(&location8, location) < 0) {
11861186
git_error_set(GIT_ERROR_OS, "failed to convert Location header to UTF-8");
11871187
git__free(location);
11881188
return -1;
@@ -1254,7 +1254,7 @@ static int winhttp_stream_read(
12541254
else
12551255
p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service);
12561256

1257-
if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
1257+
if (git_utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) {
12581258
git_error_set(GIT_ERROR_OS, "failed to convert expected content-type to wide characters");
12591259
return -1;
12601260
}

src/util/fs_path.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ int git_fs_path_find_executable(git_str *fullpath, const char *executable)
20152015
git_win32_path fullpath_w, executable_w;
20162016
int error;
20172017

2018-
if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
2018+
if (git_utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0)
20192019
return -1;
20202020

20212021
error = git_win32_path_find_executable(fullpath_w, executable_w);

src/util/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ int git__getenv(git_str *out, const char *name)
743743

744744
git_str_clear(out);
745745

746-
if (git__utf8_to_16_alloc(&wide_name, name) < 0)
746+
if (git_utf8_to_16_alloc(&wide_name, name) < 0)
747747
return -1;
748748

749749
if ((value_len = GetEnvironmentVariableW(wide_name, NULL, 0)) > 0) {

src/util/win32/error.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ char *git_win32_get_error_message(DWORD error_code)
4343
(LPWSTR)&lpMsgBuf, 0, NULL)) {
4444
/* Convert the message to UTF-8. If this fails, we will
4545
* return NULL, which is a condition expected by the caller */
46-
if (git__utf16_to_8_alloc(&utf8_msg, lpMsgBuf) < 0)
46+
if (git_utf8_from_16_alloc(&utf8_msg, lpMsgBuf) < 0)
4747
utf8_msg = NULL;
4848

4949
LocalFree(lpMsgBuf);

src/util/win32/path_w32.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,13 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
336336

337337
/* See if this is an absolute path (beginning with a drive letter) */
338338
if (git_fs_path_is_absolute(src)) {
339-
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
339+
if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src) < 0)
340340
goto on_error;
341341
}
342342
/* File-prefixed NT-style paths beginning with \\?\ */
343343
else if (path__is_nt_namespace(src)) {
344344
/* Skip the NT prefix, the destination already contains it */
345-
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
345+
if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src + PATH__NT_NAMESPACE_LEN) < 0)
346346
goto on_error;
347347
}
348348
/* UNC paths */
@@ -351,7 +351,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
351351
dest += 4;
352352

353353
/* Skip the leading "\\" */
354-
if (git__utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
354+
if (git_utf8_to_16(dest, GIT_WIN_PATH_MAX - 2, src + 2) < 0)
355355
goto on_error;
356356
}
357357
/* Absolute paths omitting the drive letter */
@@ -365,7 +365,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
365365
}
366366

367367
/* Skip the drive letter specification ("C:") */
368-
if (git__utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
368+
if (git_utf8_to_16(dest + 2, GIT_WIN_PATH_MAX - 2, src) < 0)
369369
goto on_error;
370370
}
371371
/* Relative paths */
@@ -377,7 +377,7 @@ int git_win32_path_from_utf8(git_win32_path out, const char *src)
377377

378378
dest[cwd_len++] = L'\\';
379379

380-
if (git__utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
380+
if (git_utf8_to_16(dest + cwd_len, GIT_WIN_PATH_MAX - cwd_len, src) < 0)
381381
goto on_error;
382382
}
383383

@@ -404,7 +404,7 @@ int git_win32_path_relative_from_utf8(git_win32_path out, const char *src)
404404
return git_win32_path_from_utf8(out, src);
405405
}
406406

407-
if ((len = git__utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
407+
if ((len = git_utf8_to_16(dest, GIT_WIN_PATH_MAX, src)) < 0)
408408
return -1;
409409

410410
for (p = dest; p < (dest + len); p++) {
@@ -433,7 +433,7 @@ int git_win32_path_to_utf8(git_win32_utf8_path dest, const wchar_t *src)
433433
}
434434
}
435435

436-
if ((len = git__utf16_to_8(out, GIT_WIN_PATH_UTF8, src)) < 0)
436+
if ((len = git_utf8_from_16(out, GIT_WIN_PATH_UTF8, src)) < 0)
437437
return len;
438438

439439
git_fs_path_mkposix(dest);
@@ -471,7 +471,7 @@ char *git_win32_path_8dot3_name(const char *path)
471471
if (namelen > 12 || (shortname = git__malloc(namelen + 1)) == NULL)
472472
return NULL;
473473

474-
if ((len = git__utf16_to_8(shortname, namelen + 1, start)) < 0)
474+
if ((len = git_utf8_from_16(shortname, namelen + 1, start)) < 0)
475475
return NULL;
476476

477477
return shortname;

src/util/win32/posix_w32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ int p_getcwd(char *buffer_out, size_t size)
649649
git_win32_path_remove_namespace(cwd, wcslen(cwd));
650650

651651
/* Convert the working directory back to UTF-8 */
652-
if (git__utf16_to_8(buffer_out, size, cwd) < 0) {
652+
if (git_utf8_from_16(buffer_out, size, cwd) < 0) {
653653
DWORD code = GetLastError();
654654

655655
if (code == ERROR_INSUFFICIENT_BUFFER)

0 commit comments

Comments
 (0)