maxmind-db 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Gemfile.lock +31 -17
  4. data/README.md +2 -2
  5. data/Rakefile +1 -2
  6. data/maxmind-db.gemspec +6 -3
  7. metadata +46 -73
  8. data/test/data/LICENSE +0 -4
  9. data/test/data/MaxMind-DB-spec.md +0 -574
  10. data/test/data/README.md +0 -4
  11. data/test/data/bad-data/README.md +0 -7
  12. data/test/data/bad-data/libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb +0 -0
  13. data/test/data/bad-data/maxminddb-golang/cyclic-data-structure.mmdb +0 -0
  14. data/test/data/bad-data/maxminddb-golang/invalid-bytes-length.mmdb +0 -1
  15. data/test/data/bad-data/maxminddb-golang/invalid-data-record-offset.mmdb +0 -0
  16. data/test/data/bad-data/maxminddb-golang/invalid-map-key-length.mmdb +0 -0
  17. data/test/data/bad-data/maxminddb-golang/invalid-string-length.mmdb +0 -1
  18. data/test/data/bad-data/maxminddb-golang/metadata-is-an-uint128.mmdb +0 -1
  19. data/test/data/bad-data/maxminddb-golang/unexpected-bytes.mmdb +0 -0
  20. data/test/data/bad-data/maxminddb-python/bad-unicode-in-map-key.mmdb +0 -0
  21. data/test/data/perltidyrc +0 -12
  22. data/test/data/source-data/GeoIP2-Anonymous-IP-Test.json +0 -49
  23. data/test/data/source-data/GeoIP2-City-Test.json +0 -12885
  24. data/test/data/source-data/GeoIP2-Connection-Type-Test.json +0 -122
  25. data/test/data/source-data/GeoIP2-Country-Test.json +0 -15904
  26. data/test/data/source-data/GeoIP2-DensityIncome-Test.json +0 -14
  27. data/test/data/source-data/GeoIP2-Domain-Test.json +0 -452
  28. data/test/data/source-data/GeoIP2-Enterprise-Test.json +0 -745
  29. data/test/data/source-data/GeoIP2-ISP-Test.json +0 -12605
  30. data/test/data/source-data/GeoIP2-Precision-Enterprise-Test.json +0 -2205
  31. data/test/data/source-data/GeoIP2-Static-IP-Score-Test.json +0 -2132
  32. data/test/data/source-data/GeoIP2-User-Count-Test.json +0 -2837
  33. data/test/data/source-data/GeoLite2-ASN-Test.json +0 -4120
  34. data/test/data/source-data/GeoLite2-City-Test.json +0 -12804
  35. data/test/data/source-data/GeoLite2-Country-Test.json +0 -11280
  36. data/test/data/source-data/README +0 -15
  37. data/test/data/test-data/GeoIP2-Anonymous-IP-Test.mmdb +0 -0
  38. data/test/data/test-data/GeoIP2-City-Test-Broken-Double-Format.mmdb +0 -0
  39. data/test/data/test-data/GeoIP2-City-Test-Invalid-Node-Count.mmdb +0 -0
  40. data/test/data/test-data/GeoIP2-City-Test.mmdb +0 -0
  41. data/test/data/test-data/GeoIP2-Connection-Type-Test.mmdb +0 -0
  42. data/test/data/test-data/GeoIP2-Country-Test.mmdb +0 -0
  43. data/test/data/test-data/GeoIP2-DensityIncome-Test.mmdb +0 -0
  44. data/test/data/test-data/GeoIP2-Domain-Test.mmdb +0 -0
  45. data/test/data/test-data/GeoIP2-Enterprise-Test.mmdb +0 -0
  46. data/test/data/test-data/GeoIP2-ISP-Test.mmdb +0 -0
  47. data/test/data/test-data/GeoIP2-Precision-Enterprise-Test.mmdb +0 -0
  48. data/test/data/test-data/GeoIP2-Static-IP-Score-Test.mmdb +0 -0
  49. data/test/data/test-data/GeoIP2-User-Count-Test.mmdb +0 -0
  50. data/test/data/test-data/GeoLite2-ASN-Test.mmdb +0 -0
  51. data/test/data/test-data/GeoLite2-City-Test.mmdb +0 -0
  52. data/test/data/test-data/GeoLite2-Country-Test.mmdb +0 -0
  53. data/test/data/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb +0 -0
  54. data/test/data/test-data/MaxMind-DB-string-value-entries.mmdb +0 -0
  55. data/test/data/test-data/MaxMind-DB-test-broken-pointers-24.mmdb +0 -0
  56. data/test/data/test-data/MaxMind-DB-test-broken-search-tree-24.mmdb +0 -0
  57. data/test/data/test-data/MaxMind-DB-test-decoder.mmdb +0 -0
  58. data/test/data/test-data/MaxMind-DB-test-ipv4-24.mmdb +0 -0
  59. data/test/data/test-data/MaxMind-DB-test-ipv4-28.mmdb +0 -0
  60. data/test/data/test-data/MaxMind-DB-test-ipv4-32.mmdb +0 -0
  61. data/test/data/test-data/MaxMind-DB-test-ipv6-24.mmdb +0 -0
  62. data/test/data/test-data/MaxMind-DB-test-ipv6-28.mmdb +0 -0
  63. data/test/data/test-data/MaxMind-DB-test-ipv6-32.mmdb +0 -0
  64. data/test/data/test-data/MaxMind-DB-test-metadata-pointers.mmdb +0 -0
  65. data/test/data/test-data/MaxMind-DB-test-mixed-24.mmdb +0 -0
  66. data/test/data/test-data/MaxMind-DB-test-mixed-28.mmdb +0 -0
  67. data/test/data/test-data/MaxMind-DB-test-mixed-32.mmdb +0 -0
  68. data/test/data/test-data/MaxMind-DB-test-nested.mmdb +0 -0
  69. data/test/data/test-data/MaxMind-DB-test-pointer-decoder.mmdb +0 -0
  70. data/test/data/test-data/README.md +0 -26
  71. data/test/data/test-data/maps-with-pointers.raw +0 -0
  72. data/test/data/test-data/write-test-data.pl +0 -695
  73. data/test/data/tidyall.ini +0 -5
  74. data/test/mmdb_util.rb +0 -26
  75. data/test/test_decoder.rb +0 -241
  76. data/test/test_reader.rb +0 -547
@@ -1,574 +0,0 @@
1
- ---
2
- layout: default
3
- title: MaxMind DB File Format Specification
4
- version: v2.0
5
- ---
6
- # MaxMind DB File Format Specification
7
-
8
- ## Description
9
-
10
- The MaxMind DB file format is a database format that maps IPv4 and IPv6
11
- addresses to data records using an efficient binary search tree.
12
-
13
- ## Version
14
-
15
- This spec documents **version 2.0** of the MaxMind DB binary format.
16
-
17
- The version number consists of separate major and minor version numbers. It
18
- should not be considered a decimal number. In other words, version 2.10 comes
19
- after version 2.9.
20
-
21
- Code which is capable of reading a given major version of the format should
22
- not be broken by minor version changes to the format.
23
-
24
- ## Overview
25
-
26
- The binary database is split into three parts:
27
-
28
- 1. The binary search tree. Each level of the tree corresponds to a single bit
29
- in the prefix of the network the IP address belongs to.
30
- 2. The data section with the values for the networks in the binary search
31
- tree. These values may be comprised of a single data type, e.g., the string
32
- "US" or "New York", or they may be a more complex map or array type made up
33
- of multiple fields.
34
- 3. Database metadata. Information about the database itself.
35
-
36
- ## Database Metadata
37
-
38
- This portion of the database is stored at the end of the file. It is
39
- documented first because understanding some of the metadata is key to
40
- understanding how the other sections work.
41
-
42
- This section can be found by looking for a binary sequence matching
43
- "\xab\xcd\xefMaxMind.com". The *last* occurrence of this string in the file
44
- marks the end of the data section and the beginning of the metadata. Since we
45
- allow for arbitrary binary data in the data section, some other piece of data
46
- could contain these values. This is why you need to find the last occurrence
47
- of this sequence.
48
-
49
- The maximum allowable size for the metadata section, including the marker that
50
- starts the metadata, is 128KiB.
51
-
52
- The metadata is stored as a separate data section comprised of a map data
53
- structure starting at the beginning of that section. This structure is
54
- described later in the spec.
55
-
56
- Except where otherwise specified, each key listed is required for the database
57
- to be considered valid.
58
-
59
- Changing a key's data type or removing a key would constitute a major version
60
- change for this spec. Adding a key constitutes a minor version change.
61
-
62
- The list of known keys for the current version of the format is as follows:
63
-
64
- ### node\_count
65
-
66
- This is an unsigned 32-bit integer indicating the number of nodes in the
67
- search tree.
68
-
69
- ### record\_size
70
-
71
- This is an unsigned 16-bit integer. It indicates the number of bits in a
72
- record in the search tree. Note that each node consists of *two* records.
73
-
74
- ### ip\_version
75
-
76
- This is an unsigned 16-bit integer which is always 4 or 6. It indicates
77
- whether the database contains IPv4 or IPv6 address data.
78
-
79
- ### database\_type
80
-
81
- This is a string that indicates the structure of each data record associated
82
- with an IP address. The actual definition of these structures is left up to
83
- the database creator.
84
-
85
- Names starting with "GeoIP" are reserved for use by MaxMind (and "GeoIP" is a
86
- trademark anyway).
87
-
88
- ### languages
89
-
90
- An array of strings, each of which is a locale code. A given record may
91
- contain data items that have been localized to some or all of these
92
- locales. Records should not contain localized data for locales not included in
93
- this array.
94
-
95
- This is an optional key, as this may not be relevant for all types of data.
96
-
97
- ### binary\_format\_major\_version
98
-
99
- This is an unsigned 16-bit integer indicating the major version number for the
100
- database's binary format.
101
-
102
- ### binary\_format\_minor\_version
103
-
104
- This is an unsigned 16-bit integer indicating the minor version number for the
105
- database's binary format.
106
-
107
- ### build\_epoch
108
-
109
- This is an unsigned 64-bit integer that contains the database build timestamp
110
- as a Unix epoch value.
111
-
112
- ### description
113
-
114
- This key will always point to a map. The keys of that map will be language
115
- codes, and the values will be a description in that language as a UTF-8
116
- string.
117
-
118
- The codes may include additional information such as script or country
119
- identifiers, like "zh-TW" or "mn-Cyrl-MN". The additional identifiers will be
120
- separated by a dash character ("-").
121
-
122
- This key is optional. However, creators of databases are strongly
123
- encouraged to include a description in at least one language.
124
-
125
- ### Calculating the Search Tree Section Size
126
-
127
- The formula for calculating the search tree section size *in bytes* is as
128
- follows:
129
-
130
- ( ( $record_size * 2 ) / 8 ) * $number_of_nodes
131
-
132
- The end of the search tree marks the beginning of the data section.
133
-
134
- ## Binary Search Tree Section
135
-
136
- The database file starts with a binary search tree. The number of nodes in the
137
- tree is dependent on how many unique netblocks are needed for the particular
138
- database. For example, the city database needs many more small netblocks than
139
- the country database.
140
-
141
- The top most node is always located at the beginning of the search tree
142
- section's address space. The top node is node 0.
143
-
144
- Each node consists of two records, each of which is a pointer to an address in
145
- the file.
146
-
147
- The pointers can point to one of three things. First, it may point to another
148
- node in the search tree address space. These pointers are followed as part of
149
- the IP address search algorithm, described below.
150
-
151
- The pointer can point to a value equal to `$number_of_nodes`. If this is the
152
- case, it means that the IP address we are searching for is not in the
153
- database.
154
-
155
- Finally, it may point to an address in the data section. This is the data
156
- relevant to the given netblock.
157
-
158
- ### Node Layout
159
-
160
- Each node in the search tree consists of two records, each of which is a
161
- pointer. The record size varies by database, but inside a single database node
162
- records are always the same size. A record may be anywhere from 24 to 128 bits
163
- long, depending on the number of nodes in the tree. These pointers are
164
- stored in big-endian format (most significant byte first).
165
-
166
- Here are some examples of how the records are laid out in a node for 24, 28,
167
- and 32 bit records. Larger record sizes follow this same pattern.
168
-
169
- #### 24 bits (small database), one node is 6 bytes
170
-
171
- | <------------- node --------------->|
172
- | 23 .. 0 | 23 .. 0 |
173
-
174
- #### 28 bits (medium database), one node is 7 bytes
175
-
176
- | <------------- node --------------->|
177
- | 23 .. 0 | 27..24 | 27..24 | 23 .. 0 |
178
-
179
- Note 4 bits of each pointer are combined into the middle byte. For both
180
- records, they are prepended and end up in the most significant position.
181
-
182
- #### 32 bits (large database), one node is 8 bytes
183
-
184
- | <------------- node --------------->|
185
- | 31 .. 0 | 31 .. 0 |
186
-
187
- ### Search Lookup Algorithm
188
-
189
- The first step is to convert the IP address to its big-endian binary
190
- representation. For an IPv4 address, this becomes 32 bits. For IPv6 you get
191
- 128 bits.
192
-
193
- The leftmost bit corresponds to the first node in the search tree. For each
194
- bit, a value of 0 means we choose the left record in a node, and a value of 1
195
- means we choose the right record.
196
-
197
- The record value is always interpreted as an unsigned integer. The maximum
198
- size of the integer is dependent on the number of bits in a record (24, 28, or
199
- 32).
200
-
201
- If the record value is a number that is less than the *number of nodes* (not
202
- in bytes, but the actual node count) in the search tree (this is stored in the
203
- database metadata), then the value is a node number. In this case, we find
204
- that node in the search tree and repeat the lookup algorithm from there.
205
-
206
- If the record value is equal to the number of nodes, that means that we do not
207
- have any data for the IP address, and the search ends here.
208
-
209
- If the record value is *greater* than the number of nodes in the search tree,
210
- then it is an actual pointer value pointing into the data section. The value
211
- of the pointer is relative to the start of the data section, *not* the
212
- start of the file.
213
-
214
- In order to determine where in the data section we should start looking, we use
215
- the following formula:
216
-
217
- $data_section_offset = ( $record_value - $node_count ) - 16
218
-
219
- The 16 is the size of the data section separator. We subtract it because we
220
- want to permit pointing to the first byte of the data section. Recall that
221
- the record value cannot equal the node count as that means there is no
222
- data. Instead, we choose to start values that go to the data section at
223
- `$node_count + 16`. (This has the side effect that record values
224
- `$node_count + 1` through `$node_count + 15` inclusive are not valid).
225
-
226
- This is best demonstrated by an example:
227
-
228
- Let's assume we have a 24-bit tree with 1,000 nodes. Each node contains 48
229
- bits, or 6 bytes. The size of the tree is 6,000 bytes.
230
-
231
- When a record in the tree contains a number that is less than 1,000, this
232
- is a *node number*, and we look up that node. If a record contains a value
233
- greater than or equal to 1,016, we know that it is a data section value. We
234
- subtract the node count (1,000) and then subtract 16 for the data section
235
- separator, giving us the number 0, the first byte of the data section.
236
-
237
- If a record contained the value 6,000, this formula would give us an offset of
238
- 4,984 into the data section.
239
-
240
- In order to determine where in the file this offset really points to, we also
241
- need to know where the data section starts. This can be calculated by
242
- determining the size of the search tree in bytes and then adding an additional
243
- 16 bytes for the data section separator:
244
-
245
- $offset_in_file = $data_section_offset
246
- + $search_tree_size_in_bytes
247
- + 16
248
-
249
- Since we subtract and then add 16, the final formula to determine the
250
- offset in the file can be simplified to:
251
-
252
- $offset_in_file = ( $record_value - $node_count )
253
- + $search_tree_size_in_bytes
254
-
255
- ### IPv4 addresses in an IPv6 tree
256
-
257
- When storing IPv4 addresses in an IPv6 tree, they are stored as-is, so they
258
- occupy the first 32-bits of the address space (from 0 to 2**32 - 1).
259
-
260
- Creators of databases should decide on a strategy for handling the various
261
- mappings between IPv4 and IPv6.
262
-
263
- The strategy that MaxMind uses for its GeoIP databases is to include a pointer
264
- from the `::ffff:0:0/96` subnet to the root node of the IPv4 address space in
265
- the tree. This accounts for the
266
- [IPv4-mapped IPv6 address](http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses).
267
-
268
- MaxMind also includes a pointer from the `2002::/16` subnet to the root node
269
- of the IPv4 address space in the tree. This accounts for the
270
- [6to4 mapping](http://en.wikipedia.org/wiki/6to4) subnet.
271
-
272
- Database creators are encouraged to document whether they are doing something
273
- similar for their databases.
274
-
275
- The Teredo subnet cannot be accounted for in the tree. Instead, code that
276
- searches the tree can offer to decode the IPv4 portion of a Teredo address and
277
- look that up.
278
-
279
- ## Data Section Separator
280
-
281
- There are 16 bytes of NULLs in between the search tree and the data
282
- section. This separator exists in order to make it possible for a verification
283
- tool to distinguish between the two sections.
284
-
285
- This separator is not considered part of the data section itself. In other
286
- words, the data section starts at `$size_of_search_tree + 16` bytes in the
287
- file.
288
-
289
- ## Output Data Section
290
-
291
- Each output data field has an associated type, and that type is encoded as a
292
- number that begins the data field. Some types are variable length. In those
293
- cases, the type indicator is also followed by a length. The data payload
294
- always comes at the end of the field.
295
-
296
- All binary data is stored in big-endian format.
297
-
298
- Note that the *interpretation* of a given data type's meaning is decided by
299
- higher-level APIs, not by the binary format itself.
300
-
301
- ### pointer - 1
302
-
303
- A pointer to another part of the data section's address space. The pointer
304
- will point to the beginning of a field. It is illegal for a pointer to point
305
- to another pointer.
306
-
307
- Pointer values start from the beginning of the data section, *not* the
308
- beginning of the file. Pointers in the metadata start from the beginning of
309
- the metadata section.
310
-
311
- ### UTF-8 string - 2
312
-
313
- A variable length byte sequence that contains valid utf8. If the length is
314
- zero then this is an empty string.
315
-
316
- ### double - 3
317
-
318
- This is stored as an IEEE-754 double (binary64) in big-endian format. The
319
- length of a double is always 8 bytes.
320
-
321
- ### bytes - 4
322
-
323
- A variable length byte sequence containing any sort of binary data. If the
324
- length is zero then this a zero-length byte sequence.
325
-
326
- This is not currently used but may be used in the future to embed non-text
327
- data (images, etc.).
328
-
329
- ### integer formats
330
-
331
- Integers are stored in variable length binary fields.
332
-
333
- We support 16-bit, 32-bit, 64-bit, and 128-bit unsigned integers. We also
334
- support 32-bit signed integers.
335
-
336
- A 128-bit integer can use up to 16 bytes, but may use fewer. Similarly, a
337
- 32-bit integer may use from 0-4 bytes. The number of bytes used is determined
338
- by the length specifier in the control byte. See below for details.
339
-
340
- A length of zero always indicates the number 0.
341
-
342
- When storing a signed integer, fields shorter than the maximum byte length
343
- are always positive. When the field is the maximum length, e.g., 4 bytes for
344
- 32-bit integers, the left-most bit is the sign. A 1 is negative and a 0 is
345
- positive.
346
-
347
- The type numbers for our integer types are:
348
-
349
- * unsigned 16-bit int - 5
350
- * unsigned 32-bit int - 6
351
- * signed 32-bit int - 8
352
- * unsigned 64-bit int - 9
353
- * unsigned 128-bit int - 10
354
-
355
- The unsigned 32-bit and 128-bit types may be used to store IPv4 and IPv6
356
- addresses, respectively.
357
-
358
- The signed 32-bit integers are stored using the 2's complement representation.
359
-
360
- ### map - 7
361
-
362
- A map data type contains a set of key/value pairs. Unlike other data types,
363
- the length information for maps indicates how many key/value pairs it
364
- contains, not its length in bytes. This size can be zero.
365
-
366
- See below for the algorithm used to determine the number of pairs in the
367
- hash. This algorithm is also used to determine the length of a field's
368
- payload.
369
-
370
- ### array - 11
371
-
372
- An array type contains a set of ordered values. The length information for
373
- arrays indicates how many values it contains, not its length in bytes. This
374
- size can be zero.
375
-
376
- This type uses the same algorithm as maps for determining the length of a
377
- field's payload.
378
-
379
- ### data cache container - 12
380
-
381
- This is a special data type that marks a container used to cache repeated
382
- data. For example, instead of repeating the string "United States" over and
383
- over in the database, we store it in the cache container and use pointers
384
- *into* this container instead.
385
-
386
- Nothing in the database will ever contain a pointer to this field
387
- itself. Instead, various fields will point into the container.
388
-
389
- The primary reason for making this a separate data type versus simply inlining
390
- the cached data is so that a database dumper tool can skip this cache when
391
- dumping the data section. The cache contents will end up being dumped as
392
- pointers into it are followed.
393
-
394
- ### end marker - 13
395
-
396
- The end marker marks the end of the data section. It is not strictly
397
- necessary, but including this marker allows a data section deserializer to
398
- process a stream of input, rather than having to find the end of the section
399
- before beginning the deserialization.
400
-
401
- This data type is not followed by a payload, and its size is always zero.
402
-
403
- ### boolean - 14
404
-
405
- A true or false value. The length information for a boolean type will always
406
- be 0 or 1, indicating the value. There is no payload for this field.
407
-
408
- ### float - 15
409
-
410
- This is stored as an IEEE-754 float (binary32) in big-endian format. The
411
- length of a float is always 4 bytes.
412
-
413
- This type is provided primarily for completeness. Because of the way floating
414
- point numbers are stored, this type can easily lose precision when serialized
415
- and then deserialized. If this is an issue for you, consider using a double
416
- instead.
417
-
418
- ### Data Field Format
419
-
420
- Each field starts with a control byte. This control byte provides information
421
- about the field's data type and payload size.
422
-
423
- The first three bits of the control byte tell you what type the field is. If
424
- these bits are all 0, then this is an "extended" type, which means that the
425
- *next* byte contains the actual type. Otherwise, the first three bits will
426
- contain a number from 1 to 7, the actual type for the field.
427
-
428
- We've tried to assign the most commonly used types as numbers 1-7 as an
429
- optimization.
430
-
431
- With an extended type, the type number in the second byte is the number
432
- minus 7. In other words, an array (type 11) will be stored with a 0 for the
433
- type in the first byte and a 4 in the second.
434
-
435
- Here is an example of how the control byte may combine with the next byte to
436
- tell us the type:
437
-
438
- 001XXXXX pointer
439
- 010XXXXX UTF-8 string
440
- 110XXXXX unsigned 32-bit int (ASCII)
441
- 000XXXXX 00000011 unsigned 128-bit int (binary)
442
- 000XXXXX 00000100 array
443
- 000XXXXX 00000110 end marker
444
-
445
- #### Payload Size
446
-
447
- The next five bits in the control byte tell you how long the data field's
448
- payload is, except for maps and pointers. Maps and pointers use this size
449
- information a bit differently. See below.
450
-
451
- If the five bits are smaller than 29, then those bits are the payload size in
452
- bytes. For example:
453
-
454
- 01000010 UTF-8 string - 2 bytes long
455
- 01011100 UTF-8 string - 28 bytes long
456
- 11000001 unsigned 32-bit int - 1 byte long
457
- 00000011 00000011 unsigned 128-bit int - 3 bytes long
458
-
459
- If the five bits are equal to 29, 30, or 31, then use the following algorithm
460
- to calculate the payload size.
461
-
462
- If the value is 29, then the size is 29 + *the next byte after the type
463
- specifying bytes as an unsigned integer*.
464
-
465
- If the value is 30, then the size is 285 + *the next two bytes after the type
466
- specifying bytes as a single unsigned integer*.
467
-
468
- If the value is 31, then the size is 65,821 + *the next three bytes after the
469
- type specifying bytes as a single unsigned integer*.
470
-
471
- Some examples:
472
-
473
- 01011101 00110011 UTF-8 string - 80 bytes long
474
-
475
- In this case, the last five bits of the control byte equal 29. We treat the
476
- next byte as an unsigned integer. The next byte is 51, so the total size is
477
- (29 + 51) = 80.
478
-
479
- 01011110 00110011 00110011 UTF-8 string - 13,392 bytes long
480
-
481
- The last five bits of the control byte equal 30. We treat the next two bytes
482
- as a single unsigned integer. The next two bytes equal 13,107, so the total
483
- size is (285 + 13,107) = 13,392.
484
-
485
- 01011111 00110011 00110011 00110011 UTF-8 string - 3,421,264 bytes long
486
-
487
- The last five bits of the control byte equal 31. We treat the next three bytes
488
- as a single unsigned integer. The next three bytes equal 3,355,443, so the
489
- total size is (65,821 + 3,355,443) = 3,421,264.
490
-
491
- This means that the maximum payload size for a single field is 16,843,036
492
- bytes.
493
-
494
- The binary number types always have a known size, but for consistency's sake,
495
- the control byte will always specify the correct size for these types.
496
-
497
- #### Maps
498
-
499
- Maps use the size in the control byte (and any following bytes) to indicate
500
- the number of key/value pairs in the map, not the size of the payload in
501
- bytes.
502
-
503
- This means that the maximum number of pairs for a single map is 16,843,036.
504
-
505
- Maps are laid out with each key followed by its value, followed by the next
506
- pair, etc.
507
-
508
- The keys are **always** UTF-8 strings. The values may be any data type,
509
- including maps or pointers.
510
-
511
- Once we know the number of pairs, we can look at each pair in turn to
512
- determine the size of the key and the key name, as well as the value's type
513
- and payload.
514
-
515
- #### Pointers
516
-
517
- Pointers use the last five bits in the control byte to calculate the pointer
518
- value.
519
-
520
- To calculate the pointer value, we start by subdividing the five bits into two
521
- groups. The first two bits indicate the size, and the next three bits are part
522
- of the value, so we end up with a control byte breaking down like this:
523
- 001SSVVV.
524
-
525
- The size can be 0, 1, 2, or 3.
526
-
527
- If the size is 0, the pointer is built by appending the next byte to the last
528
- three bits to produce an 11-bit value.
529
-
530
- If the size is 1, the pointer is built by appending the next two bytes to the
531
- last three bits to produce a 19-bit value + 2048.
532
-
533
- If the size is 2, the pointer is built by appending the next three bytes to the
534
- last three bits to produce a 27-bit value + 526336.
535
-
536
- Finally, if the size is 3, the pointer's value is contained in the next four
537
- bytes as a 32-bit value. In this case, the last three bits of the control byte
538
- are ignored.
539
-
540
- This means that we are limited to 4GB of address space for pointers, so the
541
- data section size for the database is limited to 4GB.
542
-
543
- ## Reference Implementations
544
-
545
- ### Writer
546
-
547
- * [Perl](https://github.com/maxmind/MaxMind-DB-Writer-perl)
548
-
549
- ### Reader
550
-
551
- * [C](https://github.com/maxmind/libmaxminddb)
552
- * [C#](https://github.com/maxmind/MaxMind-DB-Reader-dotnet)
553
- * [Java](https://github.com/maxmind/MaxMind-DB-Reader-java)
554
- * [Perl](https://github.com/maxmind/MaxMind-DB-Reader-perl)
555
- * [PHP](https://github.com/maxmind/MaxMind-DB-Reader-php)
556
- * [Python](https://github.com/maxmind/MaxMind-DB-Reader-python)
557
- * [Ruby](https://github.com/maxmind/MaxMind-DB-Reader-ruby)
558
-
559
- ## Authors
560
-
561
- This specification was created by the following authors:
562
-
563
- * Greg Oschwald \<goschwald@maxmind.com\>
564
- * Dave Rolsky \<drolsky@maxmind.com\>
565
- * Boris Zentner \<bzentner@maxmind.com\>
566
-
567
- ## License
568
-
569
- This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
570
- Unported License. To view a copy of this license, visit
571
- [http://creativecommons.org/licenses/by-sa/3.0/](http://creativecommons.org/licenses/by-sa/3.0/)
572
- or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain
573
- View, California, 94041, USA
574
-
data/test/data/README.md DELETED
@@ -1,4 +0,0 @@
1
- MaxMind DB is a binary file format that stores data indexed by IP address
2
- subnets (IPv4 or IPv6).
3
-
4
- This repository contains the spec for that format.
@@ -1,7 +0,0 @@
1
- These are corrupt databases that have been know to cause problems such as
2
- segfaults or unhandled errors on one or more MaxMind DB reader
3
- implementations. Implementations _should_ return an appropriate error
4
- or raise an exception on these databases.
5
-
6
- If you find a corrupt test-sized database that crashes a MMDB reader library,
7
- please feel free to add it here by creating a pull request.
@@ -1 +0,0 @@
1
- ���MaxMind.com�Kdescription�Ben�
@@ -1 +0,0 @@
1
- Dmap2�Earray�Dmap3�Aa�Ab�Ac����MaxMind.com�[binary_format_major_version�[binary_format_minor_version�Kbuild_epochX�2|Mdatabase_type]MaxMind DB Nested Data StructuresKdescription
@@ -1 +0,0 @@
1
- ���MaxMind.com
data/test/data/perltidyrc DELETED
@@ -1,12 +0,0 @@
1
- --blank-lines-before-packages=0
2
- --iterations=2
3
- --no-outdent-long-comments
4
- -b
5
- -bar
6
- -boc
7
- -ci=4
8
- -i=4
9
- -l=78
10
- -nolq
11
- -se
12
- -wbb="% + - * / x != == >= <= =~ !~ < > | & >= < = **= += *= &= <<= &&= -= /= |= >>= ||= .= %= ^= x="
@@ -1,49 +0,0 @@
1
- [
2
- {
3
- "::1.2.0.0/112" : {
4
- "is_anonymous" : true,
5
- "is_anonymous_vpn" : true
6
- }
7
- },
8
- {
9
- "::1.124.213.1/128" : {
10
- "is_anonymous" : true,
11
- "is_anonymous_vpn" : true,
12
- "is_tor_exit_node" : true
13
- }
14
- },
15
- {
16
- "::81.2.69.0/120" : {
17
- "is_anonymous" : true,
18
- "is_anonymous_vpn" : true,
19
- "is_hosting_provider" : true,
20
- "is_public_proxy" : true,
21
- "is_residential_proxy" : true,
22
- "is_tor_exit_node" : true
23
- }
24
- },
25
- {
26
- "::71.160.223.0/120" : {
27
- "is_anonymous" : true,
28
- "is_hosting_provider" : true
29
- }
30
- },
31
- {
32
- "::186.30.236.0/120" : {
33
- "is_anonymous" : true,
34
- "is_public_proxy" : true
35
- }
36
- },
37
- {
38
- "::65.0.0.0/109" : {
39
- "is_anonymous" : true,
40
- "is_tor_exit_node" : true
41
- }
42
- },
43
- {
44
- "abcd:1000::/112" : {
45
- "is_anonymous" : true,
46
- "is_public_proxy" : true
47
- }
48
- }
49
- ]