blob: 3042bce29fa43aa412ae5168fc074d9ae7dd80d4 [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
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020013#include "absl/memory/memory.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020014#include "rtc_base/system/arch.h"
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070015
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020016namespace {
17// Allocates new memory in the memory owned by the unique_ptr to fit the raw
18// message and returns the number of bytes read when having a string stream as
19// input.
20size_t ReadMessageBytesFromString(std::stringstream* input,
21 std::unique_ptr<uint8_t[]>* bytes) {
22 int32_t size = 0;
23 input->read(reinterpret_cast<char*>(&size), sizeof(int32_t));
24 int32_t size_read = input->gcount();
25 if (size_read != sizeof(int32_t))
26 return 0;
27 if (size <= 0)
28 return 0;
29
30 *bytes = absl::make_unique<uint8_t[]>(size);
31 input->read(reinterpret_cast<char*>(bytes->get()),
32 size * sizeof((*bytes)[0]));
33 size_read = input->gcount();
34 return size_read == size ? size : 0;
35}
36} // namespace
37
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070038namespace webrtc {
39
kwiberg62eaacf2016-02-17 06:39:05 -080040size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) {
Yves Gerey665174f2018-06-19 15:03:05 +020041// The "wire format" for the size is little-endian. Assume we're running on
42// a little-endian machine.
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070043#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
44#error "Need to convert messsage from little-endian."
45#endif
46 int32_t size = 0;
47 if (fread(&size, sizeof(size), 1, file) != 1)
48 return 0;
49 if (size <= 0)
50 return 0;
51
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020052 *bytes = absl::make_unique<uint8_t[]>(size);
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070053 return fread(bytes->get(), sizeof((*bytes)[0]), size, file);
54}
55
56// Returns true on success, false on error or end-of-file.
mbonadei7c2c8432017-04-07 00:59:12 -070057bool ReadMessageFromFile(FILE* file, MessageLite* msg) {
kwiberg62eaacf2016-02-17 06:39:05 -080058 std::unique_ptr<uint8_t[]> bytes;
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070059 size_t size = ReadMessageBytesFromFile(file, &bytes);
60 if (!size)
61 return false;
62
63 msg->Clear();
64 return msg->ParseFromArray(bytes.get(), size);
65}
66
Sonia-Florina Horchidanb75d14c2019-08-12 09:57:01 +020067// Returns true on success, false on error or end of string stream.
68bool ReadMessageFromString(std::stringstream* input, MessageLite* msg) {
69 std::unique_ptr<uint8_t[]> bytes;
70 size_t size = ReadMessageBytesFromString(input, &bytes);
71 if (!size)
72 return false;
73
74 msg->Clear();
75 return msg->ParseFromArray(bytes.get(), size);
76}
77
Andrew MacDonaldcb05b722015-05-07 22:17:51 -070078} // namespace webrtc