mk-lib1521.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. # Usage:
  26. # perl mk-lib1521.pl < ../../include/curl/curl.h > lib1521.c
  27. # minimum and maximum long signed values
  28. my $minlong = "LONG_MIN";
  29. my $maxlong = "LONG_MAX";
  30. # maximum curl_off_t
  31. my $maxofft = "CURL_OFF_T_MAX";
  32. my $line = "";
  33. my $incomment = 0;
  34. # Options allowed to return CURLE_BAD_FUNCTION_ARGUMENT if given a string they
  35. # do not recognize as valid
  36. my @bad_function_argument = (
  37. 'CURLOPT_DNS_LOCAL_IP4',
  38. 'CURLOPT_DNS_LOCAL_IP6',
  39. 'CURLOPT_DNS_SERVERS',
  40. 'CURLOPT_PROXY_TLSAUTH_TYPE',
  41. 'CURLOPT_SSLENGINE',
  42. 'CURLOPT_TLSAUTH_TYPE',
  43. );
  44. # Options allowed to return CURLE_UNSUPPORTED_PROTOCOL if given a string they
  45. # do not recognize as valid
  46. my @unsupported_protocol = (
  47. 'CURLOPT_PROTOCOLS_STR',
  48. 'CURLOPT_REDIR_PROTOCOLS_STR',
  49. );
  50. # Options allowed to return CURLE_SSL_ENGINE_NOTFOUND if given a string they
  51. # do not recognize as valid
  52. my @ssl_engine_notfound = (
  53. 'CURLOPT_SSLENGINE',
  54. );
  55. # Options allowed to return CURLE_UNSUPPORTED_PROTOCOL if given a bad
  56. # numerical input they do not recognize as valid
  57. my @unsupported_protocol_num = (
  58. 'CURLOPT_HTTP_VERSION',
  59. );
  60. # Options allowed to return CURLE_NOT_BUILT_IN if given a bad
  61. # numerical input they do not recognize as valid
  62. my @not_built_in_num = (
  63. 'CURLOPT_HTTPAUTH',
  64. 'CURLOPT_PROXYAUTH',
  65. 'CURLOPT_SOCKS5_AUTH',
  66. );
  67. #
  68. # Generate a set of string checks
  69. #
  70. my $allowedstringerrors = <<MOO
  71. switch(code) {
  72. case CURLE_BAD_FUNCTION_ARGUMENT:
  73. MOO
  74. ;
  75. for my $o (@bad_function_argument) {
  76. $allowedstringerrors .= <<MOO
  77. if(!strcmp("$o", name))
  78. return;
  79. MOO
  80. ;
  81. }
  82. $allowedstringerrors .= <<MOO
  83. break;
  84. MOO
  85. ;
  86. $allowedstringerrors .= <<MOO
  87. case CURLE_UNSUPPORTED_PROTOCOL:
  88. MOO
  89. ;
  90. for my $o (@unsupported_protocol) {
  91. $allowedstringerrors .= <<MOO
  92. if(!strcmp("$o", name))
  93. return;
  94. MOO
  95. ;
  96. }
  97. $allowedstringerrors .= <<MOO
  98. break;
  99. MOO
  100. ;
  101. $allowedstringerrors .= <<MOO
  102. case CURLE_SSL_ENGINE_NOTFOUND:
  103. MOO
  104. ;
  105. for my $o (@ssl_engine_notfound) {
  106. $allowedstringerrors .= <<MOO
  107. if(!strcmp("$o", name))
  108. return;
  109. MOO
  110. ;
  111. }
  112. $allowedstringerrors .= <<MOO
  113. break;
  114. default:
  115. break;
  116. }
  117. MOO
  118. ;
  119. #
  120. # Generate a set of string checks
  121. #
  122. my $allowednumerrors = <<MOO
  123. switch(code) {
  124. case CURLE_UNSUPPORTED_PROTOCOL:
  125. MOO
  126. ;
  127. for my $o (@unsupported_protocol_num) {
  128. $allowednumerrors .= <<MOO
  129. if(!strcmp("$o", name))
  130. return;
  131. MOO
  132. ;
  133. }
  134. $allowednumerrors .= <<MOO
  135. break;
  136. case CURLE_NOT_BUILT_IN:
  137. MOO
  138. ;
  139. for my $o (@not_built_in_num) {
  140. $allowednumerrors .= <<MOO
  141. if(!strcmp("$o", name))
  142. return;
  143. MOO
  144. ;
  145. }
  146. $allowednumerrors .= <<MOO
  147. break;
  148. default:
  149. break;
  150. }
  151. MOO
  152. ;
  153. print <<HEADER
  154. /***************************************************************************
  155. * _ _ ____ _
  156. * Project ___| | | | _ \\| |
  157. * / __| | | | |_) | |
  158. * | (__| |_| | _ <| |___
  159. * \\___|\\___/|_| \\_\\_____|
  160. *
  161. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  162. *
  163. * This software is licensed as described in the file COPYING, which
  164. * you should have received as part of this distribution. The terms
  165. * are also available at https://curl.se/docs/copyright.html.
  166. *
  167. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  168. * copies of the Software, and permit persons to whom the Software is
  169. * furnished to do so, under the terms of the COPYING file.
  170. *
  171. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  172. * KIND, either express or implied.
  173. *
  174. * SPDX-License-Identifier: curl
  175. *
  176. ***************************************************************************/
  177. #define CURL_DISABLE_DEPRECATION /* Deprecated options are tested too */
  178. #include "test.h"
  179. #include "memdebug.h"
  180. #include <limits.h>
  181. /* This source code is generated by mk-lib1521.pl ! */
  182. struct data {
  183. char *blaha;
  184. };
  185. #define LO $minlong
  186. #define HI $maxlong
  187. #define OFF_LO (curl_off_t) LO
  188. #define OFF_HI (curl_off_t) $maxofft
  189. #define OFF_NO (curl_off_t) 0
  190. static size_t writecb(char *buffer, size_t size, size_t nitems,
  191. void *outstream)
  192. {
  193. (void)buffer;
  194. (void)size;
  195. (void)nitems;
  196. (void)outstream;
  197. return 0;
  198. }
  199. static size_t readcb(char *buffer,
  200. size_t size,
  201. size_t nitems,
  202. void *instream)
  203. {
  204. (void)buffer;
  205. (void)size;
  206. (void)nitems;
  207. (void)instream;
  208. return 0;
  209. }
  210. static void errlongzero(const char *name, CURLcode code, int lineno)
  211. {
  212. printf("%s set to 0 returned %d, \\"%s\\" on line %d\\n",
  213. name, code, curl_easy_strerror(code), lineno);
  214. }
  215. static void errlong(const char *name, CURLcode code, int lineno)
  216. {
  217. $allowednumerrors
  218. printf("%s set to non-zero returned %d, \\"%s\\" on line %d\\n",
  219. name, code, curl_easy_strerror(code), lineno);
  220. }
  221. static void errstring(const char *name, CURLcode code, int lineno)
  222. {
  223. /* allow this set of options to return CURLE_BAD_FUNCTION_ARGUMENT
  224. when given a strange string input */
  225. $allowedstringerrors
  226. printf("%s set to a string returned %d, \\"%s\\" on line %d\\n",
  227. name, code, curl_easy_strerror(code), lineno);
  228. }
  229. static void err(const char *name, CURLcode val, int lineno)
  230. {
  231. printf("%s returned %d, \\"%s\\" on line %d\\n",
  232. name, val, curl_easy_strerror(val), lineno);
  233. }
  234. static void errnull(const char *name, CURLcode val, int lineno)
  235. {
  236. printf("%s set to NULL returned %d, \\"%s\\" on line %d\\n",
  237. name, val, curl_easy_strerror(val), lineno);
  238. }
  239. static void geterr(const char *name, CURLcode val, int lineno)
  240. {
  241. printf("CURLINFO_%s returned %d, \\"%s\\" on line %d\\n",
  242. name, val, curl_easy_strerror(val), lineno);
  243. }
  244. static curl_progress_callback progresscb;
  245. static curl_write_callback headercb;
  246. static curl_debug_callback debugcb;
  247. static curl_trailer_callback trailercb;
  248. static curl_ssl_ctx_callback ssl_ctx_cb;
  249. static curl_ioctl_callback ioctlcb;
  250. static curl_sockopt_callback sockoptcb;
  251. static curl_opensocket_callback opensocketcb;
  252. static curl_seek_callback seekcb;
  253. static curl_sshkeycallback ssh_keycb;
  254. static curl_sshhostkeycallback ssh_hostkeycb;
  255. static curl_chunk_bgn_callback chunk_bgn_cb;
  256. static curl_chunk_end_callback chunk_end_cb;
  257. static curl_fnmatch_callback fnmatch_cb;
  258. static curl_closesocket_callback closesocketcb;
  259. static curl_xferinfo_callback xferinfocb;
  260. static curl_hstsread_callback hstsreadcb;
  261. static curl_hstswrite_callback hstswritecb;
  262. static curl_resolver_start_callback resolver_start_cb;
  263. static curl_prereq_callback prereqcb;
  264. /* long options that are okay to return
  265. CURLE_BAD_FUNCTION_ARGUMENT */
  266. static bool bad_long(CURLcode res, int check)
  267. {
  268. if(res != CURLE_BAD_FUNCTION_ARGUMENT)
  269. return 0; /* not okay */
  270. if(check < CURLOPTTYPE_OBJECTPOINT) {
  271. /* LONG */
  272. return 1;
  273. }
  274. else if((check >= CURLOPTTYPE_OFF_T) &&
  275. (check < CURLOPTTYPE_BLOB)) {
  276. /* OFF_T */
  277. return 1;
  278. }
  279. return 0;
  280. }
  281. /* macro to check the first setopt of an option which then is allowed to get a
  282. non-existing function return code back */
  283. #define present(x) ((x != CURLE_NOT_BUILT_IN) && (x != CURLE_UNKNOWN_OPTION))
  284. CURLcode test(char *URL)
  285. {
  286. CURL *curl = NULL;
  287. CURL *dep = NULL;
  288. CURLSH *share = NULL;
  289. char errorbuffer[CURL_ERROR_SIZE];
  290. void *conv_from_network_cb = NULL;
  291. void *conv_to_network_cb = NULL;
  292. void *conv_from_utf8_cb = NULL;
  293. void *interleavecb = NULL;
  294. char *stringpointerextra = (char *)"moooo";
  295. struct curl_slist *slist = NULL;
  296. struct curl_httppost *httppost = NULL;
  297. curl_mime *mimepost = NULL;
  298. FILE *stream = stderr;
  299. struct data object;
  300. char *charp;
  301. long val;
  302. curl_off_t oval;
  303. double dval;
  304. curl_socket_t sockfd;
  305. struct curl_certinfo *certinfo;
  306. struct curl_tlssessioninfo *tlssession;
  307. struct curl_blob blob = { (void *)"silly", 5, 0};
  308. CURLcode res = CURLE_OK;
  309. (void)URL; /* not used */
  310. global_init(CURL_GLOBAL_ALL);
  311. easy_init(dep);
  312. easy_init(curl);
  313. share = curl_share_init();
  314. if(!share) {
  315. res = CURLE_OUT_OF_MEMORY;
  316. goto test_cleanup;
  317. }
  318. HEADER
  319. ;
  320. while(<STDIN>) {
  321. s/^\s*(.*?)\s*$/$1/; # Trim.
  322. # Remove multi-line comment trail.
  323. if($incomment) {
  324. if($_ !~ /.*?\*\/\s*(.*)$/) {
  325. next;
  326. }
  327. $_ = $1;
  328. $incomment = 0;
  329. }
  330. if($line ne "") {
  331. # Unfold line.
  332. $_ = "$line $1";
  333. $line = "";
  334. }
  335. # Remove comments.
  336. while($_ =~ /^(.*?)\/\*.*?\*\/(.*)$/) {
  337. $_ = "$1 $2";
  338. }
  339. s/^\s*(.*?)\s*$/$1/; # Trim again.
  340. if($_ =~ /^(.*)\/\*/) {
  341. $_ = $1;
  342. $incomment = 1;
  343. }
  344. # Ignore preprocessor directives and blank lines.
  345. if($_ =~ /^(?:#|$)/) {
  346. next;
  347. }
  348. # Handle lines that may be continued as if they were folded.
  349. if($_ !~ /[;,{}]$/) {
  350. # Folded line.
  351. $line = $_;
  352. next;
  353. }
  354. if($_ =~ / CURL_DEPRECATED\(/) {
  355. # Drop deprecation info.
  356. if($_ !~ /^(.*?) CURL_DEPRECATED\(.*?"\)(.*)$/) {
  357. # Needs unfolding.
  358. $line = $_;
  359. next;
  360. }
  361. $_ = $1 . $2;
  362. }
  363. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(/ && $_ !~ /\),$/) {
  364. # Multi-line CURLOPTs need unfolding.
  365. $line = $_;
  366. next;
  367. }
  368. if($_ =~ /^CURLOPT(?:DEPRECATED)?\(([^ ]*), ([^ ]*), (\d*)[,)]/) {
  369. my ($name, $type, $val)=($1, $2, $3);
  370. my $w=" ";
  371. my $w2="$w$w";
  372. my $w3="$w$w$w";
  373. my $opt = $name;
  374. $opt =~ s/^CURLOPT_//;
  375. my $exists = "${w}{\n";
  376. # the first check for an option
  377. my $fpref = "${exists}${w2}CURLcode first =\n${w3}curl_easy_setopt(curl, $name,";
  378. my $ifpresent = "${w2}if(present(first)) {\n";
  379. my $pref = "${w3}res = curl_easy_setopt(curl, $name,";
  380. my $i = ' ' x (length($w) + 25);
  381. my $fcheck = <<MOO
  382. if(first && present(first)) /* first setopt check only */
  383. err("$name", first, __LINE__);
  384. MOO
  385. ;
  386. my $fstringcheck = <<MOO
  387. if(first && present(first)) /* first setopt check only */
  388. errstring("$name", first, __LINE__);
  389. MOO
  390. ;
  391. my $check = <<MOO
  392. if(res)
  393. err("$name", res, __LINE__);
  394. MOO
  395. ;
  396. my $flongcheckzero = <<MOO
  397. if(first && present(first) && !bad_long(res,
  398. $name))
  399. errlongzero("$name", first, __LINE__);
  400. MOO
  401. ;
  402. my $longcheck = <<MOO
  403. if(res && !bad_long(res, $name))
  404. errlong("$name", res, __LINE__);
  405. MOO
  406. ;
  407. my $nullcheck = <<MOO
  408. if(res)
  409. errnull(\"$name\", res, __LINE__);
  410. MOO
  411. ;
  412. print "\n /****** Verify $name ******/\n";
  413. if($type eq "CURLOPTTYPE_STRINGPOINT") {
  414. print "${fpref} \"string\");\n$fstringcheck";
  415. print "$ifpresent";
  416. print "${pref} NULL);\n$nullcheck";
  417. }
  418. elsif(($type eq "CURLOPTTYPE_LONG") ||
  419. ($type eq "CURLOPTTYPE_VALUES")) {
  420. print "${fpref} 0L);\n$flongcheckzero";
  421. print "$ifpresent";
  422. print "${pref} 22L);\n$longcheck";
  423. print "${pref} LO);\n$longcheck";
  424. print "${pref} HI);\n$longcheck";
  425. }
  426. elsif($type eq "CURLOPTTYPE_OFF_T") {
  427. print "${fpref} OFF_NO);\n$flongcheckzero";
  428. print "$ifpresent";
  429. my $lvl = " " x 29;
  430. print "${pref}\n${lvl}(curl_off_t)22);\n$longcheck";
  431. print "${pref} OFF_HI);\n$longcheck";
  432. print "${pref} OFF_LO);\n$longcheck";
  433. }
  434. elsif(($type eq "CURLOPTTYPE_OBJECTPOINT") ||
  435. ($type eq "CURLOPTTYPE_CBPOINT")) {
  436. if($name =~ /DEPENDS/) {
  437. print "${fpref} dep);\n$fcheck";
  438. }
  439. elsif($name =~ "SHARE") {
  440. print "${fpref} share);\n$fcheck";
  441. }
  442. elsif($name eq "CURLOPT_ERRORBUFFER") {
  443. print "${fpref} errorbuffer);\n$fcheck";
  444. }
  445. elsif(($name eq "CURLOPT_POSTFIELDS") ||
  446. ($name eq "CURLOPT_COPYPOSTFIELDS")) {
  447. # set size to zero to avoid it being "illegal"
  448. print " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
  449. print "${fpref} stringpointerextra);\n$fcheck";
  450. }
  451. elsif($name eq "CURLOPT_HTTPPOST") {
  452. print "${fpref} httppost);\n$fcheck";
  453. }
  454. elsif($name eq "CURLOPT_MIMEPOST") {
  455. print "${fpref} mimepost);\n$fcheck";
  456. }
  457. elsif($name eq "CURLOPT_STDERR") {
  458. print "${fpref} stream);\n$fcheck";
  459. }
  460. else {
  461. print "${fpref} &object);\n$fcheck";
  462. }
  463. print "$ifpresent";
  464. print "${pref} NULL);\n$nullcheck";
  465. }
  466. elsif($type eq "CURLOPTTYPE_SLISTPOINT") {
  467. print "${fpref} slist);\n$fcheck";
  468. print "$ifpresent";
  469. print "${pref} NULL);\n$nullcheck";
  470. }
  471. elsif($type eq "CURLOPTTYPE_FUNCTIONPOINT") {
  472. if($name =~ /([^ ]*)FUNCTION/) {
  473. my $l=lc($1);
  474. $l =~ s/^curlopt_//;
  475. print "${fpref}\n$i${l}cb);\n$fcheck";
  476. }
  477. else {
  478. print "${fpref} &func);\n$fcheck";
  479. }
  480. print "$ifpresent";
  481. print "${pref} NULL);\n$nullcheck";
  482. }
  483. elsif($type eq "CURLOPTTYPE_BLOB") {
  484. print "${fpref} &blob);\n$check";
  485. print "$ifpresent";
  486. print "${pref} NULL);\n$nullcheck";
  487. }
  488. else {
  489. print STDERR "\nUnknown type: $type\n";
  490. exit 22; # exit to make this noticed!
  491. }
  492. print <<MOO
  493. } /* end of secondary checks */
  494. } /* end of single setopt */
  495. MOO
  496. ;
  497. }
  498. elsif($_ =~ /^CURLINFO_NONE/) {
  499. $infomode = 1;
  500. }
  501. elsif($infomode &&
  502. ($_ =~ /^CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
  503. my ($info, $type)=($1, $2);
  504. my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
  505. my $check = " if(res)\n geterr(\"$info\", res, __LINE__);\n";
  506. if($type eq "STRING") {
  507. print "$c &charp);\n$check";
  508. }
  509. elsif($type eq "LONG") {
  510. print "$c &val);\n$check";
  511. }
  512. elsif($type eq "OFF_T") {
  513. print "$c &oval);\n$check";
  514. }
  515. elsif($type eq "DOUBLE") {
  516. print "$c &dval);\n$check";
  517. }
  518. elsif($type eq "SLIST") {
  519. print "$c &slist);\n$check";
  520. print " if(slist)\n curl_slist_free_all(slist);\n";
  521. }
  522. elsif($type eq "SOCKET") {
  523. print "$c &sockfd);\n$check";
  524. }
  525. elsif($type eq "PTR") {
  526. if($info eq "CERTINFO") {
  527. print "$c &certinfo);\n$check";
  528. }
  529. elsif(($info eq "TLS_SESSION") ||
  530. ($info eq "TLS_SSL_PTR")) {
  531. print "$c &tlssession);\n$check";
  532. }
  533. else {
  534. print STDERR "$info/$type is unsupported\n";
  535. }
  536. }
  537. else {
  538. print STDERR "$type is unsupported\n";
  539. }
  540. }
  541. }
  542. print <<FOOTER
  543. curl_easy_setopt(curl, (CURLoption)1, 0);
  544. res = CURLE_OK;
  545. test_cleanup:
  546. curl_easy_cleanup(curl);
  547. curl_easy_cleanup(dep);
  548. curl_share_cleanup(share);
  549. curl_global_cleanup();
  550. if(!res)
  551. puts("ok");
  552. return res;
  553. }
  554. FOOTER
  555. ;