tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

protobuf_utils.cc (2618B)


      1 /*
      2 *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "modules/audio_processing/test/protobuf_utils.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 #include <cstdio>
     16 #include <memory>
     17 #include <sstream>  // no-presubmit-check TODO(webrtc:8982)
     18 
     19 #include "rtc_base/protobuf_utils.h"
     20 #include "rtc_base/system/arch.h"
     21 
     22 namespace {
     23 // Allocates new memory in the memory owned by the unique_ptr to fit the raw
     24 // message and returns the number of bytes read when having a string stream as
     25 // input.
     26 size_t ReadMessageBytesFromString(std::stringstream* input,
     27                                  std::unique_ptr<uint8_t[]>* bytes) {
     28  int32_t size = 0;
     29  input->read(reinterpret_cast<char*>(&size), sizeof(int32_t));
     30  int32_t size_read = input->gcount();
     31  if (size_read != sizeof(int32_t))
     32    return 0;
     33  if (size <= 0)
     34    return 0;
     35 
     36  *bytes = std::make_unique<uint8_t[]>(size);
     37  input->read(reinterpret_cast<char*>(bytes->get()),
     38              size * sizeof((*bytes)[0]));
     39  size_read = input->gcount();
     40  return size_read == size ? size : 0;
     41 }
     42 }  // namespace
     43 
     44 namespace webrtc {
     45 
     46 size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) {
     47 // The "wire format" for the size is little-endian. Assume we're running on
     48 // a little-endian machine.
     49 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
     50 #error "Need to convert messsage from little-endian."
     51 #endif
     52  int32_t size = 0;
     53  if (fread(&size, sizeof(size), 1, file) != 1)
     54    return 0;
     55  if (size <= 0)
     56    return 0;
     57 
     58  *bytes = std::make_unique<uint8_t[]>(size);
     59  return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
     60 }
     61 
     62 // Returns true on success, false on error or end-of-file.
     63 bool ReadMessageFromFile(FILE* file, MessageLite* msg) {
     64  std::unique_ptr<uint8_t[]> bytes;
     65  size_t size = ReadMessageBytesFromFile(file, &bytes);
     66  if (!size)
     67    return false;
     68 
     69  msg->Clear();
     70  return msg->ParseFromArray(bytes.get(), size);
     71 }
     72 
     73 // Returns true on success, false on error or end of string stream.
     74 bool ReadMessageFromString(std::stringstream* input, MessageLite* msg) {
     75  std::unique_ptr<uint8_t[]> bytes;
     76  size_t size = ReadMessageBytesFromString(input, &bytes);
     77  if (!size)
     78    return false;
     79 
     80  msg->Clear();
     81  return msg->ParseFromArray(bytes.get(), size);
     82 }
     83 
     84 }  // namespace webrtc