Skip to content

Commit 10239cb

Browse files
committed
initial files + hexdumpc
1 parent 7a05565 commit 10239cb

7 files changed

Lines changed: 223 additions & 0 deletions

File tree

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
indent_size = 4
10+
indent_style = tab
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.{json,yaml}]
15+
indent_size = 2
16+
indent_style = space

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
*~
2+
**/*#
3+
.cache
4+
*.class
5+
crash.log
6+
_data/build.json
7+
.DS_Store
8+
.env
9+
Gemfile.lock
10+
.idea/
11+
.jekyll-metadata
12+
node_modules
13+
*.pyc
14+
.sass-cache/
15+
_site/
16+
tmp/
17+
_tmp/
18+
tmp-*
19+
*.tmp
20+
venv/
21+
.venv/
22+
_work/

cmd/hexdumpc/hexdumpc.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"os"
8+
9+
"github.com/spf13/pflag"
10+
)
11+
12+
func hexdump(fileName string, offset, length int64) error {
13+
14+
// Open the file
15+
file, err := os.Open(fileName)
16+
if err != nil {
17+
return err
18+
}
19+
defer file.Close()
20+
// Seek to the offset
21+
if offset > 0 {
22+
if _, err := file.Seek(offset, io.SeekStart); err != nil {
23+
return err
24+
}
25+
}
26+
27+
reader := bufio.NewReader(file)
28+
29+
var count int64 = 0
30+
for {
31+
// Read a chunk of data
32+
buf := make([]byte, 16)
33+
n, err := reader.Read(buf)
34+
if err != nil {
35+
if err == io.EOF {
36+
break
37+
}
38+
return err
39+
}
40+
41+
printLine(offset+count, buf[:n])
42+
43+
count += int64(n)
44+
if length > 0 && count >= length {
45+
break
46+
}
47+
}
48+
return nil
49+
}
50+
51+
func printLine(offset int64, buf []byte) {
52+
// Print the offset
53+
fmt.Printf("%08x ", offset)
54+
55+
// Print the hex values
56+
for i := 0; i < len(buf); i++ {
57+
if i%8 == 0 && i != 0 {
58+
fmt.Print(" ")
59+
}
60+
fmt.Printf("%02x ", buf[i])
61+
}
62+
// Pad the line to 48 characters
63+
for i := len(buf); i < 16; i++ {
64+
if i%8 == 0 && i != 0 {
65+
fmt.Print(" ")
66+
}
67+
fmt.Print(" ")
68+
}
69+
70+
// Print the ASCII values
71+
fmt.Print(" |")
72+
for _, b := range buf {
73+
if b >= 32 && b <= 126 {
74+
fmt.Printf("%c", b)
75+
} else {
76+
fmt.Print(".")
77+
}
78+
}
79+
// Pad the line to 16 characters
80+
for i := len(buf); i < 16; i++ {
81+
fmt.Print(" ")
82+
}
83+
fmt.Println("|")
84+
}
85+
86+
func main() {
87+
88+
var head = pflag.Int64("head", 0, "number of bytes to read at the beginning of the file")
89+
var offset = pflag.Int64("offset", 0, "number of bytes to skip at the beginning of the file")
90+
var length = pflag.Int64("length", 0, "number of bytes to read from the file")
91+
//LATER: support for tail
92+
93+
pflag.Parse()
94+
95+
if *head > 0 {
96+
*offset = 0
97+
*length = *head
98+
}
99+
100+
args := pflag.Args()
101+
if len(args) == 0 {
102+
args = []string{"-"}
103+
}
104+
for _, arg := range args {
105+
if arg == "-" {
106+
arg = "/dev/stdin"
107+
}
108+
109+
if err := hexdump(arg, *offset, *length); err != nil {
110+
panic(err)
111+
}
112+
}
113+
}

docs/favicon.ico

14.8 KB
Binary file not shown.

docs/favicon.svg

Lines changed: 65 additions & 0 deletions
Loading

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/FileFormatInfo/fftools
2+
3+
go 1.23.2
4+
5+
require github.com/spf13/pflag v1.0.6 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
2+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

0 commit comments

Comments
 (0)