2014-01-10 20:12:53 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2014, Inverse inc.
|
|
|
|
All rights reserved.
|
|
|
|
|
2014-01-13 17:46:32 +01:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the Inverse inc. nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-01-10 20:12:53 +01:00
|
|
|
|
|
|
|
*/
|
|
|
|
#import "NSData+ActiveSync.h"
|
|
|
|
|
2014-01-24 17:44:12 +01:00
|
|
|
#import <Foundation/NSProcessInfo.h>
|
2014-01-10 20:12:53 +01:00
|
|
|
#import <Foundation/NSString.h>
|
|
|
|
|
2015-05-29 20:23:01 +02:00
|
|
|
#import <SOGo/SOGoSystemDefaults.h>
|
|
|
|
|
2014-02-17 14:38:34 +01:00
|
|
|
#import <NGExtensions/NGBase64Coding.h>
|
2015-01-22 19:31:31 +01:00
|
|
|
#import <NGExtensions/NSObject+Logs.h>
|
2014-02-17 14:38:34 +01:00
|
|
|
|
2014-01-14 19:37:51 +01:00
|
|
|
#include <wbxml/wbxml.h>
|
|
|
|
#include <wbxml/wbxml_conv.h>
|
|
|
|
#include <wbxml/wbxml_errors.h>
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-02-04 21:07:23 +01:00
|
|
|
#define WBXMLDEBUG 0
|
2014-01-10 20:53:49 +01:00
|
|
|
|
2014-01-10 20:12:53 +01:00
|
|
|
@implementation NSData (ActiveSync)
|
|
|
|
|
2014-01-27 17:30:43 +01:00
|
|
|
- (void) _dumpToFile
|
|
|
|
{
|
2015-05-29 20:23:01 +02:00
|
|
|
BOOL debugOn;
|
|
|
|
|
|
|
|
debugOn = [[SOGoSystemDefaults sharedSystemDefaults] easDebugEnabled];
|
|
|
|
|
|
|
|
if (debugOn)
|
|
|
|
{
|
|
|
|
NSString *path;
|
2014-01-27 17:30:43 +01:00
|
|
|
|
2015-05-29 20:23:01 +02:00
|
|
|
path = [NSString stringWithFormat: @"/tmp/%@.data", [[NSProcessInfo processInfo] globallyUniqueString]];
|
|
|
|
[self writeToFile: path atomically: YES];
|
|
|
|
[self errorWithFormat: @"Original data written to: %@", path];
|
|
|
|
}
|
2014-01-27 17:30:43 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 14:38:34 +01:00
|
|
|
//
|
|
|
|
// Encodes the data in base64 and strip newline characters
|
|
|
|
//
|
2014-02-17 16:01:44 +01:00
|
|
|
- (NSString *) activeSyncRepresentationInContext: (WOContext *) context
|
2014-02-17 14:38:34 +01:00
|
|
|
{
|
2015-07-28 12:37:33 +02:00
|
|
|
return [[self stringByEncodingBase64] stringByReplacingString: @"\n" withString: @""];
|
2014-02-17 14:38:34 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 20:12:53 +01:00
|
|
|
- (NSData *) wbxml2xml
|
|
|
|
{
|
|
|
|
WBXMLGenXMLParams params;
|
|
|
|
NSData *data;
|
|
|
|
|
|
|
|
unsigned int wbxml_len, xml_len, ret;
|
|
|
|
unsigned char *wbxml, *xml;
|
|
|
|
|
|
|
|
wbxml = (unsigned char*)[self bytes];
|
|
|
|
wbxml_len = [self length];
|
|
|
|
xml = NULL;
|
|
|
|
xml_len = 0;
|
|
|
|
|
|
|
|
params.lang = WBXML_LANG_ACTIVESYNC;
|
|
|
|
params.gen_type = WBXML_GEN_XML_INDENT;
|
|
|
|
params.indent = 1;
|
|
|
|
params.keep_ignorable_ws = FALSE;
|
|
|
|
|
|
|
|
ret = wbxml_conv_wbxml2xml_withlen(wbxml, wbxml_len, &xml, &xml_len, ¶ms);
|
|
|
|
|
|
|
|
if (ret != WBXML_OK)
|
|
|
|
{
|
2015-01-22 19:31:31 +01:00
|
|
|
[self errorWithFormat: @"wbxml2xmlFromContent: failed: %s\n", wbxml_errors_string(ret)];
|
2014-01-27 17:30:43 +01:00
|
|
|
[self _dumpToFile];
|
2014-01-10 20:12:53 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:27:10 +01:00
|
|
|
data = [NSData dataWithBytesNoCopy: xml length: xml_len freeWhenDone: YES];
|
2014-01-10 20:53:49 +01:00
|
|
|
|
|
|
|
#if WBXMLDEBUG
|
2014-01-10 20:12:53 +01:00
|
|
|
[data writeToFile: @"/tmp/protocol.decoded" atomically: YES];
|
2014-01-10 20:53:49 +01:00
|
|
|
#endif
|
2014-01-10 20:12:53 +01:00
|
|
|
|
2014-12-04 17:27:10 +01:00
|
|
|
return data;
|
2014-01-10 20:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSData *) xml2wbxml
|
|
|
|
{
|
|
|
|
WBXMLConvXML2WBXML *conv;
|
|
|
|
NSData *data;
|
|
|
|
|
|
|
|
unsigned int wbxml_len, xml_len, ret;
|
|
|
|
unsigned char *wbxml, *xml;
|
|
|
|
|
|
|
|
xml = (unsigned char*)[self bytes];
|
|
|
|
xml_len = [self length];
|
|
|
|
wbxml = NULL;
|
|
|
|
wbxml_len = 0;
|
|
|
|
conv = NULL;
|
|
|
|
|
|
|
|
ret = wbxml_conv_xml2wbxml_create(&conv);
|
|
|
|
|
|
|
|
if (ret != WBXML_OK)
|
|
|
|
{
|
2015-01-22 19:31:31 +01:00
|
|
|
[self logWithFormat: @"xml2wbxmlFromContent: failed: %s\n", wbxml_errors_string(ret)];
|
2014-01-27 17:30:43 +01:00
|
|
|
[self _dumpToFile];
|
2014-01-10 20:12:53 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
wbxml_conv_xml2wbxml_enable_preserve_whitespaces(conv);
|
|
|
|
|
|
|
|
// From libwbxml's changelog in v0.11.0: "The public ID is set to unknown and the DTD is not included. This is required for Microsoft ActiveSync."
|
|
|
|
wbxml_conv_xml2wbxml_disable_public_id(conv);
|
|
|
|
wbxml_conv_xml2wbxml_disable_string_table(conv);
|
|
|
|
|
|
|
|
ret = wbxml_conv_xml2wbxml_run(conv, xml, xml_len, &wbxml, &wbxml_len);
|
|
|
|
|
|
|
|
if (ret != WBXML_OK)
|
|
|
|
{
|
2015-01-22 19:31:31 +01:00
|
|
|
[self errorWithFormat: @"xml2wbxmlFromContent: failed: %s\n", wbxml_errors_string(ret)];
|
2014-01-27 17:30:43 +01:00
|
|
|
[self _dumpToFile];
|
2014-01-10 20:12:53 +01:00
|
|
|
free(wbxml);
|
|
|
|
wbxml_conv_xml2wbxml_destroy(conv);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2014-12-04 17:27:10 +01:00
|
|
|
data = [NSData dataWithBytesNoCopy: wbxml length: wbxml_len freeWhenDone: YES];
|
2014-01-10 20:53:49 +01:00
|
|
|
|
|
|
|
#if WBXMLDEBUG
|
2014-01-10 20:12:53 +01:00
|
|
|
[data writeToFile: @"/tmp/protocol.encoded" atomically: YES];
|
2014-01-10 20:53:49 +01:00
|
|
|
#endif
|
2014-01-10 20:12:53 +01:00
|
|
|
|
|
|
|
wbxml_conv_xml2wbxml_destroy(conv);
|
2014-12-04 17:27:10 +01:00
|
|
|
|
|
|
|
return data;
|
2014-01-10 20:12:53 +01:00
|
|
|
}
|
|
|
|
@end
|