diff options
author | René Scharfe <l.s.r@web.de> | 2016-09-03 17:59:20 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-09-07 10:42:46 -0700 |
commit | d23309733a5b2a9e1adc304ee50c5a5ed7a087c2 (patch) | |
tree | c1555e8fce5bdf708b48fb43391c98002dcd3716 /pkt-line.c | |
parent | e0c1ceafc5bece92d35773a75fff59497e1d9bd5 (diff) | |
download | git-d23309733a5b2a9e1adc304ee50c5a5ed7a087c2.tar.gz |
introduce hex2chr() for converting two hexadecimal digits to a characterrs/hex2chr
Add and use a helper function that decodes the char value of two
hexadecimal digits. It returns a negative number on error, avoids
running over the end of the given string and doesn't shift negative
values.
Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pkt-line.c')
-rw-r--r-- | pkt-line.c | 23 |
1 files changed, 2 insertions, 21 deletions
diff --git a/pkt-line.c b/pkt-line.c index 62fdb37079..30489c60b1 100644 --- a/pkt-line.c +++ b/pkt-line.c @@ -172,27 +172,8 @@ static int get_packet_data(int fd, char **src_buf, size_t *src_size, static int packet_length(const char *linelen) { - int n; - int len = 0; - - for (n = 0; n < 4; n++) { - unsigned char c = linelen[n]; - len <<= 4; - if (c >= '0' && c <= '9') { - len += c - '0'; - continue; - } - if (c >= 'a' && c <= 'f') { - len += c - 'a' + 10; - continue; - } - if (c >= 'A' && c <= 'F') { - len += c - 'A' + 10; - continue; - } - return -1; - } - return len; + int val = hex2chr(linelen); + return (val < 0) ? val : (val << 8) | hex2chr(linelen + 2); } int packet_read(int fd, char **src_buf, size_t *src_len, |