2 Commits

Author SHA1 Message Date
ee8aedce91 Add base64_encode() to utils 2022-08-17 12:57:07 +02:00
0648c75baa Refactor code a bit 2022-08-16 22:06:50 +02:00
10 changed files with 36 additions and 0 deletions

View File

@@ -8,6 +8,7 @@
#include "cache.h"
#include "utils.h"
#include "compress.h"
#include <stdio.h>
#include <magic.h>
#include <sys/ipc.h>

View File

@@ -6,6 +6,7 @@
*/
#include "compress.h"
#include <malloc.h>
#include <errno.h>

View File

@@ -7,6 +7,7 @@
#include "config.h"
#include "utils.h"
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>

View File

@@ -9,6 +9,7 @@
#include "utils.h"
#include "compress.h"
#include "../server.h"
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>

View File

@@ -8,6 +8,7 @@
#include "http.h"
#include "utils.h"
#include "compress.h"
#include <string.h>
void http_to_camel_case(char *str, int mode) {

View File

@@ -9,6 +9,7 @@
#include "utils.h"
#include "compress.h"
#include "../server.h"
#include <openssl/ssl.h>
#include <string.h>
#include <errno.h>

View File

@@ -6,6 +6,7 @@
*/
#include "sock.h"
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <string.h>

View File

@@ -7,6 +7,7 @@
#include "uri.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>

View File

@@ -6,6 +6,7 @@
*/
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -172,3 +173,24 @@ int str_trim_lws(char **start, char **end) {
(*end)++;
return 0;
}
int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len) {
unsigned long out_len = 4 * ((data_len + 2) / 3);
if (output_len != NULL) *output_len = out_len;
for (int i = 0, j = 0; i < data_len;) {
unsigned int octet_a = (i < data_len) ? ((unsigned char *) data)[i++] : 0;
unsigned int octet_b = (i < data_len) ? ((unsigned char *) data)[i++] : 0;
unsigned int octet_c = (i < data_len) ? ((unsigned char *) data)[i++] : 0;
unsigned int triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
output[j++] = base64_encode_table[(triple >> 3 * 6) & 0x3F];
output[j++] = base64_encode_table[(triple >> 2 * 6) & 0x3F];
output[j++] = base64_encode_table[(triple >> 1 * 6) & 0x3F];
output[j++] = base64_encode_table[(triple >> 0 * 6) & 0x3F];
}
for (int i = 0; i < base64_mod_table[data_len % 3]; i++)
output[out_len - 1 - i] = '=';
return 0;
}

View File

@@ -20,6 +20,10 @@
extern char *log_prefix;
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int base64_mod_table[3] = {0, 2, 1};
#define out_1(fmt) fprintf(stdout, "%s" fmt "\n", log_prefix)
#define out_2(fmt, args...) fprintf(stdout, "%s" fmt "\n", log_prefix, args)
@@ -46,4 +50,6 @@ int str_trim(char **start, char **end);
int str_trim_lws(char **start, char **end);
int base64_encode(void *data, unsigned long data_len, char *output, unsigned long *output_len);
#endif //NECRONDA_SERVER_UTILS_H