1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::scppatchsoname;
25
26use installer::files;
27use installer::globals;
28use installer::logger;
29use installer::setupscript;
30use installer::systemactions;
31
32########################################################################################
33# The length of the new string must be identical with the length of the old string
34########################################################################################
35
36sub change_length_of_string
37{
38	my ($newstringref, $oldstring) = @_;
39
40	while ( length($$newstringref) < length($oldstring) )
41	{
42		$$newstringref = $$newstringref . chr(0);
43	}
44}
45
46########################################################################################
47# The length of the new string must be identical with the length of the old string
48########################################################################################
49
50sub change_length_of_string_with_letter
51{
52	my ($newstringref, $oldstring, $onestring) = @_;
53
54	while ( length($$newstringref) < length($oldstring) )
55	{
56		$$newstringref = $$newstringref . $onestring;
57	}
58}
59
60########################################################################################
61# Converting a string to a unicode string
62########################################################################################
63
64sub convert_to_unicode
65{
66	my ($string) = @_;
67
68	my $unicodestring = "";
69
70	my $stringlength = length($string);
71
72	for ( my $i = 0; $i < $stringlength; $i++ )
73	{
74		$unicodestring = $unicodestring . substr($string, $i, 1);
75		$unicodestring = $unicodestring . chr(0);
76	}
77
78	return $unicodestring;
79}
80
81########################################################################################
82# Replacing the so name in all files with flag PATCH_SO_NAME
83########################################################################################
84
85sub replace_productname_in_file
86{
87	my ($sourcepath, $destpath, $variableshashref, $onefilehash, $styles) = @_;
88
89	my $onefile = installer::files::read_binary_file($sourcepath);
90
91	# searching for "x"
92
93	my $onestring = "x" . chr(0);
94	my $replacestring = "";
95	for ( my $i = 1; $i <= 80; $i++ ) { $replacestring .= $onestring; }
96
97    $installer::logger::Lang->printf("processing PATCH_SO_NAME: %s -> %s\n", $sourcepath, $destpath);
98
99	my $productname = $variableshashref->{'PRODUCTNAME'} . " " . $variableshashref->{'PRODUCTVERSION'};
100	if ( exists($onefilehash->{'FileDescription'}) ) { $productname = $onefilehash->{'FileDescription'}; }
101	my $unicode_productname = convert_to_unicode($productname);
102
103	change_length_of_string(\$unicode_productname, $replacestring);
104
105	my $found = $onefile =~ s/$replacestring/$unicode_productname/sg;
106
107	installer::files::save_binary_file($onefile, $destpath);
108
109	return $found;
110}
111
112#########################################################
113# Analyzing files with flag PATCH_SO_NAME
114#########################################################
115
116sub resolving_patchsoname_flag
117{
118	my ($filesarrayref, $variableshashref, $item, $languagestringref) = @_;
119
120	my $diritem = lc($item);
121
122	my $replacedirbase = installer::systemactions::create_directories("patchsoname_$diritem", $languagestringref);
123
124	installer::logger::include_header_into_logfile("$item with flag PATCH_SO_NAME:");
125
126	for ( my $i = 0; $i <= $#{$filesarrayref}; $i++ )
127	{
128		my $onefile = ${$filesarrayref}[$i];
129		my $styles = "";
130
131		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
132
133		if ( $styles =~ /\bPATCH_SO_NAME\b/ )
134		{
135			# Language specific subdirectory
136
137			my $onelanguage = $onefile->{'specificlanguage'};
138            my $filedescription = "";
139
140			if ($onelanguage eq "")
141			{
142				$onelanguage = "00";	# files without language into directory "00"
143			}
144
145			my $replacedir = $replacedirbase . $installer::globals::separator . $onelanguage . $installer::globals::separator;
146			installer::systemactions::create_directory($replacedir);	# creating language specific directories
147
148			# copy files and edit them with the variables defined in the zip.lst
149
150			my $onefilename = $onefile->{'Name'};
151			my $sourcepath = $onefile->{'sourcepath'};
152			my $destinationpath = $replacedir . $onefilename;
153			my $movepath = $destinationpath . ".orig";
154
155			# if (!(-f $destinationpath))	# do nothing if the file already exists
156			# {
157
158            $installer::logger::Lang->printf("PATCH_SO_NAME: copying '%s' to '%s'\n", $sourcepath, $movepath);
159			my $copysuccess = installer::systemactions::copy_one_file($sourcepath, $movepath);
160
161			if ( $copysuccess )
162			{
163				# Now the file can be patch (binary!)
164				my $found = replace_productname_in_file($movepath, $destinationpath, $variableshashref, $onefile, $styles);
165
166				if ($found == 0)
167				{
168					$installer::logger::Lang->printf("Did not patch the file %s\n", $destinationpath);
169				}
170				else
171				{
172					$installer::logger::Lang->printf("Successfully patched %s, Count: %s\n",
173                        $destinationpath, $found);
174				}
175			}
176
177			# }
178
179			# Saving the original source, where the file was found
180			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
181
182			# Saving the original source, where the file was found
183			$onefile->{'originalsourcepath'} = $onefile->{'sourcepath'};
184
185			# Writing the new sourcepath into the hashref, even if it was no copied
186
187			$onefile->{'sourcepath'} = $destinationpath;
188		}
189	}
190
191	$installer::logger::Lang->print("\n");
192}
193
1941;
195