Skip to content

Instantly share code, notes, and snippets.

@PhilCai1993
Last active September 9, 2019 05:55
Show Gist options
  • Select an option

  • Save PhilCai1993/9f5f6235b39467ec226e636f18ea1edd to your computer and use it in GitHub Desktop.

Select an option

Save PhilCai1993/9f5f6235b39467ec226e636f18ea1edd to your computer and use it in GitHub Desktop.
idevicesyslog unicode support
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
func main() {
cmd := exec.Command("idevicesyslog")
stdout, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
err = cmd.Start()
if err != nil {
panic(err)
}
go copyOutput(stdout)
go copyOutput(stderr)
cmd.Wait()
}
func copyOutput(r io.Reader) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
fmt.Println(decodeSyslog(scanner.Text()))
}
}
func decodeSyslog(line string) string { // this function handles all special encoded characters
specialChar := strings.Contains(line, `\134`) // this \134
if specialChar {
line = strings.Replace(line, `\134`, "", -1)
}
kBackslash := byte(0x5c)
kM := byte(0x4d)
kDash := byte(0x2d)
kCaret := byte(0x5e)
// Mask for the UTF-8 digit range.
kNum := byte(0x30)
bytes := []byte(line)
var out []byte
for i := 0; i < len(bytes); {
if (bytes[i] != kBackslash) || i > (len(bytes)-4) {
out = append(out, bytes[i])
i = i + 1
} else {
if (bytes[i+1] == kM) && (bytes[i+2] == kCaret) {
out = append(out, (bytes[i+3]&byte(0x7f))+byte(0x40))
} else if bytes[i+1] == kM && bytes[i+2] == kDash {
out = append(out, bytes[i+3]|byte(0x80))
} else if isDigit(bytes[i+1:i+3], kNum) {
out = append(out, decodeOctal(bytes[i+1], bytes[i+2], bytes[i+3]))
} else {
out = append(out, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4])
}
i = i + 4
}
}
return string(out)
}
func isDigit(b []byte, kNum byte) bool {
for _, v := range b {
if (v & byte(0xf0)) != kNum {
return false
}
}
return true
}
func decodeOctal(x, y, z byte) byte {
return (x&byte(0x3))<<byte(6) | (y&byte(0x7))<<byte(3) | z&byte(0x7)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment