forked from ic005k/OCAuxiliaryTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlist.cpp
1209 lines (1013 loc) · 35.6 KB
/
Plist.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PlistCpp Property List (plist) serialization and parsing library.
//
// https://github.com/animetrics/PlistCpp
//
// Copyright (c) 2011 Animetrics Inc. ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "Plist.hpp"
#include <boost/locale/encoding_utf.hpp>
#include <list>
#include <sstream>
#include "base64.hpp"
#include "pugixml.hpp"
namespace Plist {
struct PlistHelperData
{
public:
// binary helper data
std::vector<int32_t> _offsetTable;
std::vector<unsigned char> _objectTable;
int32_t _offsetByteSize;
int64_t _offsetTableOffset;
int32_t _objRefSize;
int32_t _refCount;
};
void writePlistBinary(
PlistHelperData& d,
const boost::any& message);
void writePlistXML(
pugi::xml_document& doc,
const boost::any& message);
// helper functions
// msvc <= 2005 doesn't have std::vector::data() method
template<typename T>
T* vecData(std::vector<T>& vec)
{
return (vec.size() > 0) ? &vec[0] : 0;
// if(vec.size() > 0)
// return &vec[0];
// else
// throw Error("vecData trying to get pointer to empty std::vector");
}
template<typename T>
const T* vecData(const std::vector<T>& vec)
{
return (vec.size() > 0) ? &vec[0] : 0;
// if(vec.size() > 0)
// return &vec[0];
// else
// throw Error("vecData trying to get pointer to empty std::vector");
}
// xml helper functions
template<typename T>
std::string stringFromValue(const T& value);
template<typename T>
void writeXMLSimpleNode(pugi::xml_node& node, const char* name, const boost::any& obj);
// xml parsing
dictionary_type parseDictionary(pugi::xml_node& node);
array_type parseArray(pugi::xml_node& node);
std::vector<char> base64Decode(const char* data);
void base64Encode(std::string& dataEncoded, const std::vector<char>& data);
Date parseDate(pugi::xml_node& node);
boost::any parse(pugi::xml_node& doc);
// xml writing
void writeXMLArray(pugi::xml_node& node, const array_type& array);
void writeXMLDictionary(pugi::xml_node& node, const dictionary_type& message);
void writeXMLNode(pugi::xml_node& node, const boost::any& obj);
// binary helper functions
template <typename IntegerType>
IntegerType bytesToInt(const unsigned char* bytes, bool littleEndian);
double bytesToDouble(const unsigned char* bytes, bool littleEndian);
std::vector<unsigned char> doubleToBytes(double val, bool littleEndian);
template<typename IntegerType>
std::vector<unsigned char> intToBytes(IntegerType val, bool littleEndian);
std::vector<unsigned char> getRange(const unsigned char* origBytes, int64_t index, int64_t size);
std::vector<unsigned char> getRange(const std::vector<unsigned char>& origBytes, int64_t index, int64_t size);
std::vector<char> getRange(const char* origBytes, int64_t index, int64_t size);
// binary parsing
boost::any parseBinary(const PlistHelperData& d, int objRef);
dictionary_type parseBinaryDictionary(const PlistHelperData& d, int objRef);
array_type parseBinaryArray(const PlistHelperData& d, int objRef);
std::vector<int32_t> getRefsForContainers(const PlistHelperData& d, int objRef);
int64_t parseBinaryInt(const PlistHelperData& d, int headerPosition, int& intByteCount);
double parseBinaryReal(const PlistHelperData& d, int headerPosition);
Date parseBinaryDate(const PlistHelperData& d, int headerPosition);
bool parseBinaryBool(const PlistHelperData& d, int headerPosition);
std::string parseBinaryString(const PlistHelperData& d, int objRef);
std::string parseBinaryUnicode(const PlistHelperData& d, int headerPosition);
data_type parseBinaryByteArray(const PlistHelperData& d, int headerPosition);
std::vector<unsigned char> regulateNullBytes(const std::vector<unsigned char>& origBytes, unsigned int minBytes);
void parseTrailer(PlistHelperData& d, const std::vector<unsigned char>& trailer);
void parseOffsetTable(PlistHelperData& d, const std::vector<unsigned char>& offsetTableBytes);
int32_t getCount(const PlistHelperData& d, int bytePosition, unsigned char headerByte, int& startOffset);
// binary writing
int countAny(const boost::any& object);
int countDictionary(const dictionary_type& dictionary);
int countArray(const array_type& array);
std::vector<unsigned char> writeBinaryDictionary(PlistHelperData& d, const dictionary_type& dictionary);
std::vector<unsigned char> writeBinaryArray(PlistHelperData& d, const array_type& array);
std::vector<unsigned char> writeBinaryByteArray(PlistHelperData& d, const data_type& byteArray);
std::vector<unsigned char> writeBinaryInteger(PlistHelperData& d, int64_t value, bool write);
std::vector<unsigned char> writeBinaryBool(PlistHelperData& d, bool value);
std::vector<unsigned char> writeBinaryDate(PlistHelperData& d, const Date& date);
std::vector<unsigned char> writeBinaryDouble(PlistHelperData& d, double value);
std::vector<unsigned char> writeBinary(PlistHelperData& d, const boost::any& obj);
std::vector<unsigned char> writeBinaryString(PlistHelperData& d, const std::string& value, bool head);
inline bool hostLittleEndian()
{
union { uint32_t x; uint8_t c[4]; } u;
u.x = 0xab0000cd;
return u.c[0] == 0xcd;
}
} // namespace Plist
namespace Plist {
template<typename T>
void writeXMLSimpleNode(pugi::xml_node& node, const char* name, const boost::any& obj)
{
pugi::xml_node newNode;
newNode = node.append_child(name);
newNode.append_child(pugi::node_pcdata).set_value(stringFromValue(boost::any_cast<const T&>(obj)).c_str());
}
void writeXMLNode(pugi::xml_node& node, const boost::any& obj)
{
using namespace std;
const std::type_info &objType = obj.type();
if(objType == typeid(int32_t))
writeXMLSimpleNode<int32_t>(node, "integer", obj);
else if(objType == typeid(int64_t))
writeXMLSimpleNode<int64_t>(node, "integer", obj);
else if(objType == typeid(long))
writeXMLSimpleNode<long>(node, "integer", obj);
else if(objType == typeid(short))
writeXMLSimpleNode<short>(node, "integer", obj);
else if(objType == typeid(dictionary_type))
writeXMLDictionary(node, boost::any_cast<const dictionary_type&>(obj));
else if(objType == typeid(string_type))
writeXMLSimpleNode<string_type>(node, "string", obj);
else if(objType == typeid(array_type))
writeXMLArray(node, boost::any_cast<const array_type&>(obj));
else if(objType == typeid(data_type))
{
string dataEncoded;
base64Encode(dataEncoded, boost::any_cast<const data_type&>(obj));
writeXMLSimpleNode<string>(node, "data", dataEncoded);
}
else if(objType == typeid(double))
writeXMLSimpleNode<double>(node, "real", obj);
else if(objType == typeid(float))
writeXMLSimpleNode<float>(node, "real", obj);
else if(objType == typeid(Date))
writeXMLSimpleNode<string>(node, "date", boost::any_cast<const Date&>(obj).timeAsXMLConvention());
else if(objType == typeid(bool))
{
bool value = boost::any_cast<const bool&>(obj);
node.append_child(value ? "true" : "false");
}
else
throw Error((string("Plist Error: Can't serialize type ") + objType.name()).c_str());
}
void writeXMLArray(
pugi::xml_node& node,
const array_type& array)
{
using namespace std;
pugi::xml_node newNode = node.append_child("array");
for(array_type::const_iterator it = array.begin();
it != array.end();
++it)
writeXMLNode(newNode, *it);
}
void writeXMLDictionary(
pugi::xml_node& node,
const dictionary_type& message)
{
using namespace std;
pugi::xml_node newNode = node.append_child("dict");
for(dictionary_type::const_iterator it = message.begin();
it != message.end();
++it)
{
pugi::xml_node keyNode = newNode.append_child("key");
keyNode.append_child(pugi::node_pcdata).set_value(it->first.c_str());
writeXMLNode(newNode, it->second);
}
}
void writePlistXML(
pugi::xml_document& doc,
const boost::any& message)
// const dictionary_type& message)
{
// declaration node
pugi::xml_node decNode = doc.append_child(pugi::node_declaration);
decNode.append_attribute("version") = "1.0";
decNode.append_attribute("encoding") = "UTF-8";
// doctype node
doc.append_child(pugi::node_doctype).set_value("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
// root node
pugi::xml_node plistNode = doc.append_child("plist");
plistNode.append_attribute("version") = "1.0";
writeXMLNode(plistNode, message);
}
void writePlistBinary(
PlistHelperData& d,
const boost::any& message)
{
using namespace std;
//int totalRefs = countDictionary(message);
int totalRefs = countAny(message) - 1;
d._refCount = totalRefs;
d._objRefSize = regulateNullBytes(intToBytes<int32_t>(d._refCount, hostLittleEndian()), 1).size();
//writeBinaryDictionary(d, message);
writeBinary(d, message);
writeBinaryString(d, "bplist00", false);
d._offsetTableOffset = (int64_t) d._objectTable.size();
d._offsetTable.push_back(d._objectTable.size() - 8);
d._offsetByteSize = regulateNullBytes(intToBytes<int>(d._offsetTable.back(), hostLittleEndian()), 1).size();
vector<unsigned char> offsetBytes;
reverse(d._offsetTable.begin(), d._offsetTable.end());
for(unsigned int i = 0; i < d._offsetTable.size(); ++i)
{
d._offsetTable[i] = d._objectTable.size() - d._offsetTable[i];
vector<unsigned char> buffer = regulateNullBytes(intToBytes<int>(d._offsetTable[i], hostLittleEndian()), d._offsetByteSize);
//reverse(buffer.begin(), buffer.end());
offsetBytes.insert(offsetBytes.end(), buffer.rbegin(), buffer.rend());
}
d._objectTable.insert(d._objectTable.end(), offsetBytes.begin(), offsetBytes.end());
vector<unsigned char> dummy(6, 0);
d._objectTable.insert(d._objectTable.end(), dummy.begin(), dummy.end());
d._objectTable.push_back((unsigned char) (d._offsetByteSize));
d._objectTable.push_back((unsigned char) (d._objRefSize));
vector<unsigned char> temp = intToBytes<int64_t>((int64_t) totalRefs + 1, hostLittleEndian());
d._objectTable.insert(d._objectTable.end(), temp.rbegin(), temp.rend());
temp = intToBytes<int64_t>(0, hostLittleEndian());
d._objectTable.insert(d._objectTable.end(), temp.begin(), temp.end());
temp = intToBytes<int64_t>(d._offsetTableOffset, hostLittleEndian());
d._objectTable.insert(d._objectTable.end(), temp.rbegin(), temp.rend());
}
void writePlistBinary(std::vector<char>& plist, const boost::any& message)
{
PlistHelperData d;
writePlistBinary(d, message);
plist.resize(d._objectTable.size());
std::copy((const char*) vecData(d._objectTable), (const char*) vecData(d._objectTable) + d._objectTable.size(), plist.begin());
}
void writePlistBinary(
std::ostream& stream,
const boost::any& message)
{
PlistHelperData d;
writePlistBinary(d, message);
stream.write((const char*) vecData(d._objectTable), d._objectTable.size());
}
void writePlistBinary(
const char* filename,
const boost::any& message)
{
std::ofstream stream(filename, std::ios::binary);
writePlistBinary(stream, message);
stream.close();
}
#if defined(_MSC_VER)
void writePlistBinary(
const wchar_t* filename,
const boost::any& message)
{
std::ofstream stream(filename, std::ios::binary);
writePlistBinary(stream, message);
stream.close();
}
#endif
void writePlistXML(std::vector<char>& plist, const boost::any& message)
{
std::stringstream ss;
writePlistXML(ss, message);
std::istreambuf_iterator<char> beg(ss);
std::istreambuf_iterator<char> end;
plist.clear();
plist.insert(plist.begin(), beg, end);
}
void writePlistXML(
std::ostream& stream,
const boost::any& message)
{
pugi::xml_document doc;
writePlistXML(doc, message);
doc.save(stream);
}
void writePlistXML(
const char* filename,
const boost::any& message)
{
std::ofstream stream(filename, std::ios::binary);
writePlistXML(stream, message);
stream.close();
}
#if defined(_MSC_VER)
void writePlistXML(
const wchar_t* filename,
const boost::any& message)
{
std::ofstream stream(filename, std::ios::binary);
writePlistXML(stream, message);
stream.close();
}
#endif
int countAny(const boost::any& object)
{
using namespace std;
static boost::any dict = dictionary_type();
static boost::any array = array_type();
int count = 0;
if(object.type() == dict.type())
count += countDictionary(boost::any_cast<dictionary_type >(object)) + 1;
else if (object.type() == array.type())
count += countArray(boost::any_cast<array_type >(object)) + 1;
else
++count;
return count;
}
std::vector<unsigned char> writeBinary(PlistHelperData& d, const boost::any& obj)
{
using namespace std;
const std::type_info &objType = obj.type();
std::vector<unsigned char> value;
if(objType == typeid(int32_t))
value = writeBinaryInteger(d, boost::any_cast<const int32_t&>(obj), true);
else if(objType == typeid(int64_t))
value = writeBinaryInteger(d, boost::any_cast<const int64_t&>(obj), true);
else if(objType == typeid(long))
value = writeBinaryInteger(d, boost::any_cast<const long&>(obj), true);
else if(objType == typeid(short))
value = writeBinaryInteger(d, boost::any_cast<const short&>(obj), true);
else if(objType == typeid(dictionary_type))
value = writeBinaryDictionary(d, boost::any_cast<const dictionary_type& >(obj));
else if(objType == typeid(string))
value = writeBinaryString(d, boost::any_cast<const string&>(obj), true);
else if(objType == typeid(array_type))
value = writeBinaryArray(d, boost::any_cast<const array_type& >(obj));
else if(objType == typeid(data_type))
value = writeBinaryByteArray(d, boost::any_cast<const data_type& >(obj));
else if(objType == typeid(double))
value = writeBinaryDouble(d, boost::any_cast<const double&>(obj));
else if(objType == typeid(float))
value = writeBinaryDouble(d, boost::any_cast<const float&>(obj));
else if(objType == typeid(Date))
value = writeBinaryDate(d, boost::any_cast<const Date&>(obj));
else if(objType == typeid(bool))
value = writeBinaryBool(d, boost::any_cast<const bool&>(obj));
else
throw Error((string("Plist Error: Can't serialize type ") + objType.name()).c_str());
return value;
}
static uint32_t nextpow2(uint32_t x)
{
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
static uint32_t ilog2(uint32_t x)
{
uint32_t r = 0;
while (x >>= 1)
++r;
return r;
}
std::vector<unsigned char> writeBinaryByteArray(PlistHelperData& d, const data_type& byteArray)
{
using namespace std;
vector<unsigned char> header;
if(byteArray.size() < 15)
header.push_back(0x40 | ((unsigned char) byteArray.size()));
else
{
header.push_back(0x40 | 0xf);
vector<unsigned char> theSize = writeBinaryInteger(d, byteArray.size(), false);
header.insert(header.end(), theSize.begin(), theSize.end());
}
vector<unsigned char> buffer(header);
buffer.insert(buffer.end(), (unsigned char*) vecData(byteArray), (unsigned char*) vecData(byteArray) + byteArray.size());
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryArray(PlistHelperData& d, const array_type& array)
{
using namespace std;
vector<int32_t> refs;
for(array_type::const_reverse_iterator it = array.rbegin();
it != array.rend();
++it)
{
writeBinary(d, *it);
d._offsetTable.push_back(d._objectTable.size());
refs.push_back(d._refCount);
d._refCount--;
}
vector<unsigned char> header;
if (array.size() < 15)
{
header.push_back(0xA0 | ((unsigned char) array.size()));
}
else
{
header.push_back(0xA0 | 0xf);
vector<unsigned char> theSize = writeBinaryInteger(d, array.size(), false);
header.insert(header.end(), theSize.begin(), theSize.end());
}
// try to do this more efficiently. Not good to insert at the begining of buffer.
vector<unsigned char> buffer;
for(vector<int32_t>::const_iterator it = refs.begin();
it != refs.end();
++it)
{
vector<unsigned char> refBuffer = regulateNullBytes(intToBytes<int32_t>(*it, hostLittleEndian()), d._objRefSize);
// reverse(refBuffer.begin(), refBuffer.end());
buffer.insert(buffer.begin(), refBuffer.rbegin(), refBuffer.rend());
}
buffer.insert(buffer.begin(), header.begin(), header.end());
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryDictionary(PlistHelperData& d, const dictionary_type& dictionary)
{
using namespace std;
vector<int32_t> refs;
for(dictionary_type::const_reverse_iterator it = dictionary.rbegin();
it != dictionary.rend();
++it)
{
writeBinary(d, it->second);
d._offsetTable.push_back(d._objectTable.size());
refs.push_back(d._refCount);
d._refCount--;
}
for(dictionary_type::const_reverse_iterator it = dictionary.rbegin();
it != dictionary.rend();
++it)
{
writeBinary(d, it->first);
d._offsetTable.push_back(d._objectTable.size());
refs.push_back(d._refCount);
d._refCount--;
}
vector<unsigned char> header;
if (dictionary.size() < 15)
{
header.push_back(0xD0 | ((unsigned char) dictionary.size()));
}
else
{
header.push_back(0xD0 | 0xf);
vector<unsigned char> theSize = writeBinaryInteger(d, dictionary.size(), false);
header.insert(header.end(), theSize.begin(), theSize.end());
}
// try to do this more efficiently. Not good to insert at the begining of buffer.
vector<unsigned char> buffer;
for(vector<int32_t>::const_iterator it = refs.begin();
it != refs.end();
++it)
{
vector<unsigned char> refBuffer = regulateNullBytes(intToBytes<int32_t>(*it, hostLittleEndian()), d._objRefSize);
// reverse(refBuffer.begin(), refBuffer.end());
buffer.insert(buffer.begin(), refBuffer.rbegin(), refBuffer.rend());
}
buffer.insert(buffer.begin(), header.begin(), header.end());
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryDouble(PlistHelperData& d, double value)
{
using namespace std;
vector<unsigned char> buffer = regulateNullBytes(doubleToBytes(value, hostLittleEndian()), 4);
buffer.resize(nextpow2(buffer.size()), 0);
unsigned char header = 0x20 | ilog2(buffer.size());
buffer.push_back(header);
reverse(buffer.begin(), buffer.end());
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryBool(PlistHelperData& d, bool value)
{
std::vector<unsigned char> buffer;
if(value)
buffer.push_back(0x09);
else
buffer.push_back(0x08);
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryDate(PlistHelperData& d, const Date& date)
{
std::vector<unsigned char> buffer;
// need to serialize as Apple epoch.
double macTime = date.timeAsAppleEpoch();
buffer = doubleToBytes(macTime, false);
buffer.insert(buffer.begin(), 0x33);
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryInteger(PlistHelperData& d, int64_t value, bool write)
{
using namespace std;
// The integer is initially forced to be 64 bit because it must be serialized
// as 8 bytes if it is negative. If it is not negative, the
// regulateNullBytes step will reduce the representation down to the min
// power base 2 bytes needed to store it.
vector<unsigned char> buffer = intToBytes<int64_t>(value, hostLittleEndian());
buffer = regulateNullBytes(intToBytes<int64_t>(value, hostLittleEndian()), 1);
buffer.resize(nextpow2(buffer.size()), 0);
unsigned char header = 0x10 | ilog2(buffer.size());
buffer.push_back(header);
reverse(buffer.begin(), buffer.end());
if(write)
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
std::vector<unsigned char> writeBinaryString(PlistHelperData& d, const std::string& value, bool head)
{
using namespace std;
vector<unsigned char> buffer;
buffer.reserve(value.size());
for(std::string::const_iterator it = value.begin();
it != value.end();
++it)
buffer.push_back((unsigned char) *it);
if(head)
{
vector<unsigned char> header;
if (value.length() < 15)
header.push_back(0x50 | ((unsigned char) value.length()));
else
{
header.push_back(0x50 | 0xf);
vector<unsigned char> theSize = writeBinaryInteger(d, buffer.size(), false);
header.insert(header.end(), theSize.begin(), theSize.end());
}
buffer.insert(buffer.begin(), header.begin(), header.end());
}
d._objectTable.insert(d._objectTable.begin(), buffer.begin(), buffer.end());
return buffer;
}
int countDictionary(const dictionary_type& dictionary)
{
using namespace std;
int count = 0;
for(dictionary_type::const_iterator it = dictionary.begin();
it != dictionary.end();
++it)
{
++count;
count += countAny(it->second);
}
return count;
}
int countArray(const array_type& array)
{
using namespace std;
int count = 0;
for(array_type::const_iterator it = array.begin();
it != array.end();
++it)
count += countAny(*it);
return count;
}
void readPlist(std::istream& stream, boost::any& message)
{
int start = stream.tellg();
stream.seekg(0, std::ifstream::end);
int size = ((int) stream.tellg()) - start;
if(size > 0)
{
stream.seekg(0, std::ifstream::beg);
std::vector<char> buffer(size);
stream.read( (char *)&buffer[0], size );
readPlist(&buffer[0], size, message);
}
else
{
throw Error("Can't read zero length data");
}
}
void readPlist(const char* byteArrayTemp, int64_t size, boost::any& message)
{
using namespace std;
const unsigned char* byteArray = (const unsigned char*) byteArrayTemp;
if (!byteArray || (size == 0))
throw Error("Plist: Empty plist data");
// infer plist type from header. If it has the bplist00 header as first 8
// bytes, then it's a binary plist. Otherwise, assume it's XML
std::string magicHeader((const char*) byteArray, 8);
if(magicHeader == "bplist00")
{
PlistHelperData d;
parseTrailer(d, getRange(byteArray, size - 32, 32));
d._objectTable = getRange(byteArray, 0, d._offsetTableOffset);
std::vector<unsigned char> offsetTableBytes = getRange(byteArray, d._offsetTableOffset, size - d._offsetTableOffset - 32);
parseOffsetTable(d, offsetTableBytes);
message = parseBinary(d, 0);
}
else
{
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_buffer(byteArray, (size_t)size);
if(!result)
throw Error((string("Plist: XML parsed with error ") + result.description()).c_str());
pugi::xml_node rootNode = doc.child("plist").first_child();
message = parse(rootNode);
}
}
dictionary_type parseDictionary(pugi::xml_node& node)
{
using namespace std;
dictionary_type dict;
for(pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
{
if(string("key") != it->name())
throw Error("Plist: XML dictionary key expected but not found");
string key(it->first_child().value());
++it;
if(it == node.end())
throw Error("Plist: XML dictionary value expected for key " + key + "but not found");
else if(string("key") == it->name())
throw Error("Plist: XML dictionary value expected for key " + key + "but found another key node");
dict[key] = parse(*it);
}
return dict;
}
array_type parseArray(pugi::xml_node& node)
{
using namespace std;
array_type array;
for(pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
array.push_back(parse(*it));
return array;
}
Date parseDate(pugi::xml_node& node)
{
Date date;
date.setTimeFromXMLConvention(node.first_child().value());
return date;
}
std::vector<char> base64Decode(const char* encodedData)
{
using namespace std;
vector<char> data;
insert_iterator<vector<char> > ii(data, data.begin());
base64<char> b64;
int state = 0;
b64.get(encodedData, encodedData + strlen(encodedData), ii, state);
return data;
}
void base64Encode(std::string& dataEncoded, const std::vector<char>& data)
{
using namespace std;
dataEncoded.clear();
insert_iterator<string> ii(dataEncoded, dataEncoded.begin());
base64<char> b64;
int state = 0;
#if defined(_WIN32) || defined(_WIN64)
b64.put(data.begin(), data.end(), ii, state , base64<>::crlf());
#else
b64.put(data.begin(), data.end(), ii, state , base64<>::lf());
#endif
}
boost::any parse(pugi::xml_node& node)
{
using namespace std;
string nodeName = node.name();
boost::any result;
if("dict" == nodeName)
result = parseDictionary(node);
else if("array" == nodeName)
result = parseArray(node);
else if("string" == nodeName)
result = string(node.first_child().value());
else if("integer" == nodeName)
result = (int64_t) atoll(node.first_child().value());
else if("real" == nodeName)
result = atof(node.first_child().value());
else if("false" == nodeName)
result = bool(false);
else if("true" == nodeName)
result = bool(true);
else if("data" == nodeName)
result = base64Decode(node.first_child().value());
else if("date" == nodeName)
result = parseDate(node);
else
throw Error(string("Plist: XML unknown node type " + nodeName));
return result;
}
void parseOffsetTable(PlistHelperData& d, const std::vector<unsigned char>& offsetTableBytes)
{
for (unsigned int i = 0; i < offsetTableBytes.size(); i += d._offsetByteSize)
{
std::vector<unsigned char> temp = getRange(offsetTableBytes, i, d._offsetByteSize);
std::reverse(temp.begin(), temp.end());
d._offsetTable.push_back(
bytesToInt<int32_t>(
vecData(regulateNullBytes(temp, 4)), hostLittleEndian()));
}
}
void parseTrailer(PlistHelperData& d, const std::vector<unsigned char>& trailer)
{
d._offsetByteSize = bytesToInt<int32_t>(vecData(regulateNullBytes(getRange(trailer, 6, 1), 4)), hostLittleEndian());
d._objRefSize = bytesToInt<int32_t>(vecData(regulateNullBytes(getRange(trailer, 7, 1), 4)), hostLittleEndian());
std::vector<unsigned char> refCountBytes = getRange(trailer, 12, 4);
// std::reverse(refCountBytes.begin(), refCountBytes.end());
d._refCount = bytesToInt<int32_t>(vecData(refCountBytes), false);
std::vector<unsigned char> offsetTableOffsetBytes = getRange(trailer, 24, 8);
// std::reverse(offsetTableOffsetBytes.begin(), offsetTableOffsetBytes.end());
d._offsetTableOffset = bytesToInt<int64_t>(vecData(offsetTableOffsetBytes), false);
}
std::vector<unsigned char> regulateNullBytes(const std::vector<unsigned char>& origBytes, unsigned int minBytes)
{
std::vector<unsigned char> bytes(origBytes);
while((bytes.back() == 0) && (bytes.size() > minBytes))
bytes.pop_back();
while(bytes.size() < minBytes)
bytes.push_back(0);
return bytes;
}
boost::any parseBinary(const PlistHelperData& d, int objRef)
{
unsigned char header = d._objectTable[d._offsetTable[objRef]];
switch (header & 0xF0)
{
case 0x00:
{
return parseBinaryBool(d, d._offsetTable[objRef]);
}
case 0x10:
{
int intByteCount;
return parseBinaryInt(d, d._offsetTable[objRef], intByteCount);
}
case 0x20:
{
return parseBinaryReal(d, d._offsetTable[objRef]);
}
case 0x30:
{
return parseBinaryDate(d, d._offsetTable[objRef]);
}
case 0x40:
{
return parseBinaryByteArray(d, d._offsetTable[objRef]);
}
case 0x50:
{
return parseBinaryString(d, d._offsetTable[objRef]);
}
case 0x60:
{
return parseBinaryUnicode(d, d._offsetTable[objRef]);
}
case 0xD0:
{
return parseBinaryDictionary(d, objRef);
}
case 0xA0:
{
return parseBinaryArray(d, objRef);
}
}
throw Error("This type is not supported");
}
std::vector<int32_t> getRefsForContainers(const PlistHelperData& d, int objRef)
{
using namespace std;
int32_t refCount = 0;
int refStartPosition;
refCount = getCount(d, d._offsetTable[objRef], d._objectTable[d._offsetTable[objRef]], refStartPosition);
refStartPosition += d._offsetTable[objRef];
vector<int32_t> refs;
int mult = 1;
if((((unsigned char) d._objectTable[d._offsetTable[objRef]]) & 0xF0) == 0xD0)
mult = 2;
for (int i = refStartPosition; i < refStartPosition + refCount * mult * d._objRefSize; i += d._objRefSize)
{
std::vector<unsigned char> refBuffer = getRange(d._objectTable, i, d._objRefSize);
reverse(refBuffer.begin(), refBuffer.end());
refs.push_back(bytesToInt<int32_t>(vecData(regulateNullBytes(refBuffer, 4)), hostLittleEndian()));
}
return refs;
}
array_type parseBinaryArray(const PlistHelperData& d, int objRef)
{
using namespace std;
vector<int32_t> refs = getRefsForContainers(d, objRef);
int32_t refCount = refs.size();
array_type array;
for(int i = 0; i < refCount; ++i)
array.push_back(parseBinary(d, refs[i]));
return array;
}
dictionary_type parseBinaryDictionary(const PlistHelperData& d, int objRef)
{
using namespace std;
vector<int32_t> refs = getRefsForContainers(d, objRef);
int32_t refCount = refs.size() / 2;
dictionary_type dict;
for (int i = 0; i < refCount; i++)
{
boost::any keyAny = parseBinary(d, refs[i]);
try
{
std::string& key = boost::any_cast<std::string&>(keyAny);
dict[key] = parseBinary(d, refs[i + refCount]);
}
catch(boost::bad_any_cast& )
{
throw Error("Error parsing dictionary. Key can't be parsed as a string");
}
}
return dict;