Update base64 compat.

This commit is contained in:
Nicholas Marriott
2026-02-26 10:55:27 +00:00
parent 0800e51d41
commit 478eaea982
2 changed files with 15 additions and 21 deletions

View File

@@ -388,7 +388,7 @@ int clock_gettime(int, struct timespec *);
/* base64.c */ /* base64.c */
#undef b64_ntop #undef b64_ntop
#undef b64_pton #undef b64_pton
int b64_ntop(const char *, size_t, char *, size_t); int b64_ntop(const u_char *, size_t, char *, size_t);
int b64_pton(const char *, u_char *, size_t); int b64_pton(const char *, u_char *, size_t);
#endif #endif

View File

@@ -1,4 +1,4 @@
/* $OpenBSD: base64.c,v 1.8 2015/01/16 16:48:51 deraadt Exp $ */ /* $OpenBSD: base64.c,v 1.15 2021/10/25 14:41:09 jca Exp $ */
/* /*
* Copyright (c) 1996 by Internet Software Consortium. * Copyright (c) 1996 by Internet Software Consortium.
@@ -46,15 +46,15 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <arpa/nameser.h>
#include <ctype.h> #include <ctype.h>
#include <resolv.h> #include <resolv.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "compat.h"
static const char Base64[] = static const char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char Pad64 = '='; static const char Pad64 = '=';
@@ -107,9 +107,9 @@ static const char Pad64 = '=';
end of the data is performed using the '=' character. end of the data is performed using the '=' character.
Since all base64 input is an integral number of octets, only the Since all base64 input is an integral number of octets, only the
------------------------------------------------- -------------------------------------------------
following cases can arise: following cases can arise:
(1) the final quantum of encoding input is an integral (1) the final quantum of encoding input is an integral
multiple of 24 bits; here, the final unit of encoded multiple of 24 bits; here, the final unit of encoded
output will be an integral multiple of 4 characters output will be an integral multiple of 4 characters
@@ -123,15 +123,12 @@ static const char Pad64 = '=';
*/ */
int int
b64_ntop(src, srclength, target, targsize) b64_ntop(unsigned char const *src, size_t srclength, char *target,
u_char const *src; size_t targsize)
size_t srclength;
char *target;
size_t targsize;
{ {
size_t datalength = 0; size_t datalength = 0;
u_char input[3]; unsigned char input[3];
u_char output[4]; unsigned char output[4];
int i; int i;
while (2 < srclength) { while (2 < srclength) {
@@ -152,14 +149,14 @@ b64_ntop(src, srclength, target, targsize)
target[datalength++] = Base64[output[2]]; target[datalength++] = Base64[output[2]];
target[datalength++] = Base64[output[3]]; target[datalength++] = Base64[output[3]];
} }
/* Now we worry about padding. */ /* Now we worry about padding. */
if (0 != srclength) { if (0 != srclength) {
/* Get what's left. */ /* Get what's left. */
input[0] = input[1] = input[2] = '\0'; input[0] = input[1] = input[2] = '\0';
for (i = 0; i < srclength; i++) for (i = 0; i < srclength; i++)
input[i] = *src++; input[i] = *src++;
output[0] = input[0] >> 2; output[0] = input[0] >> 2;
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
@@ -187,13 +184,10 @@ b64_ntop(src, srclength, target, targsize)
*/ */
int int
b64_pton(src, target, targsize) b64_pton(char const *src, unsigned char *target, size_t targsize)
char const *src;
u_char *target;
size_t targsize;
{ {
int tarindex, state, ch; int tarindex, state, ch;
u_char nextbyte; unsigned char nextbyte;
char *pos; char *pos;
state = 0; state = 0;
@@ -207,7 +201,7 @@ b64_pton(src, target, targsize)
break; break;
pos = strchr(Base64, ch); pos = strchr(Base64, ch);
if (pos == 0) /* A non-base64 character. */ if (pos == 0) /* A non-base64 character. */
return (-1); return (-1);
switch (state) { switch (state) {