From 80e3933c2896e270670dd9ba59d4ad9a52806f93 Mon Sep 17 00:00:00 2001 From: Joeky Date: Thu, 4 Jul 2024 20:12:05 +0800 Subject: [PATCH] Fix warnings and upgrade go version Signed-off-by: Joeky --- .github/workflows/build.yml | 2 +- .github/workflows/release.yml | 2 +- main.go | 219 +++++++++++++++++----------------- 3 files changed, 109 insertions(+), 114 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2f1b894..65dd0ef 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.22' - name: Build run: go build -v ./... \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 18fa789..7b62cce 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -35,7 +35,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.21' + go-version: '1.22' - name: Get OS and arch info run: | diff --git a/main.go b/main.go index 4ea6e88..d56aa74 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,8 @@ import ( ) const ( - MAX_FILENAME_LENGTH = 256 - MAX_BYTES_TO_READ = 2 * 1024 // 2KB buffer to read file + MaxFileLength = 256 + MaxBytesToRead = 2 * 1024 // 2KB buffer to read file ) func main() { @@ -41,50 +41,44 @@ func main() { for _, filename := range files { fi, err := os.Lstat(filename) if err != nil { - print(filename + ": " + err.Error()) + fmt.Print(filename + ": " + err.Error()) continue } - if len(filename) > MAX_FILENAME_LENGTH { - print("File name too long.") + if len(filename) > MaxFileLength { + fmt.Print("File name too long.") continue } - print(filename + ": ") + fmt.Print(filename + ": ") // Add padding to make columns for padding := 0; padding < longestFileName+2-len(filename); padding++ { - print(" ") + fmt.Print(" ") } - if fi.Mode()&os.ModeSymlink != 0 { + switch { + case fi.Mode()&os.ModeSymlink != 0: reallink, _ := os.Readlink(filename) - print("symbolic link to " + reallink) - } else if fi.Mode()&os.ModeDir != 0 { - print("directory") - } else if fi.Mode()&os.ModeSocket != 0 { - print("socket") - } else if fi.Mode()&os.ModeCharDevice != 0 { - print("character special device") - } else if fi.Mode()&os.ModeDevice != 0 { - print("device file") - } else if fi.Mode()&os.ModeNamedPipe != 0 { - print("fifo") - } else { + fmt.Print("symbolic link to " + reallink) + case fi.Mode()&os.ModeDir != 0: + fmt.Print("directory") + case fi.Mode()&os.ModeSocket != 0: + fmt.Print("socket") + case fi.Mode()&os.ModeCharDevice != 0: + fmt.Print("character special device") + case fi.Mode()&os.ModeDevice != 0: + fmt.Print("device file") + case fi.Mode()&os.ModeNamedPipe != 0: + fmt.Print("fifo") + default: regularFile(filename) } - println() - } -} - -func checkerr(e error) { - if e != nil { - print(e.Error()) - os.Exit(1) + fmt.Println() } } func usage() { - println("Usage: fil FILE_NAME") + fmt.Println("Usage: fil FILE_NAME") os.Exit(0) } @@ -92,10 +86,9 @@ func regularFile(filename string) { /*---------------Read file------------------------*/ file, _ := os.OpenFile(filename, os.O_RDONLY, 0666) - // checkerr(err) defer file.Close() - var contentByte = make([]byte, MAX_BYTES_TO_READ) + var contentByte = make([]byte, MaxBytesToRead) numByte, _ := file.Read(contentByte) contentByte = contentByte[:numByte] @@ -109,148 +102,148 @@ func regularFile(filename string) { switch { case lenb >= 45 && HasPrefix(contentByte, "\x7FELF"): - print("Elf file ") + fmt.Print("Elf file ") doElf(contentByte) case lenb >= 8 && HasPrefix(contentByte, "!\n"): - print("ar archive") + fmt.Print("ar archive") case lenb > 28 && HasPrefix(contentByte, "\x89PNG\x0d\x0a\x1a\x0a"): - print("PNG image data") + fmt.Print("PNG image data") case lenb > 16 && (HasPrefix(contentByte, "GIF87a") || HasPrefix(contentByte, "GIF89a")): - print("GIF image data") + fmt.Print("GIF image data") case lenb > 32 && HasPrefix(contentByte, "\xff\xd8"): - print("JPEG / jpg image data") + fmt.Print("JPEG / jpg image data") case lenb > 8 && HasPrefix(contentByte, "\xca\xfe\xba\xbe"): - print("Java class file") + fmt.Print("Java class file") case lenb > 8 && HasPrefix(contentByte, "dex\n"): - print("Android dex file") + fmt.Print("Android dex file") case lenb > 500 && Equal(contentByte[257:262], "ustar"): - print("Posix tar archive") + fmt.Print("Posix tar archive") case lenb > 5 && HasPrefix(contentByte, "PK\x03\x04"): - print(doZip(file)) + fmt.Print(doZip(file)) case lenb > 4 && HasPrefix(contentByte, "BZh"): - print("bzip2 compressed data") + fmt.Print("bzip2 compressed data") case lenb > 10 && HasPrefix(contentByte, "\x1f\x8b"): - print("gzip compressed data") + fmt.Print("gzip compressed data") case lenb > 32 && Equal(contentByte[1:4], "\xfa\xed\xfe"): - print("Mach-O") + fmt.Print("Mach-O") case lenb > 36 && HasPrefix(contentByte, "OggS\x00\x02"): - print("Ogg data") + fmt.Print("Ogg data") case lenb > 32 && HasPrefix(contentByte, "RIF") && Equal(contentByte[8:16], "WAVEfmt "): - print("WAV audio") + fmt.Print("WAV audio") case lenb > 12 && HasPrefix(contentByte, "\x00\x01\x00\x00"): - print("TrueType font") + fmt.Print("TrueType font") case lenb > 12 && HasPrefix(contentByte, "ttcf\x00"): - print("TrueType font collection") + fmt.Print("TrueType font collection") case lenb > 4 && HasPrefix(contentByte, "BC\xc0\xde"): - print("LLVM IR bitcode") + fmt.Print("LLVM IR bitcode") case HasPrefix(contentByte, "-----BEGIN CERTIFICATE-----"): - print("PEM certificate") + fmt.Print("PEM certificate") case magic != -1 && HasPrefix(contentByte, "MZ") && magic < lenb-4 && Equal(contentByte[magic:magic+4], "\x50\x45\x00\x00"): // Linux kernel images look like PE files. if Equal(contentByte[56:60], "ARMd") { - print("Linux arm64 kernel image") + fmt.Print("Linux arm64 kernel image") return } else if Equal(contentByte[514:518], "HdrS") { - print("Linux x86-64 kernel image") + fmt.Print("Linux x86-64 kernel image") return } - print("MS PE32") + fmt.Print("MS PE32") if peekLe(contentByte[magic+24:], 2) == 0x20b { - print("+") + fmt.Print("+") } - print(" executable") + fmt.Print(" executable") if peekLe(contentByte[magic+22:], 2)&0x2000 != 0 { - print("(DLL)") + fmt.Print("(DLL)") } - print(" ") + fmt.Print(" ") if peekLe(contentByte[magic+20:], 2) > 70 { types := []string{"", "native", "GUI", "console", "OS/2", "driver", "CE", "EFI", "EFI boot", "EFI runtime", "EFI ROM", "XBOX", "", "boot"} tp := peekLe(contentByte[magic+92:], 2) if tp > 0 && tp < len(types) { - print(types[tp]) + fmt.Print(types[tp]) } else { - print("unknown") + fmt.Print("unknown") } } // Ref: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format switch peekLe(contentByte[magic+4:], 2) { case 0x1c0: - print(" arm") + fmt.Print(" arm") case 0xaa64: - print(" aarch64") + fmt.Print(" aarch64") case 0x14c: - print(" Intel 80386") + fmt.Print(" Intel 80386") case 0x8664: - print(" amd64") + fmt.Print(" amd64") } case lenb > 50 && HasPrefix(contentByte, "BM") && Equal(contentByte[6:10], "\x00\x00\x00\x00"): - print("BMP image") + fmt.Print("BMP image") case lenb > 50 && HasPrefix(contentByte, "\x25\x50\x44\x46"): - print("PDF image") + fmt.Print("PDF image") case lenb > 16 && (HasPrefix(contentByte, "\x49\x49\x2a\x00") || HasPrefix(contentByte, "\x4D\x4D\x00\x2a")): - print("TIFF image data") + fmt.Print("TIFF image data") case lenb > 16 && (HasPrefix(contentByte, "ID3") || HasPrefix(contentByte, "\xff\xfb") || HasPrefix(contentByte, "\xff\xf3") || HasPrefix(contentByte, "\xff\xf2")): - print("MP3 audio file") + fmt.Print("MP3 audio file") case lenb > 16 && (HasPrefix(contentByte, "\x00\x00\x00\x20\x66\x74\x79\x70") || HasPrefix(contentByte, "\x00\x00\x00\x18\x66\x74\x79\x70") || HasPrefix(contentByte, "\x00\x00\x00\x14\x66\x74\x79\x70")): - print("MP4 video file") + fmt.Print("MP4 video file") case lenb > 16 && (HasPrefix(contentByte, "\x52\x61\x72\x21\x1A\x07\x01\x00")): - print("RAR archive data") + fmt.Print("RAR archive data") case lenb > 16 && (HasPrefix(contentByte, "\x37\x7A\xBC\xAF\x27\x1C")): - print("7zip archive data") + fmt.Print("7zip archive data") case lenb > 16 && (HasPrefix(contentByte, "\x00\x00\x01\x00")): - print("MS Windows icon resource") + fmt.Print("MS Windows icon resource") case lenb > 16 && (HasPrefix(contentByte, "\x53\x51\x4C\x69\x74\x65\x20\x66\x6F\x72\x6D\x61\x74\x20\x33\x00")): - print("SQLite database") + fmt.Print("SQLite database") case lenb > 16 && (HasPrefix(contentByte, "\x0A\x0D\x0D\x0A")): - print("PCAP-ng capture file") + fmt.Print("PCAP-ng capture file") case lenb > 16 && (HasPrefix(contentByte, "\xD4\xC3\xB2\xA1") || HasPrefix(contentByte, "\xA1\xB2\xC3\xD4") || HasPrefix(contentByte, "\x4D\x3C\xB2\xA1") || HasPrefix(contentByte, "\xA1\xB2\x3C\x4D")): - print("PCAP capture file") + fmt.Print("PCAP capture file") case lenb > 16 && (HasPrefix(contentByte, "\x66\x4C\x61\x43")): - print("FLAC audio format") + fmt.Print("FLAC audio format") case lenb > 16 && (HasPrefix(contentByte, "\x54\x44\x46\x24")): - print("Telegram Desktop file") + fmt.Print("Telegram Desktop file") case lenb > 16 && (HasPrefix(contentByte, "\x54\x44\x45\x46")): - print("Telegram Desktop encrypted file") + fmt.Print("Telegram Desktop encrypted file") case lenb > 16 && (HasPrefix(contentByte, "\x4D\x53\x43\x46")): - print("Microsoft Cabinet file") + fmt.Print("Microsoft Cabinet file") case lenb > 16 && (HasPrefix(contentByte, "\x38\x42\x50\x53")): - print("Photoshop document") + fmt.Print("Photoshop document") case lenb > 32 && HasPrefix(contentByte, "RIF") && Equal(contentByte[8:11], "AVI"): - print("AVI file") + fmt.Print("AVI file") case lenb > 32 && HasPrefix(contentByte, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"): - print("Microsoft Office (Legacy format)") + fmt.Print("Microsoft Office (Legacy format)") case lenb > 32 && HasPrefix(contentByte, "RIF") && Equal(contentByte[8:12], "WEBP"): - print("Google Webp file") + fmt.Print("Google Webp file") case lenb > 32 && HasPrefix(contentByte, "\x7B\x5C\x72\x74\x66\x31"): - print("Rich Text Format") + fmt.Print("Rich Text Format") case lenb > 32 && (HasPrefix(contentByte, ""))): - print("HTML document") + fmt.Print("HTML document") case lenb > 32 && (HasPrefix(contentByte, "