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.