forked from kdomanski/iso9660
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mangle.go
83 lines (66 loc) · 1.89 KB
/
mangle.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package iso9660
import (
"path"
"strings"
)
func manglePath(input string) (string, string) {
nonEmptySegments := splitPath(path.Clean(input))
dirSegments := nonEmptySegments[:len(nonEmptySegments)-1]
name := nonEmptySegments[len(nonEmptySegments)-1]
for i := 0; i < len(dirSegments); i++ {
dirSegments[i] = mangleDirectoryName(dirSegments[i])
}
name = mangleFileName(name)
return path.Join(dirSegments...), name
}
func splitPath(input string) []string {
rawSegments := strings.Split(input, "/")
var nonEmptySegments []string
for _, s := range rawSegments {
if len(s) > 0 {
nonEmptySegments = append(nonEmptySegments, s)
}
}
return nonEmptySegments
}
// See ECMA-119 7.5
func mangleFileName(input string) string {
input = strings.ToUpper(input)
split := strings.Split(input, ".")
version := "1"
var filename, extension string
if len(split) == 1 {
filename = split[0]
} else {
filename = strings.Join(split[:len(split)-1], "_")
extension = split[len(split)-1]
}
// enough characters for the `.ignition` extension
extension = mangleDString(extension, 8)
maxRemainingFilenameLength := primaryVolumeFileIdentifierMaxLength - (1 + len(version))
if len(extension) > 0 {
maxRemainingFilenameLength -= (1 + len(extension))
}
filename = mangleDString(filename, maxRemainingFilenameLength)
if len(extension) > 0 {
return filename + "." + extension + ";" + version
}
return filename + ";" + version
}
// See ECMA-119 7.6
func mangleDirectoryName(input string) string {
return mangleDString(input, primaryVolumeDirectoryIdentifierMaxLength)
}
func mangleDString(input string, maxCharacters int) string {
input = strings.ToUpper(input)
var mangledString string
for i := 0; i < len(input) && i < maxCharacters; i++ {
r := rune(input[i])
if strings.ContainsRune(dCharacters, r) {
mangledString += string(r)
} else {
mangledString += "_"
}
}
return mangledString
}