2024-01-25 22:38:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-02-02 00:12:24 +00:00
|
|
|
"errors"
|
2024-01-25 22:38:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
func XXDReverse(r io.Reader, w io.Writer) error {
|
|
|
|
var (
|
|
|
|
cols int
|
|
|
|
octs int
|
|
|
|
char = make([]byte, 1)
|
|
|
|
)
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if opts.Columns != -1 {
|
|
|
|
cols = opts.Columns
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
switch dumpType {
|
|
|
|
case dumpBinary:
|
|
|
|
octs = 8
|
|
|
|
case dumpCformat:
|
|
|
|
octs = 4
|
|
|
|
default:
|
|
|
|
octs = 2
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if opts.Len != -1 {
|
|
|
|
if opts.Len < int64(cols) {
|
|
|
|
cols = int(opts.Len)
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
2024-02-02 00:12:24 +00:00
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if octs < 1 {
|
|
|
|
octs = cols
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
|
2024-02-23 21:25:28 +00:00
|
|
|
// character count
|
2024-02-02 00:12:24 +00:00
|
|
|
c := int64(0)
|
|
|
|
rd := bufio.NewReader(r)
|
|
|
|
for {
|
2024-02-23 21:25:28 +00:00
|
|
|
// TODO this is causing issues with plain
|
2024-02-02 00:12:24 +00:00
|
|
|
line, err := rd.ReadBytes('\n')
|
|
|
|
n := len(line)
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
|
|
|
|
return fmt.Errorf("hexxy: %v", err)
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if n == 0 {
|
|
|
|
return nil
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if dumpType == dumpHex {
|
|
|
|
for i := 0; n >= octs; {
|
2024-02-23 21:25:28 +00:00
|
|
|
if rv, _ := hexDecode(char, line[i:i+octs]); rv == 0 {
|
2024-02-02 00:12:24 +00:00
|
|
|
w.Write(char)
|
|
|
|
i += 2
|
|
|
|
n -= 2
|
|
|
|
c++
|
|
|
|
} else if rv == -1 {
|
|
|
|
i++
|
|
|
|
n--
|
|
|
|
} else {
|
|
|
|
// rv == -2
|
|
|
|
i += 2
|
|
|
|
n -= 2
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
2024-02-02 00:12:24 +00:00
|
|
|
} else if dumpType == dumpBinary {
|
|
|
|
for i := 0; n >= octs; {
|
|
|
|
if binaryDecode(char, line[i:i+octs]) != -1 {
|
|
|
|
i++
|
|
|
|
n--
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
w.Write(char)
|
|
|
|
i += 8
|
|
|
|
n -= 8
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if dumpType == dumpPlain {
|
|
|
|
for i := 0; n >= octs; i++ {
|
2024-02-23 21:25:28 +00:00
|
|
|
if rv, _ := hexDecode(char, line[i:i+octs]); rv != 0 {
|
2024-02-02 00:12:24 +00:00
|
|
|
w.Write(char)
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
n--
|
|
|
|
}
|
|
|
|
} else if dumpType == dumpCformat {
|
|
|
|
for i := 0; n >= octs; {
|
2024-02-23 21:25:28 +00:00
|
|
|
if rv, _ := hexDecode(char, line[i:i+octs]); rv == 0 {
|
2024-02-02 00:12:24 +00:00
|
|
|
w.Write(char)
|
|
|
|
i += 4
|
|
|
|
n -= 4
|
|
|
|
c++
|
|
|
|
} else if rv == -1 {
|
|
|
|
i++
|
|
|
|
n--
|
|
|
|
} else { // rv == -2
|
|
|
|
i += 2
|
|
|
|
n -= 2
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 00:12:24 +00:00
|
|
|
if c == int64(cols) && cols > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-25 22:38:27 +00:00
|
|
|
}
|
|
|
|
}
|