|
|
@@ -49,7 +49,7 @@ func getFileSize(filename string) (int64, error) {
|
|
|
return size, nil
|
|
|
}
|
|
|
|
|
|
-func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck int) {
|
|
|
+func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck int, _addpos int) {
|
|
|
FileName := filepath.Base(_name)
|
|
|
// FileType := filepath.Ext(_name)
|
|
|
|
|
|
@@ -62,12 +62,32 @@ func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck i
|
|
|
}
|
|
|
defer inputFile.Close()
|
|
|
|
|
|
+ // 计算总帧数
|
|
|
+ fileInfo, err := inputFile.Stat()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+ fileSize := fileInfo.Size()
|
|
|
+ totalFrames := uint16((fileSize + int64(_block) - 1) / int64(_block))
|
|
|
+
|
|
|
outputFile, err := os.OpenFile(OutputFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
|
|
if err != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
defer outputFile.Close()
|
|
|
|
|
|
+ var Endian binary.ByteOrder
|
|
|
+ switch _en {
|
|
|
+ case "big":
|
|
|
+ Endian = binary.BigEndian
|
|
|
+ case "little":
|
|
|
+ Endian = binary.LittleEndian
|
|
|
+ default:
|
|
|
+ fmt.Println("错误: 未知的大小端类型", _en)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ curFrame := 0
|
|
|
if _block > 0 {
|
|
|
buffer := make([]byte, _block)
|
|
|
for {
|
|
|
@@ -76,13 +96,16 @@ func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck i
|
|
|
// 计算分片的校验和
|
|
|
check := calChecknum(_type, buffer[:n])
|
|
|
// 写入分片内容+分片校验和
|
|
|
- if _en == "big" {
|
|
|
- binary.Write(outputFile, binary.BigEndian, buffer[:n])
|
|
|
- binary.Write(outputFile, binary.BigEndian, check)
|
|
|
- } else {
|
|
|
- binary.Write(outputFile, binary.LittleEndian, buffer[:n])
|
|
|
- binary.Write(outputFile, binary.LittleEndian, check)
|
|
|
+ binary.Write(outputFile, Endian, buffer[:n])
|
|
|
+ if _addpos == 1 {
|
|
|
+ // 追加偏移量
|
|
|
+ binary.Write(outputFile, Endian, uint16(curFrame))
|
|
|
+ binary.Write(outputFile, Endian, uint16(totalFrames))
|
|
|
}
|
|
|
+ // 追加校验码
|
|
|
+ binary.Write(outputFile, Endian, check)
|
|
|
+
|
|
|
+ curFrame ++
|
|
|
}
|
|
|
|
|
|
if err != nil {
|
|
|
@@ -98,11 +121,7 @@ func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck i
|
|
|
panic(err)
|
|
|
}
|
|
|
// 写入全部内容
|
|
|
- if _en == "big" {
|
|
|
- binary.Write(outputFile, binary.BigEndian, data)
|
|
|
- } else {
|
|
|
- binary.Write(outputFile, binary.LittleEndian, data)
|
|
|
- }
|
|
|
+ binary.Write(outputFile, Endian, data)
|
|
|
}
|
|
|
|
|
|
// 读取文件内容到字节切片
|
|
|
@@ -119,11 +138,7 @@ func ReadBinFile(_name string, _type string, _en string, _block int, _addcheck i
|
|
|
check := calChecknum(_type, data[:])
|
|
|
if _addcheck == 1 {
|
|
|
// 追加校验码
|
|
|
- if _en == "big" {
|
|
|
- binary.Write(outputFile, binary.BigEndian, check)
|
|
|
- } else {
|
|
|
- binary.Write(outputFile, binary.LittleEndian, check)
|
|
|
- }
|
|
|
+ binary.Write(outputFile, Endian, check)
|
|
|
}
|
|
|
|
|
|
// 大小端转换
|
|
|
@@ -159,11 +174,12 @@ func main() {
|
|
|
_type := parser.String("t", "type", &argparse.Options{Required: false, Help: "校验方式: sum32,crc32", Default: "sum32"})
|
|
|
_en := parser.String("e", "en", &argparse.Options{Required: false, Help: "校验码大小端: little,big", Default: "little"})
|
|
|
_section := parser.Int("s", "section", &argparse.Options{Required: false, Help: "分块大小: 0,1024,2048", Default: 0})
|
|
|
- _addcheck := parser.Int("a", "addcheck", &argparse.Options{Required: false, Help: "文件结尾追加校验码", Default: 1})
|
|
|
+ _addcheck := parser.Int("c", "addcheck", &argparse.Options{Required: false, Help: "文件结尾追加校验码", Default: 1})
|
|
|
+ _addpos := parser.Int("p", "addpos", &argparse.Options{Required: false, Help: "文件结尾追加偏移", Default: 1})
|
|
|
err := parser.Parse(os.Args)
|
|
|
if err != nil {
|
|
|
fmt.Print(parser.Usage(err)) // 帮助 -h or --help
|
|
|
return
|
|
|
}
|
|
|
- ReadBinFile(*_name, *_type, *_en, *_section, *_addcheck)
|
|
|
+ ReadBinFile(*_name, *_type, *_en, *_section, *_addcheck, *_addpos)
|
|
|
}
|