Encoding and decoding of Base64 in LoadRunner

Source: Internet
Author: User
Tags base64 encode



Base64 Encode/decode for LoadRunner This article describes how to encode and decode strings Base64 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 Encode and Decode functions for LoadRunner
==================================================
This include the file provides functions to Encode and Decode
LoadRunner variables. It ' s based on source codes found on the
Internet and have 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 (s RC); 
 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;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;source&nbsp;&nbsp;&nbsp; Pointer to source string to encode
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lrvar&nbsp;&nbsp;&nbsp;&nbsp; LR variable where base64 encoded string is stored
//
Example:
//
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &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;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;source&nbsp;&nbsp;&nbsp; Pointer to source Base64 encoded string
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;lrvar&nbsp;&nbsp;&nbsp;&nbsp; LR variable where decoded string is stored
//
Example:
//
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &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 example used is as follows:


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





}



Encoding and decoding of Base64 in LoadRunner


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.