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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
/* Compilation: gcc -std=gnu99 main.c -o b64 -lm */
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
/* Mais pourquoi diable ne pas se faciliter la vie avec un tableau basique, ici ??? */
/* De plus, quand ce tableau est-il libéré ? */
static char decoding_table[256];
static int decoding_table_initialized = 0;
static int mod_table[] = {0, 2, 1};
static void build_decoding_table()
{
int i;
for (i = 0; i < 0x40; i++)
{
decoding_table[encoding_table[i]] = i;
}
decoding_table_initialized = 1;
}
static char *base64_encode(const char *data, size_t *output_length)
{
size_t input_length = strlen(data);
char *encoded_data;
int i, j;
*output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
encoded_data = malloc((*output_length + 1) * sizeof(char)); /* + 1 pour le '\0' final ! */
if (encoded_data == NULL)
{
return NULL;
}
for (i = 0, j = 0; i < input_length;)
{
uint32_t octet_a = i < input_length ? data[i++] : 0;
uint32_t octet_b = i < input_length ? data[i++] : 0;
uint32_t octet_c = i < input_length ? data[i++] : 0;
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (i = 0; i < mod_table[input_length % 3]; i++)
{
encoded_data[*output_length - 1 - i] = '=';
}
/* Ne pas oublier le '\0' final ! Sinon, strlen ne fonctionnera pas sur cette "chaîne" ! */
encoded_data[*output_length] = '\0';
return encoded_data;
}
static char *base64_decode(const char *data, size_t *output_length)
{
size_t input_length = strlen(data);
int i, j;
if (!decoding_table_initialized)
{
build_decoding_table();
}
if (input_length % 4 != 0)
{
return NULL;
}
*output_length = input_length / 4 * 3;
for (i = 0; i < input_length && data[input_length - i] == '='; i++)
{
(*output_length)--;
}
char *decoded_data = malloc((*output_length + 1) * sizeof(char)); /* Encore le '\0' final ! */
if (decoded_data == NULL)
{
return NULL;
}
for (i = 0, j = 0; i < input_length;)
{
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) + (sextet_c << 1 * 6) + (sextet_d << 0 * 6);
/* Ici, cest la même chose que j < *output_length, mais autant toujours jouer la sécurité ! */
if (j + 2 < *output_length)
{
decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}
}
/* Ne pas oublier le '\0' final ! Sinon, strlen ne fonctionnera pas sur cette "chaîne" ! */
decoded_data[*output_length] = '\0';
return decoded_data;
}
/* La taille de block dallocation de notre buffer de lecture... */
#define BUFF_BLOCK 1024
int main(int argc, char *files[])
{
/* Size_in est la taille exacte de lentrée, allocated est la taille allouée pour cette entrée... */
size_t size_out, size_in = 0, allocated = 0;
char *tmp_code = NULL;
/* Pas de déclaration de variable dans le code, ce nest plus C-correcte, et un compilateur gcc récent
* va normalement râler , sinon ! */
int i;
/* Pourquoi allouer un seul char ? on a quand même un peu mémoire, sur nos machines modernes ! */
/* Par principe, jutilise *toujours* sizeof(type), ça permet de mieux savoir à quoi on joue,
* et rend le code plus portable ! */
tmp_code = malloc(BUFF_BLOCK * sizeof(char));
if (!tmp_code)
{
printf("Error allocating some memory for the reading buffer, aborting!\n");
return -1;
}
allocated += BUFF_BLOCK;
/* Pas besoin dun strcpy pour ça ! */
/* strcpy (tmp_code, ""); */
tmp_code[0] = '\0';
for (i = 1; files[i]; i++)
{
FILE *file;
printf("Ouverture du fichier: %s\n", files[i]);
if((file = fopen(files[i], "r")) != NULL)
{
int ic;
/* Autant utiliser for... */
for(ic = fgetc(file); ic != EOF; ic = fgetc(file))
{
/* Si nécessaire, on alloue un nouveau bloc de mémoire. */
if (1 && size_in >= allocated)
{
char *tc;
allocated += BUFF_BLOCK;
tc = realloc(tmp_code, allocated);
if (!tc)
{
printf("Error reallocating some memory for the reading buffer, aborting!\n");
free(tmp_code);
return -1;
}
tmp_code = tc;
}
/* Mais pourquoi diantre utiliser malloc pour un seul char ??? */
/* Suffit de (re)convertir ic en char, et de lassigner à lindice courant ! */
tmp_code[size_in] = (char)ic;
/* Ici aussi, on pourrait "emboîter" dans linstruction précédente, mais ces effets de bords
* ne facilitent pas la lecture et peuvent être source de problèmes... */
/* De toute façon, le compilateur est parfaitement capable doptimiser ça comme un grand ! */
size_in++;
}
fclose(file);
}
else
{
printf("Impossible d'ouvrir le fichier: %s !\n", files[i]);
}
}
/* On rajoute nous-même le NULL final ! */
/* Si nécessaire, on alloue un nouveau bloc de mémoire. */
if (size_in >= allocated)
{
allocated += BUFF_BLOCK;
tmp_code = realloc(tmp_code, allocated);
/* Idéalement, il faudrait tester que tmp_code est toujours valable... */
}
tmp_code[size_in] = '\0';
size_in++;
printf("Le texte concatene est:\n%s\n", tmp_code);
char *encoded = base64_encode(tmp_code, &size_out);
free(tmp_code);
if (encoded == NULL)
{
printf("Impossible d'encoder !\n");
}
else
{
printf("Codage:\n%s\n", encoded);
char *decoded = base64_decode(encoded, &size_out);
if (decoded == NULL)
{
printf("Impossible de decoder !\n");
}
else
{
printf("Decodage:\n%s\n", decoded);
free(encoded);
}
free(decoded);
}
} |
Partager