-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathparamformat.go
More file actions
52 lines (46 loc) · 1.57 KB
/
paramformat.go
File metadata and controls
52 lines (46 loc) · 1.57 KB
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
package runtime
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
)
// isByteSlice reports whether t is []byte (or equivalently []uint8).
func isByteSlice(t reflect.Type) bool {
return t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8
}
// base64Decode decodes s as base64.
//
// Per OpenAPI 3.0, format: byte uses RFC 4648 Section 4 (standard alphabet,
// padded). We use padding presence to select the right decoder, rather than
// blindly cascading (which can produce corrupt output when RawStdEncoding
// silently accepts padded input and treats '=' as data).
//
// The logic:
// 1. If s contains '=' padding → standard padded decoder (Std or URL based on alphabet).
// 2. If s contains URL-safe characters ('_' or '-') → RawURLEncoding.
// 3. Otherwise → RawStdEncoding (unpadded, standard alphabet).
func base64Decode(s string) ([]byte, error) {
if s == "" {
return []byte{}, nil
}
if strings.ContainsRune(s, '=') {
// Padded input. Pick alphabet based on whether URL-safe chars are present.
if strings.ContainsAny(s, "-_") {
return base64Decode1(base64.URLEncoding, s)
}
return base64Decode1(base64.StdEncoding, s)
}
// Unpadded input. Pick alphabet based on whether URL-safe chars are present.
if strings.ContainsAny(s, "-_") {
return base64Decode1(base64.RawURLEncoding, s)
}
return base64Decode1(base64.RawStdEncoding, s)
}
func base64Decode1(enc *base64.Encoding, s string) ([]byte, error) {
b, err := enc.DecodeString(s)
if err != nil {
return nil, fmt.Errorf("failed to base64-decode string %q: %w", s, err)
}
return b, nil
}