title: Corrupted string as a result from a download of utf8 encoded xml data received by libcurl
body: <p>In a project that implements an Amazon S3 access library with the use of libcurl, I have problems with UTF8. The method for listing a bucket's contents sends the appropriate request to the S3 server, correctly signed and all. I receive a xml document, but the data is corrupted.</p>
  
  <p>I save it into a std::string.
  For example, it starts with the following fragment:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ListBucketResult
  </code></pre>
  
  <p>After the last "t" of "ListBucketResult", there is a "0" (zero) in the code, terminating the std::string. Viewing the contents of the string in the debugger or writing them into a file shows this, and many more zeros at different positions, e.g. at some (but not all) ">" closing brackets.</p>
  
  <p>I use MS Visual Studio 2008 running on WinXP, the project is compiled with unicode support.</p>
  
  <p>What should I do to receive proper UTF8 inside the std::string (which should be unicode agnostic, according to several sources)? Any hints on this one?</p>
  
  <pre><code>bool Http::Download(std::string&amp; url, std::string&amp; targetString, std::vector&lt;std::string&gt;* customHeaders)
  {
      CURLcode result = CURLE_FAILED_INIT;
      dl = true;
  
      if (curl)
      {
          curl = curl_easy_init();
  
          curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
          curl_easy_setopt(curl, CURLOPT_HEADER, 0);
          curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
          curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
          curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;targetString);
  
          if (unsafe)
          {
              curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
              curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
          }
  
          if (customHeaders)
          {
              curl_slist* headers = 0;
  
              for (std::vector&lt;std::string&gt;::const_iterator iter = customHeaders-&gt;begin(); iter != customHeaders-&gt;end(); iter++)
              {
                  headers = curl_slist_append(headers, (*iter).c_str());
                  headers = curl_slist_append(headers, "\n");
              }
  
              curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
          }
  
          result = curl_easy_perform(curl);
  
          long http_code = 0;
          curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &amp;http_code);
          lastHttpResult = static_cast&lt;int&gt;(http_code);
  
          curl_easy_cleanup(curl);
      }
  
      return (result == CURLE_OK);
  };
  
  size_t Http::WriteData(char* data, size_t size, size_t nmemb, void* target)
  {
      if(target)
      {
          reinterpret_cast&lt;std::string*&gt;(target)-&gt;append(data);
          size_t len = size * nmemb;
          return len;
      }
  
      return 0;
  };
  </code></pre>