Generates a lower case ASCII hex output from a binary input.
Closes #11990
{
free(p);
}
+
+/*
+ * Curl_hexencode()
+ *
+ * Converts binary input to lowercase hex-encoded ASCII output.
+ * Null-terminated.
+ */
+void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
+ unsigned char *out, size_t olen) /* output buffer size */
+{
+ const char *hex = "0123456789abcdef";
+ DEBUGASSERT(src && len && (olen >= 3));
+ if(src && len && (olen >= 3)) {
+ while(len-- && (olen >= 3)) {
+ /* clang-tidy warns on this line without this comment: */
+ /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
+ *out++ = hex[(*src & 0xF0)>>4];
+ *out++ = hex[*src & 0x0F];
+ ++src;
+ olen -= 2;
+ }
+ *out = 0;
+ }
+ else if(olen)
+ *out = 0;
+}
char **ostring, size_t *olen,
enum urlreject ctrl);
+void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
+ unsigned char *out, size_t olen); /* output buffer size */
+
#endif /* HEADER_CURL_ESCAPE_H */
#include "transfer.h"
#include "parsedate.h"
#include "sendf.h"
+#include "escape.h"
#include <time.h>
static void sha256_to_hex(char *dst, unsigned char *sha)
{
- int i;
-
- for(i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
- msnprintf(dst + (i * 2), SHA256_HEX_LENGTH - (i * 2), "%02x", sha[i]);
- }
+ Curl_hexencode(sha, SHA256_DIGEST_LENGTH,
+ (unsigned char *)dst, SHA256_HEX_LENGTH);
}
static char *find_date_hdr(struct Curl_easy *data, const char *sig_hdr)
#include "sendf.h"
#include "timeval.h"
#include "rand.h"
+#include "escape.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
size_t num)
{
CURLcode result = CURLE_BAD_FUNCTION_ARGUMENT;
- const char *hex = "0123456789abcdef";
unsigned char buffer[128];
- unsigned char *bufp = buffer;
DEBUGASSERT(num > 1);
#ifdef __clang_analyzer__
if(result)
return result;
- while(num) {
- /* clang-tidy warns on this line without this comment: */
- /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
- *rnd++ = hex[(*bufp & 0xF0)>>4];
- *rnd++ = hex[*bufp & 0x0F];
- bufp++;
- num -= 2;
- }
- *rnd = 0;
-
+ Curl_hexencode(buffer, num/2, rnd, num + 1);
return result;
}