xref: /aoo42x/main/l10ntools/scripts/keyidGen.pl (revision cdf0e10c)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweireval 'exec perl -S $0 ${1+"$@"}'
3*cdf0e10cSrcweir    if 0;
4*cdf0e10cSrcweir#*************************************************************************
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# This file is part of OpenOffice.org.
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir#
24*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir#
29*cdf0e10cSrcweir#*************************************************************************
30*cdf0e10cSrcweir#
31*cdf0e10cSrcweir# add keyids to sdf file
32*cdf0e10cSrcweir#
33*cdf0e10cSrcweir
34*cdf0e10cSrcweiruse Compress::Zlib();
35*cdf0e10cSrcweir
36*cdf0e10cSrcweirprint "\nkeyidGen version 1.0 \n\n";
37*cdf0e10cSrcweir
38*cdf0e10cSrcweirmy ( $infile,$outfile,$dbimport );
39*cdf0e10cSrcweirget_options();
40*cdf0e10cSrcweir
41*cdf0e10cSrcweirprint_help() if ( !defined $infile || $help );
42*cdf0e10cSrcweirexit 1 if ( !defined $infile );
43*cdf0e10cSrcweirif ( ! defined $outfile )
44*cdf0e10cSrcweir{
45*cdf0e10cSrcweir    $outfile = $infile;
46*cdf0e10cSrcweir    $outfile =~ s/\.sdf$//i;
47*cdf0e10cSrcweir    $outfile .= "_KeyID.sdf";
48*cdf0e10cSrcweir}
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir$collisions = 0;
51*cdf0e10cSrcweir%hashcodes = ();
52*cdf0e10cSrcweir$count = 0;
53*cdf0e10cSrcweirprint "writing to $outfile\n";
54*cdf0e10cSrcweiropen INFILE,"<$infile" || die "could not open $infile $! $^E\n";
55*cdf0e10cSrcweiropen OUTFILE,">$outfile" || die "could not open $outfile $! $^E\n";
56*cdf0e10cSrcweir
57*cdf0e10cSrcweirwhile ( <INFILE> )
58*cdf0e10cSrcweir{
59*cdf0e10cSrcweir    $line = $_;
60*cdf0e10cSrcweir    chomp $line;
61*cdf0e10cSrcweir    $hash = 0;
62*cdf0e10cSrcweir    if ( $line =~ /^([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)\t([^\t]*)$/ )
63*cdf0e10cSrcweir    {
64*cdf0e10cSrcweir        $string="$1 $2 $4 $5 $6 $7 $8";
65*cdf0e10cSrcweir        $hashp = makeID( $string );
66*cdf0e10cSrcweir
67*cdf0e10cSrcweir        if ( defined ($hashcodes{ $hashp } ) )
68*cdf0e10cSrcweir        {
69*cdf0e10cSrcweir            $collisions ++ unless $hashcodes{ $hashp } eq $string;
70*cdf0e10cSrcweir        }
71*cdf0e10cSrcweir        $hashcodes{ $hashp } = $string;
72*cdf0e10cSrcweir        $count++;
73*cdf0e10cSrcweir        if ( $dbimport )
74*cdf0e10cSrcweir        {
75*cdf0e10cSrcweir            my ( $pre, $post, $old );
76*cdf0e10cSrcweir            $pre = "$1\t$2\t";
77*cdf0e10cSrcweir            $post = "\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t$11\t$12\t$13\t$14\t$15\n";
78*cdf0e10cSrcweir            $old = $3;
79*cdf0e10cSrcweir            $old =~ s/;{0,1}keyid:......;{0,1}//;
80*cdf0e10cSrcweir            $old =~ s/^0$//;
81*cdf0e10cSrcweir            if ( $old ne "" ) { $old .= ";"; }
82*cdf0e10cSrcweir            print OUTFILE "$pre${old}keyid:$hashp$post";
83*cdf0e10cSrcweir        }
84*cdf0e10cSrcweir        else
85*cdf0e10cSrcweir        {
86*cdf0e10cSrcweir            print OUTFILE "$1\t$2\t$3\t$4\t$5\t$6\t$7\t$8\t$9\t$10\t".makekidstr($hashp,$11)."\t".makekidstr($hashp,$12)."\t$13\t".makekidstr($hashp,$14)."\t$15\n";
87*cdf0e10cSrcweir        }
88*cdf0e10cSrcweir    }
89*cdf0e10cSrcweir}
90*cdf0e10cSrcweirprint "$count entries\n";
91*cdf0e10cSrcweirprint "$collisions collisions\n";
92*cdf0e10cSrcweir
93*cdf0e10cSrcweirclose INFILE;
94*cdf0e10cSrcweirclose OUTFILE;
95*cdf0e10cSrcweir
96*cdf0e10cSrcweirsub makeID
97*cdf0e10cSrcweir{
98*cdf0e10cSrcweir	my ( $String ) = shift;
99*cdf0e10cSrcweir	my ( $hash );
100*cdf0e10cSrcweir	# hardcoded to prevent windows installer to choke on bad directoryname :-((
101*cdf0e10cSrcweir	if ( $String eq "scp2 source\\ooo\\directory_ooo.ulf LngText STR_DIR_KAPITEL   " )
102*cdf0e10cSrcweir	{
103*cdf0e10cSrcweir		return "keyid1";
104*cdf0e10cSrcweir	}
105*cdf0e10cSrcweir
106*cdf0e10cSrcweir    $hash = Compress::Zlib::crc32( $String, undef );
107*cdf0e10cSrcweir    return makenumber( $hash );
108*cdf0e10cSrcweir}
109*cdf0e10cSrcweir
110*cdf0e10cSrcweirsub makenumber
111*cdf0e10cSrcweir{
112*cdf0e10cSrcweir    $h = shift;
113*cdf0e10cSrcweir    #                  1         2         3         4
114*cdf0e10cSrcweir    #         1234567890123456789012345678901234567890
115*cdf0e10cSrcweir    $symbols="0123456789abcdefghijklmnopqrstuvwxyz+-[=]";
116*cdf0e10cSrcweir    $order = length($symbols);
117*cdf0e10cSrcweir    $result = "";
118*cdf0e10cSrcweir    while ( length( $result ) < 6 )
119*cdf0e10cSrcweir    {
120*cdf0e10cSrcweir        $result .= substr( $symbols, ($h % $order), 1 );
121*cdf0e10cSrcweir        $h = int( $h / $order );
122*cdf0e10cSrcweir    }
123*cdf0e10cSrcweir    die "makenumber failed because number is too big (this cannot be so this is a strange error)" if $h > 0;
124*cdf0e10cSrcweir
125*cdf0e10cSrcweir    return reverse $result;
126*cdf0e10cSrcweir}
127*cdf0e10cSrcweir
128*cdf0e10cSrcweir
129*cdf0e10cSrcweirsub makekidstr
130*cdf0e10cSrcweir{
131*cdf0e10cSrcweir    $kid = shift;
132*cdf0e10cSrcweir    $str = shift;
133*cdf0e10cSrcweir
134*cdf0e10cSrcweir    if ( $str ne "" )
135*cdf0e10cSrcweir    {
136*cdf0e10cSrcweir        # special handling for strings starting with font descriptions like {&Tahoma8} (win system integration)
137*cdf0e10cSrcweir        if ( $str =~ s/^(\{\&[^\}]+\})// )
138*cdf0e10cSrcweir        {
139*cdf0e10cSrcweir            return "$1$kid‖$str";
140*cdf0e10cSrcweir        }
141*cdf0e10cSrcweir        else
142*cdf0e10cSrcweir        {
143*cdf0e10cSrcweir            return "$kid‖$str";
144*cdf0e10cSrcweir        }
145*cdf0e10cSrcweir    }
146*cdf0e10cSrcweir    else
147*cdf0e10cSrcweir    {
148*cdf0e10cSrcweir        return "";
149*cdf0e10cSrcweir    }
150*cdf0e10cSrcweir#    return "default";
151*cdf0e10cSrcweir}
152*cdf0e10cSrcweir
153*cdf0e10cSrcweirsub print_help
154*cdf0e10cSrcweir{
155*cdf0e10cSrcweir    print "\n\n";
156*cdf0e10cSrcweir    print "keyidGen 0.5 for sdf files\n";
157*cdf0e10cSrcweir    print "--------------------------\n";
158*cdf0e10cSrcweir    print "Usage:\n";
159*cdf0e10cSrcweir    print "keyidGen <infile> [<outfile>] [-dbimport]\n";
160*cdf0e10cSrcweir    print "                   add keyids to the entries and write them to a file with\n";
161*cdf0e10cSrcweir    print "                   _KeyID added to the name\n";
162*cdf0e10cSrcweir    print "   -dbimport       Add KeyID to a new column instead of to the strings.\n";
163*cdf0e10cSrcweir    print "                   This is needed to import the IDs into tha database.\n";
164*cdf0e10cSrcweir    print "\n\n";
165*cdf0e10cSrcweir}
166*cdf0e10cSrcweir
167*cdf0e10cSrcweir
168*cdf0e10cSrcweirsub get_options {
169*cdf0e10cSrcweir	my ($arg,$has_infile);
170*cdf0e10cSrcweir
171*cdf0e10cSrcweir	while ($arg = shift @ARGV) {
172*cdf0e10cSrcweir		$arg =~ /^-dbimport$/  and $dbimport = 1 and next;
173*cdf0e10cSrcweir		$arg =~ /^-help$/  and $help = 1 and next; #show help
174*cdf0e10cSrcweir
175*cdf0e10cSrcweir		if ( !$has_infile )
176*cdf0e10cSrcweir		{
177*cdf0e10cSrcweir		    $infile = $arg;
178*cdf0e10cSrcweir		    $has_infile = 1;
179*cdf0e10cSrcweir		}
180*cdf0e10cSrcweir		else
181*cdf0e10cSrcweir		{
182*cdf0e10cSrcweir		    $outfile = $arg;
183*cdf0e10cSrcweir		}
184*cdf0e10cSrcweir	}
185*cdf0e10cSrcweir}
186