Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_base64.c

Nilorea Library crypto base64 api test.

Nilorea Library crypto base64 api test

Author
Castagnier Mickael
Version
1.0
Date
26/05/2015
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include "nilorea/n_list.h"
#include "nilorea/n_str.h"
int main(int argc, char* argv[]) {
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.");
example_text->written = 0;
while (example_text->data[example_text->written] != '\0') {
example_text->written++;
}
n_log(LOG_NOTICE, "Testing base64, encoding text of size (%ld/%ld):\n%s", example_text->written, example_text->length, _nstr(example_text));
N_STR* encoded_answer = n_base64_encode(example_text);
n_log(LOG_DEBUG, "encoded=\n%s", _nstr(encoded_answer));
N_STR* decoded_answer = n_base64_decode(encoded_answer);
n_log(LOG_DEBUG, "decoded=\n%s", _nstr(decoded_answer));
free_nstr(&encoded_answer);
free_nstr(&decoded_answer);
if (argc > 1 && argv[1]) {
N_STR* input_data = NULL;
N_STR* output_name = NULL;
N_STR* output_data = NULL;
N_STR* decoded_data = NULL;
nstrprintf(output_name, "%s.encoded", argv[1]);
input_data = file_to_nstr(argv[1]);
if (input_data) {
n_log(LOG_DEBUG, "Encoding file %s size %ld", argv[1], input_data->written);
output_data = n_base64_encode(input_data);
nstr_to_file(output_data, _nstr(output_name));
n_log(LOG_DEBUG, "Generated encoded %s version of size %ld", _nstr(output_name), output_data->written);
nstrprintf(output_name, "%s.decoded", argv[1]);
decoded_data = n_base64_decode(output_data);
nstr_to_file(decoded_data, _nstr(output_name));
n_log(LOG_DEBUG, "Generated decoded %s version of size %ld", _nstr(output_name), decoded_data->written);
free_nstr(&decoded_data);
}
free_nstr(&input_data);
free_nstr(&output_name);
free_nstr(&output_data);
}
free_nstr(&example_text);
/* test character classification and conversion helpers */
n_log(LOG_INFO, "n_isupper('A'): %d", n_isupper('A'));
n_log(LOG_INFO, "n_isupper('a'): %d", n_isupper('a'));
n_log(LOG_INFO, "n_islower('z'): %d", n_islower('z'));
n_log(LOG_INFO, "n_islower('Z'): %d", n_islower('Z'));
n_log(LOG_INFO, "n_isalpha('B'): %d", n_isalpha('B'));
n_log(LOG_INFO, "n_isalpha('5'): %d", n_isalpha('5'));
n_log(LOG_INFO, "n_toupper('c'): %c", n_toupper('c'));
n_log(LOG_INFO, "n_tolower('D'): %c", n_tolower('D'));
exit(0);
} /* END_OF_MAIN */
int main(void)
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:198
char n_tolower(char c)
convert char to lower case
Definition n_base64.c:142
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:187
bool n_isupper(char c)
test if char c is uppercase
Definition n_base64.c:104
char n_toupper(char c)
convert char to upper case
Definition n_base64.c:131
bool n_islower(char c)
test if char c is lowercase
Definition n_base64.c:113
bool n_isalpha(char c)
test if char c is alphabetic
Definition n_base64.c:122
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:269
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_INFO
informational
Definition n_log.h:81
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:416
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:115
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:286
A box including a string and his lenght.
Definition n_str.h:60
Base64 encoding and decoding functions using N_STR.
List structures and definitions.
Generic log system.
N_STR and string function declaration.