diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-04 15:29:32 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-04-04 15:29:32 +0200 |
commit | 4836331924b5eb7f74e000d50c99bc12d513f8c7 (patch) | |
tree | 96c25b9daf69ba688350874e9b51bfc6758237fe | |
parent | c03602baa483ed07230c88075121e0f56a4c0428 (diff) | |
download | busybox-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>
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/xfuncs.c | 35 | ||||
-rw-r--r-- | networking/interface.c | 52 | ||||
-rw-r--r-- | networking/wget.c | 6 |
4 files changed, 46 insertions, 49 deletions
diff --git a/include/libbb.h b/include/libbb.h index 794e43889..e674e4aea 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -734,6 +734,8 @@ const char *make_human_readable_str(unsigned long long size, | |||
734 | unsigned long block_size, unsigned long display_unit) FAST_FUNC; | 734 | unsigned long block_size, unsigned long display_unit) FAST_FUNC; |
735 | /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */ | 735 | /* Put a string of hex bytes ("1b2e66fe"...), return advanced pointer */ |
736 | char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC; | 736 | char *bin2hex(char *buf, const char *cp, int count) FAST_FUNC; |
737 | /* Reverse */ | ||
738 | char* hex2bin(char *dst, const char *str, int count) FAST_FUNC; | ||
737 | 739 | ||
738 | /* Generate a UUID */ | 740 | /* Generate a UUID */ |
739 | void generate_uuid(uint8_t *buf) FAST_FUNC; | 741 | void generate_uuid(uint8_t *buf) FAST_FUNC; |
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 */ | ||
126 | char* 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 |
127 | off_t FAST_FUNC fdlength(int fd) | 162 | off_t FAST_FUNC fdlength(int fd) |
diff --git a/networking/interface.c b/networking/interface.c index b59a61de4..a59f310a6 100644 --- a/networking/interface.c +++ b/networking/interface.c | |||
@@ -30,7 +30,6 @@ | |||
30 | * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu | 30 | * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu |
31 | * (default AF was wrong) | 31 | * (default AF was wrong) |
32 | */ | 32 | */ |
33 | |||
34 | #include <net/if.h> | 33 | #include <net/if.h> |
35 | #include <net/if_arp.h> | 34 | #include <net/if_arp.h> |
36 | #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION) | 35 | #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION) |
@@ -1215,53 +1214,12 @@ static int if_print(char *ifname) | |||
1215 | /* Input an Infiniband address and convert to binary. */ | 1214 | /* Input an Infiniband address and convert to binary. */ |
1216 | int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap) | 1215 | int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap) |
1217 | { | 1216 | { |
1218 | unsigned char *ptr; | ||
1219 | char c; | ||
1220 | const char *orig; | ||
1221 | int i; | ||
1222 | unsigned val; | ||
1223 | |||
1224 | sap->sa_family = ib_hwtype.type; | 1217 | sap->sa_family = ib_hwtype.type; |
1225 | ptr = (unsigned char *) sap->sa_data; | 1218 | //TODO: error check? |
1226 | 1219 | hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN); | |
1227 | i = 0; | 1220 | # ifdef HWIB_DEBUG |
1228 | orig = bufp; | 1221 | fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data)); |
1229 | while ((*bufp != '\0') && (i < INFINIBAND_ALEN)) { | 1222 | # endif |
1230 | val = 0; | ||
1231 | c = *bufp++; | ||
1232 | if (isdigit(c)) | ||
1233 | val = c - '0'; | ||
1234 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') | ||
1235 | val = (c|0x20) - ('a' - 10); | ||
1236 | else { | ||
1237 | errno = EINVAL; | ||
1238 | return -1; | ||
1239 | } | ||
1240 | val <<= 4; | ||
1241 | c = *bufp; | ||
1242 | if (isdigit(c)) | ||
1243 | val |= c - '0'; | ||
1244 | else if ((c|0x20) >= 'a' && (c|0x20) <= 'f') | ||
1245 | val |= (c|0x20) - ('a' - 10); | ||
1246 | else if (c == ':' || c == '\0') | ||
1247 | val >>= 4; | ||
1248 | else { | ||
1249 | errno = EINVAL; | ||
1250 | return -1; | ||
1251 | } | ||
1252 | if (c != '\0') | ||
1253 | bufp++; | ||
1254 | *ptr++ = (unsigned char) (val & 0377); | ||
1255 | i++; | ||
1256 | |||
1257 | /* We might get a semicolon here - not required. */ | ||
1258 | if (*bufp == ':') { | ||
1259 | bufp++; | ||
1260 | } | ||
1261 | } | ||
1262 | #ifdef DEBUG | ||
1263 | fprintf(stderr, "in_ib(%s): %s\n", orig, UNSPEC_print(sap->sa_data)); | ||
1264 | #endif | ||
1265 | return 0; | 1223 | return 0; |
1266 | } | 1224 | } |
1267 | #endif | 1225 | #endif |
diff --git a/networking/wget.c b/networking/wget.c index 5b73b933b..97f4a8f6e 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -282,8 +282,10 @@ static char *gethdr(char *buf, size_t bufsiz, FILE *fp /*, int *istrunc*/) | |||
282 | return NULL; | 282 | return NULL; |
283 | 283 | ||
284 | /* convert the header name to lower case */ | 284 | /* convert the header name to lower case */ |
285 | for (s = buf; isalnum(*s) || *s == '-' || *s == '.'; ++s) | 285 | for (s = buf; isalnum(*s) || *s == '-' || *s == '.'; ++s) { |
286 | *s = tolower(*s); | 286 | /* tolower for "A-Z", no-op for "0-9a-z-." */ |
287 | *s = (*s | 0x20); | ||
288 | } | ||
287 | 289 | ||
288 | /* verify we are at the end of the header name */ | 290 | /* verify we are at the end of the header name */ |
289 | if (*s != ':') | 291 | if (*s != ':') |