[libs] Implement Update MD5

This commit is contained in:
Kuba Szczodrzyński
2023-08-29 19:19:28 +02:00
parent 631ef6ba59
commit 1ac3d30d84
7 changed files with 116 additions and 20 deletions

View File

@@ -39,3 +39,36 @@ void hexdump(const uint8_t *buf, size_t len, uint32_t offset, uint8_t width) {
pos += lineWidth;
}
}
char *lt_btox(const uint8_t *src, int len, char *dest) {
// https://stackoverflow.com/a/53966346
const char hex[] = "0123456789abcdef";
len *= 2;
dest[len] = '\0';
while (--len >= 0)
dest[len] = hex[(src[len >> 1] >> ((1 - (len & 1)) << 2)) & 0xF];
return dest;
}
uint8_t *lt_xtob(const char *src, int len, uint8_t *dest) {
// https://gist.github.com/vi/dd3b5569af8a26b97c8e20ae06e804cb
// mapping of ASCII characters to hex values
// (16-byte swapped to reduce XOR 0x10 operation)
const uint8_t mapping[] = {
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
};
int j = 0;
uint8_t idx0;
uint8_t idx1;
for (int i = 0; i < len; i += 2) {
idx0 = ((uint8_t)src[i + 0] & 0x1F);
idx1 = ((uint8_t)src[i + 1] & 0x1F);
dest[j++] = (mapping[idx0] << 4) | (mapping[idx1] << 0);
}
return dest;
}

View File

@@ -45,3 +45,23 @@ void hexdump(
uint8_t width
#endif
);
/**
* @brief Convert a byte array to hexadecimal string.
*
* @param src source byte array
* @param len source length (bytes)
* @param dest destination string
* @return destination string
*/
char *lt_btox(const uint8_t *src, int len, char *dest);
/**
* @brief Convert a hexadecimal string to byte array.
*
* @param src source string
* @param len source length (chars)
* @param dest destination byte array
* @return destination byte array
*/
uint8_t *lt_xtob(const char *src, int len, uint8_t *dest);