From 9befdf1d2072f3366086583462332cf6f2bc1540 Mon Sep 17 00:00:00 2001 From: "Dr. Stephen Henson" Date: Mon, 10 Apr 2006 00:11:30 +0000 Subject: [PATCH] New utility function to reverse a buffer, either by copying or in-place. --- crypto/buffer/buffer.c | 23 +++++++++++++++++++++++ crypto/buffer/buffer.h | 1 + 2 files changed, 24 insertions(+) diff --git a/crypto/buffer/buffer.c b/crypto/buffer/buffer.c index 3bf03c7eff..a7fe5ba54f 100644 --- a/crypto/buffer/buffer.c +++ b/crypto/buffer/buffer.c @@ -219,3 +219,26 @@ size_t BUF_strlcat(char *dst, const char *src, size_t size) l++; return l + BUF_strlcpy(dst, src, size); } + +void BUF_reverse(unsigned char *out, unsigned char *in, size_t size) + { + size_t i; + if (in) + { + out += size - 1; + for (i = 0; i < size; i++) + *in++ = *out--; + } + else + { + unsigned char *q; + char c; + q = out + size - 1; + for (i = 0; i < size/2; i++) + { + c = *q; + *q-- = *out; + *out++ = c; + } + } + } diff --git a/crypto/buffer/buffer.h b/crypto/buffer/buffer.h index 1db9607450..f7acca56ed 100644 --- a/crypto/buffer/buffer.h +++ b/crypto/buffer/buffer.h @@ -88,6 +88,7 @@ int BUF_MEM_grow_clean(BUF_MEM *str, int len); char * BUF_strdup(const char *str); char * BUF_strndup(const char *str, size_t siz); void * BUF_memdup(const void *data, size_t siz); +void BUF_reverse(unsigned char *out, unsigned char *in, size_t siz); /* safe string functions */ size_t BUF_strlcpy(char *dst,const char *src,size_t siz); -- 2.25.1