Base64 Encode/Decode for LoadRunner

Recently I had the need to decode a Base64 string received from a server and in the process I created an include file that contains both Encode and Decode functions for base64. Use & Abuse as you see fit! Enjoy!
Below is an example of how to use the base64.h include file

#include "base64.h"
vuser_init()
{
 int res;
 // ENCODE
 lr_save_string("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789","plain");
 b64_encode_string( lr_eval_string("{plain}"), "b64str" );
 lr_output_message("Encoded: %s", lr_eval_string("{b64str}") );

 // DECODE
 b64_decode_string( lr_eval_string("{b64str}"), "plain2" );
 lr_output_message("Decoded: %s", lr_eval_string("{plain2}") );

 // Verify decoded matches original plain text
 res = strcmp( lr_eval_string("{plain}"), lr_eval_string("{plain2}") );
 if (res==0) lr_output_message("Decoded matches original plain text");

 return 0;
}

And here’s the actual base64.h include file

/*
Base 64 Encode and Decode functions for LoadRunner
==================================================
This include file provides functions to Encode and Decode
LoadRunner variables. It's based on source codes found on the
internet and has been modified to work in LoadRunner.

Created by Kim Sandell / Celarius - www.celarius.com
*/
// Encoding lookup table
char base64encode_lut[] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',
'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h',
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y',
'z','0','1','2','3','4','5','6','7','8','9','+','/','='};

// Decode lookup table
char base64decode_lut[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,62, 0, 0, 0,63,52,53,54,55,56,57,58,59,60,61, 0, 0,
0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
15,16,17,18,19,20,21,22,23,24,25, 0, 0, 0, 0, 0, 0,26,27,28,
29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,
49,50,51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };

void base64encode(char *src, char *dest, int len)
// Encodes a buffer to base64
{
 int i=0, slen=strlen(src);
 for(i=0;i<slen && i<len;i+=3,src+=3)
 { // Enc next 4 characters
 *(dest++)=base64encode_lut[(*src&0xFC)>>0x2];
 *(dest++)=base64encode_lut[(*src&0x3)<<0x4|(*(src+1)&0xF0)>>0x4];
 *(dest++)=((i+1)<slen)?base64encode_lut[(*(src+1)&0xF)<<0x2|(*(src+2)&0xC0)>>0x6]:'=';
 *(dest++)=((i+2)<slen)?base64encode_lut[*(src+2)&0x3F]:'=';
 }
 *dest='\0'; // Append terminator
}

void base64decode(char *src, char *dest, int len)
// Encodes a buffer to base64
{
 int i=0, slen=strlen(src);
 for(i=0;i<slen&&i<len;i+=4,src+=4)
 { // Store next 4 chars in vars for faster access
 char c1=base64decode_lut[*src], c2=base64decode_lut[*(src+1)], c3=base64decode_lut[*(src+2)], c4=base64decode_lut[*(src+3)];
 // Decode to 3 chars
 *(dest++)=(c1&0x3F)<<0x2|(c2&0x30)>>0x4;
 *(dest++)=(c3!=64)?((c2&0xF)<<0x4|(c3&0x3C)>>0x2):'\0';
 *(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):'\0';
 }
 *dest='\0'; // Append terminator
}

int b64_encode_string( char *source, char *lrvar )
// ----------------------------------------------------------------------------
// Encodes a string to base64 format
//
// Parameters:
//        source    Pointer to source string to encode
//        lrvar     LR variable where base64 encoded string is stored
//
// Example:
//
//        b64_encode_string( "Encode Me!", "b64" )
// ----------------------------------------------------------------------------
{
 int dest_size;
 int res;
 char *dest;
 // Allocate dest buffer
 dest_size = 1 + ((strlen(source)+2)/3*4);
 dest = (char *)malloc(dest_size);
 memset(dest,0,dest_size);
 // Encode & Save
 base64encode(source, dest, dest_size);
 lr_save_string( dest, lrvar );
 // Free dest buffer
 res = strlen(dest);
 free(dest);
 // Return length of dest string
 return res;
}

int b64_decode_string( char *source, char *lrvar )
// ----------------------------------------------------------------------------
// Decodes a base64 string to plaintext
//
// Parameters:
//        source    Pointer to source base64 encoded string
//        lrvar     LR variable where decoded string is stored
//
// Example:
//
//        b64_decode_string( lr_eval_string("{b64}"), "Plain" )
// ----------------------------------------------------------------------------
{
 int dest_size;
 int res;
 char *dest;
 // Allocate dest buffer
 dest_size = strlen(source);
 dest = (char *)malloc(dest_size);
 memset(dest,0,dest_size);
 // Encode & Save
 base64decode(source, dest, dest_size);
 lr_save_string( dest, lrvar );
 // Free dest buffer
 res = strlen(dest);
 free(dest);
 // Return length of dest string
 return res;
}

Advertisements

40 thoughts on “Base64 Encode/Decode for LoadRunner

  1. I don’t know If I said it already but …Great site…keep up the good work. 🙂 I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, 🙂

    A definite great read….

  2. Hey, I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say GREAT blog!…..I”ll be checking in on a regularly now….Keep up the good work! 🙂

    – Marc Shaw

  3. Nice article!.
    when i tried to include this base64.h file into my LR scripts, i got the following compilation errors:

    C:\\Program Files\\HP\\LoadRunner\\include/base64.h (38): illegal character `#’
    C:\\Program Files\\HP\\LoadRunner\\include/base64.h (50): missing ‘
    C:\\Program Files\\HP\\LoadRunner\\include/base64.h (51): missing ‘
    C:\\Program Files\\HP\\LoadRunner\\include/base64.h (53): illegal character `#’

  4. This was a real help for me. I was looking for something like this for 3-4 days. You saved me a lot of effort. Thank you very much.

  5. Hi Kim,

    I’ve got a web_custom_request tht needs to have the viewstate correlated. I think the viewstate needs to be encoded to base-64 but am wondering do i just need to use the b64_encode_string function after the web_reg_save_param function or will i need to decode the string first?

    Thanks

  6. Hi,

    what can i do if the complete response is in base64 encoding?
    i tried correlating the entire response but it is too big to be saved in a parameter.
    i’ve also tried the DFE settings in LR 11.0 but the response is still in base64 encoding.
    any suggestions?

    • It depends a little on how BIG big is .. You can save the whole response body with a web_reg_save(“body”, “LB=”, “RB=”, “Search=Body”, LAST); and then use the Base64Decode to decode it and do whatever you want with it.

      However, getting the complete body as decoded could mean it is a BINARY stream and that is not supported by the Base64Decode function I’ve written.

      If you don’t mind me asking, why do you need to decode the whole response?

  7. I’m a little confused…. let’s say I have a SHA-1 hashed string “57BDE1220A2E58A99CCA6D60226D5721002EC7AA” (40 chars) [initial string – 1326312870788thefrog]
    When I’m using this Base64 encoder I’m getting a 56 characters log string. However, if I’m using a java converter using org.apache.commons.codec.binary.Base64 I’m getting 28 characters long string:

    c converter – “NTdCREUxMjIwQTJFNThBOTlDQ0E2RDYwMjI2RDU3MjEwMDJFQzdBQQ==”
    java – “V73hIgouWKmcym1gIm1XIQAux6o=”

    Apparently I’m missing something. Any ideas?

    • The thing is that in Java you take the BYTES and do a Base64 for them, in LR you use the STRING and do the Base64 of that.

      With standard LR funtions there is no good way to handle binary arrays. To do so would require you to do raw C coding with byte buffers and such. This is totally possible to do, but usually not needed.

      Using an Online Base64 encoder I get (http://www.opinionatedgeek.com/dotnet/tools/base64encode/):

      Base64("57BDE1220A2E58A99CCA6D60226D5721002EC7AA") = "NTdCREUxMjIwQTJFNThBOTlDQ0E2RDYwMjI2RDU3MjEwMDJFQzdBQQ=="

      This one also takes the source data as a string and produces the same result as the C code.

  8. #include “as_web.h”
    #include “base64.h”

    char * xml_input =
    “”
    “******”
    “******t”
    “****”
    “****”
    “***”
    “****”
    “***”
    “***”
    “***”
    “**”
    “***”
    “****”
    “”
    “”
    “”
    “”;

    Action() {

    lr_save_string(xml_input,”XML_Input_Param” );
    lr_xml_replace(“XML={XML_Input_Param}”, “ResultParam=Result”,
    “Query=/CreateProspect/sg:accountName”,
    “XmlFragment={NewParam}”, LAST );
    b64_encode_string( lr_eval_string(“{Result}”),”test” );

    web_service_call( “StepName=Execute_102”,
    “SOAPMethod=Service|ServiceSoap|Execute”,
    “ResponseParam=response”,
    “Service=Service”,
    “URL=*****”,
    “ExpectedResponse=SoapResult”,
    “Snapshot=t1326141093.inf”,
    BEGIN_ARGUMENTS,
    “xml:Request=”
    “”
    “CreateProspect”
    ” {test}”
    “”
    “”
    “”
    “”,
    “xml:EsbRouterValue=”
    “”
    “***”
    “”,
    END_ARGUMENTS,
    BEGIN_RESULT,
    “ExecuteResult/Response/Data=Param_Data”,
    END_RESULT,
    BEGIN_CHECKPOINTS,
    CHECKPOINT, “XPATH=ExecuteResult[1]/Response[1]/Status[1]”, “Value=Success”,
    END_CHECKPOINTS,
    LAST);

    // DECODE
    b64_decode_string( lr_eval_string(“{Param_Data}”),
    “plain3” );
    lr_output_message(“Decoded: %s”, lr_eval_string(“{plain3}”));
    return 0;
    }

    I am using the Function and it is working great…but when i upload the script to PC and script fails when i run it. Do i have to place the include file on the server and controller also??if so what is the location for it??

    • Short explanation:
      The file needs to be included in the VuGen editor as well into the ACTIONS on the left. In the ACTION List, right click and select “Add Files To Script..”, select file and OK.

      Long Explanation (Technojargon):
      When you use PerfCenter the files are ZIPPED when sent to LoadGens. This ZIP operation omits all but the files that are known to the script. So just copying the base64.h file into the script directory is not enough, you need to tell the script that the base64.h file is to be included when the script is zipped and shipped to the LG from the PerfCenter.

  9. Hello Kim! Thanks for your awesome post.

    My decoding works realy good but ive got some problems when i was encoding the decoded code again.

    Heres my example:

    original base 64 code:
    IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    my way to decode it:
    b64_decode_string( lr_eval_string(“IDxlIHY9ImRzIi8+”
    “CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=”), “plain2” );
    lr_output_message(“Decoded:\n\n %s\n\n”, lr_eval_string(“{plain2}”) );

    my result:

    Now i like to encode my decoded result again – just for the try if i doing right:

    i doing it like this:
    lr_save_string(“ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″,”plain”);
    b64_encode_string( lr_eval_string(” ”
    ” ”
    ” ”
    “”), “b64str” );
    lr_output_message(“Encoded:\n\n %s \n\n”, lr_eval_string(“{b64str}”) );

    my result:
    ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    comparing original and reencoded base64 code:

    origal: IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    reencoded: ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    You can see – it isnt the same but it should be.

    Can you givbe me a helpful hint for positive reencoding.

    Thanks!
    Michael

    • Hi Michael,

      1st off, you do not need to use the lr_encode_string() function if you do not have a parameter enclosed in { and } inside the string.

      After decoding your Base64 data that you have I see it’s not plain text but contains special characters. For LoadRunner this is a problem, and encoding that again WILL produce a different result.

      An example of this is the ASCII character $00 (Zero). This terminates a C string (LoadRunner uses C strings internally). If this character is in the decoded data the output string will be truncated at that char.

      Is there any specific reason why you need to decode and then reencode the data? Do you need to manipulate it on the way?

      • Thank your for your response!

        The message i have posted isnt the full message i have copied into the message-window of you blog – its a fault of my computer. I will send it again when i work on another machine.
        The old message isnt a proper base for discusing my problem. You can delete the message.
        Yes, i need to manipulate the decoded data.

        Michael

  10. SECOND TRY. SORRY FOR POSTING IT TWICE!

    Hello Kim! Thanks for your awesome post.

    My decoding works realy good but ive got some problems when i was encoding the decoded code again.

    Heres my example:

    original base 64 code:
    IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    my way to decode it:
    b64_decode_string( lr_eval_string(“IDxlIHY9ImRzIi8+”
    “CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=”), “plain2” );
    lr_output_message(“Decoded:\n\n %s\n\n”, lr_eval_string(“{plain2}”) );

    my result:

    Now i like to encode my decoded result again – just for the try if i doing right:

    i doing it like this:
    lr_save_string(“ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″,”plain”);
    b64_encode_string( lr_eval_string(” ”
    ” ”
    ” ”
    “”), “b64str” );
    lr_output_message(“Encoded:\n\n %s \n\n”, lr_eval_string(“{b64str}”) );

    my result:
    ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    comparing original and reencoded base64 code:

    origal: IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    reencoded: ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    You can see – it isnt the same but it should be.

    Can you give me a helpful hint for positive reencoding. The reason why I need to encode the decoded data is I need to manipulate the decoded data. But before I start manipulating I need to be sure that I encode and decode correct.

    Thanks!
    Michael

  11. Third try -without a proxy:

    Hello Kim! Thanks for your awesome post.

    My decoding works realy good but ive got some problems when i was encoding the decoded code again.

    Heres my example:

    original base 64 code:
    IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    my way to decode it:
    b64_decode_string( lr_eval_string(“IDxlIHY9ImRzIi8+”
    “CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=”), “plain2” );
    lr_output_message(“Decoded:\n\n %s\n\n”, lr_eval_string(“{plain2}”) );

    my result:

    Now i like to encode my decoded result again – just for the try if i doing right:

    i doing it like this:
    lr_save_string(“ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789″,”plain”);
    b64_encode_string( lr_eval_string(” ”
    ” ”
    ” ”
    “”), “b64str” );
    lr_output_message(“Encoded:\n\n %s \n\n”, lr_eval_string(“{b64str}”) );

    my result:
    ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    comparing original and reencoded base64 code:

    origal: IDxlIHY9ImRzIi8CiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8IDwvcj4KICA8ciBuPSJwYXJtIiA+IDxlIHY9InBLdW5kZW5OYW1lIi8+IDxlIHY9InMiLz4gPGUvPiA8L3I+CjwvQ1NDUmVxdWVzdD4=

    reencoded: ICA8ZSB2PSJkcyIvPiAgPHIgbj0icGFybSIgPiA8ZSB2PSJwUGFzc3dvcnQiLz4gPGUgdj0icyIvPiA8ZSB2PSIqIi8+IDwvcj4gIDxyIG49InBhcm0iID4gPGUgdj0icEt1bmRlbk5hbWUiLz4gPGUgdj0icyIvPiA8ZS8+IDwvcj48L0NTQ1JlcXVlc3Q+

    You can see – it isnt the same but it should be.

    Can you give me a helpful hint for positive reencoding. The reason why I need to encode the decoded data is I need to manipulate the decoded data. But before I start manipulating I need to be sure that I encode and decode correct.

    Thanks!
    Michael

  12. Hi Kim , It is great post. I was searching a lot after trying many things and came across this great post. My requiremnet is as below, there are two headesrs i need to pass , one is a GMT timstamp in specific format and the second header and its value should be Digest which is Base64 ( SHA-1 ( GMT timstamp + password ) ). When the request is sent , the backed systems validate each time the value of the GMT timstamp part in the digest header after decoding and if it matches with GMT timstamp header then it is successfull. The GMT timstamp should be in this format 2012-10-17T23:11:47.058Z. Any idea on how to go about this ?

    • 1st off, here’s the SHA1 for LoadRunner: https://ptfrontline.wordpress.com/2010/03/02/sha-1-hash-for-loadrunner/
      You will need to include that too to get the SHA1 functionality.

      Here’s a code snippet you could try:

      // Generate the TimeStamp+Password string
      lr_save_string( lr_eval_string(“2012-10-17T23:11:47.058Z{password}”), “tstamp” );
      // Get the SHA1 HASH from the tstamp+password
      sha1_hash( lr_eval_string(“{tstamp}”) , “tstamp_hash”);
      // Base64 Encode the HASH
      b64_encode_string( lr_eval_string(“{tstamp_hash}”), “b64header” );

      // Add custom headers to next http request
      web_add_header( “x-gmt-header”, lr_eval_string(“{tstamp}”) ); // Add timestamp header
      web_add_header( “x-b64-header”, lr_eval_string(“{b64header}”) ); // Add Base64header

  13. Kim, I tweaked your code to handle base64 data that has binary characters in it. The application sends a base64 string to the server; I needed to transform it to binary, do some manipulation in memory, and then re-encode the string.

    I used the lower level functions, and of course not the LR string based utilities.

    The key changes are shown below:

    // pass in the source length; do not assume it is a null terminated string
    void base64encode(const char *src, const int slen, char *dest, int len)
    // Encodes a buffer to base64
    {
    int i=0;
    for(i=0;i<slen && i>0x2];
    *(dest++)=base64encode_lut[(*src&0x3)<>0x4];
    *(dest++)=((i+1)<slen)?base64encode_lut[(*(src+1)&0xF)<>0x6]:’=’;
    *(dest++)=((i+2)<slen)?base64encode_lut[*(src+2)&0x3F]:'=';
    }
    *dest=''; // Append terminator
    }

    int base64decode(const char *src, char *dest, int len)
    // Decodes a buffer from base64
    {
    char* orig_dest;
    int i=0, slen=strlen(src);
    orig_dest = dest;

    for(i=0;i<slen&&i<len;i+=4,src+=4)
    { // Store next 4 chars in vars for faster access
    char c1=base64decode_lut[*src], c2=base64decode_lut[*(src+1)], c3=base64decode_lut[*(src+2)], c4=base64decode_lut[*(src+3)];
    // Decode to 3 chars
    *(dest++)=(c1&0x3F)<>0x4;
    *(dest++)=(c3!=64)?((c2&0xF)<>0x2):”;
    *(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):'';
    }
    *dest=''; // Append terminator
    return dest – orig_dest -1; // return the decoded length (without the null character)
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s