View Single Post
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, 10120 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