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