AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [INC] Base64 Encoding and Decoding library functions (https://forums.alliedmods.net/showthread.php?t=101764)

SirLamer 08-27-2009 01:52

[INC] Base64 Encoding and Decoding library functions
 
1 Attachment(s)
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.

Dragonshadow 08-27-2009 10:26

Re: [INC] Base64 Encoding and Decoding library functions
 
Cool, have an example on what this would be used for/in?

SirLamer 08-27-2009 12:56

Re: [INC] Base64 Encoding and Decoding library functions
 
Yeah, good idea. I also forgot some details in the original post, so I will update it.

KyleS 08-28-2010 03:24

Re: [INC] Base64 Encoding and Decoding library functions
 
This is great :wink:

Thanks SirLamer!

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

TomXPro 08-11-2014 20:02

Re: [INC] Base64 Encoding and Decoding library functions
 
Where has the file to be placed in CS:GO?

berni 08-12-2014 22:36

Re: [INC] Base64 Encoding and Decoding library functions
 
This is a library for plugin writers, not server owners TomXPro.


All times are GMT -4. The time now is 15:08.

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