Wrap iconv stuff and write tests This adds a simple wrapper around the iconv APIs and uses it instead of the old code that was inlining the iconv stuff. This makes it possible for me to test the iconv logic in isolation. A "no iconv" version of the API was defined with macros so that I could have fewer ifdefs in the code itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
diff --git a/src/path.c b/src/path.c
index c8d6efb..27abd06 100644
--- a/src/path.c
+++ b/src/path.c
@@ -18,12 +18,6 @@
#define LOOKS_LIKE_DRIVE_PREFIX(S) (git__isalpha((S)[0]) && (S)[1] == ':')
-#if __APPLE__
-#include <iconv.h>
-#endif
-#define ICONV_REPO_ENCODING "UTF-8"
-#define ICONV_PATH_ENCODING "UTF-8-MAC"
-
#ifdef GIT_WIN32
static bool looks_like_network_computer_name(const char *path, int pos)
{
@@ -725,7 +719,7 @@ int git_path_cmp(
return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
}
-static bool path_has_non_ascii(const char *path, size_t pathlen)
+bool git_path_has_non_ascii(const char *path, size_t pathlen)
{
const uint8_t *scan = (const uint8_t *)path, *end;
@@ -736,49 +730,72 @@ static bool path_has_non_ascii(const char *path, size_t pathlen)
return false;
}
-#ifdef __APPLE__
-static int path_iconv(iconv_t map, git_buf *out, char **in, size_t *inlen)
+#ifdef GIT_USE_ICONV
+
+int git_path_iconv_init_precompose(git_path_iconv_t *ic)
+{
+ git_buf_init(&ic->buf, 0);
+ ic->map = iconv_open(GIT_PATH_REPO_ENCODING, GIT_PATH_NATIVE_ENCODING);
+ return 0;
+}
+
+void git_path_iconv_clear(git_path_iconv_t *ic)
+{
+ if (ic) {
+ if (ic->map != (iconv_t)-1)
+ iconv_close(ic->map);
+ git_buf_free(&ic->buf);
+ }
+}
+
+int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen)
{
char *nfd = *in, *nfc;
size_t nfdlen = *inlen, nfclen, wantlen = nfdlen, rv;
int retry = 1;
- if (!path_has_non_ascii(*in, *inlen))
+ if (!ic || ic->map == (iconv_t)-1 ||
+ !git_path_has_non_ascii(*in, *inlen))
return 0;
while (1) {
- if (git_buf_grow(out, wantlen) < 0)
+ if (git_buf_grow(&ic->buf, wantlen) < 0)
return -1;
- nfc = out->ptr + out->size;
- nfclen = out->asize - out->size;
+ nfc = ic->buf.ptr + ic->buf.size;
+ nfclen = ic->buf.asize - ic->buf.size;
- rv = iconv(map, &nfd, &nfdlen, &nfc, &nfclen);
+ rv = iconv(ic->map, &nfd, &nfdlen, &nfc, &nfclen);
- out->size = (nfc - out->ptr);
+ ic->buf.size = (nfc - ic->buf.ptr);
if (rv != (size_t)-1)
break;
if (errno != E2BIG)
- return -1;
+ goto fail;
/* make space for 2x the remaining data to be converted
* (with per retry overhead to avoid infinite loops)
*/
- wantlen = out->size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
+ wantlen = ic->buf.size + max(nfclen, nfdlen) * 2 + (size_t)(retry * 4);
if (retry++ > 4)
- return -1;
+ goto fail;
}
- out->ptr[out->size] = '\0';
+ ic->buf.ptr[ic->buf.size] = '\0';
- *in = out->ptr;
- *inlen = out->size;
+ *in = ic->buf.ptr;
+ *inlen = ic->buf.size;
return 0;
+
+fail:
+ giterr_set(GITERR_OS, "Unable to convert unicode path data");
+ return -1;
}
+
#endif
#if defined(__sun) || defined(__GNU__)
@@ -798,10 +815,7 @@ int git_path_direach(
DIR *dir;
path_dirent_data de_data;
struct dirent *de, *de_buf = (struct dirent *)&de_data;
-#ifdef __APPLE__
- iconv_t nfd2nfc = (iconv_t)-1;
- git_buf nfc_path = GIT_BUF_INIT;
-#endif
+ git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
if (git_path_to_dir(path) < 0)
return -1;
@@ -813,10 +827,8 @@ int git_path_direach(
return -1;
}
-#ifdef __APPLE__
if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
- nfd2nfc = iconv_open(ICONV_REPO_ENCODING, ICONV_PATH_ENCODING);
-#endif
+ (void)git_path_iconv_init_precompose(&ic);
while (p_readdir_r(dir, de_buf, &de) == 0 && de != NULL) {
char *de_path = de->d_name;
@@ -825,13 +837,8 @@ int git_path_direach(
if (git_path_is_dot_or_dotdot(de_path))
continue;
-#if __APPLE__
- if (nfd2nfc != (iconv_t)-1 &&
- (error = path_iconv(nfd2nfc, &nfc_path, &de_path, &de_len)) < 0)
- break;
-#endif
-
- if ((error = git_buf_put(path, de_path, de_len)) < 0)
+ if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0 ||
+ (error = git_buf_put(path, de_path, de_len)) < 0)
break;
error = fn(arg, path);
@@ -845,12 +852,7 @@ int git_path_direach(
}
closedir(dir);
-
-#ifdef __APPLE__
- if (nfd2nfc != (iconv_t)-1)
- iconv_close(nfd2nfc);
- git_buf_free(&nfc_path);
-#endif
+ git_path_iconv_clear(&ic);
return error;
}
@@ -867,10 +869,7 @@ int git_path_dirload(
size_t path_len;
path_dirent_data de_data;
struct dirent *de, *de_buf = (struct dirent *)&de_data;
-#ifdef __APPLE__
- iconv_t nfd2nfc = (iconv_t)-1;
- git_buf nfc_path = GIT_BUF_INIT;
-#endif
+ git_path_iconv_t ic = GIT_PATH_ICONV_INIT;
assert(path && contents);
@@ -885,10 +884,8 @@ int git_path_dirload(
return -1;
}
-#ifdef __APPLE__
if ((flags & GIT_PATH_DIR_PRECOMPOSE_UNICODE) != 0)
- nfd2nfc = iconv_open(ICONV_REPO_ENCODING, ICONV_PATH_ENCODING);
-#endif
+ (void)git_path_iconv_init_precompose(&ic);
path += prefix_len;
path_len -= prefix_len;
@@ -901,11 +898,8 @@ int git_path_dirload(
if (git_path_is_dot_or_dotdot(de_path))
continue;
-#if __APPLE__
- if (nfd2nfc != (iconv_t)-1 &&
- (error = path_iconv(nfd2nfc, &nfc_path, &de_path, &de_len)) < 0)
+ if ((error = git_path_iconv(&ic, &de_path, &de_len)) < 0)
break;
-#endif
alloc_size = path_len + need_slash + de_len + 1 + alloc_extra;
if ((entry_path = git__calloc(alloc_size, 1)) == NULL) {
@@ -924,12 +918,7 @@ int git_path_dirload(
}
closedir(dir);
-
-#ifdef __APPLE__
- if (nfd2nfc != (iconv_t)-1)
- iconv_close(nfd2nfc);
- git_buf_free(&nfc_path);
-#endif
+ git_path_iconv_clear(&ic);
if (error != 0)
giterr_set(GITERR_OS, "Failed to process directory entry in '%s'", path);
diff --git a/src/path.h b/src/path.h
index 534cfe5..2cfd714 100644
--- a/src/path.h
+++ b/src/path.h
@@ -358,4 +358,56 @@ extern int git_path_dirload_with_stat(
const char *end_stat,
git_vector *contents);
+/* check if non-ascii characters are present in filename */
+extern bool git_path_has_non_ascii(const char *path, size_t pathlen);
+
+/* only enable iconv on Mac for now */
+#ifdef __APPLE__
+#define GIT_USE_ICONV 1
+#endif
+
+#define GIT_PATH_REPO_ENCODING "UTF-8"
+
+#ifdef __APPLE__
+#define GIT_PATH_NATIVE_ENCODING "UTF-8-MAC"
+#else
+#define GIT_PATH_NATIVE_ENCODING "UTF-8"
+#endif
+
+#ifdef GIT_USE_ICONV
+
+#include <iconv.h>
+
+typedef struct {
+ iconv_t map;
+ git_buf buf;
+} git_path_iconv_t;
+
+#define GIT_PATH_ICONV_INIT { (iconv_t)-1, GIT_BUF_INIT }
+
+/* Init iconv data for converting decomposed UTF-8 to precomposed */
+extern int git_path_iconv_init_precompose(git_path_iconv_t *ic);
+
+/* Clear allocated iconv data */
+extern void git_path_iconv_clear(git_path_iconv_t *ic);
+
+/*
+ * Rewrite `in` buffer using iconv map if necessary, replacing `in`
+ * pointer internal iconv buffer if rewrite happened. The `in` pointer
+ * will be left unchanged if no rewrite was needed.
+ */
+extern int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen);
+
+#else
+
+typedef struct {
+ int unused;
+} git_path_iconv_t;
+#define GIT_PATH_ICONV_INIT { 0 }
+#define git_path_iconv_init_precompose(X) 0
+#define git_path_iconv_clear(X) (void)(X)
+#define git_path_iconv(X,Y,Z) 0
+
+#endif /* GIT_USE_ICONV */
+
#endif
diff --git a/tests-clar/core/iconv.c b/tests-clar/core/iconv.c
new file mode 100644
index 0000000..5a3e1de
--- /dev/null
+++ b/tests-clar/core/iconv.c
@@ -0,0 +1,57 @@
+#include "clar_libgit2.h"
+#include "path.h"
+
+static git_path_iconv_t ic;
+static char *nfc = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
+static char *nfd = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
+
+void test_core_iconv__initialize(void)
+{
+ cl_git_pass(git_path_iconv_init_precompose(&ic));
+}
+
+void test_core_iconv__cleanup(void)
+{
+ git_path_iconv_clear(&ic);
+}
+
+void test_core_iconv__unchanged(void)
+{
+ char *data = "Ascii data", *original = data;
+ size_t datalen = strlen(data);
+
+ cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+
+ /* There are no high bits set, so this should leave data untouched */
+ cl_assert(data == original);
+}
+
+void test_core_iconv__decomposed_to_precomposed(void)
+{
+ char *data = nfd;
+ size_t datalen = strlen(nfd);
+
+ cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+
+ /* The decomposed nfd string should be transformed to the nfc form
+ * (on platforms where iconv is enabled, of course).
+ */
+#ifdef GIT_USE_ICONV
+ cl_assert_equal_s(nfc, data);
+#else
+ cl_assert_equal_s(nfd, data);
+#endif
+}
+
+void test_core_iconv__precomposed_is_unmodified(void)
+{
+ char *data = nfc;
+ size_t datalen = strlen(nfc);
+
+ cl_git_pass(git_path_iconv(&ic, &data, &datalen));
+
+ /* data is already in precomposed form, so even though some bytes have
+ * the high-bit set, the iconv transform should result in no change.
+ */
+ cl_assert_equal_s(nfc, data);
+}
diff --git a/tests-clar/core/path.c b/tests-clar/core/path.c
index e584d61..cf2d5e9 100644
--- a/tests-clar/core/path.c
+++ b/tests-clar/core/path.c
@@ -1,5 +1,5 @@
#include "clar_libgit2.h"
-#include <fileops.h>
+#include "fileops.h"
static void
check_dirname(const char *A, const char *B)