diff options
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 |