escape.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Implements systemd-escape [--unescape] [--path]
  15. package unit
  16. import (
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. )
  21. const (
  22. allowed = `:_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`
  23. )
  24. // If isPath is true:
  25. // We remove redundant '/'s, the leading '/', and trailing '/'.
  26. // If the result is empty, a '/' is inserted.
  27. //
  28. // We always:
  29. // Replace the following characters with `\x%x`:
  30. // Leading `.`
  31. // `-`, `\`, and anything not in this set: `:-_.\[0-9a-zA-Z]`
  32. // Replace '/' with '-'.
  33. func escape(unescaped string, isPath bool) string {
  34. e := []byte{}
  35. inSlashes := false
  36. start := true
  37. for i := 0; i < len(unescaped); i++ {
  38. c := unescaped[i]
  39. if isPath {
  40. if c == '/' {
  41. inSlashes = true
  42. continue
  43. } else if inSlashes {
  44. inSlashes = false
  45. if !start {
  46. e = append(e, '-')
  47. }
  48. }
  49. }
  50. if c == '/' {
  51. e = append(e, '-')
  52. } else if start && c == '.' || strings.IndexByte(allowed, c) == -1 {
  53. e = append(e, []byte(fmt.Sprintf(`\x%x`, c))...)
  54. } else {
  55. e = append(e, c)
  56. }
  57. start = false
  58. }
  59. if isPath && len(e) == 0 {
  60. e = append(e, '-')
  61. }
  62. return string(e)
  63. }
  64. // If isPath is true:
  65. // We always return a string beginning with '/'.
  66. //
  67. // We always:
  68. // Replace '-' with '/'.
  69. // Replace `\x%x` with the value represented in hex.
  70. func unescape(escaped string, isPath bool) string {
  71. u := []byte{}
  72. for i := 0; i < len(escaped); i++ {
  73. c := escaped[i]
  74. if c == '-' {
  75. c = '/'
  76. } else if c == '\\' && len(escaped)-i >= 4 && escaped[i+1] == 'x' {
  77. n, err := strconv.ParseInt(escaped[i+2:i+4], 16, 8)
  78. if err == nil {
  79. c = byte(n)
  80. i += 3
  81. }
  82. }
  83. u = append(u, c)
  84. }
  85. if isPath && (len(u) == 0 || u[0] != '/') {
  86. u = append([]byte("/"), u...)
  87. }
  88. return string(u)
  89. }
  90. // UnitNameEscape escapes a string as `systemd-escape` would
  91. func UnitNameEscape(unescaped string) string {
  92. return escape(unescaped, false)
  93. }
  94. // UnitNameUnescape unescapes a string as `systemd-escape --unescape` would
  95. func UnitNameUnescape(escaped string) string {
  96. return unescape(escaped, false)
  97. }
  98. // UnitNamePathEscape escapes a string as `systemd-escape --path` would
  99. func UnitNamePathEscape(unescaped string) string {
  100. return escape(unescaped, true)
  101. }
  102. // UnitNamePathUnescape unescapes a string as `systemd-escape --path --unescape` would
  103. func UnitNamePathUnescape(escaped string) string {
  104. return unescape(escaped, true)
  105. }