28#ifdef OO_DEBUG_MONITOR_ENABLED
36#ifdef OO_LOG_DEBUG_PROTOCOL_PACKETS
39#define LogOOTCPStreamDecoderPacket(packet) \
44struct OOTCPStreamDecoder {
46 uint32_t headerSpaceUsed;
67 decoder = malloc(
sizeof *decoder);
71 decoder->headerSpaceUsed = 0;
72 decoder->nextPacketData = NULL;
73 decoder->nextSize = 0;
74 decoder->Packet = packetCB;
75 decoder->Error = errorCB;
76 decoder->Finalize = finalizeCB;
77 decoder->cbInfo = cbInfo;
87 if (decoder->Finalize != NULL) {
88 decoder->Finalize(decoder->cbInfo);
91 if (decoder->nextPacketData != NULL) {
93 decoder->nextPacketData = NULL;
101 if (decoder == NULL || data == NULL)
109 const unsigned char* bytes = NULL;
120 if (bytes == NULL && remaining != 0) {
121 Error(decoder,
OOALSTR(
"Invalid data -- NULL bytes but %u byte count."), remaining);
125 while (remaining != 0) {
126 if (decoder->nextPacketData != NULL) {
128 bytesToAdd = remaining;
129 if (decoder->nextSize < bytesToAdd)
130 bytesToAdd = decoder->nextSize;
134 remaining -= bytesToAdd;
135 decoder->nextSize -= bytesToAdd;
138 if (decoder->nextSize == 0) {
141 PacketReady(decoder);
146 decoder->nextPacketData = NULL;
148 }
else if (decoder->headerSpaceUsed < 4) {
151 decoder->header[decoder->headerSpaceUsed++] = *bytes++;
152 }
else if (decoder->headerSpaceUsed == 4) {
154 decoder->nextSize = (decoder->header[0] << 24) | (decoder->header[1] << 16) | (decoder->header[2] << 8) | (decoder->header[3] << 0);
156 decoder->headerSpaceUsed = 0;
157 if (decoder->nextSize != 0) {
161 Error(decoder,
OOALSTR(
"OOTCPStreamDecoder internal error: reached unreachable state. nextSize = %zu, bufferUsed = %zu, nextPacketData = %@."), (
unsigned long)decoder->nextSize, (
unsigned long)decoder->headerSpaceUsed, decoder->nextPacketData);
175 if (packet == NULL) {
176 Error(decoder,
OOALSTR(
"Protocol error: packet is not property list (property list error: %@)."), errorString);
187 LogOOTCPStreamDecoderPacket(packet);
191 if (packetType == NULL) {
192 Error(decoder,
OOALSTR(
"Protocol error: packet contains no packet type."));
201 decoder->Packet(decoder->cbInfo, packetType, packet);
209 if (decoder == NULL || decoder->Error == NULL || format == NULL)
212 va_start(args, format);
216 if (
string != NULL) {
217 decoder->Error(decoder->cbInfo,
string);
static NSError * Error(OOPListSchemaVerifierErrorCode errorCode, BackLinkChain *keyPath, NSString *format,...)
OOALStringRef OOTypeDescription(OOALObjectRef object)
bool OOALIsDictionary(OOALObjectRef object)
OOALStringRef OOALStringCreateWithFormatAndArguments(OOALStringRef format, va_list args)
const struct NSAutoreleasePool * OOALAutoreleasePoolRef
void OOALMutableDataAppendBytes(OOALMutableDataRef data, const void *bytes, size_t length)
const void * OOALDataGetBytePtr(OOALDataRef data)
const struct NSString * OOALStringRef
size_t OOALDataGetLength(OOALDataRef data)
OOALObjectRef OOALDictionaryGetValue(OOALDictionaryRef dictionary, OOALObjectRef key)
#define OOALDestroyAutoreleasePool(pool)
const struct NSDictionary * OOALDictionaryRef
bool OOALIsString(OOALObjectRef object)
OOALAutoreleasePoolRef OOALCreateAutoreleasePool(void)
struct NSData * OOALMutableDataRef
OOALMutableDataRef OOALDataCreateMutable(size_t capacity)
void OOALRelease(OOALObjectRef object)
OOALObjectRef OOALPropertyListFromData(OOALMutableDataRef data, OOALStringRef *errStr)
const struct NSData * OOALDataRef
struct OOTCPStreamDecoder * OOTCPStreamDecoderRef
void(* OOTCPStreamDecoderFinalizeCallback)(void *cbInfo)
OOTCPStreamDecoderRef OOTCPStreamDecoderCreate(OOTCPStreamDecoderPacketCallback packetCB, OOTCPStreamDecoderErrorCallback errorCB, OOTCPStreamDecoderFinalizeCallback finalizeCB, void *cbInfo)
void(* OOTCPStreamDecoderPacketCallback)(void *cbInfo, OOALStringRef packetType, OOALDictionaryRef packet)
void OOTCPStreamDecoderReceiveBytes(OOTCPStreamDecoderRef decoder, const void *bytes, size_t length)
void OOTCPStreamDecoderDestroy(OOTCPStreamDecoderRef decoder)
void(* OOTCPStreamDecoderErrorCallback)(void *cbInfo, OOALStringRef errorDesc)
void OOTCPStreamDecoderReceiveData(OOTCPStreamDecoderRef decoder, OOALDataRef data)