Base64 Encoding implementation in C Programming Language

cb64

Base64 Encoding implementation in C Programming Language

cb64 CI

Spec: https://datatracker.ietf.org/doc/html/rfc4648#page-5

Header only

So just copy cb64.h to your project, and you ready to go

Building Example

$ mkdir build
$ cd build/
$ cmake ..
$ make
$ cd ..

Run example binary

$ ./bin/cb64 ./testdata/octocat.png

Usage

Encoding

Encode and Decode Text

#include <stdio.h>
#include <string.h>
#include "cb64.h"

#define EXIT_ERR(msg) { \
        printf("%s\n", msg); \
        exit(-1); \
    }

int get_file_size(size_t* size, FILE* f);

int main(int argc, char** argv) {

    unsigned char* input = "wur0";
    unsigned char* dst = NULL;
    size_t dst_size = 0;
    int encode_ok = encode_b64(input, 0, &dst, &dst_size);
    if (encode_ok != 0)
        EXIT_ERR("error encode text to base64");

    printf("%s\n", dst);
    printf("%lu\n", dst_size);

    unsigned char* dst_decode = NULL;
    size_t dst_size_decode = 0;
    int decode_ok = decode_b64(dst, 0, &dst_decode, &dst_size_decode);
    if (decode_ok != 0)
        EXIT_ERR("error decode text to base64");

    printf("%s\n", dst_decode);
    printf("%lu\n", dst_size_decode);


    free(dst);
    free(dst_decode);
    return 0;
}

Encode File

#include <stdio.h>
#include <string.h>
#include "cb64.h"

#define EXIT_ERR(msg) { \
        printf("%s\n", msg); \
        exit(-1); \
    }

int get_file_size(size_t* size, FILE* f);

int main(int argc, char** argv) 
{

    // encode file to base64

    if (argc < 2)
    {
        EXIT_ERR("required filename");
    }

    char* filename = argv[1];
    char* filename_out = "../testdata/out.txt";

    printf("filename : %s\n", filename);

    FILE* file;
    FILE* file_out;

    file = fopen(filename, "rb");
    if (file == NULL)
    {
        EXIT_ERR("file not found");
    }

    file_out = fopen(filename_out, "wb");

    size_t file_size;
    if (get_file_size(&file_size, file) != 0)
        EXIT_ERR("error get file size");

    printf("%lu\n", file_size);

    unsigned char input[file_size+1];
    unsigned char* dst = NULL;
    size_t dst_size = 0;

    fread(input, sizeof(char), file_size, file);
    input[file_size] = '\0';

    int encode_ok = encode_b64(input, file_size, &dst, &dst_size);
    if (encode_ok != 0)
        EXIT_ERR("error encode text to base64");

    // printf("%s\n", dst);
    fwrite(dst, 1, dst_size, file_out);

    fclose(file);
    fclose(file_out);
    free(dst);

    return 0;
}

int get_file_size(size_t* size, FILE* f)
{
    // goto the end of the file
    if (fseek(f, 0L, SEEK_END) != 0)
        return -1;

    // get the current file position of the given stream.
    *size = ftell(f);

    // go back the start of the file
    if (fseek(f, 0L, SEEK_SET) != 0)
        return -1;
    
    return 0;
}

Decode File from Base64 Text

#include <stdio.h>
#include <string.h>
#include "cb64.h"

#define EXIT_ERR(msg) { \
        printf("%s\n", msg); \
        exit(-1); \
    }

int get_file_size(size_t* size, FILE* f);

int main(int argc, char** argv) 
{
    char* filename = "../testdata/haha.txt";
    char* filename_out = "../testdata/out.png";
    FILE* file;
    FILE* file_out;

    file = fopen(filename, "r");
    if (file == NULL)
    {
        EXIT_ERR("file not found");
    }

    file_out = fopen(filename_out, "wb");

    size_t file_size;
    if (get_file_size(&file_size, file) != 0)
        EXIT_ERR("error get file size");

    printf("%lu\n", file_size);

    unsigned char input[file_size+1];

    unsigned char* dst_decode = NULL;
    size_t dst_size_decode = 0;

    fread(input, sizeof(char), file_size, file);
    input[file_size] = '\0';

    int decode_ok = decode_b64(input, 0, &dst_decode, &dst_size_decode);
    if (decode_ok != 0)
        EXIT_ERR("error decode text to base64");

    printf("%s\n", dst_decode);

    fwrite(dst_decode, 1, dst_size_decode, file_out);

    free(dst_decode);
    fclose(file);
    fclose(file_out);
    return 0;
}

int get_file_size(size_t* size, FILE* f)
{
    // goto the end of the file
    if (fseek(f, 0L, SEEK_END) != 0)
        return -1;

    // get the current file position of the given stream.
    *size = ftell(f);

    // go back the start of the file
    if (fseek(f, 0L, SEEK_SET) != 0)
        return -1;
    
    return 0;
}
Owner
Telkom DEV
Telkom DEV open source projects (Powered By Telkom Indonesia)
Telkom DEV
Similar Resources

Bond is a cross-platform framework for working with schematized data. It supports cross-language de/serialization and powerful generic mechanisms for efficiently manipulating data. Bond is broadly used at Microsoft in high scale services.

Bond Bond is an open-source, cross-platform framework for working with schematized data. It supports cross-language serialization/deserialization and

Dec 31, 2022

A fast, byte-code interpreted language

Minima Minima is a small, portable, and fast programming language written in C. The Syntax Minima's syntax is optimized for a fast byte-code translati

Aug 16, 2022

MessagePack implementation for C and C++ / msgpack.org[C/C++]

msgpack for C/C++ It's like JSON but smaller and faster. Overview MessagePack is an efficient binary serialization format, which lets you exchange dat

Dec 31, 2022

Protocol Buffers implementation in C

Overview This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c, a pure C library

Dec 26, 2022

a small protobuf implementation in C

μpb - a small protobuf implementation in C Platform Build Status macOS ubuntu μpb (often written 'upb') is a small protobuf implementation written in

Jan 6, 2023

An implementation of the MessagePack serialization format in C / msgpack.org[C]

CMP CMP is a C implementation of the MessagePack serialization format. It currently implements version 5 of the MessagePack Spec. CMP's goal is to be

Dec 31, 2022

static_vector implementation in terms of std::vector. No need to implement each function again and again ;)

static_vector static_vector implementation in terms of std::vector. No need to implement each function again and again ;) The usage is basically the s

Oct 29, 2022

Base64 Encoding implementation in C Programming Language

cb64 Base64 Encoding implementation in C Programming Language Spec: https://datatracker.ietf.org/doc/html/rfc4648#page-5 Header only So just copy cb64

Dec 6, 2022

Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockford's base32. MIT licensed with consistent, flexible API.

cppcodec Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockf

Dec 28, 2022

A fast base64 module for React Native

react-native-quick-base64 A native implementation of Base64 in C++ for React Native. 4x faster than base64-js on an iPhone 11 Pro.

Dec 18, 2022

Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockford's base32. MIT licensed with consistent, flexible API.

cppcodec Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockf

Dec 28, 2022

An easy to decode base64 modification.

crzy64 An easy to decode base64 modification. This is a base64 modification designed to simplify the decoding step. For the four encoded bytes, it onl

Dec 30, 2022

🔑 Base64 is a character converter that works with ASCII characters.

🔑 Base64 is a character converter that works with ASCII characters.

🔑 Base64 What is it? Base64 is a simple character converter. It converts characters into nums, might be used in encryption protocols or as independen

Dec 22, 2021

base64 single header encode/decode

b64.h base64 single header encode/decode #include stdio.h #include "b64.h" // strings are stored on the heap, don't forget to free() them int main(i

Nov 28, 2022

C library containing useful base64 related functions.

b64 C library containing useful fast base64 related functions. Usage Get the library: It's very simple to use it, run the "build_lib.sh" shell script

May 8, 2022

PLP Project Programming Language | Programming for projects and computer science and research on computer and programming.

PLP Project Programming Language | Programming for projects and computer science and research on computer and programming.

PLPv2b PLP Project Programming Language Programming Language for projects and computer science and research on computer and programming. What is PLP L

Aug 20, 2022

StarkScript - or the Stark programming language - is a compiled C-based programming language that aims to offer the same usability as that of JavaScript's and TypeScript's

StarkScript StarkScript - or the Stark programming language - is a compiled C-based programming language that aims to offer the same usability as that

May 10, 2022

Analysing and implementation of lossless data compression techniques like Huffman encoding and LZW was conducted along with JPEG lossy compression technique based on discrete cosine transform (DCT) for Image compression.

PROJECT FILE COMPRESSION ALGORITHMS - Huffman compression LZW compression DCT Aim of the project - Implement above mentioned compression algorithms an

Dec 14, 2021
Related tags
Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockford's base32. MIT licensed with consistent, flexible API.

cppcodec Header-only C++11 library to encode/decode base64, base64url, base32, base32hex and hex (a.k.a. base16) as specified in RFC 4648, plus Crockf

Dec 28, 2022
Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift

Fast Binary Encoding (FBE) Fast Binary Encoding allows to describe any domain models, business objects, complex data structures, client/server request

Jan 2, 2023
Simple Binary Encoding (SBE) - High Performance Message Codec

Simple Binary Encoding (SBE) SBE is an OSI layer 6 presentation for encoding and decoding binary application messages for low-latency financial applic

Dec 28, 2022
Consistent Hashing Using Fibonacci Encoding

fibonacci_table This does not work as I thought it would. I did not manage to compute an inverse fibonacci mapping. It works, but just like linear has

Jan 30, 2021
A C++11 ASN.1 BER Encoding and Decoding Library

fast_ber A performant ASN.1 BER encoding and decoding library written in C++11 Introduction fast_ber is a small, lightweight library for BER encoding

Dec 21, 2022
Like my previous encoding project but actually likely finishedable. Also in C++.

EncodedCPP Like my previous encoding project but actually likely finishedable. Also in C++. Now with encoding and decoding options. Saving codes has a

Jan 1, 2022
RLE-encoding/decoding for files in C

RLE-encoding RLE-encoding/decoding for files in C My version of file encoding/decoding as an assignment for Metropolia UAS in C-programming course . T

Aug 26, 2022
A compiler for the Bon programming language.

bonc A compiler for the Bon programming language. About Note: This project is extremely WIP, like no executables being produced WIP Bon is a work in p

Mar 14, 2022
:zap: The Mobile Robot Programming Toolkit (MRPT)
:zap: The Mobile Robot Programming Toolkit (MRPT)

The MRPT project gcc/clang: MSVC: Last stable release: GH downloads: SF downloads (datasets): Last Win64 builds: Workspace: 1. Introduction Mobile Rob

Dec 30, 2022