-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nokute78/v2_dir
support v2 dir
- Loading branch information
Showing
14 changed files
with
2,847 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# readbit | ||
|
||
A command line tool to read bit from STDIN/File. | ||
|
||
## Quick Start | ||
```shell | ||
$ printf "\xff\xff" |./readbit -s 4 | ||
``` | ||
|
||
## Options | ||
``` | ||
Usage of readbit: | ||
-B uint | ||
offset (in byte) | ||
-V show version | ||
-b uint | ||
offset (in bit) | ||
-s uint | ||
read size(in bit) | ||
-v verbose mode | ||
``` | ||
|
||
## Example(STDIN) | ||
|
||
``` | ||
$ printf "\xff\xff" |./readbit -s 4 | ||
0x0f | ||
``` | ||
|
||
Read 3bit and offset is 8bit. | ||
``` | ||
$ printf "\xff\xff" |./readbit -s 3 -b 8 | ||
0x07 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
Copyright 2020 Takahiro Yamashita | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/binary" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/mattn/go-isatty" | ||
"github.com/nokute78/go-bit/v2" | ||
) | ||
|
||
const version string = "0.0.1" | ||
|
||
// Exit status | ||
const ( | ||
ExitOK int = iota | ||
ExitArgError | ||
ExitCmdError | ||
) | ||
|
||
type config struct { | ||
showVersion bool | ||
verbose bool | ||
terminalMode bool | ||
byte uint64 | ||
bit uint64 | ||
bitsize uint64 | ||
} | ||
|
||
// CLI has In/Out/Err streams. | ||
// Flags is option. | ||
type CLI struct { | ||
OutStream io.Writer | ||
InStream *os.File | ||
ErrStream io.Writer | ||
Flags *flag.FlagSet | ||
forceTerminal bool | ||
} | ||
|
||
func (cli *CLI) showBits(in string, b []bit.Bit, cnf *config) { | ||
if cnf.verbose { | ||
fmt.Fprintf(cli.OutStream, "%s (Byte:%d,Bit:%d,Size:%d): 0x%x\n", in, cnf.byte, cnf.bit, cnf.bitsize, bit.BitsToBytes(b, binary.LittleEndian)) | ||
} else { | ||
fmt.Fprintf(cli.OutStream, "0x%x\n", bit.BitsToBytes(b, binary.LittleEndian)) | ||
} | ||
} | ||
|
||
func (cli *CLI) readBits(in io.Reader, cnf *config) ([]bit.Bit, error) { | ||
buf, err := ioutil.ReadAll(in) | ||
if err != nil { | ||
return []bit.Bit{}, fmt.Errorf("ioutil.ReadAll: %s", err) | ||
} | ||
|
||
ret, err := bit.GetBits(buf, bit.Offset{Byte: cnf.byte, Bit: cnf.bit}, cnf.bitsize, binary.LittleEndian) | ||
if err != nil { | ||
return []bit.Bit{}, fmt.Errorf("GetBits: %s, len=%d size=%d", err, len(buf), cnf.bitsize) | ||
} | ||
return ret, nil | ||
} | ||
|
||
func (cli *CLI) readStdin(cnf *config) int { | ||
buf, err := cli.readBits(cli.InStream, cnf) | ||
if err != nil { | ||
fmt.Fprintf(cli.ErrStream, "readStdin :%s\n", err) | ||
return ExitCmdError | ||
} | ||
|
||
cli.showBits("(stdin)", buf, cnf) | ||
|
||
return ExitOK | ||
} | ||
|
||
func (cli *CLI) readFiles(files []string, cnf *config) (ret int) { | ||
ret = ExitCmdError | ||
for _, v := range files { | ||
f, err := os.Open(v) | ||
if err != nil { | ||
fmt.Fprintf(cli.ErrStream, "os.Open :%s\n", err) | ||
continue | ||
} | ||
defer f.Close() | ||
|
||
buf, err := cli.readBits(f, cnf) | ||
if err != nil { | ||
fmt.Fprintf(cli.ErrStream, "readFiles :%s\n", err) | ||
continue | ||
} | ||
cli.showBits(v, buf, cnf) | ||
ret = ExitOK | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func (cli *CLI) checkOption(args []string) (*config, error) { | ||
config := &config{} | ||
|
||
cli.Flags = flag.NewFlagSet(filepath.Base(args[0]), flag.ExitOnError) | ||
|
||
cli.Flags.BoolVar(&config.verbose, "v", false, "verbose mode") | ||
cli.Flags.BoolVar(&config.showVersion, "V", false, "show version") | ||
cli.Flags.Uint64Var(&config.byte, "B", 0, "offset (in byte)") | ||
cli.Flags.Uint64Var(&config.bit, "b", 0, "offset (in bit)") | ||
cli.Flags.Uint64Var(&config.bitsize, "s", 0, "read size(in bit)") | ||
|
||
cli.Flags.Parse(args[1:]) | ||
|
||
config.terminalMode = isatty.IsTerminal(cli.InStream.Fd()) | ||
if cli.forceTerminal { | ||
// for testing | ||
config.terminalMode = true | ||
} | ||
|
||
if config.showVersion { | ||
return config, nil | ||
} | ||
|
||
if config.bitsize == 0 { | ||
return nil, fmt.Errorf("read size is 0") | ||
} | ||
|
||
if config.terminalMode && cli.Flags.NArg() == 0 { | ||
return nil, fmt.Errorf("no files") | ||
} | ||
|
||
return config, nil | ||
} | ||
|
||
// Run executes real main function. | ||
func (cli *CLI) Run(args []string) (ret int) { | ||
cnf, err := cli.checkOption(args) | ||
if err != nil { | ||
fmt.Fprintf(cli.ErrStream, "Error:%s\n", err) | ||
return ExitArgError | ||
} | ||
|
||
if cnf.showVersion { | ||
fmt.Fprintf(cli.OutStream, "Ver: %s\n", version) | ||
return ExitOK | ||
} | ||
|
||
if cnf.terminalMode { | ||
ret = cli.readFiles(cli.Flags.Args(), cnf) | ||
} else { | ||
ret = cli.readStdin(cnf) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func main() { | ||
cli := &CLI{OutStream: os.Stdout, InStream: os.Stdin, ErrStream: os.Stderr} | ||
|
||
os.Exit(cli.Run(os.Args)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
Copyright 2020 Takahiro Yamashita | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func runHelper(cli *CLI, args []string, t *testing.T) { | ||
t.Helper() | ||
|
||
ret := cli.Run(args) | ||
|
||
if ret != ExitOK { | ||
t.Errorf("Return Code %d is not ExitOK", ret) | ||
} | ||
} | ||
|
||
func TestReadStdin(t *testing.T) { | ||
tempfile, err := ioutil.TempFile("", "TestReadStdin") | ||
if err != nil { | ||
t.Fatalf("ioutil.TempFile error: %s", err) | ||
} | ||
defer os.Remove(tempfile.Name()) | ||
defer tempfile.Close() | ||
|
||
n, err := tempfile.Write([]byte{0xff, 0x07}) | ||
if err != nil { | ||
t.Fatalf("File.Write error: %s, n=%d", err, n) | ||
} | ||
|
||
retOutput := make([]byte, 16) | ||
out := bytes.NewBuffer(retOutput) | ||
|
||
cli := &CLI{OutStream: out, ErrStream: os.Stderr, InStream: tempfile} | ||
|
||
tempfile.Seek(0, 0) // to read from head of file | ||
runHelper(cli, []string{"hoge", "-s", "11"}, t) | ||
|
||
if strings.Contains(string(retOutput), "0xff07") { | ||
t.Errorf("ReadFile Error. got %s want %s", string(retOutput), "0xff07") | ||
} | ||
} | ||
|
||
func TestReadFiles(t *testing.T) { | ||
tempfile, err := ioutil.TempFile("", "TestReadFiles") | ||
if err != nil { | ||
t.Fatalf("ioutil.TempFile error: %s", err) | ||
} | ||
defer os.Remove(tempfile.Name()) | ||
defer tempfile.Close() | ||
|
||
n, err := tempfile.Write([]byte{0xff, 0x07}) | ||
if err != nil { | ||
t.Fatalf("File.Write error: %s, n=%d", err, n) | ||
} | ||
|
||
retOutput := make([]byte, 16) | ||
out := bytes.NewBuffer(retOutput) | ||
|
||
cli := &CLI{OutStream: out, ErrStream: os.Stderr, forceTerminal: true} | ||
runHelper(cli, []string{"hoge", "-s", "11", tempfile.Name()}, t) | ||
|
||
if strings.Contains(string(retOutput), "0xff07") { | ||
t.Errorf("ReadFile Error. got %s want %s", string(retOutput), "0xff07") | ||
} | ||
} | ||
|
||
func TestShowVersion(t *testing.T) { | ||
retOutput := make([]byte, len(version)+8) | ||
out := bytes.NewBuffer(retOutput) | ||
|
||
cli := &CLI{OutStream: out, ErrStream: os.Stderr} | ||
runHelper(cli, []string{"hoge", "-V"}, t) | ||
|
||
if strings.Contains(string(retOutput), version) { | ||
t.Errorf("Version Error. got %s want %s", string(retOutput), version) | ||
} | ||
} |
Oops, something went wrong.