blob: 6ecc97eca3ec3bb89046e81d6abeea0537e1b4d4 [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020012#include "typedefs.h" // NOLINT(build/include)
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) {
Yves Gerey665174f2018-06-19 15:03:05 +020017// The "wire format" for the size is little-endian. Assume we're running on
18// a little-endian machine.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070019#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.
mbonadei7c2c8432017-04-07 00:59:12 -070033bool ReadMessageFromFile(FILE* file, 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