Use void pointers to opaque buffers.
This commit is contained in:
parent
332b55d472
commit
75e5b2e906
2 changed files with 13 additions and 11 deletions
14
src/utils.c
14
src/utils.c
|
@ -52,14 +52,16 @@ static int charhex2bin(char c) {
|
|||
return toupper(c) - 'A' + 10;
|
||||
}
|
||||
|
||||
int hex2bin(const char *src, char *dst, int length) {
|
||||
int hex2bin(const char *src, void *vdst, int length) {
|
||||
char *dst = vdst;
|
||||
int i;
|
||||
for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
|
||||
dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
|
||||
return i;
|
||||
}
|
||||
|
||||
int bin2hex(const char *src, char *dst, int length) {
|
||||
int bin2hex(const void *vsrc, char *dst, int length) {
|
||||
const char *src = vsrc;
|
||||
for(int i = length - 1; i >= 0; i--) {
|
||||
dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
|
||||
dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
|
||||
|
@ -68,7 +70,7 @@ int bin2hex(const char *src, char *dst, int length) {
|
|||
return length * 2;
|
||||
}
|
||||
|
||||
int b64decode(const char *src, char *dst, int length) {
|
||||
int b64decode(const char *src, void *dst, int length) {
|
||||
int i;
|
||||
uint32_t triplet = 0;
|
||||
unsigned char *udst = (unsigned char *)dst;
|
||||
|
@ -99,7 +101,7 @@ int b64decode(const char *src, char *dst, int length) {
|
|||
}
|
||||
}
|
||||
|
||||
static int b64encode_internal(const char *src, char *dst, int length, const char *alphabet) {
|
||||
static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) {
|
||||
uint32_t triplet;
|
||||
const unsigned char *usrc = (unsigned char *)src;
|
||||
int si = length / 3 * 3;
|
||||
|
@ -140,11 +142,11 @@ static int b64encode_internal(const char *src, char *dst, int length, const char
|
|||
return length;
|
||||
}
|
||||
|
||||
int b64encode(const char *src, char *dst, int length) {
|
||||
int b64encode(const void *src, char *dst, int length) {
|
||||
return b64encode_internal(src, dst, length, base64_original);
|
||||
}
|
||||
|
||||
int b64encode_urlsafe(const char *src, char *dst, int length) {
|
||||
int b64encode_urlsafe(const void *src, char *dst, int length) {
|
||||
return b64encode_internal(src, dst, length, base64_urlsafe);
|
||||
}
|
||||
|
||||
|
|
10
src/utils.h
10
src/utils.h
|
@ -21,12 +21,12 @@
|
|||
#ifndef __TINC_UTILS_H__
|
||||
#define __TINC_UTILS_H__
|
||||
|
||||
extern int hex2bin(const char *src, char *dst, int length);
|
||||
extern int bin2hex(const char *src, char *dst, int length);
|
||||
extern int hex2bin(const char *src, void *dst, int length);
|
||||
extern int bin2hex(const void *src, char *dst, int length);
|
||||
|
||||
extern int b64encode(const char *src, char *dst, int length);
|
||||
extern int b64encode_urlsafe(const char *src, char *dst, int length);
|
||||
extern int b64decode(const char *src, char *dst, int length);
|
||||
extern int b64encode(const void *src, char *dst, int length);
|
||||
extern int b64encode_urlsafe(const void *src, char *dst, int length);
|
||||
extern int b64decode(const char *src, void *dst, int length);
|
||||
|
||||
#ifdef HAVE_MINGW
|
||||
extern const char *winerror(int);
|
||||
|
|
Loading…
Reference in a new issue