| 1 |
// |
| 2 |
// Copyright (c) 2016-2022 The Aurora Authors. All rights reserved. |
| 3 |
// This program is free software. It comes without any warranty, |
| 4 |
// to the extent permitted by applicable law. You can redistribute |
| 5 |
// it and/or modify it under the terms of the Unlicense. See LICENSE |
| 6 |
// file for more details or see below. |
| 7 |
// |
| 8 |
|
| 9 |
// |
| 10 |
// This is free and unencumbered software released into the public domain. |
| 11 |
// |
| 12 |
// Anyone is free to copy, modify, publish, use, compile, sell, or |
| 13 |
// distribute this software, either in source code form or as a compiled |
| 14 |
// binary, for any purpose, commercial or non-commercial, and by any |
| 15 |
// means. |
| 16 |
// |
| 17 |
// In jurisdictions that recognize copyright laws, the author or authors |
| 18 |
// of this software dedicate any and all copyright interest in the |
| 19 |
// software to the public domain. We make this dedication for the benefit |
| 20 |
// of the public at large and to the detriment of our heirs and |
| 21 |
// successors. We intend this dedication to be an overt act of |
| 22 |
// relinquishment in perpetuity of all present and future rights to this |
| 23 |
// software under copyright law. |
| 24 |
// |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 |
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 29 |
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 30 |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 31 |
// OTHER DEALINGS IN THE SOFTWARE. |
| 32 |
// |
| 33 |
// For more information, please refer to <http://unlicense.org/> |
| 34 |
// |
| 35 |
|
| 36 |
package aurora |
| 37 |
|
| 38 |
import ( |
| 39 |
"fmt" |
| 40 |
"io" |
| 41 |
) |
| 42 |
|
| 43 |
const ( |
| 44 |
linkStartEsc = "\033]8;" |
| 45 |
linkMiddleEsc = "\033\\" |
| 46 |
linkEndEsc = linkStartEsc + ";" + linkMiddleEsc |
| 47 |
) |
| 48 |
|
| 49 |
// Hyperlinks related constants |
| 50 |
const ( |
| 51 |
HyperlinkIDKey = "id" // hyperlink id parameter key |
| 52 |
) |
| 53 |
|
| 54 |
// The HyperlinkParam represents a hyperlink parameter. |
| 55 |
type HyperlinkParam struct { |
| 56 |
Key string // parameter name |
| 57 |
Value string // parameter value |
| 58 |
} |
| 59 |
|
| 60 |
func (hp HyperlinkParam) stringLen() int { |
| 61 |
return len(hp.Key) + 1 + len(hp.Value) |
| 62 |
} |
| 63 |
|
| 64 |
// String represents the HyperlinkParam as string, e.g. in key=value form. |
| 65 |
func (hp HyperlinkParam) String() string { |
| 66 |
return hp.Key + "=" + hp.Value |
| 67 |
} |
| 68 |
|
| 69 |
// IsValidHyperlinkTarget returns true if the target contains symbols only |
| 70 |
// in 32-126 ASCII range. All symbols outside this range should be URL-escaped. |
| 71 |
func IsValidHyperlinkTarget(target string) (valid bool) { |
| 72 |
// we can walk over bytes, without Unicode runes decoding |
| 73 |
for i := 0; i < len(target); i++ { |
| 74 |
if target[i] < 32 || 126 < target[i] { |
| 75 |
return // false, should be URL-escaped |
| 76 |
} |
| 77 |
} |
| 78 |
return true |
| 79 |
} |
| 80 |
|
| 81 |
// range over bytes (not Unicode runes) and check |
| 82 |
func containsAny(in string, any ...byte) (contains bool) { |
| 83 |
for i := 0; i < len(in); i++ { |
| 84 |
for _, b := range any { |
| 85 |
if in[i] == b { |
| 86 |
return true |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
return // false |
| 91 |
} |
| 92 |
|
| 93 |
// IsValidHyperlinkParam returns true for given string, if the string |
| 94 |
// is valid hyperlink target (see IsValidHyperlinkTarget) and doesn't |
| 95 |
// contains ':', ';' and '='. |
| 96 |
func IsValidHyperlinkParam(param string) (valid bool) { |
| 97 |
return IsValidHyperlinkTarget(param) && !containsAny(param, ':', ';', '=') |
| 98 |
} |
| 99 |
|
| 100 |
// HyperlinkID returns list of HyperlinkParams that contains only id parameter |
| 101 |
// with given value of the id parameter. |
| 102 |
func HyperlinkID(id string) HyperlinkParam { |
| 103 |
return HyperlinkParam{ |
| 104 |
Key: HyperlinkIDKey, |
| 105 |
Value: id, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
type hyperlink struct { |
| 110 |
target string // hyperlink target |
| 111 |
params []HyperlinkParam // hyperlink parameters |
| 112 |
} |
| 113 |
|
| 114 |
func (h *hyperlink) isExists() (ok bool) { |
| 115 |
if h == nil { |
| 116 |
return // does not exist |
| 117 |
} |
| 118 |
return h.target != "" |
| 119 |
} |
| 120 |
|
| 121 |
func (h *hyperlink) stringParamsLen() (ln int) { |
| 122 |
for i, p := range h.params { |
| 123 |
if i > 0 { |
| 124 |
ln++ // + colon separator |
| 125 |
} |
| 126 |
ln += p.stringLen() |
| 127 |
} |
| 128 |
return |
| 129 |
} |
| 130 |
|
| 131 |
func (h *hyperlink) headLen() int { |
| 132 |
return len(linkStartEsc) + |
| 133 |
h.stringParamsLen() + |
| 134 |
len(";") + |
| 135 |
len(h.target) + |
| 136 |
+len(linkMiddleEsc) |
| 137 |
} |
| 138 |
|
| 139 |
func (h *hyperlink) headBytes() (t []byte) { |
| 140 |
t = make([]byte, 0, h.headLen()) |
| 141 |
|
| 142 |
t = append(t, linkStartEsc...) |
| 143 |
for i, param := range h.params { |
| 144 |
if i > 0 { |
| 145 |
t = append(t, ':') |
| 146 |
} |
| 147 |
t = append(t, param.Key...) |
| 148 |
t = append(t, '=') |
| 149 |
t = append(t, param.Value...) |
| 150 |
} |
| 151 |
t = append(t, ';') |
| 152 |
t = append(t, h.target...) |
| 153 |
t = append(t, linkMiddleEsc...) |
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
func (h *hyperlink) tailLen() int { |
| 158 |
return len(linkEndEsc) |
| 159 |
} |
| 160 |
|
| 161 |
func (h *hyperlink) tailBytes() []byte { |
| 162 |
return []byte(linkEndEsc) |
| 163 |
} |
| 164 |
|
| 165 |
func (h *hyperlink) writeHead(w io.Writer) { |
| 166 |
if h == nil || h.target == "" { |
| 167 |
return |
| 168 |
} |
| 169 |
w.Write(h.headBytes()) //nolint |
| 170 |
} |
| 171 |
|
| 172 |
func (h *hyperlink) writeTail(w io.Writer) { |
| 173 |
if h == nil || h.target == "" { |
| 174 |
return |
| 175 |
} |
| 176 |
w.Write(h.tailBytes()) //nolint |
| 177 |
} |
| 178 |
|
| 179 |
func shouldEscape(c byte) bool { |
| 180 |
return c < 32 || 126 < c |
| 181 |
} |
| 182 |
|
| 183 |
func isHex(c byte) bool { |
| 184 |
switch { |
| 185 |
case '0' <= c && c <= '9': |
| 186 |
return true |
| 187 |
case 'a' <= c && c <= 'f': |
| 188 |
return true |
| 189 |
case 'A' <= c && c <= 'F': |
| 190 |
return true |
| 191 |
} |
| 192 |
return false |
| 193 |
} |
| 194 |
|
| 195 |
func unhex(c byte) byte { |
| 196 |
switch { |
| 197 |
case '0' <= c && c <= '9': |
| 198 |
return c - '0' |
| 199 |
case 'a' <= c && c <= 'f': |
| 200 |
return c - 'a' + 10 |
| 201 |
case 'A' <= c && c <= 'F': |
| 202 |
return c - 'A' + 10 |
| 203 |
} |
| 204 |
return 0 |
| 205 |
} |
| 206 |
|
| 207 |
// HyperlinkEscape escapes all symbols of given string out of [32; 126] range |
| 208 |
// using URL-encoding. Used to escape a hyperlink target. |
| 209 |
func HyperlinkEscape(s string) string { |
| 210 |
|
| 211 |
var hexCount int |
| 212 |
for i := 0; i < len(s); i++ { |
| 213 |
if shouldEscape(s[i]) { |
| 214 |
hexCount++ |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if hexCount == 0 { |
| 219 |
return s |
| 220 |
} |
| 221 |
|
| 222 |
const upperhex = "0123456789ABCDEF" |
| 223 |
|
| 224 |
var ( |
| 225 |
t = make([]byte, len(s)+2*hexCount) |
| 226 |
j int |
| 227 |
) |
| 228 |
|
| 229 |
for i := 0; i < len(s); i++ { |
| 230 |
switch c := s[i]; { |
| 231 |
case shouldEscape(c): |
| 232 |
t[j] = '%' |
| 233 |
t[j+1] = upperhex[c>>4] |
| 234 |
t[j+2] = upperhex[c&15] |
| 235 |
j += 3 |
| 236 |
default: |
| 237 |
t[j] = s[i] |
| 238 |
j++ |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return string(t) |
| 243 |
} |
| 244 |
|
| 245 |
// HyperlinkUnescape reverts a string escaped by the HyperlinkEscape. |
| 246 |
func HyperlinkUnescape(s string) (raw string, err error) { |
| 247 |
|
| 248 |
var n int |
| 249 |
for i := 0; i < len(s); { |
| 250 |
switch s[i] { |
| 251 |
case '%': |
| 252 |
n++ |
| 253 |
if i+2 >= len(s) || !isHex(s[i+1]) || !isHex(s[i+2]) { |
| 254 |
s = s[i:] |
| 255 |
if len(s) > 3 { |
| 256 |
s = s[:3] |
| 257 |
} |
| 258 |
return "", fmt.Errorf("invalid URL-escape sequence: %q", s) |
| 259 |
} |
| 260 |
i += 3 |
| 261 |
default: |
| 262 |
i++ |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if n == 0 { |
| 267 |
return s, nil |
| 268 |
} |
| 269 |
|
| 270 |
var t = make([]byte, 0, len(s)-2*n) |
| 271 |
|
| 272 |
for i := 0; i < len(s); i++ { |
| 273 |
switch s[i] { |
| 274 |
case '%': |
| 275 |
t = append(t, unhex(s[i+1])<<4|unhex(s[i+2])) |
| 276 |
i += 2 |
| 277 |
default: |
| 278 |
t = append(t, s[i]) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return string(t), nil |
| 283 |
} |