File: bin.go

package info (click to toggle)
golang-github-gitleaks-go-gitdiff 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 800 kB
  • sloc: makefile: 2; sh: 1
file content (124 lines) | stat: -rw-r--r-- 2,547 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//+build ignore

// bin.go is a helper CLI to manipulate binary diff data for testing purposes.
// It can decode patches generated by git using the standard parsing functions
// or it can encode binary data back into the format expected by Git. It
// operates on stdin writes results (possibly binary) to stdout.

package main

import (
	"bytes"
	"compress/zlib"
	"encoding/binary"
	"flag"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/gitleaks/go-gitdiff/gitdiff"
)

var (
	b85Powers = []uint32{52200625, 614125, 7225, 85, 1}
	b85Alpha  = []byte(
		"0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "!#$%&()*+-;<=>?@^_`{|}~",
	)
)

var mode string

func base85Encode(data []byte) []byte {
	chunks, remaining := len(data)/4, len(data)%4
	if remaining > 0 {
		data = append(data, make([]byte, 4-remaining)...)
		chunks++
	}

	var n int
	out := make([]byte, 5*chunks)

	for i := 0; i < len(data); i += 4 {
		v := binary.BigEndian.Uint32(data[i : i+4])
		for j := 0; j < 5; j++ {
			p := v / b85Powers[j]
			out[n+j] = b85Alpha[p]
			v -= b85Powers[j] * p
		}
		n += 5
	}

	return out
}

func compress(data []byte) ([]byte, error) {
	var b bytes.Buffer
	w := zlib.NewWriter(&b)

	if _, err := w.Write(data); err != nil {
		return nil, err
	}
	if err := w.Close(); err != nil {
		return nil, err
	}

	return b.Bytes(), nil
}

func wrap(data []byte) string {
	var s strings.Builder
	for i := 0; i < len(data); i += 52 {
		c := 52
		if c > len(data)-i {
			c = len(data) - i
		}
		b := (c / 5) * 4

		if b <= 26 {
			s.WriteByte(byte('A' + b - 1))
		} else {
			s.WriteByte(byte('a' + b - 27))
		}
		s.Write(data[i : i+c])
		s.WriteByte('\n')
	}
	return s.String()
}

func init() {
	flag.StringVar(&mode, "mode", "parse", "operation mode, one of 'parse' or 'encode'")
}

func main() {
	flag.Parse()

	switch mode {
	case "parse":
		files, _, err := gitdiff.Parse(os.Stdin)
		if err != nil {
			log.Fatalf("failed to parse file: %v", err)
		}
		if len(files) != 1 {
			log.Fatalf("patch contains more than one file: %d", len(files))
		}
		if files[0].BinaryFragment == nil {
			log.Fatalf("patch file does not contain a binary fragment")
		}
		os.Stdout.Write(files[0].BinaryFragment.Data)

	case "encode":
		data, err := ioutil.ReadAll(os.Stdin)
		if err != nil {
			log.Fatalf("failed to read input: %v", err)
		}
		data, err = compress(data)
		if err != nil {
			log.Fatalf("failed to compress data: %v", err)
		}
		os.Stdout.WriteString(wrap(base85Encode(data)))

	default:
		log.Fatalf("unknown mode: %s", mode)
	}
}