xref: /trunk/main/l10ntools/scripts/keyidGen.pl (revision c667dd47)
1*c667dd47SPedro Giffuni:
2cdf0e10cSrcweireval 'exec perl -S $0 ${1+"$@"}'
3cdf0e10cSrcweir    if 0;
47e90fac2SAndrew Rist#**************************************************************
57e90fac2SAndrew Rist#
67e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
77e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
87e90fac2SAndrew Rist#  distributed with this work for additional information
97e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
107e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
117e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
127e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
137e90fac2SAndrew Rist#
147e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
157e90fac2SAndrew Rist#
167e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
177e90fac2SAndrew Rist#  software distributed under the License is distributed on an
187e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
197e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
207e90fac2SAndrew Rist#  specific language governing permissions and limitations
217e90fac2SAndrew Rist#  under the License.
227e90fac2SAndrew Rist#
237e90fac2SAndrew Rist#**************************************************************
247e90fac2SAndrew Rist
257e90fac2SAndrew Rist
26cdf0e10cSrcweir#
27cdf0e10cSrcweir# add keyids to sdf file
28cdf0e10cSrcweir#
29cdf0e10cSrcweir
30cdf0e10cSrcweiruse Compress::Zlib();
31cdf0e10cSrcweir
32cdf0e10cSrcweirprint "\nkeyidGen version 1.0 \n\n";
33cdf0e10cSrcweir
34cdf0e10cSrcweirmy ( $infile,$outfile,$dbimport );
35cdf0e10cSrcweirget_options();
36cdf0e10cSrcweir
37cdf0e10cSrcweirprint_help() if ( !defined $infile || $help );
38cdf0e10cSrcweirexit 1 if ( !defined $infile );
39cdf0e10cSrcweirif ( ! defined $outfile )
40cdf0e10cSrcweir{
41cdf0e10cSrcweir    $outfile = $infile;
42cdf0e10cSrcweir    $outfile =~ s/\.sdf$//i;
43cdf0e10cSrcweir    $outfile .= "_KeyID.sdf";
44cdf0e10cSrcweir}
45cdf0e10cSrcweir
46cdf0e10cSrcweir$collisions = 0;
47cdf0e10cSrcweir%hashcodes = ();
48cdf0e10cSrcweir$count = 0;
49cdf0e10cSrcweirprint "writing to $outfile\n";
50cdf0e10cSrcweiropen INFILE,"<$infile" || die "could not open $infile $! $^E\n";
51cdf0e10cSrcweiropen OUTFILE,">$outfile" || die "could not open $outfile $! $^E\n";
52cdf0e10cSrcweir
53cdf0e10cSrcweirwhile ( <INFILE> )
54cdf0e10cSrcweir{
55cdf0e10cSrcweir    $line = $_;
56cdf0e10cSrcweir    chomp $line;
57cdf0e10cSrcweir    $hash = 0;
58cdf0e10cSrcweir    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]*)$/ )
59cdf0e10cSrcweir    {
60cdf0e10cSrcweir        $string="$1 $2 $4 $5 $6 $7 $8";
61cdf0e10cSrcweir        $hashp = makeID( $string );
62cdf0e10cSrcweir
63cdf0e10cSrcweir        if ( defined ($hashcodes{ $hashp } ) )
64cdf0e10cSrcweir        {
65cdf0e10cSrcweir            $collisions ++ unless $hashcodes{ $hashp } eq $string;
66cdf0e10cSrcweir        }
67cdf0e10cSrcweir        $hashcodes{ $hashp } = $string;
68cdf0e10cSrcweir        $count++;
69cdf0e10cSrcweir        if ( $dbimport )
70cdf0e10cSrcweir        {
71cdf0e10cSrcweir            my ( $pre, $post, $old );
72cdf0e10cSrcweir            $pre = "$1\t$2\t";
73cdf0e10cSrcweir            $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";
74cdf0e10cSrcweir            $old = $3;
75cdf0e10cSrcweir            $old =~ s/;{0,1}keyid:......;{0,1}//;
76cdf0e10cSrcweir            $old =~ s/^0$//;
77cdf0e10cSrcweir            if ( $old ne "" ) { $old .= ";"; }
78cdf0e10cSrcweir            print OUTFILE "$pre${old}keyid:$hashp$post";
79cdf0e10cSrcweir        }
80cdf0e10cSrcweir        else
81cdf0e10cSrcweir        {
82cdf0e10cSrcweir            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";
83cdf0e10cSrcweir        }
84cdf0e10cSrcweir    }
85cdf0e10cSrcweir}
86cdf0e10cSrcweirprint "$count entries\n";
87cdf0e10cSrcweirprint "$collisions collisions\n";
88cdf0e10cSrcweir
89cdf0e10cSrcweirclose INFILE;
90cdf0e10cSrcweirclose OUTFILE;
91cdf0e10cSrcweir
92cdf0e10cSrcweirsub makeID
93cdf0e10cSrcweir{
94cdf0e10cSrcweir	my ( $String ) = shift;
95cdf0e10cSrcweir	my ( $hash );
96cdf0e10cSrcweir	# hardcoded to prevent windows installer to choke on bad directoryname :-((
97cdf0e10cSrcweir	if ( $String eq "scp2 source\\ooo\\directory_ooo.ulf LngText STR_DIR_KAPITEL   " )
98cdf0e10cSrcweir	{
99cdf0e10cSrcweir		return "keyid1";
100cdf0e10cSrcweir	}
101cdf0e10cSrcweir
102cdf0e10cSrcweir    $hash = Compress::Zlib::crc32( $String, undef );
103cdf0e10cSrcweir    return makenumber( $hash );
104cdf0e10cSrcweir}
105cdf0e10cSrcweir
106cdf0e10cSrcweirsub makenumber
107cdf0e10cSrcweir{
108cdf0e10cSrcweir    $h = shift;
109cdf0e10cSrcweir    #                  1         2         3         4
110cdf0e10cSrcweir    #         1234567890123456789012345678901234567890
111cdf0e10cSrcweir    $symbols="0123456789abcdefghijklmnopqrstuvwxyz+-[=]";
112cdf0e10cSrcweir    $order = length($symbols);
113cdf0e10cSrcweir    $result = "";
114cdf0e10cSrcweir    while ( length( $result ) < 6 )
115cdf0e10cSrcweir    {
116cdf0e10cSrcweir        $result .= substr( $symbols, ($h % $order), 1 );
117cdf0e10cSrcweir        $h = int( $h / $order );
118cdf0e10cSrcweir    }
119cdf0e10cSrcweir    die "makenumber failed because number is too big (this cannot be so this is a strange error)" if $h > 0;
120cdf0e10cSrcweir
121cdf0e10cSrcweir    return reverse $result;
122cdf0e10cSrcweir}
123cdf0e10cSrcweir
124cdf0e10cSrcweir
125cdf0e10cSrcweirsub makekidstr
126cdf0e10cSrcweir{
127cdf0e10cSrcweir    $kid = shift;
128cdf0e10cSrcweir    $str = shift;
129cdf0e10cSrcweir
130cdf0e10cSrcweir    if ( $str ne "" )
131cdf0e10cSrcweir    {
132cdf0e10cSrcweir        # special handling for strings starting with font descriptions like {&Tahoma8} (win system integration)
133cdf0e10cSrcweir        if ( $str =~ s/^(\{\&[^\}]+\})// )
134cdf0e10cSrcweir        {
135cdf0e10cSrcweir            return "$1$kid‖$str";
136cdf0e10cSrcweir        }
137cdf0e10cSrcweir        else
138cdf0e10cSrcweir        {
139cdf0e10cSrcweir            return "$kid‖$str";
140cdf0e10cSrcweir        }
141cdf0e10cSrcweir    }
142cdf0e10cSrcweir    else
143cdf0e10cSrcweir    {
144cdf0e10cSrcweir        return "";
145cdf0e10cSrcweir    }
146cdf0e10cSrcweir#    return "default";
147cdf0e10cSrcweir}
148cdf0e10cSrcweir
149cdf0e10cSrcweirsub print_help
150cdf0e10cSrcweir{
151cdf0e10cSrcweir    print "\n\n";
152cdf0e10cSrcweir    print "keyidGen 0.5 for sdf files\n";
153cdf0e10cSrcweir    print "--------------------------\n";
154cdf0e10cSrcweir    print "Usage:\n";
155cdf0e10cSrcweir    print "keyidGen <infile> [<outfile>] [-dbimport]\n";
156cdf0e10cSrcweir    print "                   add keyids to the entries and write them to a file with\n";
157cdf0e10cSrcweir    print "                   _KeyID added to the name\n";
158cdf0e10cSrcweir    print "   -dbimport       Add KeyID to a new column instead of to the strings.\n";
159cdf0e10cSrcweir    print "                   This is needed to import the IDs into tha database.\n";
160cdf0e10cSrcweir    print "\n\n";
161cdf0e10cSrcweir}
162cdf0e10cSrcweir
163cdf0e10cSrcweir
164cdf0e10cSrcweirsub get_options {
165cdf0e10cSrcweir	my ($arg,$has_infile);
166cdf0e10cSrcweir
167cdf0e10cSrcweir	while ($arg = shift @ARGV) {
168cdf0e10cSrcweir		$arg =~ /^-dbimport$/  and $dbimport = 1 and next;
169cdf0e10cSrcweir		$arg =~ /^-help$/  and $help = 1 and next; #show help
170cdf0e10cSrcweir
171cdf0e10cSrcweir		if ( !$has_infile )
172cdf0e10cSrcweir		{
173cdf0e10cSrcweir		    $infile = $arg;
174cdf0e10cSrcweir		    $has_infile = 1;
175cdf0e10cSrcweir		}
176cdf0e10cSrcweir		else
177cdf0e10cSrcweir		{
178cdf0e10cSrcweir		    $outfile = $arg;
179cdf0e10cSrcweir		}
180cdf0e10cSrcweir	}
181cdf0e10cSrcweir}
182