File: decoder.go

package info (click to toggle)
python-maxminddb 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,600 kB
  • sloc: ansic: 7,565; python: 1,711; perl: 987; makefile: 273; sh: 190
file content (178 lines) | stat: -rw-r--r-- 4,847 bytes parent folder | download | duplicates (3)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package writer

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"math"
	"math/big"
	"net/netip"

	"github.com/maxmind/mmdbwriter"
	"github.com/maxmind/mmdbwriter/mmdbtype"
	"go4.org/netipx"
)

// WriteDecoderTestDB writes an mmdb file with all possible record value types.
func (w *Writer) WriteDecoderTestDB() error {
	dbWriter, err := mmdbwriter.New(
		mmdbwriter.Options{
			DatabaseType: "MaxMind DB Decoder Test",
			Description: map[string]string{
				"en": "MaxMind DB Decoder Test database - contains every MaxMind DB data type",
			},
			DisableIPv4Aliasing:     false,
			IncludeReservedNetworks: true,
			IPVersion:               6,
			Languages:               []string{"en"},
			RecordSize:              24,
		},
	)
	if err != nil {
		return fmt.Errorf("creating mmdbwriter: %w", err)
	}

	addrs, err := parseIPSlice(ipSample)
	if err != nil {
		return fmt.Errorf("parsing ip addresses: %w", err)
	}
	if err := insertAllTypes(dbWriter, addrs); err != nil {
		return fmt.Errorf("inserting all types records: %w", err)
	}

	zeroAddr, err := netip.ParsePrefix("::0.0.0.0/128")
	if err != nil {
		return fmt.Errorf("parsing ip: %w", err)
	}
	if err := insertAllTypesZero(dbWriter, []netip.Prefix{zeroAddr}); err != nil {
		return fmt.Errorf("inserting all types records: %w", err)
	}

	maxAddr, err := netip.ParsePrefix("::255.255.255.255/128")
	if err != nil {
		return fmt.Errorf("parsing ip: %w", err)
	}
	if err := insertNumericMax(dbWriter, []netip.Prefix{maxAddr}); err != nil {
		return fmt.Errorf("inserting all types records: %w", err)
	}

	if err := w.write(dbWriter, "MaxMind-DB-test-decoder.mmdb"); err != nil {
		return fmt.Errorf("writing database: %w", err)
	}
	return nil
}

// insertAllTypes inserts records with all possible value types.
func insertAllTypes(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
	buf := new(bytes.Buffer)
	if err := binary.Write(buf, binary.BigEndian, uint32(42)); err != nil {
		return fmt.Errorf("creating buffer for all types record: %w", err)
	}

	ui64 := big.Int{}
	ui64.Lsh(big.NewInt(1), 60)

	ui128 := big.Int{}
	ui128.Lsh(big.NewInt(1), 120)
	mmdbUint128 := mmdbtype.Uint128(ui128)

	allTypes := mmdbtype.Map{
		"array": mmdbtype.Slice{
			mmdbtype.Uint32(1),
			mmdbtype.Uint32(2),
			mmdbtype.Uint32(3),
		},
		"bytes":   mmdbtype.Bytes(buf.Bytes()),
		"boolean": mmdbtype.Bool(true),
		"double":  mmdbtype.Float64(42.123456),
		"float":   mmdbtype.Float32(1.1),
		"int32":   mmdbtype.Int32(-1 * math.Pow(2, 28)),
		"map": mmdbtype.Map{
			"mapX": mmdbtype.Map{
				"utf8_stringX": mmdbtype.String("hello"),
				"arrayX": mmdbtype.Slice{
					mmdbtype.Uint32(7),
					mmdbtype.Uint32(8),
					mmdbtype.Uint32(9),
				},
			},
		},
		"uint16":      mmdbtype.Uint16(100),
		"uint32":      mmdbtype.Uint32(math.Pow(2, 28)),
		"uint64":      mmdbtype.Uint64(ui64.Uint64()),
		"uint128":     mmdbUint128.Copy(),
		"utf8_string": mmdbtype.String("unicode! ☯ - ♫"),
	}

	for _, addr := range ipAddresses {
		err := w.Insert(
			netipx.PrefixIPNet(addr),
			allTypes,
		)
		if err != nil {
			return fmt.Errorf("inserting ip: %w", err)
		}
	}
	return nil
}

// insertAllTypesZero inserts records with all possible value types with zero values.
func insertAllTypesZero(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
	var uint128 big.Int
	mmdbUint128 := mmdbtype.Uint128(uint128)

	zeroValues := mmdbtype.Map{
		"array":       mmdbtype.Slice{},
		"bytes":       mmdbtype.Bytes([]byte{}),
		"boolean":     mmdbtype.Bool(false),
		"double":      mmdbtype.Float64(0),
		"float":       mmdbtype.Float32(0),
		"int32":       mmdbtype.Int32(0),
		"map":         mmdbtype.Map{},
		"uint16":      mmdbtype.Uint16(0),
		"uint32":      mmdbtype.Uint32(0),
		"uint64":      mmdbtype.Uint64(0),
		"uint128":     mmdbUint128.Copy(),
		"utf8_string": mmdbtype.String(""),
	}

	for _, addr := range ipAddresses {
		err := w.Insert(
			netipx.PrefixIPNet(addr),
			zeroValues,
		)
		if err != nil {
			return fmt.Errorf("inserting ip: %w", err)
		}
	}
	return nil
}

// insertNumericMax inserts records with numeric types maxed out.
func insertNumericMax(w *mmdbwriter.Tree, ipAddresses []netip.Prefix) error {
	var uint128Max big.Int
	uint128Max.Exp(big.NewInt(2), big.NewInt(128), nil)
	uint128Max.Sub(&uint128Max, big.NewInt(1))
	mmdbUint128 := mmdbtype.Uint128(uint128Max)

	numMax := mmdbtype.Map{
		"double":  mmdbtype.Float64(math.Inf(1)),
		"float":   mmdbtype.Float32(float32(math.Inf(1))),
		"int32":   mmdbtype.Int32(1<<31 - 1),
		"uint16":  mmdbtype.Uint16(0xffff),
		"uint32":  mmdbtype.Uint32(0xffffffff),
		"uint64":  mmdbtype.Uint64(0xffffffffffffffff),
		"uint128": mmdbUint128.Copy(),
	}

	for _, addr := range ipAddresses {
		err := w.Insert(
			netipx.PrefixIPNet(addr),
			numMax,
		)
		if err != nil {
			return fmt.Errorf("inserting ip: %w", err)
		}
	}
	return nil
}