-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64.c
91 lines (69 loc) · 2.08 KB
/
base64.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/********************************************************************************************
Base64 encoding implementation based on openSSL
Author: CARSON
Date: 11.04.2006
URL: http://www.ioncannon.net/programming/34/howto-base64-encode-with-cc-and-openssl/
-- EDIT --
Author: Martin Albrecht <[email protected]>
Date: 22.08.2009
URL: http://code.google.com/p/smtpmail
DEPENDENCIES:
-------------
- openSSL-devel (http://www.deanlee.cn/programming/openssl-for-windows/)
DESCRIPTION:
------------
This is a simple base64 encoding implementation,
based on the openSSL library.
I found this code on http://www.ioncannon.net
To compile this code you need the openSSL development
libraries. In Windows I copied all the headers and lib
files into my Dev-Cpp folder and compiled the code with
the command:
gcc base64.c -l libeay32
In Linux/Unix it is:
gcc base64.c -l ssl
WINDOWS NOTE: After compiling, you need the libeay32.dll file in
the directory of your compiled binary!
********************************************************************************************/
#include "fd.h"
/*
Encode a string to base64 and return it
*/
char *b64_encode(char *string, int length)
{
BIO *bmem=NULL, *b64=NULL;
BUF_MEM *bptr=NULL;
char *buff=NULL;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, string, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
buff = malloc(bptr->length);
#ifdef _WIN32
memset(buff,0,sizeof(char*));
memcpy(buff, bptr->data, bptr->length-1);
#else
memset(buff,0,sizeof(char*));
sprintf(buff, "%s", bptr->data);
#endif
buff[bptr->length-1] = '\0';
BIO_free_all(b64);
return buff;
}
/*
Decode a string to plain text and return it
*/
char *b64_decode(char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(strlen(input));
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf(input, length);
bmem = BIO_push(b64, bmem);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}