diff options
Diffstat (limited to 'src/microhttpd/mhd_str.c')
-rw-r--r-- | src/microhttpd/mhd_str.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c index 60c198bc..1d4b9257 100644 --- a/src/microhttpd/mhd_str.c +++ b/src/microhttpd/mhd_str.c | |||
@@ -32,6 +32,7 @@ | |||
32 | 32 | ||
33 | #include "mhd_assert.h" | 33 | #include "mhd_assert.h" |
34 | #include "mhd_limits.h" | 34 | #include "mhd_limits.h" |
35 | #include "mhd_assert.h" | ||
35 | 36 | ||
36 | #ifdef MHD_FAVOR_SMALL_CODE | 37 | #ifdef MHD_FAVOR_SMALL_CODE |
37 | #ifdef _MHD_static_inline | 38 | #ifdef _MHD_static_inline |
@@ -1184,3 +1185,70 @@ MHD_str_to_uvalue_n_ (const char *str, | |||
1184 | 1185 | ||
1185 | 1186 | ||
1186 | #endif /* MHD_FAVOR_SMALL_CODE */ | 1187 | #endif /* MHD_FAVOR_SMALL_CODE */ |
1188 | |||
1189 | |||
1190 | size_t | ||
1191 | MHD_uint32_to_strx (uint32_t val, | ||
1192 | char *buf, | ||
1193 | size_t buf_size) | ||
1194 | { | ||
1195 | char *chr; /**< pointer to the current printed digit */ | ||
1196 | int digit_pos = 7; /** zero-based, digit position in @a 'val' */ | ||
1197 | int digit; | ||
1198 | |||
1199 | chr = buf; | ||
1200 | digit = (int) (((val) >> (4 * digit_pos)) & 0xf); | ||
1201 | |||
1202 | /* Skip leading zeros */ | ||
1203 | while ((0 == digit) && (0 != digit_pos)) | ||
1204 | digit = (int) (((val) >> (4 * --digit_pos)) & 0xf); | ||
1205 | |||
1206 | while (0 != buf_size) | ||
1207 | { | ||
1208 | *chr = (digit <= 9) ? ('0' + (char) digit) : ('A' + (char) digit - 10); | ||
1209 | chr++; | ||
1210 | buf_size--; | ||
1211 | if (0 == digit_pos) | ||
1212 | return (size_t) (chr - buf); | ||
1213 | digit = (int) (((val) >> (4 * --digit_pos)) & 0xf); | ||
1214 | } | ||
1215 | return 0; /* The buffer is too small */ | ||
1216 | } | ||
1217 | |||
1218 | |||
1219 | size_t | ||
1220 | MHD_uint16_to_str (uint16_t val, | ||
1221 | char *buf, | ||
1222 | size_t buf_size) | ||
1223 | { | ||
1224 | char *chr; /**< pointer to the current printed digit */ | ||
1225 | /* The biggest printable number is 65535 */ | ||
1226 | uint16_t divisor = UINT16_C (10000); | ||
1227 | int digit; | ||
1228 | |||
1229 | chr = buf; | ||
1230 | digit = (int) (val / divisor); | ||
1231 | mhd_assert (digit < 10); | ||
1232 | |||
1233 | /* Do not print leading zeros */ | ||
1234 | while ((0 == digit) && (1 < divisor)) | ||
1235 | { | ||
1236 | divisor /= 10; | ||
1237 | digit = (int) (val / divisor); | ||
1238 | mhd_assert (digit < 10); | ||
1239 | } | ||
1240 | |||
1241 | while (0 != buf_size) | ||
1242 | { | ||
1243 | *chr = (char) digit + '0'; | ||
1244 | chr++; | ||
1245 | buf_size--; | ||
1246 | if (1 == divisor) | ||
1247 | return (size_t) (chr - buf); | ||
1248 | val %= divisor; | ||
1249 | divisor /= 10; | ||
1250 | digit = (int) (val / divisor); | ||
1251 | mhd_assert (digit < 10); | ||
1252 | } | ||
1253 | return 0; /* The buffer is too small */ | ||
1254 | } | ||