Encode/decode data into/from base64 encoding
base64.Rdbase64encode encodes a data into base64 encoding. The source
can be a file, binary connection or a raw vector.
base64decode decodes a base64-encoded string into binary
data. The source can be a string or a connection, the output is
either a raw vector (output=NULL) or a binary connection.
Usage
base64encode(what, linewidth, newline)
base64decode(what, output = NULL, file, strict = FALSE)Arguments
- what
data to be encoded/decoded. For
base64encodeit can be a raw vector, text connection or file name. Forbase64decodeit can be a string, raw vector or a binary connection.- linewidth
if set, the output is split into lines with at most
linewidthcharacters per line. Zero orNAdenotes no limit and values 1 .. 3 are silently treated as 4 since that is the shortest valid line.- newline
only applicable if
linewidthis set; if set (string), the result will be a single string with all lines joined using thenewlinestring- output
if
NULLthen the output will be a raw vector with the decoded data, otherwise it must be either a filename (string) or a binary connection.- file
file name (string) for data to use as input instead of
what. It is essentially just a shorthand forbase64decode(file(name)). Only one ofwhatandfilecan be specified.- strict
logical scalar. If
TRUEthen the decoder validates the input contents making sure it strictly adheres to the standard, does not include any other characters (including white spaces, newlines etc.), is correctly padded and does not have any trailing content. Any validation failure will result in an error. Otherwise the decoding skips over invalid characters, permits lack of padding and ignores trailing content.
Value
base64encode: A character vector. If linewith > 0 and
newline is not set then it will consist of as many elements
as there are lines. Otherwise it is a single string.
base64decode: If output = NULL then a raw vector with
the decoded content, otherwise the number of bytes written into the
connection.
Examples
base64encode(1:100)
#> [1] "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA=="
base64encode(1:100, 70)
#> [1] "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIz"
#> [2] "NDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA=="
base64encode(1:100, 70, "\n")
#> [1] "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIz\nNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZA=="
x <- charToRaw("the decoded content, otherwise the number of bytes")
y <- base64decode(base64encode(x))
stopifnot(identical(x, y))