Debian Codegraph @

jose-util/generator/encoding.go

raw
1 /*-
2 * Copyright 2019 Square Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package generator
18
19 import "io"
20
21 // Base64Reader wraps an input stream consisting of either standard or url-safe
22 // base64 data, and maps it to a raw (unpadded) standard encoding. This can be used
23 // to read any base64-encoded data as input, whether padded, unpadded, standard or
24 // url-safe.
25 type Base64Reader struct {
26 In io.Reader
27 }
28
29 func (r Base64Reader) Read(p []byte) (n int, err error) {
30 n, err = r.In.Read(p)
31
32 for i := 0; i < n; i++ {
33 switch p[i] {
34 // Map - to +
35 case 0x2D:
36 p[i] = 0x2B
37 // Map _ to /
38 case 0x5F:
39 p[i] = 0x2F
40 // Strip =
41 case 0x3D:
42 n = i
43 break
44 default:
45 }
46 }
47
48 if n == 0 {
49 err = io.EOF
50 }
51
52 return
53 }