SHARE
TWEET

Untitled

a guest Sep 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "io"
  7.     "os/exec"
  8.     "strings"
  9. )
  10.  
  11. func main() {
  12.     cmd := exec.Command("idevicesyslog")
  13.     stdout, err := cmd.StdoutPipe()
  14.     if err != nil {
  15.         panic(err)
  16.     }
  17.     stderr, err := cmd.StderrPipe()
  18.     if err != nil {
  19.         panic(err)
  20.     }
  21.     err = cmd.Start()
  22.     if err != nil {
  23.         panic(err)
  24.     }
  25.  
  26.     go copyOutput(stdout)
  27.     go copyOutput(stderr)
  28.     cmd.Wait()
  29. }
  30.  
  31. func copyOutput(r io.Reader) {
  32.     scanner := bufio.NewScanner(r)
  33.     for scanner.Scan() {
  34.         fmt.Println(decodeSyslog(scanner.Text()))
  35.     }
  36. }
  37.  
  38. func decodeSyslog(line string) string { // this function handles all special encoded characters
  39.     specialChar := strings.Contains(line, `\134`) // this \134
  40.     if specialChar {
  41.         line = strings.Replace(line, `\134`, "", -1)
  42.     }
  43.     kBackslash := byte(0x5c)
  44.     kM := byte(0x4d)
  45.     kDash := byte(0x2d)
  46.     kCaret := byte(0x5e)
  47.  
  48.     // Mask for the UTF-8 digit range.
  49.     kNum := byte(0x30)
  50.  
  51.     bytes := []byte(line)
  52.     var out []byte
  53.     for i := 0; i < len(bytes); {
  54.  
  55.         if (bytes[i] != kBackslash) || i > (len(bytes)-4) {
  56.             out = append(out, bytes[i])
  57.             i = i + 1
  58.         } else {
  59.             if (bytes[i+1] == kM) && (bytes[i+2] == kCaret) {
  60.                 out = append(out, (bytes[i+3]&byte(0x7f))+byte(0x40))
  61.             } else if bytes[i+1] == kM && bytes[i+2] == kDash {
  62.                 out = append(out, bytes[i+3]|byte(0x80))
  63.             } else if isDigit(bytes[i+1:i+3], kNum) {
  64.                 out = append(out, decodeOctal(bytes[i+1], bytes[i+2], bytes[i+3]))
  65.             } else {
  66.                 out = append(out, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4])
  67.             }
  68.             i = i + 4
  69.         }
  70.     }
  71.     return string(out)
  72. }
  73.  
  74. func isDigit(b []byte, kNum byte) bool {
  75.     for _, v := range b {
  76.         if (v & byte(0xf0)) != kNum {
  77.             return false
  78.         }
  79.     }
  80.     return true
  81. }
  82.  
  83. func decodeOctal(x, y, z byte) byte {
  84.     return (x&byte(0x3))<<byte(6) | (y&byte(0x7))<<byte(3) | z&byte(0x7)
  85. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top