diff options
author | nulltoken <emeric.fermas@gmail.com> | 2011-12-27 11:18:57 +0100 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2011-12-28 20:31:11 +0100 |
commit | 459e2dcd7deb379b9a013ab70aa70206fc17f16a (patch) | |
tree | 0f36d2c1d1d2147c85f9bb739ee5dfd1ba61a096 /src | |
parent | eb8de7476b4d3caeac518ff9af459c49cfd78e35 (diff) | |
download | libgit2-459e2dcd7deb379b9a013ab70aa70206fc17f16a.tar.gz |
path: add git__percent_decode()
Diffstat (limited to 'src')
-rw-r--r-- | src/path.c | 35 | ||||
-rw-r--r-- | src/path.h | 2 |
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 |