aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-04-04 15:29:32 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-04-04 15:29:32 +0200
commit4836331924b5eb7f74e000d50c99bc12d513f8c7 (patch)
tree96c25b9daf69ba688350874e9b51bfc6758237fe /libbb
parentc03602baa483ed07230c88075121e0f56a4c0428 (diff)
downloadbusybox-w32-4836331924b5eb7f74e000d50c99bc12d513f8c7.tar.gz
busybox-w32-4836331924b5eb7f74e000d50c99bc12d513f8c7.tar.bz2
busybox-w32-4836331924b5eb7f74e000d50c99bc12d513f8c7.zip
libbb: factor out hex2bin() for infiniband address parser
function old new delta hex2bin - 149 +149 in_ib 172 27 -145 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
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)