Skip to main content

Function Library — Encoding

C.Decode and C.Encode convert between encoded and plain representations for JavaScript scripts. The two namespaces mirror each other.

Decoding

FunctionSignatureReturns
C.Decode.base64(value, resultEncoding)The decoded text or bytes
C.Decode.gzip(value, encoding)The decompressed text
C.Decode.inflate(value, encoding, isRaw)The decompressed text
C.Decode.hex(value)The number the hexadecimal string represents
C.Decode.mime(value)The decoded header text
C.Decode.uri(value)The percent-decoded string

Encoding

FunctionSignatureReturns
C.Encode.base64(value, trimTrailEq, encoding)The Base64 string
C.Encode.gzip(value, encoding)The compressed result
C.Encode.deflate(value, encoding, toRaw)The compressed result
C.Encode.hex(value)The hexadecimal representation
C.Encode.mime(value, encoding, maxLineLength, charset)The encoded header
C.Encode.uri(value)The percent-encoded string

Details

Base64

C.Decode.base64() returns text by default. The second argument changes that:

resultEncodingReturns
utf8 (default)Text, decoded leniently
utf8-validText, or undefined where the result is not valid UTF-8
bufferThe raw bytes

Decoding is lenient — characters that are not valid Base64 are skipped rather than causing a failure — so use utf8-valid where you need to know that the field really held Base64 text.

C.Encode.base64() takes true as its second argument to strip trailing = padding. Its third argument states how to interpret an input string before encoding it, which is how a hexadecimal field is converted to Base64 in one step.

Decode a payload, rejecting anything that is not text...

- script:
lang: js
source: |
const decoded = C.Decode.base64(__e.payload, 'utf8-valid');
if (decoded !== undefined) {
__e.payload_text = decoded;
} else {
__e.payload_binary = true;
}

Compression

C.Decode.gzip() and C.Decode.inflate() both take the encoding of the input as their second argument, defaulting to base64. Where the field already holds raw bytes the argument is ignored. Pass true as inflate()'s third argument for raw deflate data with no zlib header.

Decompression stops at 512 MiB and returns undefined rather than continuing, which bounds the cost of a deliberately crafted archive.

The encoding functions take none as their encoding argument to return raw bytes instead of a string.

Hexadecimal

The two hexadecimal functions are not inverses, and the asymmetry catches people out:

  • C.Decode.hex() reads a hexadecimal string as a number, accepting an optional sign and an optional 0x prefix. A string it cannot read returns NaN rather than throwing.
  • C.Encode.hex() writes a number as hexadecimal, rounding a fractional value first.

Neither converts a hexadecimal string to its bytes — use C.Decode.base64() with a hex input encoding, or the digest functions on the Masking page.

MIME Headers

C.Decode.mime() decodes RFC 2047 encoded-words, the =?UTF-8?B?...?= form that carries non-ASCII text in mail headers.

C.Encode.mime() produces them. Its second argument selects the encoding — Q for a quoted-printable-style result, B for Base64 — and the fourth names the character set, defaulting to UTF-8. Set the third argument to a positive number to fold long headers across continuation lines.

URIs

C.Encode.uri() percent-encodes everything outside the unreserved set, matching JavaScript's own encodeURIComponent(). C.Decode.uri() reverses it, returning undefined on a malformed escape sequence rather than throwing.