1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * This is a simple example showing how a program on a non-ASCII platform
24 * would invoke callbacks to do its own codeset conversions instead of
25 * using the built-in iconv functions in libcurl.
30 The IBM-1047 EBCDIC codeset is used for this example but the code
31 would be similar for other non-ASCII codesets.
33 Three callback functions are created below:
34 my_conv_from_ascii_to_ebcdic,
35 my_conv_from_ebcdic_to_ascii, and
36 my_conv_from_utf8_to_ebcdic
38 The "platform_xxx" calls represent platform-specific conversion routines.
43 #include <curl/curl.h>
45 static CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
47 char *tempptrin, *tempptrout;
48 size_t bytes = length;
50 tempptrin = tempptrout = buffer;
51 rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
52 if(rc == PLATFORM_CONV_OK) {
56 return CURLE_CONV_FAILED;
60 static CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
62 char *tempptrin, *tempptrout;
63 size_t bytes = length;
65 tempptrin = tempptrout = buffer;
66 rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
67 if(rc == PLATFORM_CONV_OK) {
71 return CURLE_CONV_FAILED;
75 static CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
77 char *tempptrin, *tempptrout;
78 size_t bytes = length;
80 tempptrin = tempptrout = buffer;
81 rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
82 if(rc == PLATFORM_CONV_OK) {
86 return CURLE_CONV_FAILED;
95 curl = curl_easy_init();
97 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
99 /* use platform-specific functions for codeset conversions */
100 curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
101 my_conv_from_ascii_to_ebcdic);
102 curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
103 my_conv_from_ebcdic_to_ascii);
104 curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
105 my_conv_from_utf8_to_ebcdic);
107 res = curl_easy_perform(curl);
110 curl_easy_cleanup(curl);