blob: c18a13e6edfe306688491efe17f834169484e9d3 [file] [log] [blame]
Andrew MacDonaldcb05b722015-05-07 22:17:51 -07001/*
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"
kwiberg62eaacf2016-02-17 06:39:05 -080012#include "webrtc/typedefs.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070013
14namespace webrtc {
15
kwiberg62eaacf2016-02-17 06:39:05 -080016size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) {
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070017 // The "wire format" for the size is little-endian. Assume we're running on
18 // a little-endian machine.
19#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
20#error "Need to convert messsage from little-endian."
21#endif
22 int32_t size = 0;
23 if (fread(&size, sizeof(size), 1, file) != 1)
24 return 0;
25 if (size <= 0)
26 return 0;
27
28 bytes->reset(new uint8_t[size]);
29 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
30}
31
32// Returns true on success, false on error or end-of-file.
33bool ReadMessageFromFile(FILE* file, ::google::protobuf::MessageLite* msg) {
kwiberg62eaacf2016-02-17 06:39:05 -080034 std::unique_ptr<uint8_t[]> bytes;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070035 size_t size = ReadMessageBytesFromFile(file, &bytes);
36 if (!size)
37 return false;
38
39 msg->Clear();
40 return msg->ParseFromArray(bytes.get(), size);
41}
42
43} // namespace webrtc