File: base64.R

package info (click to toggle)
r-cran-jsonlite 1.8.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,340 kB
  • sloc: ansic: 3,794; sh: 9; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,706 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#' Encode/decode base64
#'
#' Simple in-memory base64 encoder and decoder. Used internally for converting
#' raw vectors to text. Interchangeable with encoder from `base64enc` or
#' `openssl` package.
#'
#' The [base64url_enc] and [base64url_dec] functions use a variation of base64
#' that substitute characters `+/`  for `-_` respectively, such that the output
#' does not require URL-encoding. See also section 5 of rfc4648.
#'
#' @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)
}

#' @export
#' @rdname base64
base64url_enc <- function(input){
  text <- base64_enc(input)
  sub("=+$", "", chartr('+/', '-_', text))
}

#' @export
#' @rdname base64
base64url_dec <- function(input){
  text <- fix_padding(chartr('-_', '+/', input))
  base64_dec(text)
}

# Ensures base64 length is a multiple of 4
fix_padding <- function(text){
  text <- gsub("[\r\n]", "", text)[[1]]
  mod <- nchar(text) %% 4;
  if(mod > 0){
    padding <- paste(rep("=", (4 - mod)), collapse = "")
    text <- paste0(text, padding)
  }
  text
}