Skip to content

Instantly share code, notes, and snippets.

@sanuj
Last active August 29, 2015 14:17
Show Gist options
  • Save sanuj/0bbdaa5f267270b53d89 to your computer and use it in GitHub Desktop.
Save sanuj/0bbdaa5f267270b53d89 to your computer and use it in GitHub Desktop.
Base 64 encoding and decoding. It's currently set for encoding. If you want to decode then comment encode and uncomment decode. You will also have to swap the input and output text files.
#include <stdlib.h>
#include <stdio.h>
const char base_64_arr[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char decode_arr[]="|&&&}rstuvwxyz{&&&&&&&>?@ABCDEFGHIJKLMNOPQRSTUVW&&&&&&XYZ[\\]^_`abcdefghijklmnopq";
void decode(FILE *f_in, FILE *f_out)
{
int i, len, ch;
char in[4], out[3];
*in = (char) 0;
*out = (char) 0;
while(!feof(f_in))
{
len=0;
for(i=0; i < 4 && feof(f_in) == 0; i++)
{
ch=0;
while(feof(f_in) == 0 && ch == 0)
{
ch = getc(f_in);
if(ch != EOF)
{
ch = ((ch < 43 || ch>122) ? 0 : (int) decode_arr[ch-43]);
if(ch != 0)
{
if(ch == (int)'&')
ch = 0;
else
ch -= 61;
}
}
}
if(feof(f_in) == 0)
{
len++;
if(ch != 0) in[i] = (char) (ch-1);
}
else in[i] = (char) 0;
}
if(len > 0)
{
out[0] = (char) (in[0] << 2 | in[1] >> 4);
out[1] = (char) (in[1] << 4 | in[2] >> 2);
out[2] = (char) (((in[2] << 6) & 0xc0) | in[3]);
for(i=0; i < len - 1; i++)
if(putc((int) out[i], f_out) == 0) break;
}
}
}
void encode(FILE *f_in, FILE *f_out)
{
char in[3], out[4];
int i, len, six_bits=0;
*in = (char) 0;
*out = (char) 0;
while(feof(f_in) == 0)
{
len=0;
for(i=0; i<3; i++)
{
in[i] = (char) getc(f_in);
if(!feof(f_in)) len++;
else in[i] = (char) 0;
}
if(len > 0)
{
out[0] = (char) base_64_arr[(int)(in[0] >> 2)];
out[1] = (char) base_64_arr[(int)(((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4))];
// Add padding if len <= 1
if(len>1)
out[2] = (char) base_64_arr[(int)(((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6))];
else
out[2] = '=';
// Add padding if len <= 2
if(len>2)
out[3] = (char) base_64_arr[(int)(in[2] & 0x3f)];
else
out[3] = '=';
for(i=0; i<4; i++)
if(putc((int)(out[i]), f_out) == 0) break;
six_bits++;
}
// printf("number of six bits: %d\n", six_bits);
}
}
int main()
{
FILE* f_in = fopen("input_file.txt", "rb");
FILE* f_out = fopen("output_file.txt", "wb");
// ENCODE
encode(f_in, f_out);
// DECODE
// decode(f_in, f_out);
return 0;
}
OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision, developed by Intel Russia research center in Nizhny Novgorod, and now supported by Willow Garage and Itseez. It is free for use under the open-source BSD license. The library is cross-platform. It focuses mainly on real-time image processing. If the library finds Intel's Integrated Performance Primitives on the system, it will use these proprietary optimized routines to accelerate itself.
-- Taken from Wikipedia
T3BlbkNWIChPcGVuIFNvdXJjZSBDb21wdXRlciBWaXNpb24pIGlzIGEgbGlicmFyeSBvZiBwcm9ncmFtbWluZyBmdW5jdGlvbnMgbWFpbmx5IGFpbWVkIGF0IHJlYWwtdGltZSBjb21wdXRlciB2aXNpb24sIGRldmVsb3BlZCBieSBJbnRlbCBSdXNzaWEgcmVzZWFyY2ggY2VudGVyIGluIE5pemhueSBOb3Znb3JvZCwgYW5kIG5vdyBzdXBwb3J0ZWQgYnkgV2lsbG93IEdhcmFnZSBhbmQgSXRzZWV6LiBJdCBpcyBmcmVlIGZvciB1c2UgdW5kZXIgdGhlIG9wZW4tc291cmNlIEJTRCBsaWNlbnNlLiBUaGUgbGlicmFyeSBpcyBjcm9zcy1wbGF0Zm9ybS4gSXQgZm9jdXNlcyBtYWlubHkgb24gcmVhbC10aW1lIGltYWdlIHByb2Nlc3NpbmcuIElmIHRoZSBsaWJyYXJ5IGZpbmRzIEludGVsJ3MgSW50ZWdyYXRlZCBQZXJmb3JtYW5jZSBQcmltaXRpdmVzIG9uIHRoZSBzeXN0ZW0sIGl0IHdpbGwgdXNlIHRoZXNlIHByb3ByaWV0YXJ5IG9wdGltaXplZCByb3V0aW5lcyB0byBhY2NlbGVyYXRlIGl0c2VsZi4KCi0tIFRha2VuIGZyb20gV2lraXBlZGlhCg==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment