Commit c828646f authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

CURL_DOES_CONVERSIONS: cleanup

Massively reduce #ifdefs all over (23 #ifdef lines less so far)
Moved conversion-specific code to non-ascii.c
parent eb65a49b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -21,7 +21,8 @@ CSOURCES = file.c timeval.c base64.c hostip.c progress.c formdata.c \
  socks_gssapi.c socks_sspi.c curl_sspi.c slist.c nonblock.c		\
  curl_memrchr.c imap.c pop3.c smtp.c pingpong.c rtsp.c curl_threads.c	\
  warnless.c hmac.c polarssl.c curl_rtmp.c openldap.c curl_gethostname.c\
  gopher.c axtls.c idn_win32.c http_negotiate_sspi.c cyassl.c http_proxy.c
  gopher.c axtls.c idn_win32.c http_negotiate_sspi.c cyassl.c http_proxy.c \
  non-ascii.c

HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h	\
  progress.h formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h	\
@@ -36,5 +37,5 @@ HHEADERS = arpa_telnet.h netrc.h file.h timeval.h qssl.h hostip.h \
  curl_base64.h rawstr.h curl_addrinfo.h curl_sspi.h slist.h nonblock.h	\
  curl_memrchr.h imap.h pop3.h smtp.h pingpong.h rtsp.h curl_threads.h	\
  warnless.h curl_hmac.h polarssl.h curl_rtmp.h curl_gethostname.h      \
  gopher.h axtls.h cyassl.h http_proxy.h
  gopher.h axtls.h cyassl.h http_proxy.h non-ascii.h
+8 −24
Original line number Diff line number Diff line
@@ -31,10 +31,10 @@
#include <curl/mprintf.h>

#include "urldata.h" /* for the SessionHandle definition */
#include "easyif.h"  /* for Curl_convert_... prototypes */
#include "warnless.h"
#include "curl_base64.h"
#include "curl_memory.h"
#include "non-ascii.h"

/* include memdebug.h last */
#include "memdebug.h"
@@ -146,9 +146,7 @@ size_t Curl_base64_encode(struct SessionHandle *data,
  int inputparts;
  char *output;
  char *base64data;
#ifdef CURL_DOES_CONVERSIONS
  char *convbuf = NULL;
#endif

  const char *indata = inputbuff;

@@ -161,29 +159,16 @@ size_t Curl_base64_encode(struct SessionHandle *data,
  if(NULL == output)
    return 0;

#ifdef CURL_DOES_CONVERSIONS
  /*
   * The base64 data needs to be created using the network encoding
   * not the host encoding.  And we can't change the actual input
   * so we copy it to a buffer, translate it, and use that instead.
   */
  if(data) {
    convbuf = malloc(insize);
    if(!convbuf) {
      free(output);
  if(Curl_convert_clone(data, indata, insize, &convbuf))
    return 0;
    }
    memcpy(convbuf, indata, insize);
    if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
      free(convbuf);
      free(output);
      return 0;
    }
    indata = convbuf; /* switch to the converted buffer */
  }
#else
  (void)data;
#endif

  if(convbuf)
    indata = (char *)convbuf;

  while(insize > 0) {
    for (i = inputparts = 0; i < 3; i++) {
@@ -229,10 +214,9 @@ size_t Curl_base64_encode(struct SessionHandle *data,
  *output=0;
  *outptr = base64data; /* make it return the actual data memory */

#ifdef CURL_DOES_CONVERSIONS
  if(data)
  if(convbuf)
    free(convbuf);
#endif

  return strlen(base64data); /* return the length of the new data */
}
/* ---- End of Base64 Encoding ---- */
+2 −210
Original line number Diff line number Diff line
@@ -85,22 +85,11 @@
#include "connect.h" /* for Curl_getconnectinfo */
#include "slist.h"
#include "curl_rand.h"
#include "non-ascii.h"

#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>

#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
#include <iconv.h>
/* set default codesets for iconv */
#ifndef CURL_ICONV_CODESET_OF_NETWORK
#define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
#endif
#ifndef CURL_ICONV_CODESET_FOR_UTF8
#define CURL_ICONV_CODESET_FOR_UTF8   "UTF-8"
#endif
#define ICONV_ERROR  (size_t)-1
#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */

/* The last #include file should be: */
#include "memdebug.h"

@@ -694,14 +683,7 @@ CURL *curl_easy_duphandle(CURL *incurl)
    goto fail;
#endif

#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
  outcurl->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
                                   CURL_ICONV_CODESET_OF_NETWORK);
  outcurl->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
                                    CURL_ICONV_CODESET_OF_HOST);
  outcurl->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
                                CURL_ICONV_CODESET_FOR_UTF8);
#endif
  Curl_convert_setup(outcurl);

  Curl_easy_initHandleData(outcurl);

@@ -863,196 +845,6 @@ CURLcode curl_easy_pause(CURL *curl, int action)
  return result;
}

#ifdef CURL_DOES_CONVERSIONS
/*
 * Curl_convert_to_network() is an internal function
 * for performing ASCII conversions on non-ASCII platforms.
 */
CURLcode Curl_convert_to_network(struct SessionHandle *data,
                                 char *buffer, size_t length)
{
  CURLcode rc;

  if(data->set.convtonetwork) {
    /* use translation callback */
    rc = data->set.convtonetwork(buffer, length);
    if(rc != CURLE_OK) {
      failf(data,
            "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %d: %s",
            (int)rc, curl_easy_strerror(rc));
    }
    return(rc);
  }
  else {
#ifdef HAVE_ICONV
    /* do the translation ourselves */
    char *input_ptr, *output_ptr;
    size_t in_bytes, out_bytes, rc;
    int error;

    /* open an iconv conversion descriptor if necessary */
    if(data->outbound_cd == (iconv_t)-1) {
      data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
                                     CURL_ICONV_CODESET_OF_HOST);
      if(data->outbound_cd == (iconv_t)-1) {
        error = ERRNO;
        failf(data,
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
               CURL_ICONV_CODESET_OF_NETWORK,
               CURL_ICONV_CODESET_OF_HOST,
               error, strerror(error));
        return CURLE_CONV_FAILED;
      }
    }
    /* call iconv */
    input_ptr = output_ptr = buffer;
    in_bytes = out_bytes = length;
    rc = iconv(data->outbound_cd, (const char**)&input_ptr, &in_bytes,
               &output_ptr, &out_bytes);
    if((rc == ICONV_ERROR) || (in_bytes != 0)) {
      error = ERRNO;
      failf(data,
        "The Curl_convert_to_network iconv call failed with errno %i: %s",
             error, strerror(error));
      return CURLE_CONV_FAILED;
    }
#else
    failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
    return CURLE_CONV_REQD;
#endif /* HAVE_ICONV */
  }

  return CURLE_OK;
}

/*
 * Curl_convert_from_network() is an internal function
 * for performing ASCII conversions on non-ASCII platforms.
 */
CURLcode Curl_convert_from_network(struct SessionHandle *data,
                                      char *buffer, size_t length)
{
  CURLcode rc;

  if(data->set.convfromnetwork) {
    /* use translation callback */
    rc = data->set.convfromnetwork(buffer, length);
    if(rc != CURLE_OK) {
      failf(data,
            "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %d: %s",
            (int)rc, curl_easy_strerror(rc));
    }
    return(rc);
  }
  else {
#ifdef HAVE_ICONV
    /* do the translation ourselves */
    char *input_ptr, *output_ptr;
    size_t in_bytes, out_bytes, rc;
    int error;

    /* open an iconv conversion descriptor if necessary */
    if(data->inbound_cd == (iconv_t)-1) {
      data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
                                    CURL_ICONV_CODESET_OF_NETWORK);
      if(data->inbound_cd == (iconv_t)-1) {
        error = ERRNO;
        failf(data,
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
              CURL_ICONV_CODESET_OF_HOST,
              CURL_ICONV_CODESET_OF_NETWORK,
              error, strerror(error));
        return CURLE_CONV_FAILED;
      }
    }
    /* call iconv */
    input_ptr = output_ptr = buffer;
    in_bytes = out_bytes = length;
    rc = iconv(data->inbound_cd, (const char **)&input_ptr, &in_bytes,
               &output_ptr, &out_bytes);
    if((rc == ICONV_ERROR) || (in_bytes != 0)) {
      error = ERRNO;
      failf(data,
            "The Curl_convert_from_network iconv call failed with errno %i: %s",
            error, strerror(error));
      return CURLE_CONV_FAILED;
    }
#else
    failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
    return CURLE_CONV_REQD;
#endif /* HAVE_ICONV */
  }

  return CURLE_OK;
}

/*
 * Curl_convert_from_utf8() is an internal function
 * for performing UTF-8 conversions on non-ASCII platforms.
 */
CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
                                     char *buffer, size_t length)
{
  CURLcode rc;

  if(data->set.convfromutf8) {
    /* use translation callback */
    rc = data->set.convfromutf8(buffer, length);
    if(rc != CURLE_OK) {
      failf(data,
            "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %d: %s",
            (int)rc, curl_easy_strerror(rc));
    }
    return(rc);
  }
  else {
#ifdef HAVE_ICONV
    /* do the translation ourselves */
    const char *input_ptr;
    char *output_ptr;
    size_t in_bytes, out_bytes, rc;
    int error;

    /* open an iconv conversion descriptor if necessary */
    if(data->utf8_cd == (iconv_t)-1) {
      data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
                                 CURL_ICONV_CODESET_FOR_UTF8);
      if(data->utf8_cd == (iconv_t)-1) {
        error = ERRNO;
        failf(data,
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
              CURL_ICONV_CODESET_OF_HOST,
              CURL_ICONV_CODESET_FOR_UTF8,
              error, strerror(error));
        return CURLE_CONV_FAILED;
      }
    }
    /* call iconv */
    input_ptr = output_ptr = buffer;
    in_bytes = out_bytes = length;
    rc = iconv(data->utf8_cd, &input_ptr, &in_bytes,
               &output_ptr, &out_bytes);
    if((rc == ICONV_ERROR) || (in_bytes != 0)) {
      error = ERRNO;
      failf(data,
            "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
            error, strerror(error));
      return CURLE_CONV_FAILED;
    }
    if(output_ptr < input_ptr) {
      /* null terminate the now shorter output string */
      *output_ptr = 0x00;
    }
#else
    failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
    return CURLE_CONV_REQD;
#endif /* HAVE_ICONV */
  }

  return CURLE_OK;
}

#endif /* CURL_DOES_CONVERSIONS */

static CURLcode easy_connection(struct SessionHandle *data,
                                curl_socket_t *sfd,
+1 −8
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2006, Daniel Stenberg, <daniel@haxx.se>, et al.
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
@@ -29,11 +29,4 @@ void Curl_easy_addmulti(struct SessionHandle *data, void *multi);

void Curl_easy_initHandleData(struct SessionHandle *data);

CURLcode Curl_convert_to_network(struct SessionHandle *data,
                                 char *buffer, size_t length);
CURLcode Curl_convert_from_network(struct SessionHandle *data,
                                 char *buffer, size_t length);
CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
                                 char *buffer, size_t length);

#endif /* __EASYIF_H */
+5 −22
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
@@ -31,10 +31,9 @@
#include <stdlib.h>
#include <string.h>
#include "curl_memory.h"
/* urldata.h and easyif.h are included for Curl_convert_... prototypes */
#include "urldata.h"
#include "easyif.h"
#include "warnless.h"
#include "non-ascii.h"

#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
@@ -91,10 +90,6 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
  int strindex=0;
  size_t length;

#ifndef CURL_DOES_CONVERSIONS
  /* avoid compiler warnings */
  (void)handle;
#endif
  ns = malloc(alloc);
  if(!ns)
    return NULL;
@@ -122,15 +117,11 @@ char *curl_easy_escape(CURL *handle, const char *string, int inlength)
        }
      }

#ifdef CURL_DOES_CONVERSIONS
/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
      if(!handle ||
          (Curl_convert_to_network(handle, &in, 1) != CURLE_OK)) {
      if(Curl_convert_to_network(handle, &in, 1)) {
        /* Curl_convert_to_network calls failf if unsuccessful */
        free(ns);
        return NULL;
      }
#endif /* CURL_DOES_CONVERSIONS */

      snprintf(&ns[strindex], 4, "%%%02X", in);

@@ -157,10 +148,6 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length,
  int strindex=0;
  unsigned long hex;

#ifndef CURL_DOES_CONVERSIONS
  /* avoid compiler warnings */
  (void)handle;
#endif
  if(!ns)
    return NULL;

@@ -178,15 +165,11 @@ char *curl_easy_unescape(CURL *handle, const char *string, int length,

      in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */

#ifdef CURL_DOES_CONVERSIONS
/* escape sequences are always in ASCII so convert them on non-ASCII hosts */
      if(!handle ||
          (Curl_convert_from_network(handle, &in, 1) != CURLE_OK)) {
      if(Curl_convert_from_network(handle, &in, 1)) {
        /* Curl_convert_from_network calls failf if unsuccessful */
        free(ns);
        return NULL;
      }
#endif /* CURL_DOES_CONVERSIONS */

      string+=2;
      alloc-=2;
Loading