Browse Source

Allow `base16_encode()` to re-use the input buffer as output

Signed-off-by: Steffen Jaeckel <[email protected]>
Steffen Jaeckel 3 months ago
parent
commit
19a5520936
1 changed files with 4 additions and 3 deletions
  1. 4 3
      src/misc/base16/base16_encode.c

+ 4 - 3
src/misc/base16/base16_encode.c

@@ -52,10 +52,11 @@ int base16_encode(const unsigned char *in,  unsigned long  inlen,
       alphabet = alphabets[1];
    }
 
-   for (i = 0; i < x; i += 2) {
-      out[i]   = alphabet[(in[i/2] >> 4) & 0x0f];
-      out[i+1] = alphabet[in[i/2] & 0x0f];
+   for (i = x; i > 0; i -= 2) {
+      out[i-2] = alphabet[(in[(i-1)/2] >> 4) & 0x0f];
+      out[i-1] = alphabet[in[(i-1)/2] & 0x0f];
    }
+
    out[x] = '\0';
 
    return CRYPT_OK;