binary

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: BSD-3-Clause Imports: 5 Imported by: 3

README

Binary

Go module and command line utility for checking if the given file or data is likely to be binary or text.

  • It does so by reading the first, middle and last 24 bytes of the file and trying to convert the data to utf8.
  • If one of the 24 byte blocks can not be converted to utf8, it's considered to be a binary file.
  • Also, if one of the blocks have more than 33% zero bytes, it's considered to be a binary file.
  • If the first 24 bytes indicates that it's a binary file, the deduction is done, and no more seeking or reading will happen.
  • If the file is empty, it's considered to be a text file.
  • The binary utility has overlapping functionality with the file utility, but with a more limited focus.

Installing the utility

With Go 1.24 or later:

go install github.com/xyproto/binary/cmd/binary@latest

Example use

  • binary /usr/bin/ls returns binary.
  • binary /etc/fstab returns text.

Using the Go module

package main

import (
    "fmt"
    "os"

    "github.com/xyproto/binary"
)

func main() {
    filename := os.Args[0]
    isBinary, err := binary.File(filename)
    if err != nil {
        fmt.Fprintf(os.Stderr, "%s\n", err)
        os.Exit(1)
    }
    fmt.Printf("%s is a binary file: %v\n", filename, isBinary)
}

The binary.Data function can be used to determine if a byte slice contains binary data or not.

General info

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Data added in v1.3.0

func Data(data []byte) bool

Data tries to determine if the given data is binary by examining the first, last and middle 24 bytes, then using the probablyBinaryData function on each of them in turn.

func DataAccurate added in v1.4.0

func DataAccurate(data []byte) bool

DataAccurate determines if the given data is binary using thorough analysis. Unlike Data, this reads more of the input and uses magic number detection.

func DataAccurateAndUTF16 added in v1.4.0

func DataAccurateAndUTF16(data []byte) (bool, bool)

DataAccurateAndUTF16 determines if the given data is binary using thorough analysis. Also returns whether the data appears to be UTF-16 encoded.

func DataAndUTF16 added in v1.3.3

func DataAndUTF16(data []byte) (bool, bool)

DataAndUTF16 tries to determine if the given data is binary by examining the first, last and middle 24 bytes, then using the probablyBinaryData function on each of them in turn. Also returns a bool if the data appears to be UTF-16.

func File added in v1.3.0

func File(filename string) (bool, error)

File tries to determine if the given filename is a binary file by reading the first, last and middle 24 bytes, then using the probablyBinaryData function on each of them in turn.

func FileAccurate added in v1.4.0

func FileAccurate(filename string) (bool, error)

FileAccurate determines if the given file is binary using thorough analysis. Unlike File, this reads more of the file and uses magic number detection.

func FileAccurateAndUTF16 added in v1.4.0

func FileAccurateAndUTF16(filename string) (bool, bool, error)

FileAccurateAndUTF16 determines if the given file is binary using thorough analysis. Also returns whether the file appears to be UTF-16 encoded.

func FileAndUTF16 added in v1.3.3

func FileAndUTF16(filename string) (bool, bool, error)

FileAndUTF16 tries to determine if the given filename is a binary file by reading the first, last and middle 24 bytes, then using the probablyBinaryData function on each of them in turn. Also return true if it is probably UTF-16.

Types

This section is empty.

Directories

Path Synopsis
cmd
binary command
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL