mk-lib1521.pl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2017, 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_ssl_ctx_callback ssl_ctx_cb;
  113. static curl_ioctl_callback ioctlcb;
  114. static curl_sockopt_callback sockoptcb;
  115. static curl_opensocket_callback opensocketcb;
  116. static curl_seek_callback seekcb;
  117. static curl_sshkeycallback ssh_keycb;
  118. static curl_chunk_bgn_callback chunk_bgn_cb;
  119. static curl_chunk_end_callback chunk_end_cb;
  120. static curl_fnmatch_callback fnmatch_cb;
  121. static curl_closesocket_callback closesocketcb;
  122. static curl_xferinfo_callback xferinfocb;
  123. static curl_resolver_start_callback resolver_start_cb;
  124. int test(char *URL)
  125. {
  126. CURL *curl = NULL;
  127. CURL *dep = NULL;
  128. CURLSH *share = NULL;
  129. char errorbuffer[CURL_ERROR_SIZE];
  130. void *conv_from_network_cb = NULL;
  131. void *conv_to_network_cb = NULL;
  132. void *conv_from_utf8_cb = NULL;
  133. void *interleavecb = NULL;
  134. char *stringpointerextra = (char *)"moooo";
  135. struct curl_slist *slist = NULL;
  136. struct curl_httppost *httppost = NULL;
  137. curl_mime *mimepost = NULL;
  138. FILE *stream = stderr;
  139. struct data object;
  140. char *charp;
  141. long val;
  142. curl_off_t oval;
  143. double dval;
  144. curl_socket_t sockfd;
  145. struct curl_certinfo *certinfo;
  146. struct curl_tlssessioninfo *tlssession;
  147. CURLcode res = CURLE_OK;
  148. (void)URL; /* not used */
  149. global_init(CURL_GLOBAL_ALL);
  150. easy_init(dep);
  151. easy_init(curl);
  152. share = curl_share_init();
  153. if(!share) {
  154. res = CURLE_OUT_OF_MEMORY;
  155. goto test_cleanup;
  156. }
  157. HEADER
  158. ;
  159. while(<STDIN>) {
  160. if($_ =~ /^ CINIT\(([^ ]*), ([^ ]*), (\d*)\)/) {
  161. my ($name, $type, $val)=($1, $2, $3);
  162. my $w=" ";
  163. my $pref = "${w}res = curl_easy_setopt(curl, CURLOPT_$name,";
  164. my $i = ' ' x (length($w) + 23);
  165. my $check = " if(UNEX(res)) {\n err(\"$name\", res, __LINE__); goto test_cleanup; }\n";
  166. if($type eq "STRINGPOINT") {
  167. print "${pref} \"string\");\n$check";
  168. print "${pref} NULL);\n$check";
  169. }
  170. elsif($type eq "LONG") {
  171. print "${pref} 0L);\n$check";
  172. print "${pref} 22L);\n$check";
  173. print "${pref} LO);\n$check";
  174. print "${pref} HI);\n$check";
  175. }
  176. elsif($type eq "OBJECTPOINT") {
  177. if($name =~ /DEPENDS/) {
  178. print "${pref} dep);\n$check";
  179. }
  180. elsif($name =~ "SHARE") {
  181. print "${pref} share);\n$check";
  182. }
  183. elsif($name eq "ERRORBUFFER") {
  184. print "${pref} errorbuffer);\n$check";
  185. }
  186. elsif(($name eq "POSTFIELDS") ||
  187. ($name eq "COPYPOSTFIELDS")) {
  188. # set size to zero to avoid it being "illegal"
  189. print " (void)curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);\n";
  190. print "${pref} stringpointerextra);\n$check";
  191. }
  192. elsif(($name eq "HTTPHEADER") ||
  193. ($name eq "POSTQUOTE") ||
  194. ($name eq "PREQUOTE") ||
  195. ($name eq "HTTP200ALIASES") ||
  196. ($name eq "TELNETOPTIONS") ||
  197. ($name eq "MAIL_RCPT") ||
  198. ($name eq "RESOLVE") ||
  199. ($name eq "PROXYHEADER") ||
  200. ($name eq "QUOTE")) {
  201. print "${pref} slist);\n$check";
  202. }
  203. elsif($name eq "HTTPPOST") {
  204. print "${pref} httppost);\n$check";
  205. }
  206. elsif($name eq "MIMEPOST") {
  207. print "${pref} mimepost);\n$check";
  208. }
  209. elsif($name eq "STDERR") {
  210. print "${pref} stream);\n$check";
  211. }
  212. else {
  213. print "${pref} &object);\n$check";
  214. }
  215. print "${pref} NULL);\n$check";
  216. }
  217. elsif($type eq "FUNCTIONPOINT") {
  218. if($name =~ /([^ ]*)FUNCTION/) {
  219. my $l=lc($1);
  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 "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. else {
  234. print STDERR "\n---- $type\n";
  235. }
  236. }
  237. elsif($_ =~ /^ CURLINFO_NONE/) {
  238. $infomode = 1;
  239. }
  240. elsif($infomode &&
  241. ($_ =~ /^ CURLINFO_([^ ]*) *= *CURLINFO_([^ ]*)/)) {
  242. my ($info, $type)=($1, $2);
  243. my $c = " res = curl_easy_getinfo(curl, CURLINFO_$info,";
  244. my $check = " if(UNEX(res)) {\n geterr(\"$info\", res, __LINE__); goto test_cleanup; }\n";
  245. if($type eq "STRING") {
  246. print "$c &charp);\n$check";
  247. }
  248. elsif($type eq "LONG") {
  249. print "$c &val);\n$check";
  250. }
  251. elsif($type eq "OFF_T") {
  252. print "$c &oval);\n$check";
  253. }
  254. elsif($type eq "DOUBLE") {
  255. print "$c &dval);\n$check";
  256. }
  257. elsif($type eq "SLIST") {
  258. print "$c &slist);\n$check";
  259. print " if(slist)\n curl_slist_free_all(slist);\n";
  260. }
  261. elsif($type eq "SOCKET") {
  262. print "$c &sockfd);\n$check";
  263. }
  264. elsif($type eq "PTR") {
  265. if($info eq "CERTINFO") {
  266. print "$c &certinfo);\n$check";
  267. }
  268. elsif(($info eq "TLS_SESSION") ||
  269. ($info eq "TLS_SSL_PTR")) {
  270. print "$c &tlssession);\n$check";
  271. }
  272. else {
  273. print STDERR "$info/$type is unsupported\n";
  274. }
  275. }
  276. else {
  277. print STDERR "$type is unsupported\n";
  278. }
  279. }
  280. }
  281. print <<FOOTER
  282. curl_easy_setopt(curl, 1, 0);
  283. res = CURLE_OK;
  284. test_cleanup:
  285. curl_easy_cleanup(curl);
  286. curl_easy_cleanup(dep);
  287. curl_share_cleanup(share);
  288. curl_global_cleanup();
  289. return (int)res;
  290. }
  291. FOOTER
  292. ;