Skip to content
Snippets Groups Projects
Commit 9a748595 authored by CAPACES Nicolas's avatar CAPACES Nicolas
Browse files

ajout librairie

parent be0a4bda
No related branches found
No related tags found
No related merge requests found
base64.c 0 → 100644
/*****************************************
* Nom: base64.c *
* Auteur: Nicolas CAPACES *
* Date de Creation: 19/05/2024 *
* Date de Modification: 20/05/2024 *
*****************************************/
#include <sys/types.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <base64.h>
#include <stdio.h>
const char pad = '=';
size_t base64_encoded_size(size_t binlen){
size_t ret;
ret = binlen;
if (binlen % 3 != 0)
ret += 3 - (binlen % 3);
ret /= 3;
ret *= 4;
return ret;
}
int bin_to_base64(const char* src, size_t len_src, char* dst, size_t len_dst, enum64 charset){
size_t i;
size_t j;
size_t v;
char* used_set;
if (src == NULL || len_src == 0 || len_dst == 0)
return -1;
if (charset == STANDARD){
used_set="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
} else if (charset == URL){
used_set= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
} else {
return -1;
}
dst = malloc(len_dst);
dst[len_dst] = '\0';
for (i=0, j=0; i<len_src; i+=3, j+=4) {
v = src[i];
v = i+1 < len_src ? v << 8 | src[i+1] : v << 8;
v = i+2 < len_src ? v << 8 | src[i+2] : v << 8;
dst[j] = used_set[(v >> 18) & 0x3F];
dst[j+1] = used_set[(v >> 12) & 0x3F];
if (i+1 < len_src) {
dst[j+2] = used_set[(v >> 6) & 0x3F];
} else {
dst[j+2] = pad;
}
if (i+2 < len_src) {
dst[j+3] = used_set[v & 0x3F];
} else {
dst[j+3] = pad;
}
}
return 0;
}
base64.h 0 → 100644
/*****************************************
* Nom: base64.h *
* Auteur: Nicolas CAPACES *
* Date de Creation: 19/05/2024 *
* Date de Modification: 20/05/2024 *
*****************************************/
#ifndef BASE_64_H_
#define BASE_64_H_
#include <sys/types.h>
#include <stdint.h>
extern const char standb64[];
extern const char URLb64[];
extern const char pad;
typedef enum {
STANDARD,
URL
}enum64;
//TODO: implementer le décodage
extern int bin_to_base64(const char* src, size_t len_src, char* dst, size_t len_dst, enum64 charset);
//extern int base64_to_bin(const char* src, size_t len_src, char* dst, size_t len_dst, enum64 charset);
extern size_t base64_encoded_size(size_t binlen);
//extern size_t base64_decoded_size(size_t base64len);
#endif /*base64.h*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment