summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2011-12-27 11:18:57 +0100
committernulltoken <emeric.fermas@gmail.com>2011-12-28 20:31:11 +0100
commit459e2dcd7deb379b9a013ab70aa70206fc17f16a (patch)
tree0f36d2c1d1d2147c85f9bb739ee5dfd1ba61a096 /src
parenteb8de7476b4d3caeac518ff9af459c49cfd78e35 (diff)
downloadlibgit2-459e2dcd7deb379b9a013ab70aa70206fc17f16a.tar.gz
path: add git__percent_decode()
Diffstat (limited to 'src')
-rw-r--r--src/path.c35
-rw-r--r--src/path.h2
2 files changed, 37 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c
index e4b49f35d..bd62a3e4d 100644
--- a/src/path.c
+++ b/src/path.c
@@ -237,3 +237,38 @@ void git_path_string_to_dir(char* path, size_t size)
}
}
+int git__percent_decode(git_buf *decoded_out, const char *input)
+{
+ int len, hi, lo, i, error = GIT_SUCCESS;
+ assert(decoded_out && input);
+
+ len = strlen(input);
+ git_buf_clear(decoded_out);
+
+ for(i = 0; i < len; i++)
+ {
+ char c = input[i];
+
+ if (c != '%')
+ goto append;
+
+ if (i >= len - 2)
+ goto append;
+
+ hi = git__fromhex(input[i + 1]);
+ lo = git__fromhex(input[i + 2]);
+
+ if (hi < 0 || lo < 0)
+ goto append;
+
+ c = (char)(hi << 4 | lo);
+ i += 2;
+
+append:
+ error = git_buf_putc(decoded_out, c);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to percent decode '%s'.", input);
+ }
+
+ return error;
+}
diff --git a/src/path.h b/src/path.h
index 0c8cc349c..6397feedf 100644
--- a/src/path.h
+++ b/src/path.h
@@ -74,4 +74,6 @@ GIT_INLINE(void) git_path_mkposix(char *path)
# define git_path_mkposix(p) /* blank */
#endif
+extern int git__percent_decode(git_buf *decoded_out, const char *input);
+
#endif