Raised This Month: $51 Target: $400
 12% 

[INC] Base64 Encoding and Decoding library functions


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SirLamer
Senior Member
Join Date: Oct 2008
Old 08-27-2009 , 01:52   [INC] Base64 Encoding and Decoding library functions
Reply With Quote #1

Base64 functions for SourceMod

These were developed from a similar library for AMX Mod X.
http://forums.alliedmods.net/showthread.php?t=87252

This library file includes functions for encoding and decoding text strings or binary data using the Base64 standard.

Base64 has two main useful purposes:
  • To safely store or transmit binary data in a text string format very efficiently (25% storage space overhead compared to 50% for hexadecimal storage).
  • To obfuscate text or other information such that it is not human readable but still unencrypted.

This library includes the following stock functions:

EncodeBase64

EncodeBase64(String:sResult[], len, const String:sString[], sourcelen=0)

Encodes a string or binary data into Base64.
  • sResult: The storage buffer for the Base64-encoded result.
  • len: The maximum length of the storage buffer, in characters/bytes.
  • sString: The input string or binary data to be encoded.
  • sourcelen (optional): The number of characters or length in bytes to be read from the input source. This is not needed for a text string, but is important for binary data since there is no end-of-line character.
Return: The length of the written Base64 string, in bytes.


DecodeBase64

DecodeBase64(String:sResult[], len, const String:sString[])

Decodes a Base64 string.
  • sResult: The storage buffer for the decoded text strihg or binary data.
  • len: The maximum length of the storage buffer, in characters/bytes.
  • sString: The input string in compliant Base64 format to be decoded.
Return: The length of the decoded data, in bytes.


Base64MimeToUrl

Base64MimeToUrl(String:sResult[], len, const String:sString[])

Converts a standards-compliant Base64 string to the commonly accepted URL-compliant alternative.
  • sResult: The storage buffer for the URL-compliant result.
  • len: The maximum length of the storage buffer in characters/bytes.
  • sString: The standards-compliant Base64 input string to converted.
Return: Number of cells written.

Remember: The result will be the same length as the input string.


Base64UrlToMime

Base64UrlToMime(String:sResult[], len, const String:sString[], sourcelen)

Converts a URL-compliant Base64 string to the standards-compliant version.
  • sResult: The storage buffer for the standards-compliant result.
  • len: The maximum length of the storage buffer in characters/bytes.
  • sString: The URL-compliant Base64 input string to converted.
Return: Number of cells written.

Remember: The result will be the same length as the input string.


For an example application, check out the Jukebox for Source plugin, written by myself:
http://forums.alliedmods.net/showthread.php?t=100537
I use Base64 to encode a multi-track playlist into the URL in a way that is compact and difficult to manipulate by hand.
Attached Files
File Type: inc base64.inc (8.6 KB, 10125 views)
__________________
Visit TEAM CHEESUS, a TF2 community with our own brand of injected shenanigans, for added flavor.

Plugins: Jukebox for Source

Last edited by SirLamer; 08-27-2009 at 13:53. Reason: Version 1.0.1 update, expanded info in post
SirLamer is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 08-27-2009 , 10:26   Re: [INC] Base64 Encoding and Decoding library functions
Reply With Quote #2

Cool, have an example on what this would be used for/in?
Dragonshadow is offline
SirLamer
Senior Member
Join Date: Oct 2008
Old 08-27-2009 , 12:56   Re: [INC] Base64 Encoding and Decoding library functions
Reply With Quote #3

Yeah, good idea. I also forgot some details in the original post, so I will update it.
__________________
Visit TEAM CHEESUS, a TF2 community with our own brand of injected shenanigans, for added flavor.

Plugins: Jukebox for Source
SirLamer is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 08-28-2010 , 03:24   Re: [INC] Base64 Encoding and Decoding library functions
Reply With Quote #4

This is great

Thanks SirLamer!

EDIT: Don't use decl on any statements that you want to fire through this thing, they need to be zero'd.

Last edited by KyleS; 08-28-2010 at 19:51.
KyleS is offline
TomXPro
Senior Member
Join Date: Aug 2014
Location: Germany
Old 08-11-2014 , 20:02   Re: [INC] Base64 Encoding and Decoding library functions
Reply With Quote #5

Where has the file to be placed in CS:GO?
TomXPro is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 08-12-2014 , 22:36   Re: [INC] Base64 Encoding and Decoding library functions
Reply With Quote #6

This is a library for plugin writers, not server owners TomXPro.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode