File: base64.R

package info (click to toggle)
r-cran-jsonlite 1.7.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,320 kB
  • sloc: ansic: 3,792; sh: 9; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#' Encode/decode base64
#'
#' Simple in-memory base64 encoder and decoder. Used internally for converting
#' raw vectors to text. Interchangeable with encoder from \code{base64enc} or
#' \code{openssl} package.
#'
#' @param input string or raw vector to be encoded/decoded
#' @export
#' @rdname base64
#' @name base64
#' @useDynLib jsonlite R_base64_decode
#' @examples str <- base64_enc(serialize(iris, NULL))
#' out <- unserialize(base64_dec(str))
#' stopifnot(identical(out, iris))
base64_dec <- function(input) {
  if(is.character(input)){
    input <- charToRaw(paste(input, collapse = "\n"))
  }
  stopifnot(is.raw(input))
  .Call(R_base64_decode, input)
}

#' @export
#' @rdname base64
#' @useDynLib jsonlite R_base64_encode
base64_enc <- function(input) {
  if(is.null(input))
    return(NA_character_)
  if(is.character(input)){
    input <- charToRaw(paste(input, collapse = "\n"))
  }
  stopifnot(is.raw(input))
  .Call(R_base64_encode, input)
}