Encode and decode base64 in LoadRunner

Source: Internet
Author: User
Tags base64 encode

Base64 encode/decode for LoadRunner this article describes how to encode and decode strings in LoadRunner:

Http://ptfrontline.wordpress.com/2009/09/30/base64-encodedecode-for-loadrunner/

 

 

 

Encapsulate the b64_encode_string and b64_decode_string functions in the header 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
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', '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, 0, 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, 22, 24, 25, 0, 0, 0, 0, 0, 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 ,};

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:
// & Nbsp; Source & nbsp; pointer to source string to encode
// & Nbsp; & lrvar & nbsp; LR variable where base64 encoded string is stored
//
// Example:
//
// & Nbsp; 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:
// & Nbsp; Source & nbsp; pointer to source base64 encoded string
// & Nbsp; lrvar & nbsp; LR variable where decoded string is stored
//
// Example:
//
// & Nbsp; 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;
}

 

 

 

 

 

The following is an example:

 

# 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;

}

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.