File: verifier.go

package info (click to toggle)
golang-github-oschwald-maxminddb-golang-v2 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: perl: 557; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,445 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
package decoder

import (
	"reflect"

	"github.com/oschwald/maxminddb-golang/v2/internal/mmdberrors"
)

// VerifyDataSection verifies the data section against the provided
// offsets from the tree.
func (d *ReflectionDecoder) VerifyDataSection(offsets map[uint]bool) error {
	pointerCount := len(offsets)

	var offset uint
	bufferLen := uint(len(d.buffer))
	for offset < bufferLen {
		var data any
		rv := reflect.ValueOf(&data)
		newOffset, err := d.decode(offset, rv, 0)
		if err != nil {
			return mmdberrors.NewInvalidDatabaseError(
				"received decoding error (%v) at offset of %v",
				err,
				offset,
			)
		}
		if newOffset <= offset {
			return mmdberrors.NewInvalidDatabaseError(
				"data section offset unexpectedly went from %v to %v",
				offset,
				newOffset,
			)
		}

		pointer := offset

		if _, ok := offsets[pointer]; !ok {
			return mmdberrors.NewInvalidDatabaseError(
				"found data (%v) at %v that the search tree does not point to",
				data,
				pointer,
			)
		}
		delete(offsets, pointer)

		offset = newOffset
	}

	if offset != bufferLen {
		return mmdberrors.NewInvalidDatabaseError(
			"unexpected data at the end of the data section (last offset: %v, end: %v)",
			offset,
			bufferLen,
		)
	}

	if len(offsets) != 0 {
		return mmdberrors.NewInvalidDatabaseError(
			"found %v pointers (of %v) in the search tree that we did not see in the data section",
			len(offsets),
			pointerCount,
		)
	}
	return nil
}