mk-lib1521.pl 10 KB

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