summaryrefslogtreecommitdiffstats
path: root/Liaison/LiDataTranslator.m
diff options
context:
space:
mode:
authorBrian Cully <bjc@kublai.com>2008-04-14 21:45:08 -0400
committerBrian Cully <github.20.shmit@spamgourmet.com>2008-04-14 21:45:08 -0400
commit17349a5e426dc7acf1216a3767a22f69974cbca0 (patch)
tree20029d02f07ab6257cccec36d34fb312f796e1d1 /Liaison/LiDataTranslator.m
downloadliaison-17349a5e426dc7acf1216a3767a22f69974cbca0.tar.gz
liaison-17349a5e426dc7acf1216a3767a22f69974cbca0.zip
Initial commit.
Diffstat (limited to 'Liaison/LiDataTranslator.m')
-rw-r--r--Liaison/LiDataTranslator.m120
1 files changed, 120 insertions, 0 deletions
diff --git a/Liaison/LiDataTranslator.m b/Liaison/LiDataTranslator.m
new file mode 100644
index 0000000..4481c21
--- /dev/null
+++ b/Liaison/LiDataTranslator.m
@@ -0,0 +1,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 \ No newline at end of file