mk-lib1521.pl 10.0 KB

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