sampleconv.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  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.
  26. * </DESC>
  27. */
  28. /*
  29. The IBM-1047 EBCDIC codeset is used for this example but the code
  30. would be similar for other non-ASCII codesets.
  31. Three callback functions are created below:
  32. my_conv_from_ascii_to_ebcdic,
  33. my_conv_from_ebcdic_to_ascii, and
  34. my_conv_from_utf8_to_ebcdic
  35. The "platform_xxx" calls represent platform-specific conversion routines.
  36. */
  37. #include <stdio.h>
  38. #include <curl/curl.h>
  39. static CURLcode my_conv_from_ascii_to_ebcdic(char *buffer, size_t length)
  40. {
  41. char *tempptrin, *tempptrout;
  42. size_t bytes = length;
  43. int rc;
  44. tempptrin = tempptrout = buffer;
  45. rc = platform_a2e(&tempptrin, &bytes, &tempptrout, &bytes);
  46. if(rc == PLATFORM_CONV_OK) {
  47. return CURLE_OK;
  48. }
  49. else {
  50. return CURLE_CONV_FAILED;
  51. }
  52. }
  53. static CURLcode my_conv_from_ebcdic_to_ascii(char *buffer, size_t length)
  54. {
  55. char *tempptrin, *tempptrout;
  56. size_t bytes = length;
  57. int rc;
  58. tempptrin = tempptrout = buffer;
  59. rc = platform_e2a(&tempptrin, &bytes, &tempptrout, &bytes);
  60. if(rc == PLATFORM_CONV_OK) {
  61. return CURLE_OK;
  62. }
  63. else {
  64. return CURLE_CONV_FAILED;
  65. }
  66. }
  67. static CURLcode my_conv_from_utf8_to_ebcdic(char *buffer, size_t length)
  68. {
  69. char *tempptrin, *tempptrout;
  70. size_t bytes = length;
  71. int rc;
  72. tempptrin = tempptrout = buffer;
  73. rc = platform_u2e(&tempptrin, &bytes, &tempptrout, &bytes);
  74. if(rc == PLATFORM_CONV_OK) {
  75. return CURLE_OK;
  76. }
  77. else {
  78. return CURLE_CONV_FAILED;
  79. }
  80. }
  81. int main(void)
  82. {
  83. CURL *curl;
  84. curl = curl_easy_init();
  85. if(curl) {
  86. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  87. /* use platform-specific functions for codeset conversions */
  88. curl_easy_setopt(curl, CURLOPT_CONV_FROM_NETWORK_FUNCTION,
  89. my_conv_from_ascii_to_ebcdic);
  90. curl_easy_setopt(curl, CURLOPT_CONV_TO_NETWORK_FUNCTION,
  91. my_conv_from_ebcdic_to_ascii);
  92. curl_easy_setopt(curl, CURLOPT_CONV_FROM_UTF8_FUNCTION,
  93. my_conv_from_utf8_to_ebcdic);
  94. curl_easy_perform(curl);
  95. /* always cleanup */
  96. curl_easy_cleanup(curl);
  97. }
  98. return 0;
  99. }