mk-lib1521.pl 11 KB

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