aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/xfuncs.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index aac46f414..aec165f06 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -122,6 +122,41 @@ char* FAST_FUNC bin2hex(char *p, const char *cp, int count)
122 return p; 122 return p;
123} 123}
124 124
125/* Convert "[x]x[:][x]x[:][x]x[:][x]x" hex string to binary, no more than COUNT bytes */
126char* FAST_FUNC hex2bin(char *dst, const char *str, int count)
127{
128 errno = EINVAL;
129 while (*str && count) {
130 uint8_t val;
131 uint8_t c = *str++;
132 if (isdigit(c))
133 val = c - '0';
134 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
135 val = (c|0x20) - ('a' - 10);
136 else
137 return NULL;
138 val <<= 4;
139 c = *str;
140 if (isdigit(c))
141 val |= c - '0';
142 else if ((c|0x20) >= 'a' && (c|0x20) <= 'f')
143 val |= (c|0x20) - ('a' - 10);
144 else if (c == ':' || c == '\0')
145 val >>= 4;
146 else
147 return NULL;
148
149 *dst++ = val;
150 if (c != '\0')
151 str++;
152 if (*str == ':')
153 str++;
154 count--;
155 }
156 errno = (*str ? ERANGE : 0);
157 return dst;
158}
159
125/* Return how long the file at fd is, if there's any way to determine it. */ 160/* Return how long the file at fd is, if there's any way to determine it. */
126#ifdef UNUSED 161#ifdef UNUSED
127off_t FAST_FUNC fdlength(int fd) 162off_t FAST_FUNC fdlength(int fd)