MPD  0.20.6
OpusReader.hxx
Go to the documentation of this file.
1 /*
2  * Copyright 2003-2017 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_OPUS_READER_HXX
21 #define MPD_OPUS_READER_HXX
22 
23 #include "check.h"
24 
25 #include <algorithm>
26 
27 #include <stdint.h>
28 #include <string.h>
29 
30 class OpusReader {
31  const uint8_t *p, *const end;
32 
33 public:
34  OpusReader(const void *_p, size_t size)
35  :p((const uint8_t *)_p), end(p + size) {}
36 
37  bool Skip(size_t length) {
38  p += length;
39  return p <= end;
40  }
41 
42  const void *Read(size_t length) {
43  const uint8_t *result = p;
44  return Skip(length)
45  ? result
46  : nullptr;
47  }
48 
49  bool Expect(const void *value, size_t length) {
50  const void *data = Read(length);
51  return data != nullptr && memcmp(value, data, length) == 0;
52  }
53 
54  bool ReadByte(uint8_t &value_r) {
55  if (p >= end)
56  return false;
57 
58  value_r = *p++;
59  return true;
60  }
61 
62  bool ReadShort(uint16_t &value_r) {
63  const uint8_t *value = (const uint8_t *)Read(sizeof(value_r));
64  if (value == nullptr)
65  return false;
66 
67  value_r = value[0] | (value[1] << 8);
68  return true;
69  }
70 
71  bool ReadWord(uint32_t &value_r) {
72  const uint8_t *value = (const uint8_t *)Read(sizeof(value_r));
73  if (value == nullptr)
74  return false;
75 
76  value_r = value[0] | (value[1] << 8)
77  | (value[2] << 16) | (value[3] << 24);
78  return true;
79  }
80 
81  bool SkipString() {
82  uint32_t length;
83  return ReadWord(length) && Skip(length);
84  }
85 
86  char *ReadString() {
87  uint32_t length;
88  if (!ReadWord(length) || length >= 65536)
89  return nullptr;
90 
91  const char *src = (const char *)Read(length);
92  if (src == nullptr)
93  return nullptr;
94 
95  char *dest = new char[length + 1];
96  *std::copy_n(src, length, dest) = 0;
97  return dest;
98  }
99 };
100 
101 #endif
OpusReader(const void *_p, size_t size)
Definition: OpusReader.hxx:34
bool ReadWord(uint32_t &value_r)
Definition: OpusReader.hxx:71
bool SkipString()
Definition: OpusReader.hxx:81
bool ReadByte(uint8_t &value_r)
Definition: OpusReader.hxx:54
bool Skip(size_t length)
Definition: OpusReader.hxx:37
bool ReadShort(uint16_t &value_r)
Definition: OpusReader.hxx:62
char * ReadString()
Definition: OpusReader.hxx:86
const void * Read(size_t length)
Definition: OpusReader.hxx:42
bool Expect(const void *value, size_t length)
Definition: OpusReader.hxx:49