Debian Codegraph @

R/base64.R

raw
1
2 the$base64_table <- as.integer(charToRaw("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="))
3
4 renv_base64_encode_main <- function(input) {
5
6 ni <- as.integer(length(input))
7 if (ni < 3L)
8 return(integer())
9
10 no <- ni %/% 3L * 4L
11 output <- integer(no)
12
13 i0 <- seq.int(1L, ni - 2L, by = 3L)
14 i1 <- seq.int(2L, ni - 1L, by = 3L)
15 i2 <- seq.int(3L, ni - 0L, by = 3L)
16
17 o0 <- seq.int(1L, no - 3L, by = 4L)
18 o1 <- seq.int(2L, no - 2L, by = 4L)
19 o2 <- seq.int(3L, no - 1L, by = 4L)
20 o3 <- seq.int(4L, no - 0L, by = 4L)
21
22 output[o0] <- the$base64_table[1L + bitwShiftR(input[i0], 2L)]
23
24 output[o1] <- the$base64_table[1L + bitwOr(
25 bitwShiftL(bitwAnd(input[i0], 0x03L), 4L),
26 bitwShiftR(bitwAnd(input[i1], 0xF0L), 4L)
27 )]
28
29 output[o2] <- the$base64_table[1L + bitwOr(
30 bitwShiftL(bitwAnd(input[i1], 0x0FL), 2L),
31 bitwShiftR(bitwAnd(input[i2], 0xC0L), 6L)
32 )]
33
34 output[o3] <- the$base64_table[1L + bitwAnd(input[i2], 0x3FL)]
35
36 output
37
38 }
39
40 renv_base64_encode_rest <- function(input) {
41
42 ni <- as.integer(length(input))
43 remaining <- ni %% 3L
44 if (remaining == 0L)
45 return(integer())
46
47 output <- rep.int(61L, 4L)
48 i <- ni - remaining + 1
49
50 output[1L] <- the$base64_table[1L + bitwShiftR(input[i + 0L], 2L)]
51
52 if (remaining == 1L) {
53
54 output[2L] <- the$base64_table[1L + bitwShiftL(bitwAnd(input[i + 0L], 0x03L), 4L)]
55
56 } else if (remaining == 2L) {
57
58 output[2L] <- the$base64_table[1L + bitwOr(
59 bitwShiftL(bitwAnd(input[i + 0L], 0x03L), 4L),
60 bitwShiftR(bitwAnd(input[i + 1L], 0xF0L), 4L)
61 )]
62
63 output[3L] <- the$base64_table[1L + bitwShiftL(bitwAnd(input[i + 1L], 0x0FL), 2L)]
64
65 }
66
67 output
68
69 }
70
71 renv_base64_encode <- function(text) {
72
73 # convert to raw vector
74 input <- case(
75 is.character(text) ~ as.integer(charToRaw(text)),
76 is.raw(text) ~ as.integer(text),
77 ~ stopf("unexpected input type '%s'", typeof(text))
78 )
79
80 encoded <- c(
81 renv_base64_encode_main(input),
82 renv_base64_encode_rest(input)
83 )
84
85 rawToChar(as.raw(encoded))
86
87 }
88
89 the$base64_decode_table <- NULL
90 renv_base64_decode_table <- function() {
91 the$base64_decode_table <- the$base64_decode_table %||% {
92 table <- integer(255)
93 text <- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
94 table[utf8ToInt(text)] <- seq_len(nchar(text)) - 1L
95 table
96 }
97 }
98
99 renv_base64_decode_main <- function(input) {
100
101 ni <- length(input)
102 no <- (ni * 3L) %/% 4L
103
104 output <- integer(no)
105
106 i0 <- seq(from = 1L, to = ni - 3L, by = 4L)
107 i1 <- seq(from = 2L, to = ni - 2L, by = 4L)
108 i2 <- seq(from = 3L, to = ni - 1L, by = 4L)
109 i3 <- seq(from = 4L, to = ni - 0L, by = 4L)
110
111 o0 <- seq.int(1L, no - 2L, by = 3L)
112 o1 <- seq.int(2L, no - 1L, by = 3L)
113 o2 <- seq.int(3L, no - 0L, by = 3L)
114
115 t <- renv_base64_decode_table()
116
117 output[o0] <- bitwOr(
118 bitwAnd(bitwShiftL(t[input[i0]], 2L), 255L),
119 bitwAnd(bitwShiftR(t[input[i1]], 4L), 255L)
120 )
121
122 output[o1] <- bitwOr(
123 bitwAnd(bitwShiftL(t[input[i1]], 4L), 255L),
124 bitwAnd(bitwShiftR(t[input[i2]], 2L), 255L)
125 )
126
127 output[o2] <- bitwOr(
128 bitwAnd(bitwShiftL(t[input[i2]], 6L), 255L),
129 bitwAnd(bitwShiftR(t[input[i3]], 0L), 255L)
130 )
131
132 output
133
134 }
135
136 renv_base64_decode <- function(encoded) {
137
138 # remove newlines
139 if (c(regexpr("\n", encoded, fixed = TRUE)) != -1L)
140 encoded <- gsub("\n", "", encoded, fixed = TRUE)
141
142 # convert to raw vector
143 input <- case(
144 is.character(encoded) ~ as.integer(charToRaw(encoded)),
145 is.raw(encoded) ~ as.integer(encoded),
146 ~ stopf("unexpected input type '%s'", typeof(encoded))
147 )
148
149 # decode vector
150 output <- renv_base64_decode_main(input)
151
152 # trim off padded bits
153 n <- length(input)
154 if (input[n - 1L] == 61L)
155 output <- head(output, n = -2L)
156 else if (input[n] == 61L)
157 output <- head(output, n = -1L)
158
159 # convert back to string
160 rawToChar(as.raw(output))
161
162 }