Ver Fonte

第一次提交

kinve há 1 ano atrás
commit
a696e48e43
7 ficheiros alterados com 207 adições e 0 exclusões
  1. BIN
      logo.ico
  2. BIN
      main.exe
  3. 193 0
      main.go
  4. 1 0
      main.rc
  5. BIN
      main.syso
  6. 7 0
      生成bin.bat
  7. 6 0
      生成exe.bat

BIN
logo.ico


BIN
main.exe


+ 193 - 0
main.go

@@ -0,0 +1,193 @@
+package main
+
+import (
+	"encoding/binary"
+	"encoding/hex"
+	"fmt"
+	"hash/crc32"
+	"io"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"strconv"
+)
+
+func swab16(x uint16) uint16 {
+	return (x&0x00ff)<<8 | (x&0xff00)>>8
+}
+
+func swab32(x uint32) uint32 {
+	return (x&0x000000ff)<<24 | (x&0x0000ff00)<<8 | (x&0x00ff0000)>>8 | (x&0xff000000)>>24
+}
+
+func sumArr(arr []uint8) uint32 {
+	sum := uint32(0)
+	for _, val := range arr {
+		sum += uint32(val)
+	}
+	return sum
+}
+
+func calChecknum(_type string, datas []byte) uint32 {
+	if _type == "sum32" {
+		return sumArr(datas)
+	} else if _type == "crc32" {
+		return crc32.ChecksumIEEE(datas)
+	} else {
+		print("unKown type: " + _type)
+		return 0
+	}
+}
+
+func getFileSize(filename string) (int64, error) {
+	fileinfo, err := os.Stat(filename)
+	if err != nil {
+		return 0, err
+	}
+	size := fileinfo.Size()
+	return size, nil
+}
+
+func ReadBinFile(_name string, _type string, _en string, _block int) {
+	FileName := filepath.Base(_name)
+	// FileType := filepath.Ext(_name)
+
+	InputFilePath := _name
+	OutputFilePath := FileName + "." + _type
+
+	inputFile, err := os.Open(InputFilePath)
+	if err != nil {
+		panic(err)
+	}
+	defer inputFile.Close()
+
+	outputFile, err := os.OpenFile(OutputFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
+	if err != nil {
+		panic(err)
+	}
+	defer outputFile.Close()
+
+	if _block > 0 {
+		buffer := make([]byte, _block)
+		for {
+			n, err := inputFile.Read(buffer)
+			if n > 0 {
+				// 计算分片的校验和
+				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)
+				}
+			}
+
+			if err != nil {
+				if err == io.EOF {
+					break // 文件读取完毕
+				}
+				panic(err)
+			}
+		}
+	} else {
+		data, err := ioutil.ReadAll(inputFile)
+		if err != nil {
+			panic(err)
+		}
+		// 写入全部内容
+		if _en == "big" {
+			binary.Write(outputFile, binary.BigEndian, data)
+		} else {
+			binary.Write(outputFile, binary.LittleEndian, data)
+		}
+	}
+
+	// 读取文件内容到字节切片
+	inputFile, err = os.Open(InputFilePath)
+	if err != nil {
+		panic(err)
+	}
+	defer inputFile.Close()
+	data, err := ioutil.ReadAll(inputFile)
+	if err != nil {
+		panic(err)
+	}
+
+	check := calChecknum(_type, data[:])
+	// 写入源文件校验码
+	if _en == "big" {
+		binary.Write(outputFile, binary.BigEndian, check)
+	} else {
+		binary.Write(outputFile, binary.LittleEndian, check)
+	}
+
+	// 大小端转换
+	var networkBytes [4]byte
+	if _en == "big" {
+		binary.BigEndian.PutUint32(networkBytes[:], check)
+	} else {
+		binary.LittleEndian.PutUint32(networkBytes[:], check)
+	}
+	check_hex := hex.EncodeToString(networkBytes[:])
+
+	// 打印输出内容
+	size, err := getFileSize(InputFilePath)
+	fmt.Println(InputFilePath, fmt.Sprintf("%d", size), _type, check_hex)
+
+	// 关闭文件
+	err = outputFile.Close()
+	if err != nil {
+		fmt.Println(err)
+	}
+	// 重命名文件
+	out_name1 := FileName + "." + _type + "[" + check_hex + "]"
+	err = os.Rename(OutputFilePath, out_name1)
+	if err != nil {
+		fmt.Println("重命名文件出错:", err)
+		return
+	}
+}
+
+func main() {
+	// parser.add_argument("n", help="bin文件名,譬如 COAB21232D.bin")
+	// parser.add_argument("-t", help="校验方式:sum32,crc32, 默认sum32")
+	// parser.add_argument("-e", help="校验码大小端:little,big, 默认little")
+	// parser.add_argument("-s", help="分块大小:0,1024,2048, 默认0不分块")
+
+	// 参数解析
+	// var _name = flag.String("n", "COAB21232D.bin", "bin文件名,譬如 COAB21232D.bin")
+	// var _type = flag.String("-t", "sum32", "校验方式:sum32,crc32, 默认sum32")
+	// var _en = flag.String("-e", "little", "校验码大小端:little,big, 默认little")
+	// var _block = flag.Int("-s", 0, "代理,譬如 分块大小:0,1024,2048, 默认0不分块")
+	// // 解析命令行参数
+	// flag.Parse()
+
+	_name := ""
+	_type := "sum32"
+	_en := "little"
+	_block := 0
+	for i, num := range os.Args[1:] {
+		switch i {
+		case 0:
+			_name = num
+		case 1:
+			_type = num
+		case 2:
+			_en = num
+		case 3:
+			numInt, err := strconv.Atoi(num)
+			if err != nil {
+				// 处理转换错误
+				fmt.Println("转换错误:", err)
+				return
+			}
+			_block = numInt
+		default:
+			fmt.Println("未知参数")
+		}
+	}
+
+	ReadBinFile(_name, _type, _en, _block)
+}

+ 1 - 0
main.rc

@@ -0,0 +1 @@
+IDI_ICON1 ICON "logo.ico"

BIN
main.syso


+ 7 - 0
生成bin.bat

@@ -0,0 +1,7 @@
+@echo off & title ¸ù¾ÝÁбílist.txt×Ô¶¯svnת»¯git
+setlocal enabledelayedexpansion
+
+
+.\main.exe COAB21232D.bin sum32 big 1024
+
+

+ 6 - 0
生成exe.bat

@@ -0,0 +1,6 @@
+::图标默认logo.ico
+windres -o main.syso main.rc
+go build -o main.exe main.go
+
+::go build    //带有cmd黑框
+::go build -ldflags "-s -w -H=windowsgui"     //没有cmd黑框,适合有gui的