#!/usr/local/bin/perl

# pcmwdb - PalmCrack Make Wordlist Database
#
# 3/25/99 modified 11/22/99
# 
# By: Steve Lodin for Noncon, Inc.
#
# Little tool to create the wordlist database from a text file of the
# form of /usr/dict/words (one word per line)
#

$debug = 0;
$maxlen = 8;	# for UNIX password checking
# $maxlen = 14	# for NT and UNIX password checking

open(FILE, $ARGV[0]) || die "usage:  wordlist\n";

# read in wordlist into array, chopping each word at maxlen
while (<FILE>) {
	chop();
	++$nwords;
	push(@WORD, substr($_, 0, $maxlen));
}

if ($debug) { print "Number of words = $nwords\n"; }

open(DB,">pc.pdb") || die "can't open pc.pdb file\n";

# HEADER Field Name    bytes   Type
# ------------------   -----   -------
# name                 32      string
# file attrib           2      numeric
# version               2      numeric
# creation date         4      numeric
# modification date     4      numeric
# last backup date      4      numeric
# modification number   4      numeric
# AppInfoArea           4      numeric
# SortInfoArea          4      numeric
# database type         4      string
# creator ID            4      string
# unique ID seed        4      numeric
# NextRecordList ID     4      numeric
# number of records     2      numeric

# HEADER name
printf(DB "PalmCrackDB");
for ($i=length("PalmCrackDB"); $i<32; $i++) { printf(DB "\0"); }

# HEADER file attrib, version, creation date, modification date,
# HEADER last backup date, modification number
printf(DB "\0\0\0\0%c%c%c%c%c%c%c%c%c%c%c%c\0\0\0\0",0xb1,0xdd,0x35,0xc2,0xb1,0xdf,0x70,0x27,0xff,0xff,0xf1,0xf0);

# HEADER AppInfoArea, SortInfoArea, database type, creator ID, unique ID seed,
# HEADER NextRecordList ID
printf(DB "\0\0\0\0\0\0\0\0%c%c%c%c%c%c%c%c\0\0\0\0\0\0\0\0",0x44,0x61,0x74,0x61,0x43,0x72,0x63,0x6b);

# HEADER number of records
printf(DB "%c%c", $nwords/256, $nwords%256);

# offset is the header size plus the number of records times the record
# header size
$offset = 78 + ($nwords * 8);

# RECORD list Field Name  bytes  Type
# ----------------------  -----  -------
# record data offset      4      numeric
# record attribtutes      1      numeric
# uniqueID                3      numeric

foreach $word (@WORD) {
	printf(DB "\0%c%c%c\0\0\0\0", ($offset > 65536) ? $offset/65536 : 0, ($offset > 65536) ? ($offset - 65536)/256 : $offset/256, $offset%256);
	$offset += length($word)+1; # length plus null terminator
}

# DATA Field
# print each word with a null terminator
foreach $word (@WORD) {
	printf(DB "%s\0", $word);
}

close(DB);
exit(0);
