Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elzoughby/base64
C library to encode and decode strings with base64 format
https://github.com/elzoughby/base64
ascii base64 c decode-strings decoding encode-strings encoding string
Last synced: 10 days ago
JSON representation
C library to encode and decode strings with base64 format
- Host: GitHub
- URL: https://github.com/elzoughby/base64
- Owner: elzoughby
- Created: 2017-07-23T10:14:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-09T23:28:19.000Z (over 6 years ago)
- Last Synced: 2024-10-12T20:03:45.758Z (26 days ago)
- Topics: ascii, base64, c, decode-strings, decoding, encode-strings, encoding, string
- Language: C
- Size: 9.77 KB
- Stars: 66
- Watchers: 2
- Forks: 25
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Base64
**[Base64](https://en.wikipedia.org/wiki/Base64)** is a group of similar [binary-to-text encoding](https://en.wikipedia.org/wiki/Binary-to-text_encoding) schemes that represent [binary data](https://en.wikipedia.org/wiki/Binary_data) in an [ASCII](https://en.wikipedia.org/wiki/ASCII) string format by translating it into a [radix](https://en.wikipedia.org/wiki/Radix)-64 representation. The term *Base64* originates from a specific [MIME content transfer encoding](https://en.wikipedia.org/wiki/MIME#Content-Transfer-Encoding). Each base64 digit represents exactly 6 bits of data.
The particular set of 64 characters chosen to represent the 64 place-values for the base varies between implementations. The general strategy is to choose 64 characters that are both members of a subset common to most encodings, and also [printable](https://en.wikipedia.org/wiki/Graphic_character). This combination leaves the data unlikely to be modified in transit through information systems, such as email, that were traditionally not [8-bit clean](https://en.wikipedia.org/wiki/8-bit_clean).[[1\]](https://en.wikipedia.org/wiki/Base64#cite_note-autogenerated2006-1) For example, MIME's Base64 implementation uses `A`–`Z`, `a`–`z`, and `0`–`9` for the first 62 values.
#### Example
A quote from [Thomas Hobbes](https://en.wikipedia.org/wiki/Thomas_Hobbes)' *Leviathan*:
> Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
is represented as a byte sequence of 8-bit-padded [ASCII](https://en.wikipedia.org/wiki/ASCII) characters encoded in [MIME](https://en.wikipedia.org/wiki/MIME)'s Base64 scheme as follows:
> TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4K
In the above quote, the encoded value of *Man* is *TWFu*. Encoded in ASCII, the characters *M*, *a*, and *n* are stored as the bytes `77`, `97`, and `110`, which are the 8-bit binary values `01001101`, `01100001`, and `01101110`. These three values are joined together into a 24-bit string, producing `010011010110000101101110`. Groups of 6 bits (6 bits have a maximum of 26 = 64 different binary values) are [converted into individual numbers](https://en.wikipedia.org/wiki/Binary_number#Counting_in_binary) from left to right (in this case, there are four numbers in a 24-bit string), which are then converted into their corresponding Base64 character values.
The Base64 index table: [[2]](https://en.wikipedia.org/wiki/Base64)
| Value | Char | Value | Char | Value | Char | Value | Char |
| ----- | ---- | ----- | ---- | ----- | ---- | ----- | ---- |
| 0 | `A` | 16 | `Q` | 32 | `g` | 48 | `w` |
| 1 | `B` | 17 | `R` | 33 | `h` | 49 | `x` |
| 2 | `C` | 18 | `S` | 34 | `i` | 50 | `y` |
| 3 | `D` | 19 | `T` | 35 | `j` | 51 | `z` |
| 4 | `E` | 20 | `U` | 36 | `k` | 52 | `0` |
| 5 | `F` | 21 | `V` | 37 | `l` | 53 | `1` |
| 6 | `G` | 22 | `W` | 38 | `m` | 54 | `2` |
| 7 | `H` | 23 | `X` | 39 | `n` | 55 | `3` |
| 8 | `I` | 24 | `Y` | 40 | `o` | 56 | `4` |
| 9 | `J` | 25 | `Z` | 41 | `p` | 57 | `5` |
| 10 | `K` | 26 | `a` | 42 | `q` | 58 | `6` |
| 11 | `L` | 27 | `b` | 43 | `r` | 59 | `7` |
| 12 | `M` | 28 | `c` | 44 | `s` | 60 | `8` |
| 13 | `N` | 29 | `d` | 45 | `t` | 61 | `9` |
| 14 | `O` | 30 | `e` | 46 | `u` | 62 | `+` |
| 15 | `P` | 31 | `f` | 47 | `v` | 63 | `/` |## Base64 Library
This is a C library to encode ASCII string to base64 format and decode base64 string to ASCII.
### Functions
- **base64_encode**
```c
char* base64_encode(char* plain);
/***********************************************
Encodes ASCCI string into base64 format string
@param plain: ASCII string to be encoded
@return encoded base64 format string
***********************************************/
```- **base64_decode**
```c
char* base64_decode(char* cipher);
/***********************************************
decodes base64 format string into ASCCI string
@param plain encoded base64 format string
@return ASCII string to be encoded
***********************************************/
```## Base64 App Example
An Example program to demonstrate the Base64 library by giving the inputs through command line arguments and getting the output on the screen or written in a file.
- **Usage** `base64 (encode | decode) []`
- **Options**
- *`encode`* - Convert ASCII string into Base64 format
- *`decode`* - Convert Base64 format into ASCII string
- **Arguments**
- *``* - String or path/to/file to be converted
- *``* - Path/to/converted/file