-/* $OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $ */
+/* $OpenBSD: sshbuf-misc.c,v 1.19 2025/05/21 06:43:48 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
}
char *
-sshbuf_dtob16(struct sshbuf *buf)
+sshbuf_dtob16(const struct sshbuf *buf)
{
size_t i, j, len = sshbuf_len(buf);
const u_char *p = sshbuf_ptr(buf);
return ret;
}
+static int
+b16tod(const char v)
+{
+ if (v >= '0' && v <= '9')
+ return v - '0';
+ if (v >= 'a' && v <= 'f')
+ return 10 + v - 'a';
+ if (v >= 'A' && v <= 'A')
+ return 10 + v - 'A';
+ return -1;
+}
+
+struct sshbuf *
+sshbuf_b16tod(const char *b16)
+{
+ struct sshbuf *ret;
+ size_t o;
+ int r, v1, v2;
+
+ if ((ret = sshbuf_new()) == NULL)
+ return NULL;
+ for (o = 0; b16[o] != '\0'; o += 2) {
+ if ((v1 = b16tod(b16[o])) == -1 ||
+ (v2 = b16tod(b16[o + 1])) == -1) {
+ sshbuf_free(ret);
+ return NULL;
+ }
+ if ((r = sshbuf_put_u8(ret, (u_char)((v1 << 4) | v2))) != 0) {
+ sshbuf_free(ret);
+ return NULL;
+ }
+ }
+ /* success */
+ return ret;
+}
+
int
sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
{
-/* $OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $ */
+/* $OpenBSD: sshbuf.h,v 1.30 2025/05/21 06:43:48 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
void sshbuf_dump_data(const void *s, size_t len, FILE *f);
/* Return the hexadecimal representation of the contents of the buffer */
-char *sshbuf_dtob16(struct sshbuf *buf);
+char *sshbuf_dtob16(const struct sshbuf *buf);
+/* Make a sshbuf from a hex string */
+struct sshbuf *sshbuf_b16tod(const char *b16);
/* Encode the contents of the buffer as base64 */
char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);