1#!/usr/bin/perl
2
3# Name:         dbinfo -- identify berkeley DB version used to create
4#                         a database file
5#
6# Author:       Paul Marquess  <pmqs@cpan.org>
7# Version:      1.07
8# Date          2nd April 2011
9#
10#     Copyright (c) 1998-2021 Paul Marquess. All rights reserved.
11#     This program is free software; you can redistribute it and/or
12#     modify it under the same terms as Perl itself.
13
14# Todo: Print more stats on a db file, e.g. no of records
15#       add log/txn/lock files
16
17use strict ;
18
19my %Data =
20        (
21        0x053162 =>     # DB_BTREEMAGIC
22            {
23                          Type     => "Btree",
24                          Versions => # DB_BTREEVERSION
25                                {
26                                  1     => [0, "Unknown (older than 1.71)"],
27                                  2     => [0, "Unknown (older than 1.71)"],
28                                  3     => [0, "1.71 -> 1.85, 1.86"],
29                                  4     => [0, "Unknown"],
30                                  5     => [0, "2.0.0 -> 2.3.0"],
31                                  6     => [0, "2.3.1 -> 2.7.7"],
32                                  7     => [0, "3.0.x"],
33                                  8     => [0, "3.1.x -> 4.0.x"],
34                                  9     => [1, "4.1.x or greater"],
35                                }
36                        },
37        0x061561 =>     # DB_HASHMAGIC
38            {
39                          Type     => "Hash",
40                          Versions => # DB_HASHVERSION
41                                {
42                                  1     => [0, "Unknown (older than 1.71)"],
43                                  2     => [0, "1.71 -> 1.85"],
44                                  3     => [0, "1.86"],
45                                  4     => [0, "2.0.0 -> 2.1.0"],
46                                  5     => [0, "2.2.6 -> 2.7.7"],
47                                  6     => [0, "3.0.x"],
48                                  7     => [0, "3.1.x -> 4.0.x"],
49                                  8     => [1, "4.1.x or greater"],
50                                  9     => [1, "4.6.x or greater"],
51                                }
52                        },
53        0x042253 =>     # DB_QAMMAGIC
54            {
55                          Type     => "Queue",
56                          Versions => # DB_QAMVERSION
57                                {
58                                  1     => [0, "3.0.x"],
59                                  2     => [0, "3.1.x"],
60                                  3     => [0, "3.2.x -> 4.0.x"],
61                                  4     => [1, "4.1.x or greater"],
62                                }
63                        },
64        0x074582 => 	  # DB_HEAPMAGIC
65            {
66                          Type     => "Heap",
67                          Versions => # DB_HEAPVERSION
68                                {
69                                  1	    => [1, "5.2.x"],
70                                }
71                        },
72        ) ;
73
74die "Usage: dbinfo file\n" unless @ARGV == 1 ;
75
76print "testing file $ARGV[0]...\n\n" ;
77open (F, "<$ARGV[0]") or die "Cannot open file $ARGV[0]: $!\n" ;
78
79my $buff ;
80read F, $buff, 30 ;
81
82
83my (@info) = unpack("NNNNNNC", $buff) ;
84my (@info1) = unpack("VVVVVVC", $buff) ;
85my ($magic, $version, $endian, $encrypt) ;
86
87if ($Data{$info[0]}) # first try DB 1.x format, big endian
88{
89    $magic = $info[0] ;
90    $version = $info[1] ;
91    $endian  = "Big Endian" ;
92    $encrypt = "Not Supported";
93}
94elsif ($Data{$info1[0]}) # first try DB 1.x format, little endian
95{
96    $magic = $info1[0] ;
97    $version = $info1[1] ;
98    $endian  = "Little Endian" ;
99    $encrypt = "Not Supported";
100}
101elsif ($Data{$info[3]}) # next DB 2.x big endian
102{
103    $magic = $info[3] ;
104    $version = $info[4] ;
105    $endian  = "Big Endian" ;
106}
107elsif ($Data{$info1[3]}) # next DB 2.x little endian
108{
109    $magic = $info1[3] ;
110    $version = $info1[4] ;
111    $endian  = "Little Endian" ;
112}
113else
114  { die "not a Berkeley DB database file.\n" }
115
116my $type = $Data{$magic} ;
117$magic = sprintf "%06X", $magic ;
118
119my $ver_string = "Unknown" ;
120
121if ( defined $type->{Versions}{$version} )
122{
123     $ver_string = $type->{Versions}{$version}[1];
124     if ($type->{Versions}{$version}[0] )
125       { $encrypt = $info[6] ? "Enabled" : "Disabled" }
126     else
127       { $encrypt = "Not Supported" }
128}
129
130print <<EOM ;
131File Type:              Berkeley DB $type->{Type} file.
132File Version ID:        $version
133Built with Berkeley DB: $ver_string
134Byte Order:             $endian
135Magic:                  $magic
136Encryption:             $encrypt
137EOM
138
139close F ;
140
141exit ;
142