From 190dd364905c979549f967625427e701bc158140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 25 Jul 2023 23:11:48 +0800 Subject: [PATCH] =?UTF-8?q?enhance(crc):CRC=E7=B3=BB=E5=88=97=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E7=AE=97=E6=B3=95=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=A4=A7=E7=AB=AF=E5=BA=8F=E6=88=96=E8=80=85=E5=B0=8F=E7=AB=AF?= =?UTF-8?q?=E5=BA=8F=E8=BE=93=E5=87=BA=E7=9A=84=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/crc16.go | 7 +++++++ cmd/crc32.go | 7 +++++++ cmd/crc64.go | 7 +++++++ types/endian.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 types/endian.go diff --git a/cmd/crc16.go b/cmd/crc16.go index 50a6a52..342ec81 100644 --- a/cmd/crc16.go +++ b/cmd/crc16.go @@ -11,6 +11,7 @@ import ( var ( crc16Algorithm types.CRC16Algorithm = types.CRC16_IBM + crc16Endian types.Endian = types.LittleEndian crc16File bool ) @@ -36,6 +37,11 @@ func crc16Execute(cmd *cobra.Command, args []string) { fmt.Printf("计算CRC16校验值时发生错误:%s\n", err) return } + if crc16Endian == types.LittleEndian { + for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { + result[i], result[j] = result[j], result[i] + } + } var builder strings.Builder for _, b := range result { builder.WriteString(fmt.Sprintf(" %02X", b)) @@ -45,6 +51,7 @@ func crc16Execute(cmd *cobra.Command, args []string) { func init() { crc16Cmd.PersistentFlags().VarP(&crc16Algorithm, "algorithm", "a", "CRC16算法,可选值有:ccitt, ccitt-false, scsi, ibm, mbus,缺省值为ibm。") + crc16Cmd.PersistentFlags().VarP(&crc16Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。") crc16Cmd.PersistentFlags().BoolVarP(&crc16File, "file", "f", false, "从文件中读取内容") rootCmd.AddCommand(crc16Cmd) } diff --git a/cmd/crc32.go b/cmd/crc32.go index e0b8818..314fee7 100644 --- a/cmd/crc32.go +++ b/cmd/crc32.go @@ -11,6 +11,7 @@ import ( var ( crc32Algorithm types.CRC32Algorithm = types.CRC32_IEEE + crc32Endian types.Endian = types.LittleEndian crc32File bool ) @@ -36,6 +37,11 @@ func crc32Execute(cmd *cobra.Command, args []string) { fmt.Printf("计算CRC32校验值时发生错误:%s\n", err) return } + if crc32Endian == types.LittleEndian { + for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { + result[i], result[j] = result[j], result[i] + } + } var builder strings.Builder for _, b := range result { builder.WriteString(fmt.Sprintf(" %02X", b)) @@ -45,6 +51,7 @@ func crc32Execute(cmd *cobra.Command, args []string) { func init() { crc32Cmd.PersistentFlags().VarP(&crc32Algorithm, "algorithm", "a", "CRC32算法,可选值有:ieee, castagnoli, koopman,缺省值为ieee。") + crc32Cmd.PersistentFlags().VarP(&crc32Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。") crc32Cmd.PersistentFlags().BoolVarP(&crc32File, "file", "f", false, "从文件中读取内容") rootCmd.AddCommand(crc32Cmd) } diff --git a/cmd/crc64.go b/cmd/crc64.go index 648a4aa..cb9003f 100644 --- a/cmd/crc64.go +++ b/cmd/crc64.go @@ -11,6 +11,7 @@ import ( var ( crc64Algorithm types.CRC64Algorithm = types.CRC64_ISO + crc64Endian types.Endian = types.LittleEndian crc64File bool ) @@ -36,6 +37,11 @@ func crc64Execute(cmd *cobra.Command, args []string) { fmt.Printf("计算CRC64校验值时发生错误:%s\n", err) return } + if crc64Endian == types.LittleEndian { + for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 { + result[i], result[j] = result[j], result[i] + } + } var builder strings.Builder for _, b := range result { builder.WriteString(fmt.Sprintf(" %02X", b)) @@ -45,6 +51,7 @@ func crc64Execute(cmd *cobra.Command, args []string) { func init() { crc64Cmd.PersistentFlags().VarP(&crc64Algorithm, "algorithm", "a", "CRC64算法,可选值有:iso, ecma,缺省值为iso。") + crc64Cmd.PersistentFlags().VarP(&crc64Endian, "endian", "e", "字节序,可选值有:big, little,缺省值为little。") crc64Cmd.PersistentFlags().BoolVarP(&crc64File, "file", "f", false, "从文件中读取内容") rootCmd.AddCommand(crc64Cmd) } diff --git a/types/endian.go b/types/endian.go new file mode 100644 index 0000000..a6eaf53 --- /dev/null +++ b/types/endian.go @@ -0,0 +1,28 @@ +package types + +import "fmt" + +type Endian string + +const ( + BigEndian Endian = "big" + LittleEndian Endian = "little" +) + +func (e *Endian) String() string { + return string(*e) +} + +func (e *Endian) Set(s string) error { + switch s { + case "big", "little": + *e = Endian(s) + return nil + default: + return fmt.Errorf("不支持的字节序:%s", s) + } +} + +func (e *Endian) Type() string { + return "Endian" +}