Преглед на файлове

Use vectors to encode / decode base64 binary data

anon преди 5 години
родител
ревизия
aedb032ab2
променени са 1 файла, в които са добавени 12 реда и са изтрити 3 реда
  1. 12 3
      daemon/src/Server.cpp

+ 12 - 3
daemon/src/Server.cpp

@@ -246,9 +246,12 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
         // upload chunks
 
         // decode base64 string
-        const std::string data = base64::decode(root["data"].asString());
+        const std::vector<char> data =
+            base64::decodeVector(root["data"].asString());
 
-        this->putFile << data;
+        // write the vector data to the output file
+        std::ostream_iterator<char> output_iterator(putFile);
+        std::copy(data.begin(), data.end(), output_iterator);
 
         // close file if put sends remaining = 0
         if (root["remaining"].asInt() == 0) {
@@ -385,9 +388,15 @@ void con_handler::handle_read_command(const boost::system::error_code &err,
               size -= read;
               int remaining = size / max_data_length +
                               (size % max_data_length == 0 ? 0 : 1);
+
+              // store binary data in vector because a string whould end with
+              // '\0'
+              std::vector<char> data;
+              data.assign(fileBuffer, fileBuffer + read);
+
               answer["remaining"] = remaining;
               answer["cancel"] = false;
-              answer["data"] = base64::encode(fileBuffer);
+              answer["data"] = base64::encodeVector(data);
 
               const std::string answerString =
                   Json::writeString(stringBuilder, answer);