aboutsummaryrefslogtreecommitdiff
path: root/object/signature/parse.go
blob: b39100cdb58f70bbd0275aa262d093ce906a2aa8 (about) (plain) (blame)
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
package signature

import (
	"bytes"
	"fmt"
	"strconv"

	"lindenii.org/go/lgo/intconv"
)

// Parse parses a canonical Git signature line.
//
// Labels: Life-Independent.
func Parse(line []byte) (*Signature, error) {
	lt := bytes.IndexByte(line, '<')
	if lt < 0 {
		return nil, fmt.Errorf("%w: missing '<' before email", ErrInvalidSignature)
	}

	gtRel := bytes.IndexByte(line[lt+1:], '>')
	if gtRel < 0 {
		return nil, fmt.Errorf("%w: missing '>' after email", ErrInvalidSignature)
	}

	gt := lt + 1 + gtRel

	nameBytes := append([]byte(nil), bytes.TrimRight(line[:lt], " ")...)
	emailBytes := append([]byte(nil), line[lt+1:gt]...)

	rest := line[gt+1:]
	if len(rest) == 0 || rest[0] != ' ' {
		return nil, fmt.Errorf("%w: missing ' ' before timestamp", ErrInvalidSignature)
	}

	rest = rest[1:]

	before, after, ok := bytes.Cut(rest, []byte{' '})
	if !ok {
		return nil, fmt.Errorf("%w: missing ' ' between timestamp and timezone", ErrInvalidSignature)
	}

	when, err := strconv.ParseInt(string(before), 10, 64)
	if err != nil {
		return nil, fmt.Errorf("%w: timestamp %q: %w", ErrInvalidSignature, before, err)
	}

	tz := after
	if len(tz) < 5 {
		return nil, fmt.Errorf("%w: timezone %q is too short", ErrInvalidSignature, tz)
	}

	sign := 1

	switch tz[0] {
	case '-':
		sign = -1
	case '+':
	default:
		return nil, fmt.Errorf("%w: timezone sign %q is not '+' or '-'", ErrInvalidSignature, tz[0])
	}

	hh, err := strconv.Atoi(string(tz[1:3]))
	if err != nil {
		return nil, fmt.Errorf("%w: timezone hour %q: %w", ErrInvalidSignature, tz[1:3], err)
	}

	mm, err := strconv.Atoi(string(tz[3:5]))
	if err != nil {
		return nil, fmt.Errorf("%w: timezone minute %q: %w", ErrInvalidSignature, tz[3:5], err)
	}

	if hh < 0 || hh > 23 {
		return nil, fmt.Errorf("%w: timezone hour %d out of range", ErrInvalidSignature, hh)
	}

	if mm < 0 || mm > 59 {
		return nil, fmt.Errorf("%w: timezone minute %d out of range", ErrInvalidSignature, mm)
	}

	total := int64(hh)*60 + int64(mm)

	offset, err := intconv.Int64ToInt32(total)
	if err != nil {
		return nil, fmt.Errorf("%w: timezone offset out of range", ErrInvalidSignature)
	}

	if sign < 0 {
		offset = -offset
	}

	return &Signature{
		Name:          nameBytes,
		Email:         emailBytes,
		WhenUnix:      when,
		OffsetMinutes: offset,
	}, nil
}