wininet: Load persistent cookies from other paths in get_cookie function.
[wine] / tools / winapi / winapi_local.pm
1 #
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 #
18
19 package winapi_local;
20
21 use strict;
22
23 use nativeapi qw($nativeapi);
24 use options qw($options);
25 use output qw($output);
26 use winapi qw($win16api $win32api @winapis);
27
28 sub _check_function($$$$$$) {
29     my $return_type = shift;
30     my $calling_convention = shift;
31     my $external_name = shift;
32     my $internal_name = shift;
33     my $refargument_types = shift;
34     my @argument_types = @$refargument_types;
35     my $winapi = shift;
36
37     my $module = $winapi->function_internal_module($internal_name);
38
39     if($winapi->name eq "win16") {
40         if($winapi->is_function_stub_in_module($module, $internal_name)) {
41             if($options->implemented) {
42                 $output->write("function implemented but declared as stub in .spec file\n");
43             }
44             return;
45         } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
46             if($options->implemented_win32) {
47                 $output->write("32-bit variant of function implemented but declared as stub in .spec file\n");
48             }
49             return;
50         }
51     } elsif($winapi->is_function_stub_in_module($module, $internal_name)) {
52         if($options->implemented) {
53             $output->write("function implemented but declared as stub in .spec file\n");
54         }
55         return;
56     }
57
58     my $forbidden_return_type = 0;
59     my $implemented_return_kind;
60     $winapi->type_used_in_module($return_type,$module);
61     if(!defined($implemented_return_kind = $winapi->translate_argument($return_type))) {
62         $winapi->declare_argument($return_type, "unknown");
63         if($return_type ne "") {
64             $output->write("no win*.api translation defined: " . $return_type . "\n");
65         }
66     } elsif(!$winapi->is_allowed_kind($implemented_return_kind) ||
67             !$winapi->is_allowed_type_in_module($return_type, $module))
68     {
69         $forbidden_return_type = 1;
70         $winapi->allow_kind($implemented_return_kind);
71         $winapi->allow_type_in_module($return_type, $module);
72         if($options->report_argument_forbidden($return_type)) {
73             $output->write("return type is forbidden: $return_type ($implemented_return_kind)\n");
74         }
75     }
76
77     my $segmented = 0;
78     if(defined($implemented_return_kind) && $implemented_return_kind =~ /^seg[sp]tr$/) {
79         $segmented = 1;
80     }
81
82     my $implemented_calling_convention;
83     if($winapi->name eq "win16") {
84         if($calling_convention eq "__cdecl") {
85             $implemented_calling_convention = "cdecl";
86         } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
87             $implemented_calling_convention = "varargs";
88         } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|NET_API_FUNCTION|RPC_ENTRY|VFWAPI|WINAPI|CALLBACK)$/) {
89             if(defined($implemented_return_kind) && $implemented_return_kind =~ /^(?:s_word|word|void)$/) {
90                 $implemented_calling_convention = "pascal16";
91             } else {
92                 $implemented_calling_convention = "pascal";
93             }
94         } elsif($calling_convention eq "__asm") {
95             $implemented_calling_convention = "asm";
96         } else {
97             $implemented_calling_convention = "cdecl";
98         }
99     } elsif($winapi->name eq "win32") {
100         if($calling_convention eq "__cdecl") {
101             $implemented_calling_convention = "cdecl";
102         } elsif($calling_convention =~ /^(?:VFWAPIV|WINAPIV)$/) {
103             $implemented_calling_convention = "varargs";
104         } elsif($calling_convention =~ /^(?:__stdcall|__RPC_STUB|__RPC_USER|APIENTRY|NET_API_FUNCTION|RPC_ENTRY|VFWAPI|WINAPI|CALLBACK)$/) {
105             if(defined($implemented_return_kind) && $implemented_return_kind eq "longlong") {
106                 $implemented_calling_convention = "stdcall"; # FIXME: Check entry flags
107             } else {
108                 $implemented_calling_convention = "stdcall";
109             }
110         } elsif($calling_convention eq "__asm") {
111             $implemented_calling_convention = "asm";
112         } else {
113             $implemented_calling_convention = "cdecl";
114         }
115     }
116
117     my $declared_calling_convention = $winapi->function_internal_calling_convention($internal_name) || "";
118     my @declared_argument_kinds = split(/\s+/, $winapi->function_internal_arguments($internal_name));
119
120     my $declared_register = ($declared_calling_convention =~ / -register\b/);
121     my $declared_i386 = ($declared_calling_convention =~ /(?:^pascal| -i386)\b/);
122     $declared_calling_convention =~ s/ .*$//;
123
124     if(!$declared_register &&
125        $implemented_calling_convention ne $declared_calling_convention &&
126        $implemented_calling_convention ne "asm" &&
127        !($declared_calling_convention =~ /^pascal/ && $forbidden_return_type) &&
128        !($implemented_calling_convention =~ /^(?:cdecl|varargs)$/ && $declared_calling_convention =~ /^(?:cdecl|varargs)$/))
129     {
130         if($options->calling_convention && (
131             ($options->calling_convention_win16 && $winapi->name eq "win16") ||
132             ($options->calling_convention_win32 && $winapi->name eq "win32")) &&
133             !$nativeapi->is_function($internal_name))
134         {
135             $output->write("calling convention mismatch: $implemented_calling_convention != $declared_calling_convention\n");
136         }
137     }
138
139     if($declared_calling_convention eq "varargs") {
140         if ($#argument_types != -1 &&
141             (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
142             ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
143         {
144             pop @argument_types;
145         } else {
146             $output->write("function not implemented as varargs\n");
147         }
148     } elsif ($#argument_types != -1 &&
149         (($winapi->name eq "win32" && $argument_types[$#argument_types] eq "...") ||
150         ($winapi->name eq "win16" && $argument_types[$#argument_types] eq "VA_LIST16")))
151     {
152         if($#argument_types == 0) {
153             pop @argument_types;
154         } else {
155             $output->write("function not declared as varargs\n");
156         }
157     }
158
159     if($internal_name =~ /^(?:NTDLL__ftol|NTDLL__CIpow)$/) { # FIXME: Kludge
160         # ignore
161     } else {
162         my $n = 0;
163         my @argument_kinds = map {
164             my $type = $_;
165             my $kind = "unknown";
166             $winapi->type_used_in_module($type,$module);
167             if($type eq "CONTEXT *") {
168                 $kind = "context";
169             } elsif($type eq "CONTEXT86 *") {
170                 $kind = "context86";
171             } elsif(!defined($kind = $winapi->translate_argument($type))) {
172                 $winapi->declare_argument($type, "unknown");
173                 $output->write("no win*.api translation defined: " . $type . "\n");
174             } elsif(!$winapi->is_allowed_kind($kind) ||
175                     !$winapi->is_allowed_type_in_module($type, $module))
176             {
177                 $winapi->allow_kind($kind);
178                 $winapi->allow_type_in_module($type, $module);
179                 if($options->report_argument_forbidden($type)) {
180                     $output->write("argument " . ($n + 1) . " type is forbidden: " . $type . " (" . $kind . ")\n");
181                 }
182             }
183
184             # FIXME: Kludge
185             if(defined($kind) && $kind eq "struct16") {
186                 $n+=2;
187                 ("double", "double");
188             } elsif(defined($kind) && $kind eq "longlong") {
189                 $n+=1;
190                 "longlong";
191             } else {
192                 $n++;
193                 $kind;
194             }
195         } @argument_types;
196
197         if ($declared_register)
198         {
199             if (!$declared_i386 &&
200                 $argument_kinds[$#argument_kinds] ne "context") {
201                 $output->write("function declared as register, but CONTEXT * is not last argument\n");
202             } elsif ($declared_i386 &&
203                      $argument_kinds[$#argument_kinds] ne "context86") {
204                 $output->write("function declared as register, but CONTEXT86 * is not last argument\n");
205             }
206         }
207
208         for my $n (0..$#argument_kinds) {
209             if(!defined($argument_kinds[$n]) || !defined($declared_argument_kinds[$n])) { next; }
210
211             if($argument_kinds[$n] =~ /^seg[ps]tr$/ ||
212                $declared_argument_kinds[$n] =~ /^seg[ps]tr$/)
213             {
214                 $segmented = 1;
215             }
216
217             # FIXME: Kludge
218             if(!defined($argument_types[$n])) {
219                 $argument_types[$n] = "";
220             }
221
222             if($argument_kinds[$n] =~ /^context(?:86)?$/) {
223                 # Nothing
224             } elsif(!$winapi->is_allowed_kind($argument_kinds[$n]) ||
225                !$winapi->is_allowed_type_in_module($argument_types[$n], $module))
226             {
227                 $winapi->allow_kind($argument_kinds[$n]);
228                 $winapi->allow_type_in_module($argument_types[$n],, $module);
229                 if($options->report_argument_forbidden($argument_types[$n])) {
230                     $output->write("argument " . ($n + 1) . " type is forbidden: " .
231                                    "$argument_types[$n] ($argument_kinds[$n])\n");
232                 }
233             } elsif($argument_kinds[$n] ne $declared_argument_kinds[$n] &&
234                    !($argument_kinds[$n] eq "longlong" && $declared_argument_kinds[$n] eq "double")) {
235                 if($options->report_argument_kind($argument_kinds[$n]) ||
236                    $options->report_argument_kind($declared_argument_kinds[$n]))
237                 {
238                     $output->write("argument " . ($n + 1) . " type mismatch: " .
239                              $argument_types[$n] . " ($argument_kinds[$n]) != " .
240                              $declared_argument_kinds[$n] . "\n");
241                 }
242             }
243         }
244
245         if ($options->argument_count &&
246             $implemented_calling_convention ne "asm")
247         {
248             if ($#argument_kinds != $#declared_argument_kinds and
249                 $#argument_types != $#declared_argument_kinds) {
250                 $output->write("argument count differs: " .
251                     ($#argument_kinds + 1) . " != " .
252                     ($#declared_argument_kinds + 1) . "\n");
253             } elsif ($#argument_kinds != $#declared_argument_kinds or
254                      $#argument_types != $#declared_argument_kinds) {
255                 $output->write("argument count differs: " .
256                     ($#argument_kinds + 1) . "/" . ($#argument_types + 1) .
257                      " != " . ($#declared_argument_kinds + 1) .
258                      " (long vs. long long problem?)\n");
259             }
260         }
261
262     }
263
264     if($segmented && $options->shared_segmented && $winapi->is_shared_internal_function($internal_name)) {
265         $output->write("function using segmented pointers shared between Win16 and Win32\n");
266     }
267 }
268
269 sub check_function($) {
270     my $function = shift;
271
272     my $return_type = $function->return_type;
273     my $calling_convention = $function->calling_convention;
274     my $calling_convention16 = $function->calling_convention16;
275     my $calling_convention32 = $function->calling_convention32;
276     my $internal_name = $function->internal_name;
277     my $external_name16 = $function->external_name16;
278     my $external_name32 = $function->external_name32;
279     my $module16 = $function->module16;
280     my $module32 = $function->module32;
281     my $refargument_types = $function->argument_types;
282
283     if(!defined($refargument_types)) {
284         return;
285     }
286
287     if($options->win16 && $options->report_module($module16)) {
288         _check_function($return_type,
289                         $calling_convention, $external_name16,
290                         $internal_name, $refargument_types,
291                         $win16api);
292     }
293
294     if($options->win32 && $options->report_module($module32)) {
295         _check_function($return_type,
296                         $calling_convention, $external_name32,
297                         $internal_name, $refargument_types,
298                         $win32api);
299     }
300 }
301
302 sub _check_statements($$$) {
303     my $winapi = shift;
304     my $functions = shift;
305     my $function = shift;
306
307     my $module = $function->module;
308     my $internal_name = $function->internal_name;
309
310     my $first_debug_message = 1;
311     local $_ = $function->statements;
312     while(defined($_)) {
313         if(s/(\w+)\s*(?:\(\s*(\w+)\s*\))?\s*\(\s*((?:\"[^\"]*\"|\([^\)]*\)|[^\)])*?)\s*\)//) {
314             my $called_name = $1;
315             my $channel = $2;
316             my $called_arguments = $3;
317             if($called_name =~ /^(?:if|for|while|switch|sizeof)$/) {
318                 # Nothing
319             } elsif($called_name =~ /^(?:ERR|FIXME|MSG|TRACE|WARN)$/) {
320                 if($first_debug_message && $called_name =~ /^(?:FIXME|TRACE)$/) {
321                     $first_debug_message = 0;
322                     if($called_arguments =~ /^\"\((.*?)\)(.*?)\"\s*,\s*(.*?)$/) {
323                         my $formating = $1;
324                         my $extra = $2;
325                         my $arguments = $3;
326
327                         my $format;
328                         my $argument;
329                         my $n = 0;
330                         while($formating && ($formating =~ s/^([^,]*),?//, $format = $1, $format =~ s/^\s*(.*?)\s*$/$1/) &&
331                               $arguments && ($arguments =~ s/^([^,]*),?//, $argument = $1, $argument =~ s/^\s*(.*?)\s*$/$1/))
332                         {
333                             my $type = @{$function->argument_types}[$n];
334                             my $name = @{$function->argument_names}[$n];
335
336                             $n++;
337
338                             if(!defined($type)) { last; }
339
340                             $format =~ s/^\w+\s*[:=]?\s*//;
341                             $format =~ s/\s*\{[^\{\}]*\}$//;
342                             $format =~ s/\s*\[[^\[\]]*\]$//;
343                             $format =~ s/^\'(.*?)\'$/$1/;
344                             $format =~ s/^\\\"(.*?)\\\"$/$1/;
345
346                             if($options->debug_messages) {
347                                 if($argument !~ /$name/) {
348                                     $output->write("$called_name: argument $n is wrong ($name != '$argument')\n");
349                                 } elsif(!$winapi->is_allowed_type_format($module, $type, $format)) {
350                                     $output->write("$called_name: argument $n ($type $name) has illegal format ($format)\n");
351                                 }
352                             }
353                         }
354
355                         if($options->debug_messages) {
356                             my $count = $#{$function->argument_types} + 1;
357                             if($n != $count) {
358                                 $output->write("$called_name: argument count mismatch ($n != $count)\n");
359                             }
360                         }
361                     }
362                 }
363             } elsif($options->cross_call) {
364                 # $output->write("$internal_name: called $called_name\n");
365                 $$functions{$internal_name}->function_called($called_name);
366                 if(!defined($$functions{$called_name})) {
367                     my $called_function = 'winapi_function'->new;
368
369                     $called_function->internal_name($called_name);
370
371                     $$functions{$called_name} = $called_function;       
372                 }
373                 $$functions{$called_name}->function_called_by($internal_name);
374             }
375         } else {
376             undef $_;
377         }
378     }
379 }
380
381 sub check_statements($$) {
382     my $functions = shift;
383     my $function = shift;
384
385     my $module16 = $function->module16;
386     my $module32 = $function->module32;
387
388     if($options->win16 && $options->report_module($module16)) {
389         _check_statements($win16api, $functions, $function);
390     }
391
392     if($options->win32 && $options->report_module($module32)) {
393         _check_statements($win32api, $functions, $function);
394     }
395 }
396
397 sub check_file($$) {
398     my $file = shift;
399     my $functions = shift;
400
401     if($options->cross_call) {
402         my @names = sort(keys(%$functions));
403         for my $name (@names) {
404             my $function = $$functions{$name};
405
406             my @called_names = $function->called_function_names;
407             my @called_by_names = $function->called_by_function_names;
408             my $module = $function->module;
409
410             if($options->cross_call_win32_win16) {
411                 my $module16 = $function->module16;
412                 my $module32 = $function->module32;
413
414                 if($#called_names >= 0 && (defined($module16) || defined($module32)) ) {
415                     for my $called_name (@called_names) {
416                         my $called_function = $$functions{$called_name};
417
418                         my $called_module16 = $called_function->module16;
419                         my $called_module32 = $called_function->module32;
420                         if(defined($module32) &&
421                            defined($called_module16) && !defined($called_module32) &&
422                            $name ne $called_name)
423                         {
424                             $output->write("$file: $module: $name: illegal call to $called_name (Win32 -> Win16)\n");
425                         }
426                     }
427                 }
428             }
429
430             if($options->cross_call_unicode_ascii) {
431                 if($name =~ /(?<!A)W$/) {
432                     for my $called_name (@called_names) {
433                         if($called_name =~ /A$/) {
434                             $output->write("$file: $module: $name: illegal call to $called_name (Unicode -> ASCII)\n");
435                         }
436                     }
437                 }
438             }
439         }
440     }
441 }
442
443 1;