Skip to content

Commit

Permalink
Add Danmaku decode CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Jul 30, 2023
1 parent 3fad418 commit e8c1fc3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"bufio"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/keuin/slbr/danmaku/dmpkg"
"log"
"os"
)

func main() {
r := bufio.NewReader(os.Stdin)
for {
line, _, err := r.ReadLine()
if err != nil {
panic(err)
}
var data []byte
err = func() error {
data, err = hex.DecodeString(string(line))
if err == nil {
return nil
}
data, err = base64.StdEncoding.DecodeString(string(line))
if err != nil {
log.Println("Failed to decode as Hex or Base64 string")
}
return err
}()
if err != nil {
continue
}
ex, err := dmpkg.DecodeExchange(data)
if err != nil {
log.Println("Failed to decode Danmaku exchange: ", err)
continue
}
ex, err = ex.Inflate()
if err != nil {
log.Println("Failed to decompress Danmaku exchange: ", err)
continue
}
fmt.Println(ex.PrettyString())
}
}
5 changes: 5 additions & 0 deletions danmaku/dmpkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (e *DanmakuExchange) String() string {
e.Length, e.ProtocolVer, e.Operation, e.Body)
}

func (e *DanmakuExchange) PrettyString() string {
return fmt.Sprintf("DanmakuExchange{length=%v, protocol=%v, operation=%v, body=%v}",
e.Length, e.ProtocolVer, e.Operation.String(), string(e.Body))
}

const (
HeaderLength = 16
SequenceId = 1
Expand Down

0 comments on commit e8c1fc3

Please sign in to comment.