Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_base64.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include "nilorea/n_log.h"
29#include "nilorea/n_list.h"
30#include "nilorea/n_str.h"
31#include "nilorea/n_base64.h"
32
33int main(int argc, char* argv[]) {
35
36 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.");
37 example_text->written = 0;
38 while (example_text->data[example_text->written] != '\0') {
39 example_text->written++;
40 }
41
42 n_log(LOG_NOTICE, "Testing base64, encoding text of size (%ld/%ld):\n%s", example_text->written, example_text->length, _nstr(example_text));
43
44 N_STR* encoded_answer = n_base64_encode(example_text);
45 n_log(LOG_DEBUG, "encoded=\n%s", _nstr(encoded_answer));
46
47 N_STR* decoded_answer = n_base64_decode(encoded_answer);
48 n_log(LOG_DEBUG, "decoded=\n%s", _nstr(decoded_answer));
49
50 free_nstr(&encoded_answer);
51 free_nstr(&decoded_answer);
52
53 if (argc > 1 && argv[1]) {
54 N_STR* input_data = NULL;
55 N_STR* output_name = NULL;
56 N_STR* output_data = NULL;
57 N_STR* decoded_data = NULL;
58
59 nstrprintf(output_name, "%s.encoded", argv[1]);
60 input_data = file_to_nstr(argv[1]);
61
62 if (input_data) {
63 n_log(LOG_DEBUG, "Encoding file %s size %ld", argv[1], input_data->written);
64 output_data = n_base64_encode(input_data);
65 nstr_to_file(output_data, _nstr(output_name));
66 n_log(LOG_DEBUG, "Generated encoded %s version of size %ld", _nstr(output_name), output_data->written);
67
68 nstrprintf(output_name, "%s.decoded", argv[1]);
69 decoded_data = n_base64_decode(output_data);
70 nstr_to_file(decoded_data, _nstr(output_name));
71
72 n_log(LOG_DEBUG, "Generated decoded %s version of size %ld", _nstr(output_name), decoded_data->written);
73 free_nstr(&decoded_data);
74 }
75 free_nstr(&input_data);
76 free_nstr(&output_name);
77 free_nstr(&output_data);
78 }
79 free_nstr(&example_text);
80
81 /* test character classification and conversion helpers */
82 n_log(LOG_INFO, "n_isupper('A'): %d", n_isupper('A'));
83 n_log(LOG_INFO, "n_isupper('a'): %d", n_isupper('a'));
84 n_log(LOG_INFO, "n_islower('z'): %d", n_islower('z'));
85 n_log(LOG_INFO, "n_islower('Z'): %d", n_islower('Z'));
86 n_log(LOG_INFO, "n_isalpha('B'): %d", n_isalpha('B'));
87 n_log(LOG_INFO, "n_isalpha('5'): %d", n_isalpha('5'));
88 n_log(LOG_INFO, "n_toupper('c'): %c", n_toupper('c'));
89 n_log(LOG_INFO, "n_tolower('D'): %c", n_tolower('D'));
90
91 exit(0);
92} /* END_OF_MAIN */
int main(void)
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
Definition n_common.h:199
char n_tolower(char c)
convert char to lower case
Definition n_base64.c:143
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
Definition n_base64.c:188
bool n_isupper(char c)
test if char c is uppercase
Definition n_base64.c:105
char n_toupper(char c)
convert char to upper case
Definition n_base64.c:132
bool n_islower(char c)
test if char c is lowercase
Definition n_base64.c:114
bool n_isalpha(char c)
test if char c is alphabetic
Definition n_base64.c:123
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
Definition n_base64.c:270
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_INFO
informational
Definition n_log.h:82
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
size_t length
total allocation (in bytes) of the data buffer, padding included
Definition n_str.h:65
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
Definition n_str.c:424
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:255
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
Definition n_str.h:117
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
Definition n_str.c:288
A box including a string and his lenght.
Definition n_str.h:61
Base64 encoding and decoding functions using N_STR.
List structures and definitions.
Generic log system.
N_STR and string function declaration.