blob: 1e00be834f40c347ad24f14cf9dd8d1e1a4422e4 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001#!/usr/bin/env perl
2#***************************************************************************
3# _ _ ____ _
4# Project ___| | | | _ \| |
5# / __| | | | |_) | |
6# | (__| |_| | _ <| |___
7# \___|\___/|_| \_\_____|
8#
9# Copyright (C) 2017 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22# SPDX-License-Identifier: curl
23#
24###########################################################################
25
26# Usage:
27# perl mk-lib1521.pl < ../../include/curl/curl.h > lib1521.c
28
29# minimum and maximum long signed values
30my $minlong = "LONG_MIN";
31my $maxlong = "LONG_MAX";
32# maximum long unsigned value
33my $maxulong = "ULONG_MAX";
34
35print <<HEADER
36/***************************************************************************
37 * _ _ ____ _
38 * Project ___| | | | _ \\| |
39 * / __| | | | |_) | |
40 * | (__| |_| | _ <| |___
41 * \\___|\\___/|_| \\_\\_____|
42 *
43 * Copyright (C) 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
44 *
45 * This software is licensed as described in the file COPYING, which
46 * you should have received as part of this distribution. The terms
47 * are also available at https://curl.se/docs/copyright.html.
48 *
49 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
50 * copies of the Software, and permit persons to whom the Software is
51 * furnished to do so, under the terms of the COPYING file.
52 *
53 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
54 * KIND, either express or implied.
55 *
56 * SPDX-License-Identifier: curl
57 *
58 ***************************************************************************/
59#include "test.h"
60#include "memdebug.h"
61#include <limits.h>
62
63/* This source code is generated by mk-lib1521.pl ! */
64
65struct data {
66 char *blaha;
67};
68
69#define LO $minlong
70#define HI $maxlong
71#define OFF_LO (curl_off_t) LO
72#define OFF_HI (curl_off_t) $maxulong
73#define OFF_NO (curl_off_t) 0
74
75/* Unexpected error.
76 CURLE_NOT_BUILT_IN - means disabled at build
77 CURLE_UNKNOWN_OPTION - means no such option (anymore?)
78 CURLE_SSL_ENGINE_NOTFOUND - set unknown ssl engine
79 CURLE_UNSUPPORTED_PROTOCOL - set bad HTTP version
80 CURLE_BAD_FUNCTION_ARGUMENT - unsupported value
81 */
82#define UNEX(x) ((x) && \\
83 ((x) != CURLE_NOT_BUILT_IN) && \\
84 ((x) != CURLE_UNKNOWN_OPTION) && \\
85 ((x) != CURLE_SSL_ENGINE_NOTFOUND) && \\
86 ((x) != CURLE_UNSUPPORTED_PROTOCOL) && \\
87 ((x) != CURLE_BAD_FUNCTION_ARGUMENT) )
88
89static size_t writecb(char *buffer, size_t size, size_t nitems,
90 void *outstream)
91{
92 (void)buffer;
93 (void)size;
94 (void)nitems;
95 (void)outstream;
96 return 0;
97}
98
99static size_t readcb(char *buffer,
100 size_t size,
101 size_t nitems,
102 void *instream)
103{
104 (void)buffer;
105 (void)size;
106 (void)nitems;
107 (void)instream;
108 return 0;
109}
110
111static int err(const char *name, CURLcode val, int lineno)
112{
113 printf("CURLOPT_%s returned %d, \\"%s\\" on line %d\\n",
114 name, val, curl_easy_strerror(val), lineno);
115 return (int)val;
116}
117
118static int geterr(const char *name, CURLcode val, int lineno)
119{
120 printf("CURLINFO_%s returned %d, \\"%s\\" on line %d\\n",
121 name, val, curl_easy_strerror(val), lineno);
122 return (int)val;
123}
124
125static curl_progress_callback progresscb;
126static curl_write_callback headercb;
127static curl_debug_callback debugcb;
128static curl_trailer_callback trailercb;
129static curl_ssl_ctx_callback ssl_ctx_cb;
130static curl_ioctl_callback ioctlcb;
131static curl_sockopt_callback sockoptcb;
132static curl_opensocket_callback opensocketcb;
133static curl_seek_callback seekcb;
134static curl_sshkeycallback ssh_keycb;
135static curl_sshhostkeycallback ssh_hostkeycb;
136static curl_chunk_bgn_callback chunk_bgn_cb;
137static curl_chunk_end_callback chunk_end_cb;
138static curl_fnmatch_callback fnmatch_cb;
139static curl_closesocket_callback closesocketcb;
140static curl_xferinfo_callback xferinfocb;
141static curl_hstsread_callback hstsreadcb;
142static curl_hstswrite_callback hstswritecb;
143static curl_resolver_start_callback resolver_start_cb;
144static curl_prereq_callback prereqcb;
145
146int test(char *URL)
147{
148 CURL *curl = NULL;
149 CURL *dep = NULL;
150 CURLSH *share = NULL;
151 char errorbuffer[CURL_ERROR_SIZE];
152 void *conv_from_network_cb = NULL;
153 void *conv_to_network_cb = NULL;
154 void *conv_from_utf8_cb = NULL;
155 void *interleavecb = NULL;
156 char *stringpointerextra = (char *)"moooo";
157 struct curl_slist *slist = NULL;
158 struct curl_httppost *httppost = NULL;
159 curl_mime *mimepost = NULL;
160 FILE *stream = stderr;
161 struct data object;
162 char *charp;
163 long val;
164 curl_off_t oval;
165 double dval;
166 curl_socket_t sockfd;
167 struct curl_certinfo *certinfo;
168 struct curl_tlssessioninfo *tlssession;
169 struct curl_blob blob = { (void *)"silly", 5, 0};
170 CURLcode res = CURLE_OK;
171 (void)URL; /* not used */
172 global_init(CURL_GLOBAL_ALL);
173 easy_init(dep);
174 easy_init(curl);
175 share = curl_share_init();
176 if(!share) {
177 res = CURLE_OUT_OF_MEMORY;
178 goto test_cleanup;
179 }
180
181HEADER
182 ;
183
184while(<STDIN>) {
185 if($_ =~ /^ CURLOPT\(([^ ]*), ([^ ]*), (\d*)\)/) {
186 my ($name, $type, $val)=($1, $2, $3);
187 my $w=" ";
188 my $pref = "${w}res = curl_easy_setopt(curl, $name,";
189 my $i = ' ' x (length($w) + 23);
190 my $check = " if(UNEX(res)) {\n err(\"$name\", res, __LINE__);\n goto test_cleanup;\n }\n";
191 if($type eq "CURLOPTTYPE_STRINGPOINT") {
192 print "${pref} \"string\");\n$check";
193 print "${pref} NULL);\n$check";
194 }
195 elsif(($type eq "CURLOPTTYPE_LONG") ||
196 ($type eq "CURLOPTTYPE_VALUES")) {
197 print "${pref} 0L);\n$check";
198 print "${pref} 22L);\n$check";
199 print "${pref} LO);\n$check";
200 print "${pref} HI);\n$check";
201 }
202 elsif(($type eq "CURLOPTTYPE_OBJECTPOINT") ||
203 ($type eq "CURLOPTTYPE_CBPOINT")) {
204 if($name =~ /DEPENDS/) {
205 print "${pref} dep);\n$check";
206 }
207 elsif($name =~ "SHARE") {
208 print "${pref} share);\n$check";
209 }
210 elsif($name eq "CURLOPT_ERRORBUFFER") {
211 print "${pref} errorbuffer);\n$check";
212 }
213 elsif(($name eq "CURLOPT_POSTFIELDS") ||
214 ($name eq "CURLOPT_COPYPOSTFIELDS")) {
215 # set size to zero to avoid it being "illegal"
216 print " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
217 print "${pref} stringpointerextra);\n$check";
218 }
219 elsif($name eq "CURLOPT_HTTPPOST") {
220 print "${pref} httppost);\n$check";
221 }
222 elsif($name eq "CURLOPT_MIMEPOST") {
223 print "${pref} mimepost);\n$check";
224 }
225 elsif($name eq "CURLOPT_STDERR") {
226 print "${pref} stream);\n$check";
227 }
228 else {
229 print "${pref} &object);\n$check";
230 }
231 print "${pref} NULL);\n$check";
232 }
233 elsif($type eq "CURLOPTTYPE_SLISTPOINT") {
234 print "${pref} slist);\n$check";
235 }
236 elsif($type eq "CURLOPTTYPE_FUNCTIONPOINT") {
237 if($name =~ /([^ ]*)FUNCTION/) {
238 my $l=lc($1);
239 $l =~ s/^curlopt_//;
240 print "${pref}\n$i${l}cb);\n$check";
241 }
242 else {
243 print "${pref} &func);\n$check";
244 }
245 print "${pref} NULL);\n$check";
246 }
247 elsif($type eq "CURLOPTTYPE_OFF_T") {
248 # play conservative to work with 32bit curl_off_t
249 print "${pref} OFF_NO);\n$check";
250 print "${pref} OFF_HI);\n$check";
251 print "${pref} OFF_LO);\n$check";
252 }
253 elsif($type eq "CURLOPTTYPE_BLOB") {
254 print "${pref} &blob);\n$check";
255 }
256 else {
257 print STDERR "\nUnknown type: $type\n";
258 exit 22; # exit to make this noticed!
259 }
260 }
261 elsif($_ =~ /^ CURLINFO_NONE/) {
262 $infomode = 1;
263 }
264 elsif($infomode &&
265 ($_ =~ /^ CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
266 my ($info, $type)=($1, $2);
267 my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
268 my $check = " if(UNEX(res)) {\n geterr(\"$info\", res, __LINE__);\n goto test_cleanup;\n }\n";
269 if($type eq "STRING") {
270 print "$c &charp);\n$check";
271 }
272 elsif($type eq "LONG") {
273 print "$c &val);\n$check";
274 }
275 elsif($type eq "OFF_T") {
276 print "$c &oval);\n$check";
277 }
278 elsif($type eq "DOUBLE") {
279 print "$c &dval);\n$check";
280 }
281 elsif($type eq "SLIST") {
282 print "$c &slist);\n$check";
283 print " if(slist)\n curl_slist_free_all(slist);\n";
284 }
285 elsif($type eq "SOCKET") {
286 print "$c &sockfd);\n$check";
287 }
288 elsif($type eq "PTR") {
289 if($info eq "CERTINFO") {
290 print "$c &certinfo);\n$check";
291 }
292 elsif(($info eq "TLS_SESSION") ||
293 ($info eq "TLS_SSL_PTR")) {
294 print "$c &tlssession);\n$check";
295 }
296 else {
297 print STDERR "$info/$type is unsupported\n";
298 }
299 }
300 else {
301 print STDERR "$type is unsupported\n";
302 }
303 }
304}
305
306
307print <<FOOTER
308 curl_easy_setopt(curl, (CURLoption)1, 0);
309 res = CURLE_OK;
310test_cleanup:
311 curl_easy_cleanup(curl);
312 curl_easy_cleanup(dep);
313 curl_share_cleanup(share);
314 curl_global_cleanup();
315
316 return (int)res;
317}
318FOOTER
319 ;