Using Libb64 for BASE64 encoding in C language

Source: Internet
Author: User
Tags base64 fread

Tag: Write for PAC bat language color txt pil buffer

Libb64http://sourceforge.net/projects/libb64

The following is demo

Clibb64demo.c

#include <stdio.h>#include<string.h>#include"libb64/cencode.h"#include"libb64/cdecode.h"voidBase64encodestring (Const Char*texttoencode,Char*buffer);voidBase64decodestring (Const Char*codetodecode,Char*buffer);voidBase64encodefile (file *filetoencode, file *encodedfile);voidBase64decodefile (file *encodedfile, file *decodedfile);intMain () {Const Char*text ="Matt Damon"; Charbuffer[ +];    Base64encodestring (text, buffer); printf ("%s", buffer); Const Char*code ="r29vzcbxawxsieh1bnrpbmc=";    Base64decodestring (code, buffer); printf ("%s\n", buffer); Const Char*inputfilename ="E:/packages/java/android/docs-24_r01.zip"; FILE*inputfile = fopen (InputFileName,"RB"); if(inputfile) {Const Char*encodedfilename ="E:/packages/java/android/docs-24_r01.zip.txt"; FILE*encodedfile = fopen (Encodedfilename,"WB"); if(encodedfile) {base64encodefile (inputfile, encodedfile);        Fclose (Encodedfile); }        Elseprintf ("cannot open file:%s", Encodedfile);    Fclose (Inputfile); }    Elseprintf ("cannot open file:%s", Inputfile); Const Char*encodedfilename ="En_office_professional_2007_cd_x12-42316.iso.txt"; FILE*encodedfile = fopen (Encodedfilename,"RB"); if(encodedfile) {Const Char*decodedfilename ="Office"; FILE*decodedfile = fopen (Decodedfilename,"WB"); if(decodedfile) {base64decodefile (encodedfile, decodedfile);        Fclose (Decodedfile); }        Elseprintf ("cannot open file:%s", Decodedfile);    Fclose (Encodedfile); }    Elseprintf ("cannot open file:%s", Encodedfile); return 0;}voidBase64encodestring (Const Char*texttoencode,Char*buffer)    {Base64_encodestate state; Base64_init_encodestate (&State ); intNumberofencodedbytes = Base64_encode_block (Texttoencode, strlen (texttoencode), buffer, &State ); Numberofencodedbytes+ = base64_encode_blockend (buffer + numberofencodedbytes, &State ); Buffer[numberofencodedbytes]=' /';}voidBase64decodestring (Const Char*codetodecode,Char*buffer)    {Base64_decodestate state; Base64_init_decodestate (&State ); intNumberofcharacters = Base64_decode_block (Codetodecode, strlen (codetodecode), buffer, &State ); Buffer[numberofcharacters]=' /';}voidBase64encodefile (file *filetoencode, file *encodedfile) {    Charreadbuffer[1024x768]; Charencodedbuffer[2048]; intnumberofencodedbytes;    Base64_encodestate State; Base64_init_encodestate (&State );  while(!feof (Filetoencode)) {size_t numberofobjects= Fread (Readbuffer,sizeof(Char),1024x768, Filetoencode); Numberofencodedbytes= Base64_encode_block (Readbuffer, Numberofobjects, Encodedbuffer, &State ); Fwrite (Encodedbuffer,sizeof(Char), numberofencodedbytes, encodedfile); } numberofencodedbytes= Base64_encode_blockend (Encodedbuffer, &State ); Fwrite (Encodedbuffer,sizeof(Char), numberofencodedbytes, encodedfile);}voidBase64decodefile (file *encodedfile, file *decodedfile) {    Charinputbuffer[1024x768]; Chardecodedbuffer[2048]; intnumberofdecodedbytes;    Base64_decodestate State; Base64_init_decodestate (&State );  while(!feof (Encodedfile)) {size_t numberofcharacters= Fread (InputBuffer,sizeof(Char),1024x768, Encodedfile); Numberofdecodedbytes= Base64_decode_block (InputBuffer, Numberofcharacters, Decodedbuffer, &State ); Fwrite (Decodedbuffer,sizeof(Char), numberofdecodedbytes, decodedfile); }}

Compilation.bat

Gcc-std=c11 clibb64demo.c Libb64/*.c-o Clibb64democlibb64demo

Using Libb64 for BASE64 encoding in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.