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 "webrtc/modules/audio_processing/test/protobuf_utils.h"
12 
13 namespace webrtc {
14 
ReadMessageBytesFromFile(FILE * file,rtc::scoped_ptr<uint8_t[]> * bytes)15 size_t ReadMessageBytesFromFile(FILE* file, rtc::scoped_ptr<uint8_t[]>* bytes) {
16   // The "wire format" for the size is little-endian. Assume we're running on
17   // a little-endian machine.
18 #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
19 #error "Need to convert messsage from little-endian."
20 #endif
21   int32_t size = 0;
22   if (fread(&size, sizeof(size), 1, file) != 1)
23     return 0;
24   if (size <= 0)
25     return 0;
26 
27   bytes->reset(new uint8_t[size]);
28   return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
29 }
30 
31 // Returns true on success, false on error or end-of-file.
ReadMessageFromFile(FILE * file,::google::protobuf::MessageLite * msg)32 bool ReadMessageFromFile(FILE* file, ::google::protobuf::MessageLite* msg) {
33   rtc::scoped_ptr<uint8_t[]> bytes;
34   size_t size = ReadMessageBytesFromFile(file, &bytes);
35   if (!size)
36     return false;
37 
38   msg->Clear();
39   return msg->ParseFromArray(bytes.get(), size);
40 }
41 
42 }  // namespace webrtc
43