-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoji.go
74 lines (61 loc) · 1.77 KB
/
encoji.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
package main
import (
"bytes"
"encoding/base64"
"flag"
"io/ioutil"
"os"
)
func makeEncoding(decode bool) (encoding map[rune]rune) {
base64Runes := bytes.Runes([]byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="))
encojiRunes := bytes.Runes([]byte("😀😬😁😂😃😄😅😆😇😉😊😋😌😍😘😗😙😚😜😝😛😎😏💩👻😺😸😹😻🐶🐱🐭🐹🐰🐻🐼🐨🐯🐮🐷🐽🐸🐙🐵🙈🙉🙊🐒🐔🐧🐦🐤🐣🐥🐺🐗🐴🐝🐛🐌🐞🐜🐘🐬🐳"))
if len(base64Runes) != len(encojiRunes) {
panic("Charsets must be of same length")
}
encoding = make(map[rune]rune)
var from, to []rune
if decode {
from = encojiRunes
to = base64Runes
} else {
from = base64Runes
to = encojiRunes
}
for i := 0; i < len(from); i++ {
encoding[from[i]] = to[i]
}
return encoding
}
func makeMapper(encoding map[rune]rune) func(rune) rune {
return func(in rune) rune {
if out, ok := encoding[in]; ok {
return out
} else {
return rune(-1)
}
}
}
func encode(in []byte) (out []byte) {
intermediate := make([]byte, base64.StdEncoding.EncodedLen(len(in)))
base64.StdEncoding.Encode(intermediate, in)
return bytes.Map(makeMapper(makeEncoding(false)), intermediate)
}
func decode(in []byte) (out []byte) {
intermediate := bytes.Map(makeMapper(makeEncoding(true)), in)
out = make([]byte, base64.StdEncoding.DecodedLen(len(intermediate)))
base64.StdEncoding.Decode(out, intermediate)
return out
}
func main() {
decodeFlag := flag.Bool("d", false, "Decode encoji data (default is to encode)")
flag.Parse()
raw, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic("Could not read from stdin\n")
}
if *decodeFlag {
os.Stdout.Write(decode(raw))
} else {
os.Stdout.Write(encode(raw))
}
}