~ubuntu-branches/ubuntu/lucid/curl/lucid-security

« back to all changes in this revision

Viewing changes to lib/easy.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2006-06-29 15:04:24 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20060629150424-be178abcwks1n519
Tags: upstream-7.15.4
ImportĀ upstreamĀ versionĀ 7.15.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: easy.c,v 1.73 2005/07/17 12:44:11 bagder Exp $
 
21
 * $Id: easy.c,v 1.76 2006-04-07 21:50:47 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
83
83
#include "memory.h"
84
84
#include "progress.h"
85
85
#include "easyif.h"
 
86
#include "sendf.h" /* for failf function prototype */
86
87
 
87
88
#define _MPRINTF_REPLACE /* use our functions only */
88
89
#include <curl/mprintf.h>
89
90
 
 
91
#if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV)
 
92
#include <iconv.h>
 
93
/* set default codesets for iconv */
 
94
#ifndef CURL_ICONV_CODESET_OF_NETWORK
 
95
#define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1"
 
96
#endif
 
97
#ifndef CURL_ICONV_CODESET_FOR_UTF8
 
98
#define CURL_ICONV_CODESET_FOR_UTF8   "UTF-8"
 
99
#endif
 
100
#endif /* CURL_DOES_CONVERSIONS && HAVE_ICONV */
 
101
 
90
102
/* The last #include file should be: */
91
103
#include "memdebug.h"
92
104
 
190
202
 */
191
203
CURLcode curl_global_init(long flags)
192
204
{
193
 
  if (initialized)
 
205
  if (initialized++)
194
206
    return CURLE_OK;
195
207
 
196
208
  /* Setup the default memory functions here (again) */
217
229
  idna_init();
218
230
#endif
219
231
 
220
 
  initialized = 1;
221
232
  init_flags  = flags;
222
233
 
223
234
  return CURLE_OK;
263
274
  if (!initialized)
264
275
    return;
265
276
 
 
277
  if (--initialized)
 
278
    return;
 
279
 
266
280
  Curl_global_host_cache_dtor();
267
281
 
268
282
  if (init_flags & CURL_GLOBAL_SSL)
275
289
  amiga_cleanup();
276
290
#endif
277
291
 
278
 
  initialized = 0;
279
292
  init_flags  = 0;
280
293
}
281
294
 
526
539
    memset(outcurl->state.connects, 0,
527
540
           sizeof(struct connectdata *)*outcurl->state.numconnects);
528
541
 
 
542
    outcurl->state.lastconnect = -1;
 
543
 
529
544
    outcurl->progress.flags    = data->progress.flags;
530
545
    outcurl->progress.callback = data->progress.callback;
531
546
 
653
668
  data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
654
669
#endif
655
670
}
 
671
 
 
672
#ifdef CURL_DOES_CONVERSIONS
 
673
/*
 
674
 * Curl_convert_to_network() is an internal function
 
675
 * for performing ASCII conversions on non-ASCII platforms.
 
676
 */
 
677
CURLcode Curl_convert_to_network(struct SessionHandle *data,
 
678
                                 char *buffer, size_t length)
 
679
{
 
680
  CURLcode rc;
 
681
 
 
682
  if(data->set.convtonetwork) {
 
683
    /* use translation callback */
 
684
    rc = data->set.convtonetwork(buffer, length);
 
685
    if(rc != CURLE_OK) {
 
686
      failf(data,
 
687
            "CURLOPT_CONV_TO_NETWORK_FUNCTION callback returned %i: %s",
 
688
            rc, curl_easy_strerror(rc));
 
689
    }
 
690
    return(rc);
 
691
  } else {
 
692
#ifdef HAVE_ICONV
 
693
    /* do the translation ourselves */
 
694
    char *input_ptr, *output_ptr;
 
695
    size_t in_bytes, out_bytes, rc;
 
696
 
 
697
    /* open an iconv conversion descriptor if necessary */
 
698
    if(data->outbound_cd == (iconv_t)-1) {
 
699
      data->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK,
 
700
                                     CURL_ICONV_CODESET_OF_HOST);
 
701
      if(data->outbound_cd == (iconv_t)-1) {
 
702
        failf(data,
 
703
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
 
704
               CURL_ICONV_CODESET_OF_NETWORK,
 
705
               CURL_ICONV_CODESET_OF_HOST,
 
706
               errno, strerror(errno));
 
707
        return CURLE_CONV_FAILED;
 
708
      }
 
709
    }
 
710
    /* call iconv */
 
711
    input_ptr = output_ptr = buffer;
 
712
    in_bytes = out_bytes = length;
 
713
    rc = iconv(data->outbound_cd, &input_ptr, &in_bytes,
 
714
               &output_ptr, &out_bytes);
 
715
    if ((rc == -1) || (in_bytes != 0)) {
 
716
      failf(data,
 
717
        "The Curl_convert_to_network iconv call failed with errno %i: %s",
 
718
             errno, strerror(errno));
 
719
      return CURLE_CONV_FAILED;
 
720
    }
 
721
#else
 
722
    failf(data, "CURLOPT_CONV_TO_NETWORK_FUNCTION callback required");
 
723
    return CURLE_CONV_REQD;
 
724
#endif /* HAVE_ICONV */
 
725
  }
 
726
 
 
727
  return CURLE_OK;
 
728
}
 
729
 
 
730
/*
 
731
 * Curl_convert_from_network() is an internal function
 
732
 * for performing ASCII conversions on non-ASCII platforms.
 
733
 */
 
734
CURLcode Curl_convert_from_network(struct SessionHandle *data,
 
735
                                      char *buffer, size_t length)
 
736
{
 
737
  CURLcode rc;
 
738
 
 
739
  if(data->set.convfromnetwork) {
 
740
    /* use translation callback */
 
741
    rc = data->set.convfromnetwork(buffer, length);
 
742
    if(rc != CURLE_OK) {
 
743
      failf(data,
 
744
            "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback returned %i: %s",
 
745
            rc, curl_easy_strerror(rc));
 
746
    }
 
747
    return(rc);
 
748
  } else {
 
749
#ifdef HAVE_ICONV
 
750
    /* do the translation ourselves */
 
751
    char *input_ptr, *output_ptr;
 
752
    size_t in_bytes, out_bytes, rc;
 
753
 
 
754
    /* open an iconv conversion descriptor if necessary */
 
755
    if(data->inbound_cd == (iconv_t)-1) {
 
756
      data->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
 
757
                                    CURL_ICONV_CODESET_OF_NETWORK);
 
758
      if(data->inbound_cd == (iconv_t)-1) {
 
759
        failf(data,
 
760
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
 
761
               CURL_ICONV_CODESET_OF_HOST,
 
762
               CURL_ICONV_CODESET_OF_NETWORK,
 
763
               errno, strerror(errno));
 
764
        return CURLE_CONV_FAILED;
 
765
      }
 
766
    }
 
767
    /* call iconv */
 
768
    input_ptr = output_ptr = buffer;
 
769
    in_bytes = out_bytes = length;
 
770
    rc = iconv(data->inbound_cd, &input_ptr, &in_bytes,
 
771
               &output_ptr, &out_bytes);
 
772
    if ((rc == -1) || (in_bytes != 0)) {
 
773
      failf(data,
 
774
        "The Curl_convert_from_network iconv call failed with errno %i: %s",
 
775
             errno, strerror(errno));
 
776
      return CURLE_CONV_FAILED;
 
777
    }
 
778
#else
 
779
    failf(data, "CURLOPT_CONV_FROM_NETWORK_FUNCTION callback required");
 
780
    return CURLE_CONV_REQD;
 
781
#endif /* HAVE_ICONV */
 
782
  }
 
783
 
 
784
  return CURLE_OK;
 
785
}
 
786
 
 
787
/*
 
788
 * Curl_convert_from_utf8() is an internal function
 
789
 * for performing UTF-8 conversions on non-ASCII platforms.
 
790
 */
 
791
CURLcode Curl_convert_from_utf8(struct SessionHandle *data,
 
792
                                     char *buffer, size_t length)
 
793
{
 
794
  CURLcode rc;
 
795
 
 
796
  if(data->set.convfromutf8) {
 
797
    /* use translation callback */
 
798
    rc = data->set.convfromutf8(buffer, length);
 
799
    if(rc != CURLE_OK) {
 
800
      failf(data,
 
801
            "CURLOPT_CONV_FROM_UTF8_FUNCTION callback returned %i: %s",
 
802
            rc, curl_easy_strerror(rc));
 
803
    }
 
804
    return(rc);
 
805
  } else {
 
806
#ifdef HAVE_ICONV
 
807
    /* do the translation ourselves */
 
808
    char *input_ptr, *output_ptr;
 
809
    size_t in_bytes, out_bytes, rc;
 
810
 
 
811
    /* open an iconv conversion descriptor if necessary */
 
812
    if(data->utf8_cd == (iconv_t)-1) {
 
813
      data->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST,
 
814
                                 CURL_ICONV_CODESET_FOR_UTF8);
 
815
      if(data->utf8_cd == (iconv_t)-1) {
 
816
        failf(data,
 
817
              "The iconv_open(\"%s\", \"%s\") call failed with errno %i: %s",
 
818
               CURL_ICONV_CODESET_OF_HOST,
 
819
               CURL_ICONV_CODESET_FOR_UTF8,
 
820
               errno, strerror(errno));
 
821
        return CURLE_CONV_FAILED;
 
822
      }
 
823
    }
 
824
    /* call iconv */
 
825
    input_ptr = output_ptr = buffer;
 
826
    in_bytes = out_bytes = length;
 
827
    rc = iconv(data->utf8_cd, &input_ptr, &in_bytes, &output_ptr, &out_bytes);
 
828
    if ((rc == -1) || (in_bytes != 0)) {
 
829
      failf(data,
 
830
        "The Curl_convert_from_utf8 iconv call failed with errno %i: %s",
 
831
             errno, strerror(errno));
 
832
      return CURLE_CONV_FAILED;
 
833
    }
 
834
    if (output_ptr < input_ptr) {
 
835
      /* null terminate the now shorter output string */
 
836
      *output_ptr = 0x00;
 
837
    }
 
838
#else
 
839
    failf(data, "CURLOPT_CONV_FROM_UTF8_FUNCTION callback required");
 
840
    return CURLE_CONV_REQD;
 
841
#endif /* HAVE_ICONV */
 
842
  }
 
843
 
 
844
  return CURLE_OK;
 
845
}
 
846
 
 
847
#endif /* CURL_DOES_CONVERSIONS */