blob: 75574961b0dfcb5573453702c050cdff648e0d75 [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
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
14
Niels Möllera12c42a2018-07-25 16:05:48 +020015#include "rtc_base/system/arch.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070016
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020017namespace {
18// Allocates new memory in the memory owned by the unique_ptr to fit the raw
19// message and returns the number of bytes read when having a string stream as
20// input.
21size_t ReadMessageBytesFromString(std::stringstream* input,
22 std::unique_ptr<uint8_t[]>* bytes) {
23 int32_t size = 0;
24 input->read(reinterpret_cast<char*>(&size), sizeof(int32_t));
25 int32_t size_read = input->gcount();
26 if (size_read != sizeof(int32_t))
27 return 0;
28 if (size <= 0)
29 return 0;
30
Mirko Bonadei317a1f02019-09-17 17:06:18 +020031 *bytes = std::make_unique<uint8_t[]>(size);
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020032 input->read(reinterpret_cast<char*>(bytes->get()),
33 size * sizeof((*bytes)[0]));
34 size_read = input->gcount();
35 return size_read == size ? size : 0;
36}
37} // namespace
38
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070039namespace webrtc {
40
kwiberg62eaacf2016-02-17 06:39:05 -080041size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) {
Yves Gerey665174f2018-06-19 15:03:05 +020042// The "wire format" for the size is little-endian. Assume we're running on
43// a little-endian machine.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070044#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
45#error "Need to convert messsage from little-endian."
46#endif
47 int32_t size = 0;
48 if (fread(&size, sizeof(size), 1, file) != 1)
49 return 0;
50 if (size <= 0)
51 return 0;
52
Mirko Bonadei317a1f02019-09-17 17:06:18 +020053 *bytes = std::make_unique<uint8_t[]>(size);
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070054 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
55}
56
57// Returns true on success, false on error or end-of-file.
mbonadei7c2c8432017-04-07 00:59:12 -070058bool ReadMessageFromFile(FILE* file, MessageLite* msg) {
kwiberg62eaacf2016-02-17 06:39:05 -080059 std::unique_ptr<uint8_t[]> bytes;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070060 size_t size = ReadMessageBytesFromFile(file, &bytes);
61 if (!size)
62 return false;
63
64 msg->Clear();
65 return msg->ParseFromArray(bytes.get(), size);
66}
67
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020068// Returns true on success, false on error or end of string stream.
69bool ReadMessageFromString(std::stringstream* input, MessageLite* msg) {
70 std::unique_ptr<uint8_t[]> bytes;
71 size_t size = ReadMessageBytesFromString(input, &bytes);
72 if (!size)
73 return false;
74
75 msg->Clear();
76 return msg->ParseFromArray(bytes.get(), size);
77}
78
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070079} // namespace webrtc