Skip to content

Commit

Permalink
b64_encode: Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed May 9, 2019
1 parent 94a059d commit 3ea7687
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 190 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Expand Up @@ -12,7 +12,7 @@ find_package(Libev REQUIRED)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/buffer ${CMAKE_CURRENT_BINARY_DIR} ${LIBEV_INCLUDE_DIR})

set(EXTRA_LIBS ${LIBEV_LIBRARY} dl)
set(SOURCE_FILES uwsc.c log.c utils.c buffer/buffer.c sha1.c base64.c ssl.c)
set(SOURCE_FILES uwsc.c log.c utils.c buffer/buffer.c sha1.c ssl.c)

set(UWSC_SSL_SUPPORT_CONFIG 1)
option(UWSC_SSL_SUPPORT "SSL support" ON)
Expand Down
155 changes: 0 additions & 155 deletions src/base64.c

This file was deleted.

33 changes: 0 additions & 33 deletions src/base64.h

This file was deleted.

47 changes: 47 additions & 0 deletions src/utils.c
Expand Up @@ -161,3 +161,50 @@ int tcp_connect(const char *host, int port, int flags, bool *inprogress, int *ea
freeaddrinfo(result);
return sock;
}

/* reference from https://tools.ietf.org/html/rfc4648#section-4 */
int b64_encode(const void *src, size_t srclen, void *dest, size_t destsize)
{
char *Base64 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const uint8_t *input = src;
char *output = dest;

while (srclen > 0) {
int i0 = input[0] >> 2;
int skip = 1;

if (destsize < 5)
return -1;

*output++ = Base64[i0];

if (srclen > 1) {
int i1 = ((input[0] & 0x3) << 4) + (input[1] >> 4);
*output++ = Base64[i1];
if (srclen > 2) {
int i2 = ((input[1] & 0xF) << 2) + (input[2] >> 6);
int i3 = input[2] & 0x3F;
*output++ = Base64[i2];
*output++ = Base64[i3];
skip = 3;
} else {
*output++ = Base64[i1];
*output++ = '=';
skip = 2;
}
} else {
int i1 = (input[0] & 0x3) << 4;
*output++ = Base64[i1];
*output++ = '=';
*output++ = '=';
}

input += skip;
srclen -= skip;
destsize -= 4;
}

*output++ = 0;
return output - (char *)dest - 1;
}
2 changes: 2 additions & 0 deletions src/utils.h
Expand Up @@ -45,4 +45,6 @@ int parse_url(const char *url, char *host, int host_len,

int tcp_connect(const char *host, int port, int flags, bool *inprogress, int *eai);

int b64_encode(const void *src, size_t srclen, void *dest, size_t destsize);

#endif
1 change: 0 additions & 1 deletion src/uwsc.c
Expand Up @@ -35,7 +35,6 @@
#include "uwsc.h"
#include "sha1.h"
#include "utils.h"
#include "base64.h"

static void uwsc_free(struct uwsc_client *cl)
{
Expand Down

0 comments on commit 3ea7687

Please sign in to comment.