Skip to content

Commit 7d0bf2a

Browse files
author
Chris Hulbert
committed
Initial commit
0 parents  commit 7d0bf2a

25 files changed

+4008
-0
lines changed

MessagePack.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2011 Media Innovations
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// MessagePack.h
16+
// This file is to be your main include point for access to this framework
17+
#import "NSData+MessagePack.h"

MessagePackParser.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2011 Media Innovations
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// MessagePackParser.h
16+
// Created by Chris Hulbert on 23/06/11.
17+
18+
#import <Foundation/Foundation.h>
19+
20+
@interface MessagePackParser : NSObject
21+
22+
+ (id)parseData:(NSData*)data;
23+
24+
@end

MessagePackParser.m

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2011 Media Innovations
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// MessagePackParser.m
16+
// Created by Sugendran Ganess and Chris Hulbert
17+
18+
#import "MessagePackParser.h"
19+
#include "msgpack_src/msgpack.h"
20+
21+
@implementation MessagePackParser
22+
23+
// This function returns a parsed object that you have the responsibility to release/autorelease (see 'create rule' in apple docs)
24+
+(id) createUnpackedObject:(msgpack_object)obj {
25+
switch (obj.type) {
26+
case MSGPACK_OBJECT_BOOLEAN:
27+
return [[NSNumber alloc] initWithBool:obj.via.boolean];
28+
break;
29+
case MSGPACK_OBJECT_POSITIVE_INTEGER:
30+
return [[NSNumber alloc] initWithUnsignedLongLong:obj.via.u64];
31+
break;
32+
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
33+
return [[NSNumber alloc] initWithLongLong:obj.via.i64];
34+
break;
35+
case MSGPACK_OBJECT_DOUBLE:
36+
return [[NSNumber alloc] initWithDouble:obj.via.dec];
37+
break;
38+
case MSGPACK_OBJECT_RAW:
39+
return [[NSString alloc] initWithBytes:obj.via.raw.ptr length:obj.via.raw.size encoding:NSUTF8StringEncoding];
40+
break;
41+
case MSGPACK_OBJECT_ARRAY:
42+
{
43+
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:obj.via.array.size];
44+
msgpack_object* const pend = obj.via.array.ptr + obj.via.array.size;
45+
for(msgpack_object *p= obj.via.array.ptr;p < pend;p++){
46+
id newArrayItem = [self createUnpackedObject:*p];
47+
[arr addObject:newArrayItem];
48+
[newArrayItem release];
49+
}
50+
return arr;
51+
}
52+
break;
53+
case MSGPACK_OBJECT_MAP:
54+
{
55+
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:obj.via.map.size];
56+
msgpack_object_kv* const pend = obj.via.map.ptr + obj.via.map.size;
57+
for(msgpack_object_kv* p = obj.via.map.ptr; p < pend; p++){
58+
id key = [self createUnpackedObject:p->key];
59+
id val = [self createUnpackedObject:p->val];
60+
[dict setValue:val forKey:key];
61+
[key release];
62+
[val release];
63+
}
64+
return dict;
65+
}
66+
break;
67+
case MSGPACK_OBJECT_NIL:
68+
default:
69+
return [NSNull null]; // Since nsnull is a system singleton, we don't have to worry about ownership of it
70+
break;
71+
}
72+
}
73+
74+
// Parse the given messagepack data into a NSDictionary or NSArray typically
75+
+ (id)parseData:(NSData*)data {
76+
msgpack_unpacked msg;
77+
msgpack_unpacked_init(&msg);
78+
bool success = msgpack_unpack_next(&msg, data.bytes, data.length, NULL); // Parse it into C-land
79+
id results = success ? [self createUnpackedObject:msg.data] : nil; // Convert from C-land to Obj-c-land
80+
msgpack_unpacked_destroy(&msg); // Free the parser
81+
return [results autorelease];
82+
}
83+
84+
@end

NSData+MessagePack.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2011 Media Innovations
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// NSData+MessagePack.h
16+
// Created by Chris Hulbert on 23/06/11.
17+
18+
#import <Foundation/Foundation.h>
19+
20+
// Adds MessagePack parsing to NSData
21+
@interface NSData (NSData_MessagePack)
22+
23+
// Parses the receiver's data into a message pack array or dictionary
24+
- (id)messagePackParse;
25+
26+
@end

NSData+MessagePack.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2011 Media Innovations
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// NSData+MessagePack.m
16+
// Created by Chris Hulbert on 23/06/11.
17+
18+
#import "NSData+MessagePack.h"
19+
#import "MessagePackParser.h"
20+
21+
@implementation NSData (NSData_MessagePack)
22+
23+
-(id)messagePackParse {
24+
return [MessagePackParser parseData:self];
25+
}
26+
27+
@end

msgpack_src/msgpack.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* MessagePack for C
3+
*
4+
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
/**
19+
* @defgroup msgpack MessagePack C
20+
* @{
21+
* @}
22+
*/
23+
#include "msgpack/object.h"
24+
#include "msgpack/zone.h"
25+
#include "msgpack/pack.h"
26+
#include "msgpack/unpack.h"
27+
#include "msgpack/sbuffer.h"
28+
#include "msgpack/vrefbuffer.h"
29+
#include "msgpack/version.h"
30+

msgpack_src/msgpack/object.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* MessagePack for C dynamic typing routine
3+
*
4+
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#ifndef MSGPACK_OBJECT_H__
19+
#define MSGPACK_OBJECT_H__
20+
21+
#include "zone.h"
22+
#include <stdio.h>
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
29+
/**
30+
* @defgroup msgpack_object Dynamically typed object
31+
* @ingroup msgpack
32+
* @{
33+
*/
34+
35+
typedef enum {
36+
MSGPACK_OBJECT_NIL = 0x00,
37+
MSGPACK_OBJECT_BOOLEAN = 0x01,
38+
MSGPACK_OBJECT_POSITIVE_INTEGER = 0x02,
39+
MSGPACK_OBJECT_NEGATIVE_INTEGER = 0x03,
40+
MSGPACK_OBJECT_DOUBLE = 0x04,
41+
MSGPACK_OBJECT_RAW = 0x05,
42+
MSGPACK_OBJECT_ARRAY = 0x06,
43+
MSGPACK_OBJECT_MAP = 0x07,
44+
} msgpack_object_type;
45+
46+
47+
struct msgpack_object;
48+
struct msgpack_object_kv;
49+
50+
typedef struct {
51+
uint32_t size;
52+
struct msgpack_object* ptr;
53+
} msgpack_object_array;
54+
55+
typedef struct {
56+
uint32_t size;
57+
struct msgpack_object_kv* ptr;
58+
} msgpack_object_map;
59+
60+
typedef struct {
61+
uint32_t size;
62+
const char* ptr;
63+
} msgpack_object_raw;
64+
65+
typedef union {
66+
bool boolean;
67+
uint64_t u64;
68+
int64_t i64;
69+
double dec;
70+
msgpack_object_array array;
71+
msgpack_object_map map;
72+
msgpack_object_raw raw;
73+
} msgpack_object_union;
74+
75+
typedef struct msgpack_object {
76+
msgpack_object_type type;
77+
msgpack_object_union via;
78+
} msgpack_object;
79+
80+
typedef struct msgpack_object_kv {
81+
msgpack_object key;
82+
msgpack_object val;
83+
} msgpack_object_kv;
84+
85+
86+
void msgpack_object_print(FILE* out, msgpack_object o);
87+
88+
bool msgpack_object_equal(const msgpack_object x, const msgpack_object y);
89+
90+
/** @} */
91+
92+
93+
#ifdef __cplusplus
94+
}
95+
#endif
96+
97+
#endif /* msgpack/object.h */
98+

0 commit comments

Comments
 (0)