1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#include <sys/types.h>
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
class MyOSCInputStream
{
public:
/** Creates an MyOSCInputStream.
@param sourceData the block of data to use as the stream's source
@param sourceDataSize the number of bytes in the source data block
*/
MyOSCInputStream (const void* sourceData, size_t sourceDataSize) :
input (sourceData, sourceDataSize, false)
{
}
//==============================================================================
/** Returns a pointer to the source data block from which this stream is reading. */
const void* getData() const noexcept { return input.getData(); }
/** Returns the number of bytes of source data in the block from which this stream is reading. */
size_t getDataSize() const noexcept { return input.getDataSize(); }
/** Returns the current position of the stream. */
juce::uint64 getPosition() { return (juce::uint64) input.getPosition(); }
/** Attempts to set the current position of the stream. Returns true if this was successful. */
bool setPosition (juce::int64 pos) { return input.setPosition (pos); }
/** Returns the total amount of data in bytes accessible by this stream. */
juce::int64 getTotalLength() { return input.getTotalLength(); }
/** Returns true if the stream has no more data to read. */
bool isExhausted() { return input.isExhausted(); }
//==============================================================================
juce::int32 readInt32()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading int32");
return input.readIntBigEndian();
}
juce::uint64 readUint64()
{
checkBytesAvailable (8, "OSC input stream exhausted while reading uint64");
return (juce::uint64) input.readInt64BigEndian();
}
float readFloat32()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading float");
return input.readFloatBigEndian();
}
juce::String readString()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading string");
auto posBegin = (size_t) getPosition();
auto s = input.readString();
auto posEnd = (size_t) getPosition();
if (static_cast<const char*> (getData())[posEnd - 1] != '\0')
throw juce::OSCFormatError (
"OSC input stream exhausted before finding null terminator of string");
size_t bytesRead = posEnd - posBegin;
readPaddingZeros (bytesRead);
return s;
}
juce::MemoryBlock readBlob()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading blob");
auto blobDataSize = input.readIntBigEndian();
checkBytesAvailable ((blobDataSize + 3) % 4,
"OSC input stream exhausted before reaching end of blob");
juce::MemoryBlock blob;
auto bytesRead = input.readIntoMemoryBlock (blob, (ssize_t) blobDataSize);
readPaddingZeros (bytesRead);
return blob;
}
juce::OSCColour readColour()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading colour");
return juce::OSCColour::fromInt32 ((juce::uint32) input.readIntBigEndian());
}
juce::OSCTimeTag readTimeTag()
{
checkBytesAvailable (8, "OSC input stream exhausted while reading time tag");
return juce::OSCTimeTag (juce::uint64 (input.readInt64BigEndian()));
}
juce::OSCAddress readAddress() { return juce::OSCAddress (readString()); }
juce::OSCAddressPattern readAddressPattern() { return juce::OSCAddressPattern (readString()); }
//==============================================================================
juce::OSCTypeList readTypeTagString()
{
juce::OSCTypeList typeList;
checkBytesAvailable (4, "OSC input stream exhausted while reading type tag string");
if (input.readByte() != ',')
throw juce::OSCFormatError ("OSC input stream format error: expected type tag string");
for (;;)
{
if (isExhausted())
throw juce::OSCFormatError (
"OSC input stream exhausted while reading type tag string");
const juce::OSCType type = input.readByte();
if (type == 0)
break; // encountered null terminator. list is complete.
if (! juce::OSCTypes::isSupportedType (type))
throw juce::OSCFormatError (
"OSC input stream format error: encountered unsupported type tag");
typeList.add (type);
}
auto bytesRead = (size_t) typeList.size() + 2;
readPaddingZeros (bytesRead);
return typeList;
}
//==============================================================================
juce::OSCArgument readArgument (juce::OSCType type)
{
switch (type)
{
case 'i':
return juce::OSCArgument (readInt32());
case 'f':
return juce::OSCArgument (readFloat32());
case 's':
return juce::OSCArgument (readString());
case 'b':
return juce::OSCArgument (readBlob());
case 'r':
return juce::OSCArgument (readColour());
default:
// You supplied an invalid OSCType when calling readArgument! This should never happen.
jassertfalse;
throw juce::OSCInternalError (
"OSC input stream: internal error while reading message argument");
}
}
//==============================================================================
juce::OSCMessage readMessage()
{
auto ap = readAddressPattern();
auto types = readTypeTagString();
juce::OSCMessage msg (ap);
for (auto& type : types)
msg.addArgument (readArgument (type));
return msg;
}
//==============================================================================
juce::OSCBundle readBundle (size_t maxBytesToRead = std::numeric_limits<size_t>::max())
{
// maxBytesToRead is only passed in here in case this bundle is a nested
// bundle, so we know when to consider the next element *not* part of this
// bundle anymore (but part of the outer bundle) and return.
checkBytesAvailable (16, "OSC input stream exhausted while reading bundle");
if (readString() != "#bundle")
throw juce::OSCFormatError (
"OSC input stream format error: bundle does not start with string '#bundle'");
juce::OSCBundle bundle (readTimeTag());
size_t bytesRead = 16; // already read "#bundle" and timeTag
auto pos = getPosition();
while (! isExhausted() && bytesRead < maxBytesToRead)
{
bundle.addElement (readElement());
auto newPos = getPosition();
bytesRead += (size_t) (newPos - pos);
pos = newPos;
}
return bundle;
}
//==============================================================================
juce::OSCBundle::Element readElement()
{
checkBytesAvailable (4, "OSC input stream exhausted while reading bundle element size");
auto elementSize = (size_t) readInt32();
if (elementSize < 4)
throw juce::OSCFormatError (
"OSC input stream format error: invalid bundle element size");
return readElementWithKnownSize (elementSize);
}
//==============================================================================
juce::OSCBundle::Element readElementWithKnownSize (size_t elementSize)
{
checkBytesAvailable ((juce::int64) elementSize,
"OSC input stream exhausted while reading bundle element content");
auto firstContentChar = static_cast<const char*> (getData())[getPosition()];
if (firstContentChar == '/')
return juce::OSCBundle::Element (readMessageWithCheckedSize (elementSize));
if (firstContentChar == '#')
return juce::OSCBundle::Element (readBundleWithCheckedSize (elementSize));
throw juce::OSCFormatError ("OSC input stream: invalid bundle element content");
}
private:
juce::MemoryInputStream input;
//==============================================================================
void readPaddingZeros (size_t bytesRead)
{
size_t numZeros = ~(bytesRead - 1) & 0x03;
while (numZeros > 0)
{
if (isExhausted() || input.readByte() != 0)
throw juce::OSCFormatError ("OSC input stream format error: missing padding zeros");
--numZeros;
}
}
juce::OSCBundle readBundleWithCheckedSize (size_t size)
{
auto begin = (size_t) getPosition();
auto maxBytesToRead = size - 4; // we've already read 4 bytes (the bundle size)
juce::OSCBundle bundle (readBundle (maxBytesToRead));
if (getPosition() - begin != size)
throw juce::OSCFormatError (
"OSC input stream format error: wrong element content size encountered while reading");
return bundle;
}
juce::OSCMessage readMessageWithCheckedSize (size_t size)
{
auto begin = (size_t) getPosition();
auto message = readMessage();
if (getPosition() - begin != size)
throw juce::OSCFormatError (
"OSC input stream format error: wrong element content size encountered while reading");
return message;
}
void checkBytesAvailable (juce::int64 requiredBytes, const char* message)
{
if (input.getNumBytesRemaining() < requiredBytes)
throw juce::OSCFormatError (message);
}
};
|