| 1 |
\name{base64} |
| 2 |
\alias{base64} |
| 3 |
\alias{base64Encode} |
| 4 |
\alias{base64Decode} |
| 5 |
\title{Encode/Decode base64 content} |
| 6 |
\description{ |
| 7 |
These functions encode and decode strings using base64 representations. |
| 8 |
\code{base64} can be used as a single entry point with an argument to |
| 9 |
encode or decode. The other two functions perform the specific action. |
| 10 |
} |
| 11 |
\usage{ |
| 12 |
base64(txt, encode = !inherits(txt, "base64"), mode = "character") |
| 13 |
} |
| 14 |
\arguments{ |
| 15 |
\item{txt}{character string to encode or decode} |
| 16 |
\item{encode}{logical value indicating whether the desired action is to encode or decode the object. |
| 17 |
If \code{txt} has (S3) class \code{base64}, the default is to decode this. |
| 18 |
} |
| 19 |
\item{mode}{a character string which is either "raw" or "character". |
| 20 |
This controls the type of vector that is returned. |
| 21 |
If this is "raw", a raw vector is created. Otherwise, a character |
| 22 |
vector of length 1 is returned and its element is the text version |
| 23 |
of the original data given in \code{txt}. |
| 24 |
} |
| 25 |
} |
| 26 |
\details{ |
| 27 |
This calls the routines in libcurl. These are not |
| 28 |
declared in the curl header files. So the support |
| 29 |
may need to be handled carefully on some platforms, |
| 30 |
e.g. Microsoft Windows. |
| 31 |
} |
| 32 |
\value{ |
| 33 |
If encode is \code{TRUE}, a character vector |
| 34 |
with a class named \code{base64}. |
| 35 |
If decode is \code{TRUE}, a simple string. |
| 36 |
} |
| 37 |
\references{ |
| 38 |
libcurl - \url{https://curl.se/} |
| 39 |
Wikipedia's explanation of base 64 encoding - \url{https://en.wikipedia.org/wiki/Base64} |
| 40 |
} |
| 41 |
\author{Duncan Temple Lang} |
| 42 |
\note{ |
| 43 |
This is currently not vectorized. |
| 44 |
|
| 45 |
We might extend this to work with raw objects. |
| 46 |
} |
| 47 |
%\seealso{} |
| 48 |
\examples{ |
| 49 |
# encode and then decode a simple string. |
| 50 |
txt = "Some simple text for base 64 to handle" |
| 51 |
x = base64(txt) |
| 52 |
base64(x) |
| 53 |
|
| 54 |
# encode to a raw vector |
| 55 |
x = base64("Simple text", TRUE, "raw") |
| 56 |
|
| 57 |
# decode to a character string. |
| 58 |
ans = base64Decode(x) |
| 59 |
ans == txt |
| 60 |
# decoded to a raw format. |
| 61 |
ans = base64Decode(x, "raw") |
| 62 |
|
| 63 |
|
| 64 |
# Binary data |
| 65 |
# f = paste(R.home(), "doc", "html", "logo.jpg", sep = .Platform$file.sep) |
| 66 |
f = system.file("examples", "logo.jpg", package = "RCurl") |
| 67 |
img = readBin(f, "raw", file.info(f)[1, "size"]) |
| 68 |
b64 = base64Encode(img, "raw") |
| 69 |
back = base64Decode(b64, "raw") |
| 70 |
identical(img, back) |
| 71 |
|
| 72 |
# alternatively, we can encode to a string and then decode back again |
| 73 |
# to raw and see that we preserve the date. |
| 74 |
|
| 75 |
enc = base64Encode(img, "character") |
| 76 |
dec = base64Decode(enc, "raw") |
| 77 |
identical(img, dec) |
| 78 |
|
| 79 |
|
| 80 |
# The following would be the sort of computation we could do if we |
| 81 |
# could have in-memory raw connections. |
| 82 |
# We would save() some objects to such an in-memory binary/raw connection |
| 83 |
# and then encode the resulting raw vector into a character vector. |
| 84 |
# Then we can insert that into a message, e.g. an email message or |
| 85 |
# an XML document and when we receive it in a different R session |
| 86 |
# we would get the string and reverse the encoding from the string to |
| 87 |
# a raw vector |
| 88 |
# In the absence of that in-memory connection facility in save(), |
| 89 |
# we can use a file. |
| 90 |
|
| 91 |
x = 1:10 |
| 92 |
|
| 93 |
# save two objects - a function and a vector |
| 94 |
f = paste(tempfile(), "rda", sep = ".") |
| 95 |
save(base64, x, file = f) |
| 96 |
|
| 97 |
# now read the results back from that file as a raw vector |
| 98 |
data = readBin(f, "raw", file.info(f)[1,"size"]) |
| 99 |
|
| 100 |
# base64 encode it |
| 101 |
txt = base64Encode(data, "character") |
| 102 |
|
| 103 |
if(require(XML)) { |
| 104 |
tt = xmlTree("r:data", namespaces = c(r = "http://www.r-project.org")) |
| 105 |
tt$addNode(newXMLTextNode(txt)) |
| 106 |
out = saveXML(tt) |
| 107 |
|
| 108 |
|
| 109 |
doc = xmlRoot(xmlTreeParse(out, asText = TRUE)) |
| 110 |
rda = base64Decode(xmlValue(doc), "raw") |
| 111 |
f = tempfile() |
| 112 |
writeBin(rda, f) |
| 113 |
e = new.env() |
| 114 |
load(f, e) |
| 115 |
objects(e) |
| 116 |
} |
| 117 |
|
| 118 |
# we'd like to be able to do |
| 119 |
# con = rawConnection(raw(), 'r+') |
| 120 |
# save(base64, x, file = con) |
| 121 |
# txt = base64Encode(rawConnectionValue(con), "character") |
| 122 |
# ... write and read xml stuff |
| 123 |
# val = xmlValue(doc) |
| 124 |
# rda = base64Decode(val, "raw") |
| 125 |
# e = new.env() |
| 126 |
# input = rawConnection(o, "r") |
| 127 |
# load(input, e) |
| 128 |
} |
| 129 |
\keyword{IO } |
| 130 |
\keyword{programming} |