Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_base64.c
Go to the documentation of this file.
1
9#include "nilorea/n_log.h"
10#include "nilorea/n_list.h"
11#include "nilorea/n_str.h"
12#include "nilorea/n_base64.h"
13
14int main(int argc, char* argv[]) {
16
17 N_STR* example_text = char_to_nstr("This is an example of base64 encode/decode, with a newline:\nThis is the end of the testing test.");
18 example_text->written = 0;
19 while (example_text->data[example_text->written] != '\0') {
20 example_text->written++;
21 }
22
23 n_log(LOG_NOTICE, "Testing base64, encoding text of size (%ld/%ld):\n%s", example_text->written, example_text->length, _nstr(example_text));
24
25 N_STR* encoded_answer = n_base64_encode(example_text);
26 n_log(LOG_DEBUG, "encoded=\n%s", _nstr(encoded_answer));
27
28 N_STR* decoded_answer = n_base64_decode(encoded_answer);
29 n_log(LOG_DEBUG, "decoded=\n%s", _nstr(decoded_answer));
30
31 free_nstr(&encoded_answer);
32 free_nstr(&decoded_answer);
33
34 if (argc > 0 && argv[1]) {
35 N_STR* input_data = NULL;
36 N_STR* output_name = NULL;
37 N_STR* output_data = NULL;
38 N_STR* decoded_data = NULL;
39
40 nstrprintf(output_name, "%s.encoded", argv[1]);
41 input_data = file_to_nstr(argv[1]);
42 n_log(LOG_DEBUG, "Encoding file %s size %ld", argv[1], input_data->written);
43
44 if (input_data) {
45 output_data = n_base64_encode(input_data);
46 nstr_to_file(output_data, _nstr(output_name));
47 n_log(LOG_DEBUG, "Generated encoded %s version of size %ld", _nstr(output_name), output_data->written);
48
49 nstrprintf(output_name, "%s.decoded", argv[1]);
50 decoded_data = n_base64_decode(output_data);
51 nstr_to_file(decoded_data, _nstr(output_name));
52
53 n_log(LOG_DEBUG, "Generated decoded %s version of size %ld", _nstr(output_name), decoded_data->written);
54 free_nstr(&decoded_data);
55 }
56 free_nstr(&input_data);
57 free_nstr(&output_name);
58 free_nstr(&output_data);
59 }
60 free_nstr(&example_text);
61 exit(0);
62} /* END_OF_MAIN */
int main(void)
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:180
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:169
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:251
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:104
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
size_t written
size of the written data inside the string
Definition n_str.h:45
char * data
the string
Definition n_str.h:41
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:386
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:228
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:94
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:260
A box including a string and his lenght.
Definition n_str.h:39
Base64 encoding and decoding functions using N_STR.
List structures and definitions.
Generic log system.
N_STR and string function declaration.