Debian Codegraph @

fat/short_name.go

raw symbols SCIP
1 package fat
2
3 import (
4 "bytes"
5 "fmt"
6 "strings"
7 )
8
9 // checksumShortName returns the checksum for the shortname that is used
10 // for the long name entries.
11 func checksumShortName(name string) uint8 {
12 var sum uint8 = name[0]
13 for i := uint8(1); i < 11; i++ {
14 sum = name[i] + (((sum & 1) << 7) + ((sum & 0xFE) >> 1))
15 }
16
17 return sum
18 }
19
20 // generateShortName takes a list of existing short names and a long
21 // name and generates the next valid short name. This process is done
22 // according to the MS specification.
23 func generateShortName(longName string, used []string) (string, error) {
24 longName = strings.ToUpper(longName)
25
26 // Split the string at the final "."
27 dotIdx := strings.LastIndex(longName, ".")
28
29 var ext string
30 if dotIdx == -1 {
31 dotIdx = len(longName)
32 } else {
33 ext = longName[dotIdx+1:]
34 }
35
36 ext = cleanShortString(ext)
37 ext = ext[0:]
38 rawName := longName[0:dotIdx]
39 name := cleanShortString(rawName)
40 simpleName := fmt.Sprintf("%s.%s", name, ext)
41 if ext == "" {
42 simpleName = simpleName[0 : len(simpleName)-1]
43 }
44
45 doSuffix := name != rawName || len(name) > 8 || len(ext) > 3
46 if !doSuffix {
47 for _, usedSingle := range used {
48 if strings.ToUpper(usedSingle) == simpleName {
49 doSuffix = true
50 break
51 }
52 }
53 }
54
55 if doSuffix {
56 if len(ext) > 3 {
57 ext = ext[:3]
58 }
59 found := false
60 for i := 1; i < 99999; i++ {
61 serial := fmt.Sprintf("~%d", i)
62
63 nameOffset := 8 - len(serial)
64 if len(name) < nameOffset {
65 nameOffset = len(name)
66 }
67
68 serialName := fmt.Sprintf("%s%s", name[0:nameOffset], serial)
69 simpleName = fmt.Sprintf("%s.%s", serialName, ext)
70
71 exists := false
72 for _, usedSingle := range used {
73 if strings.ToUpper(usedSingle) == simpleName {
74 exists = true
75 break
76 }
77 }
78
79 if !exists {
80 found = true
81 break
82 }
83 }
84
85 if !found {
86 return "", fmt.Errorf("could not generate short name for %s", longName)
87 }
88 }
89
90 return simpleName, nil
91 }
92
93 // shortNameEntryValue returns the proper formatted short name value
94 // for the directory cluster entry.
95 func shortNameEntryValue(name string) string {
96 var shortParts []string
97 if name == "." || name == ".." {
98 shortParts = []string{name, ""}
99 } else {
100 shortParts = strings.Split(name, ".")
101 }
102
103 if len(shortParts) == 1 {
104 shortParts = append(shortParts, "")
105 }
106
107 if len(shortParts[0]) < 8 {
108 var temp bytes.Buffer
109 temp.WriteString(shortParts[0])
110 for i := 0; i < 8-len(shortParts[0]); i++ {
111 temp.WriteRune(' ')
112 }
113
114 shortParts[0] = temp.String()
115 }
116
117 if len(shortParts[1]) < 3 {
118 var temp bytes.Buffer
119 temp.WriteString(shortParts[1])
120 for i := 0; i < 3-len(shortParts[1]); i++ {
121 temp.WriteRune(' ')
122 }
123
124 shortParts[1] = temp.String()
125 }
126
127 return fmt.Sprintf("%s%s", shortParts[0], shortParts[1])
128 }
129
130 func cleanShortString(v string) string {
131 var result bytes.Buffer
132 for _, char := range v {
133 // We skip these chars
134 if char == '.' || char == ' ' {
135 continue
136 }
137
138 if !validShortChar(char) {
139 char = '_'
140 }
141
142 result.WriteRune(char)
143 }
144
145 return result.String()
146 }
147
148 func validShortChar(char rune) bool {
149 if char >= 'A' && char <= 'Z' {
150 return true
151 }
152
153 if char >= '0' && char <= '9' {
154 return true
155 }
156
157 validShortSymbols := []rune{
158 '_', '^', '$', '~', '!', '#', '%', '&', '-', '{', '}', '(',
159 ')', '@', '\'', '`',
160 }
161
162 for _, valid := range validShortSymbols {
163 if char == valid {
164 return true
165 }
166 }
167
168 return false
169 }