2
0

mk-lib1521.pl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2017 - 2020, 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.haxx.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.haxx.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_resolver_start_callback resolver_start_cb;
  125. int test(char *URL)
  126. {
  127. CURL *curl = NULL;
  128. CURL *dep = NULL;
  129. CURLSH *share = NULL;
  130. char errorbuffer[CURL_ERROR_SIZE];
  131. void *conv_from_network_cb = NULL;
  132. void *conv_to_network_cb = NULL;
  133. void *conv_from_utf8_cb = NULL;
  134. void *interleavecb = NULL;
  135. char *stringpointerextra = (char *)"moooo";
  136. struct curl_slist *slist = NULL;
  137. struct curl_httppost *httppost = NULL;
  138. curl_mime *mimepost = NULL;
  139. FILE *stream = stderr;
  140. struct data object;
  141. char *charp;
  142. long val;
  143. curl_off_t oval;
  144. double dval;
  145. curl_socket_t sockfd;
  146. struct curl_certinfo *certinfo;
  147. struct curl_tlssessioninfo *tlssession;
  148. struct curl_blob blob = { (void *)"silly", 5, 0};
  149. CURLcode res = CURLE_OK;
  150. (void)URL; /* not used */
  151. global_init(CURL_GLOBAL_ALL);
  152. easy_init(dep);
  153. easy_init(curl);
  154. share = curl_share_init();
  155. if(!share) {
  156. res = CURLE_OUT_OF_MEMORY;
  157. goto test_cleanup;
  158. }
  159. HEADER
  160. ;
  161. while(<STDIN>) {
  162. if($_ =~ /^ CURLOPT\(([^ ]*), ([^ ]*), (\d*)\)/) {
  163. my ($name, $type, $val)=($1, $2, $3);
  164. my $w=" ";
  165. my $pref = "${w}res = curl_easy_setopt(curl, $name,";
  166. my $i = ' ' x (length($w) + 23);
  167. my $check = " if(UNEX(res)) {\n err(\"$name\", res, __LINE__);\n goto test_cleanup;\n }\n";
  168. if($type eq "CURLOPTTYPE_STRINGPOINT") {
  169. print "${pref} \"string\");\n$check";
  170. print "${pref} NULL);\n$check";
  171. }
  172. elsif($type eq "CURLOPTTYPE_LONG") {
  173. print "${pref} 0L);\n$check";
  174. print "${pref} 22L);\n$check";
  175. print "${pref} LO);\n$check";
  176. print "${pref} HI);\n$check";
  177. }
  178. elsif($type eq "CURLOPTTYPE_OBJECTPOINT") {
  179. if($name =~ /DEPENDS/) {
  180. print "${pref} dep);\n$check";
  181. }
  182. elsif($name =~ "SHARE") {
  183. print "${pref} share);\n$check";
  184. }
  185. elsif($name eq "CURLOPT_ERRORBUFFER") {
  186. print "${pref} errorbuffer);\n$check";
  187. }
  188. elsif(($name eq "CURLOPT_POSTFIELDS") ||
  189. ($name eq "CURLOPT_COPYPOSTFIELDS")) {
  190. # set size to zero to avoid it being "illegal"
  191. print " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
  192. print "${pref} stringpointerextra);\n$check";
  193. }
  194. elsif($name eq "CURLOPT_HTTPPOST") {
  195. print "${pref} httppost);\n$check";
  196. }
  197. elsif($name eq "CURLOPT_MIMEPOST") {
  198. print "${pref} mimepost);\n$check";
  199. }
  200. elsif($name eq "CURLOPT_STDERR") {
  201. print "${pref} stream);\n$check";
  202. }
  203. else {
  204. print "${pref} &object);\n$check";
  205. }
  206. print "${pref} NULL);\n$check";
  207. }
  208. elsif($type eq "CURLOPTTYPE_SLISTPOINT") {
  209. print "${pref} slist);\n$check";
  210. }
  211. elsif($type eq "CURLOPTTYPE_FUNCTIONPOINT") {
  212. if($name =~ /([^ ]*)FUNCTION/) {
  213. my $l=lc($1);
  214. $l =~ s/^curlopt_//;
  215. print "${pref}\n$i${l}cb);\n$check";
  216. }
  217. else {
  218. print "${pref} &func);\n$check";
  219. }
  220. print "${pref} NULL);\n$check";
  221. }
  222. elsif($type eq "CURLOPTTYPE_OFF_T") {
  223. # play conservative to work with 32bit curl_off_t
  224. print "${pref} OFF_NO);\n$check";
  225. print "${pref} OFF_HI);\n$check";
  226. print "${pref} OFF_LO);\n$check";
  227. }
  228. elsif($type eq "CURLOPTTYPE_BLOB") {
  229. print "${pref} &blob);\n$check";
  230. }
  231. else {
  232. print STDERR "\n---- $type\n";
  233. exit; # exit to make this noticed!
  234. }
  235. }
  236. elsif($_ =~ /^ CURLINFO_NONE/) {
  237. $infomode = 1;
  238. }
  239. elsif($infomode &&
  240. ($_ =~ /^ CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
  241. my ($info, $type)=($1, $2);
  242. my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
  243. my $check = " if(UNEX(res)) {\n geterr(\"$info\", res, __LINE__);\n goto test_cleanup;\n }\n";
  244. if($type eq "STRING") {
  245. print "$c &charp);\n$check";
  246. }
  247. elsif($type eq "LONG") {
  248. print "$c &val);\n$check";
  249. }
  250. elsif($type eq "OFF_T") {
  251. print "$c &oval);\n$check";
  252. }
  253. elsif($type eq "DOUBLE") {
  254. print "$c &dval);\n$check";
  255. }
  256. elsif($type eq "SLIST") {
  257. print "$c &slist);\n$check";
  258. print " if(slist)\n curl_slist_free_all(slist);\n";
  259. }
  260. elsif($type eq "SOCKET") {
  261. print "$c &sockfd);\n$check";
  262. }
  263. elsif($type eq "PTR") {
  264. if($info eq "CERTINFO") {
  265. print "$c &certinfo);\n$check";
  266. }
  267. elsif(($info eq "TLS_SESSION") ||
  268. ($info eq "TLS_SSL_PTR")) {
  269. print "$c &tlssession);\n$check";
  270. }
  271. else {
  272. print STDERR "$info/$type is unsupported\n";
  273. }
  274. }
  275. else {
  276. print STDERR "$type is unsupported\n";
  277. }
  278. }
  279. }
  280. print <<FOOTER
  281. curl_easy_setopt(curl, 1, 0);
  282. res = CURLE_OK;
  283. test_cleanup:
  284. curl_easy_cleanup(curl);
  285. curl_easy_cleanup(dep);
  286. curl_share_cleanup(share);
  287. curl_global_cleanup();
  288. return (int)res;
  289. }
  290. FOOTER
  291. ;