title: base64 encoding removing carriage return from dos header
body: <p>I have been trying to encode the binary data of an application as base64 (specifically boosts base64), but I have run into an issue where the carriage return after the dos header is not being encoded correctly.</p>
  <p>it should look like this:</p>
  <pre><code>This program cannot be run in DOS mode.[CR]
  [CR][LF]
  </code></pre>
  <p>but instead its outputting like this:</p>
  <pre><code>This program cannot be run in DOS mode.[CR][LF]
  </code></pre>
  <p>it seems this first carriage return is being skipped, which then causes the DOS header to be invalid when attempting to run the program.</p>
  <p>the code for the base64 algorithm I am using can be found at: <a href="https://www.boost.org/doc/libs/1_66_0/boost/beast/core/detail/base64.hpp" rel="nofollow noreferrer">https://www.boost.org/doc/libs/1_66_0/boost/beast/core/detail/base64.hpp</a></p>
  <p>Thanks so much!</p>
  <pre><code>void load_file(const char* filename, char** file_out, size_t&amp; size_out)
  {
          FILE* file;
          fopen_s(&amp;file, filename, &quot;r&quot;);
          if (!file)
              return false;
  
          fseek(file, 0, SEEK_END);
          size = ftell(file);
          rewind(file);
  
          *out = new char[size];
          fread(*out, size, 1, file);
          fclose(file);
  }
  
  void some_func()
  {
      char* file_in;
      size_t file_in_size;
      load_file(&quot;filename.bin&quot;, &amp;file_in, file_in_size);
      auto encoded_size = base64::encoded_size(file_in_size);
      auto file_encoded = new char[encoded_size];
      memset(0, file_encoded, encoded_size);
      base64::encode(file_encoded, file_in, file_in_size);
      std::ofstream orig(&quot;orig.bin&quot;, std::ios_base::binary);
      for (int i = 0; i &lt; file_in_size; i++)
      {
          auto c = file_in[i];
          orig &lt;&lt; c; // DOS header contains a NULL as the 3rd char, don't allow it to be null terminated early, may cause ending nulls but does not affect binary files.
      }
      orig.close();
      std::ofstream encoded(&quot;encoded.txt&quot;); //pass this output through a base64 to file website.
      encoded &lt;&lt; file_encoded; // for loop not required, does not contain nulls (besides ending null) will contain trailing encoded nulls.
     encoded.close();
     auto decoded_size = base64::decoded_size(encoded_size);
     auto file_decoded = new char[decoded_size];
     memset(0, file_decoded, decoded_size); // again trailing nulls but it doesn't matter for binary file operation. just wasted disk space.
     base64::decode(file_decoded, file_encoded, encoded_size);
     std::ofstream decoded(&quot;decoded.bin&quot;, std::ios_base::binary);
     for (int i = 0; i &lt; decoded_size; i++)
     {
          auto c = file_decoded[i];
          decoded &lt;&lt; c;
     }
     decoded.close();
     free(file_in);
     free(file_encoded);
     free(file_decoded);
  }
  </code></pre>
  <p>The above code will show that the file reading does not remove the carriage return, while the encoding of the file into base64 does.</p>