1#!/usr/local/bin/perl
2
3# Name:		dbinfo -- identify berkeley DB version used to create 
4#			  a database file
5#
6# Author:	Paul Marquess  <Paul.Marquess@btinternet.com>
7# Version: 	1.06 
8# Date		27th MArch 2008
9#
10#     Copyright (c) 1998-2008 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	) ;
65
66die "Usage: dbinfo file\n" unless @ARGV == 1 ;
67
68print "testing file $ARGV[0]...\n\n" ;
69open (F, "<$ARGV[0]") or die "Cannot open file $ARGV[0]: $!\n" ;
70
71my $buff ;
72read F, $buff, 30 ;
73
74
75my (@info) = unpack("NNNNNNC", $buff) ;
76my (@info1) = unpack("VVVVVVC", $buff) ;
77my ($magic, $version, $endian, $encrypt) ;
78
79if ($Data{$info[0]}) # first try DB 1.x format, big endian
80{
81    $magic = $info[0] ;
82    $version = $info[1] ;
83    $endian  = "Big Endian" ;
84    $encrypt = "Not Supported";
85}
86elsif ($Data{$info1[0]}) # first try DB 1.x format, little endian
87{
88    $magic = $info1[0] ;
89    $version = $info1[1] ;
90    $endian  = "Little Endian" ;
91    $encrypt = "Not Supported";
92}
93elsif ($Data{$info[3]}) # next DB 2.x big endian
94{
95    $magic = $info[3] ;
96    $version = $info[4] ;
97    $endian  = "Big Endian" ;
98}
99elsif ($Data{$info1[3]}) # next DB 2.x little endian
100{
101    $magic = $info1[3] ;
102    $version = $info1[4] ;
103    $endian  = "Little Endian" ;
104}
105else
106  { die "not a Berkeley DB database file.\n" }
107
108my $type = $Data{$magic} ;
109$magic = sprintf "%06X", $magic ;
110
111my $ver_string = "Unknown" ;
112
113if ( defined $type->{Versions}{$version} )
114{
115     $ver_string = $type->{Versions}{$version}[1];
116     if ($type->{Versions}{$version}[0] )
117       { $encrypt = $info[6] ? "Enabled" : "Disabled" }
118     else
119       { $encrypt = "Not Supported" }
120}
121
122print <<EOM ;
123File Type:		Berkeley DB $type->{Type} file.
124File Version ID:	$version
125Built with Berkeley DB:	$ver_string
126Byte Order:		$endian
127Magic:			$magic
128Encryption:             $encrypt
129EOM
130
131close F ;
132
133exit ;
134