summaryrefslogtreecommitdiffstats
path: root/Liaison/LiDataTranslator.m
blob: 4481c2150cdf607b1fa05b9e6b709b51ee494a60 (plain)
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
//
//  LiDataTranslator.m
//  Liaison
//
//  Created by Brian Cully on Thu Sep 25 2003.
//  Copyright (c) 2003 Brian Cully. All rights reserved.
//

#import "LiDataTranslator.h"

@implementation LiDataTranslator
static LiDataTranslator *sharedTranslator = nil;
+ (LiDataTranslator *)sharedTranslator
{
    if (sharedTranslator == nil)
        sharedTranslator = [[self alloc] init];
    return sharedTranslator;
}

- (NSData *)decodeData: (NSData *)someData
{
    NSData *myData;
    NSRange headerRange;
    unsigned long encoding, length;
    unsigned dataLen;

    myData = nil;
    headerRange.location = 0;
    headerRange.length = sizeof(encoding) + sizeof(length);
    dataLen = [someData length];
    if (dataLen > headerRange.length) {

        dataLen -= headerRange.length;
        [someData getBytes: &encoding range: NSMakeRange(0, sizeof(encoding))];
        [someData getBytes: &length range: NSMakeRange(sizeof(encoding), sizeof(length))];
        encoding = ntohl(encoding);
        length = ntohl(length);

        if (dataLen >= length) {
            myData = [someData subdataWithRange: NSMakeRange(headerRange.length, length)];
        }
    }
    
    return myData;
}

- (NSData *)encodeData: (NSData *)someData
{
    NSMutableData *myData;

    myData = nil;
    if (someData != nil) {
        unsigned long encoding, length;

        myData = [NSMutableData data];
        encoding = htonl(LiPlainEncoding);
        length = htonl([someData length]);

        [myData appendBytes: &encoding length: sizeof(encoding)];
        [myData appendBytes: &length length: sizeof(length)];
        [myData appendData: someData];
    }
    return myData;
}
@end

@implementation NSData (LiDataTranslator)
- (NSData *)decodedData
{
    return [[LiDataTranslator sharedTranslator] decodeData: self];
}

- (NSData *)encodedData
{
    return [[LiDataTranslator sharedTranslator] encodeData: self];
}
@end

@implementation NSDictionary (LiDataTranslator)
+ (NSDictionary *)dictionaryWithEncodedData: (NSData *)someData
{
    NSData *myData;
    NSDictionary *msg;

    msg = nil;
    myData = [someData decodedData];
    if (myData != nil) {
        NSString *errorString;

        errorString = nil;
        msg = [NSPropertyListSerialization propertyListFromData: myData
                                               mutabilityOption: NSPropertyListImmutable
                                                         format: NULL
                                               errorDescription: &errorString];
        if (errorString != nil || [msg isKindOfClass: [NSDictionary class]] == NO) {
            [msg release];
            [errorString release];
            return nil;
        }
    }
    return msg;
}

- (NSData *)encodedData
{
    NSData *myData;
    NSString *errorString;

    errorString = nil;
    myData = [NSPropertyListSerialization dataFromPropertyList: self
                                                        format: NSPropertyListBinaryFormat_v1_0
                                              errorDescription: &errorString];
    if (errorString != nil) {
        [LiLog logAsError: @"Couldn't serialize dictionary: %@.", errorString];
        [errorString release];
        return nil;
    }
    return [myData encodedData];
}
@end