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