blob: f3c97eef7f343b97b8b46604d16178d04813700f [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/test/protobuf_utils.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Niels Möllera12c42a2018-07-25 16:05:48 +020013#include "rtc_base/system/arch.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070014
15namespace webrtc {
16
kwiberg62eaacf2016-02-17 06:39:05 -080017size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) {
Yves Gerey665174f2018-06-19 15:03:05 +020018// The "wire format" for the size is little-endian. Assume we're running on
19// a little-endian machine.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070020#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
21#error "Need to convert messsage from little-endian."
22#endif
23 int32_t size = 0;
24 if (fread(&size, sizeof(size), 1, file) != 1)
25 return 0;
26 if (size <= 0)
27 return 0;
28
29 bytes->reset(new uint8_t[size]);
30 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
31}
32
33// Returns true on success, false on error or end-of-file.
mbonadei7c2c8432017-04-07 00:59:12 -070034bool ReadMessageFromFile(FILE* file, MessageLite* msg) {
kwiberg62eaacf2016-02-17 06:39:05 -080035 std::unique_ptr<uint8_t[]> bytes;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070036 size_t size = ReadMessageBytesFromFile(file, &bytes);
37 if (!size)
38 return false;
39
40 msg->Clear();
41 return msg->ParseFromArray(bytes.get(), size);
42}
43
44} // namespace webrtc