-
Notifications
You must be signed in to change notification settings - Fork 140
/
opennurbs_archive.cpp
18970 lines (17111 loc) · 503 KB
/
opennurbs_archive.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
//
// Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
// obsolete V5 dimension style
#include "opennurbs_internal_V5_dimstyle.h"
// obsolete V2 and V5 annotation objects
#include "opennurbs_internal_V2_annotation.h"
#include "opennurbs_internal_V5_annotation.h"
const ON_String Internal_RuntimeEnvironmentToString(
ON::RuntimeEnvironment runtime_environment
)
{
switch (runtime_environment)
{
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::Unset);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::None);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::Windows);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::Apple);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::Android);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::Linux);
ON_ENUM_TO_STRING_CASE(ON::RuntimeEnvironment::WebAssembly);
}
ON_ERROR("Invalid runtime_environment parameter value.");
return ON_String::EmptyString;
}
const ON_wString Internal_RuntimeEnvironmentToWideString(
ON::RuntimeEnvironment runtime_environment
)
{
const ON_String s = Internal_RuntimeEnvironmentToString(runtime_environment);
return ON_wString(s);
}
unsigned int ON_BinaryArchive::ArchiveOpenNURBSVersionToWrite(
unsigned int archive_3dm_version,
unsigned int opennurbs_version
)
{
// When writing V5 files, any version number in the new V6 format needs to be
// converted to the old V5 format
unsigned int opennurbs_version_to_write = opennurbs_version;
if (
((archive_3dm_version >= 2 && archive_3dm_version <= 4) || (50 == archive_3dm_version))
&& false == ON_VersionNumberIsYearMonthDateFormat(archive_3dm_version, opennurbs_version)
)
{
unsigned int yyyy = 0;
unsigned int mm = 0;
unsigned int dd = 0;
unsigned int major_version_number = 0;
if (ON_VersionNumberParse(opennurbs_version, &major_version_number, 0, &yyyy, &mm, &dd, 0))
{
unsigned int n = major_version_number < 10 ? major_version_number : 9;
opennurbs_version_to_write = ((yyyy * 100 + mm) * 100 + dd) * 10 + n;
}
}
return opennurbs_version_to_write;
}
ON_UserDataItemFilter::ON_UserDataItemFilter()
: m_application_id(ON_nil_uuid)
, m_item_id(ON_nil_uuid)
, m_precedence(0)
, m_bSerialize(false)
{}
ON_UserDataItemFilter::ON_UserDataItemFilter(
ON_UUID application_id,
bool bSerialize
)
: m_application_id(application_id)
, m_item_id(ON_nil_uuid)
, m_precedence(0)
, m_bSerialize(bSerialize ? true : false)
{}
ON_UserDataItemFilter::ON_UserDataItemFilter(
ON_UUID application_id,
ON_UUID item_id,
bool bSerialize
)
: m_application_id(application_id)
, m_item_id(item_id)
, m_precedence(0)
, m_bSerialize(bSerialize ? true : false)
{}
int ON_UserDataItemFilter::Compare(
const class ON_UserDataItemFilter* a,
const class ON_UserDataItemFilter* b
)
{
// null pointer handling
if (a == b)
return 0;
if (0 == b)
return -1; // non-null < null
if (0 == a)
return 1;
int rc = ON_UuidCompare(a->m_application_id, b->m_application_id);
if (0 != rc)
return rc;
rc = ON_UuidCompare(a->m_item_id, b->m_item_id);
if (0 != rc)
return rc;
if (a->m_precedence < b->m_precedence)
return -1;
if (b->m_precedence < a->m_precedence)
return 1;
rc = ((int)(a->m_bSerialize ? 1 : 0)) - ((int)(a->m_bSerialize ? 1 : 0));
return rc;
}
class ON_ReadChunkHelper
{
public:
/*
Parameters:
archive - [in]
bReadSuccess - [out]
The value of *bReadSuccess is set to false if an error
occurs. Otherwise the value of *bReadSuccess is not
changed.
*/
ON_ReadChunkHelper(
ON_BinaryArchive&,
bool& bReadSuccess
);
~ON_ReadChunkHelper();
ON_BinaryArchive& m_binary_archive;
ON__UINT32 m_chunk_tcode = 0;
bool m_bSupressPartiallyReadChunkWarning = false;
ON__INT64 m_chunk_value = 0;
bool& m_bReadSuccess;
private:
bool m_bCallEndRead3dmChunk = false;
private:
// prohibit use - no implementation
ON_ReadChunkHelper() = delete;
ON_ReadChunkHelper(const ON_ReadChunkHelper&) = delete;
ON_ReadChunkHelper& operator=(const ON_ReadChunkHelper&) = delete;
};
ON_ReadChunkHelper::ON_ReadChunkHelper(
ON_BinaryArchive& binary_archive,
bool &bReadSuccess
)
: m_binary_archive(binary_archive)
, m_bReadSuccess(bReadSuccess)
{
m_bCallEndRead3dmChunk = m_binary_archive.BeginRead3dmBigChunk(&m_chunk_tcode,&m_chunk_value);
if ( false == m_bCallEndRead3dmChunk || 0 == m_chunk_tcode )
{
// RH-22447 - valid chunk typecodes are never zero
m_bReadSuccess = false;
}
}
ON_ReadChunkHelper::~ON_ReadChunkHelper()
{
if (m_bCallEndRead3dmChunk)
{
if ( !m_binary_archive.EndRead3dmChunk(m_bSupressPartiallyReadChunkWarning) )
{
m_bReadSuccess = false;
}
}
}
bool ON_IsUnsignedChunkTypecode( ON__UINT32 typecode )
{
// returns tru if the chunk value should be treated as an unsigned int.
return ( 0 == (TCODE_SHORT & typecode)
|| TCODE_RGB == typecode
|| TCODE_RGBDISPLAY == typecode
|| TCODE_PROPERTIES_OPENNURBS_VERSION == typecode
|| TCODE_OBJECT_RECORD_TYPE == typecode
);
}
bool ON_IsLongChunkTypecode(ON__UINT32 typecode)
{
// NOTE: RenderXXXX plug-in used zero as a typecode in material userdata, sigh ...
//return (0 != typecode && 0 == (TCODE_SHORT & typecode));
return (0 == (TCODE_SHORT & typecode));
}
bool ON_IsShortChunkTypecode(ON__UINT32 typecode)
{
return (0 != (TCODE_SHORT & typecode));
}
static
bool DownSizeINT( ON__INT64 i64, ON__INT32* i32 )
{
const static ON__INT64 i32max = 2147483647;
if ( i64 <= i32max && i64 >= (-i32max - 1) )
{
*i32 = (ON__INT32)i64;
return true;
}
ON_ERROR("i64 too big to convert to 4 byte signed int");
*i32 = 0;
return false;
}
static
bool DownSizeUINT( ON__UINT64 u64, ON__UINT32* u32 )
{
if ( u64 <= 0xFFFFFFFF )
{
*u32 = (ON__UINT32)u64;
return true;
}
ON_ERROR("u64 too big to convert to 4 byte unsigned int");
*u32 = 0;
return false;
}
struct ON__3dmV1LayerIndex
{
int m_layer_index;
int m_layer_name_length;
char* m_layer_name;
struct ON__3dmV1LayerIndex* m_next;
};
ON_BinaryArchive::ON_BinaryArchive( ON::archive_mode mode )
: m_mode(mode)
{
if (ON::archive_mode::read3dm == mode || ON::archive_mode::write3dm == mode)
m_bChunkBoundaryCheck = true;
m_annotation_context.SetReferencedBinaryArchive(this);
}
class ON_3dmTableStatusLink
{
public:
ON_3dmTableStatusLink() = default;
~ON_3dmTableStatusLink() = default;
ON_3dmTableStatusLink(const ON_3dmTableStatusLink&) = default;
ON_3dmTableStatusLink& operator=(const ON_3dmTableStatusLink&) = default;
ON_3dmTableStatusLink* m_next = nullptr;
ON_3dmArchiveTableStatus m_table_status;
};
ON_BinaryArchive::~ON_BinaryArchive()
{
if ( 0 != m_V1_layer_list )
{
struct ON__3dmV1LayerIndex* next = m_V1_layer_list;
m_V1_layer_list = 0;
for ( int i = 0; 0 != next && i < 1000; i++ )
{
struct ON__3dmV1LayerIndex* p = next;
next = p->m_next;
onfree(p);
}
}
if (nullptr != m_compressor)
{
CompressionEnd();
onfree(m_compressor);
}
ON_3dmTableStatusLink* next = m_3dm_table_status_list;
m_3dm_table_status_list = nullptr;
while (nullptr != next)
{
ON_3dmTableStatusLink* p = next;
next = const_cast<ON_3dmTableStatusLink*>(next->m_next);
delete p;
}
m_annotation_context.SetReferencedDimStyle(nullptr,nullptr,ON_UNSET_INT_INDEX);
if (nullptr != m_archive_3dm_properties)
{
delete m_archive_3dm_properties;
m_archive_3dm_properties = nullptr;
}
if (nullptr != m_archive_3dm_settings)
{
delete m_archive_3dm_settings;
m_archive_3dm_settings = nullptr;
}
for (int i = 0; i < m_archive_text_style_table.Count(); i++)
{
if (nullptr != m_archive_text_style_table[i])
delete m_archive_text_style_table[i];
}
m_archive_text_style_table.Destroy();
for (int i = 0; i < m_archive_dim_style_table.Count(); i++)
{
if (nullptr != m_archive_dim_style_table[i])
delete m_archive_dim_style_table[i];
}
m_archive_dim_style_table.Destroy();
}
bool ON_BinaryArchive::ArchiveFileMoved() const
{
return m_b3dmArchiveMoved;
}
const ON_wString& ON_BinaryArchive::ArchiveFileName() const
{
return m_archive_file_name;
}
const ON_wString& ON_BinaryArchive::ArchiveDirectoryName() const
{
return m_archive_directory_name;
}
const ON_wString& ON_BinaryArchive::ArchiveFullPath() const
{
return m_archive_full_path;
}
const ON_wString& ON_BinaryArchive::ArchiveSavedAsFullPath() const
{
return m_archive_saved_as_full_path;
}
const wchar_t* ON_BinaryArchive::ArchiveFileNameAsPointer() const
{
return static_cast<const wchar_t*>(m_archive_file_name);
}
const wchar_t* ON_BinaryArchive::ArchiveDirectoryNameAsPointer() const
{
return static_cast<const wchar_t*>(m_archive_directory_name);
}
const wchar_t* ON_BinaryArchive::ArchiveFullPathAsPointer() const
{
return static_cast<const wchar_t*>(m_archive_full_path);
}
const wchar_t* ON_BinaryArchive::ArchiveSavedAsFullPathPointer() const
{
return static_cast<const wchar_t*>(m_archive_saved_as_full_path);
}
void ON_BinaryArchive::SetArchiveFullPath(
const wchar_t* archive_full_path
)
{
if (m_archive_full_path.IsNotEmpty())
{
// The first attempt wins!
if (false == m_archive_full_path.EqualOrdinal(archive_full_path, false))
{
// You need to get this right on the first try.
// If you are hitting this error, figure out why you are attempting to change
// this value and/or and make sure ArchiveFullPath().IsEmpty()
// is true before you attempt to set it.
// This is here because the value gets set correctly and then changed
// to the wrong value when people use temp files and rename after writing
// and don't pay attention to what they are doing.
ON_ERROR("Attempt to change archive path.");
}
return;
}
ON_wString local_full_path(archive_full_path);
archive_full_path = local_full_path;
ON_wString archive_file_name;
ON_wString archive_directory_name;
if (nullptr != archive_full_path && 0 != archive_full_path[0])
{
const wchar_t* dr = 0;
const wchar_t* d = 0;
const wchar_t* f = 0;
const wchar_t* e = 0;
on_wsplitpath(archive_full_path, &dr, &d, &f, &e);
if (archive_full_path == f || (nullptr != d && f > archive_full_path && ON_FileSystemPath::IsRelativePath(archive_full_path) ) )
{
const ON_wString current_directory = ON_FileSystemPath::CurrentDirectory(true);
if (current_directory.IsNotEmpty())
{
local_full_path = ON_FileSystemPath::CombinePaths(static_cast<const wchar_t*>(current_directory), false, archive_full_path, true, false);
archive_full_path = local_full_path;
on_wsplitpath(archive_full_path, &dr, &d, &f, &e);
}
}
if (nullptr != f && 0 != f[0])
{
archive_file_name = f;
if (nullptr == dr)
dr = d;
if (nullptr != dr && 0 != dr[0] && dr < f)
{
archive_directory_name = dr;
archive_directory_name.SetLength(f-dr);
}
}
}
SetArchiveFullPath(
static_cast<const wchar_t*>(archive_directory_name),
static_cast<const wchar_t*>(archive_file_name)
);
m_archive_full_path = archive_full_path;
switch (m_mode)
{
case ON::archive_mode::write3dm:
case ON::archive_mode::write:
m_archive_saved_as_full_path = m_archive_full_path;
break;
default:
break;
}
}
void ON_BinaryArchive::SetArchiveFullPath(
const wchar_t* archive_directory_name,
const wchar_t* archive_file_name
)
{
ON_wString s(archive_directory_name);
s.TrimRight(L"/\\");
if (s.IsEmpty() || (2 == s.Length() && ':' == s[1]))
s = archive_directory_name;
const ON_wString local_directory_name(s);
if (nullptr != archive_file_name)
{
switch (archive_file_name[0])
{
case '/':
case '\\':
case ':':
ON_ERROR("archive_file_name is not valid.");
archive_file_name = nullptr;
}
}
const ON_wString local_file_name(archive_file_name);
if (m_archive_directory_name.IsNotEmpty() || m_archive_full_path.IsNotEmpty())
{
// The first attempt wins!
if (false == m_archive_directory_name.EqualOrdinal(local_directory_name, false))
{
// You need to get this right on the first try.
// If you are hitting this error, figure out why you are attempting to change
// this value and/or and make sure ArchiveFullPath().IsEmpty()
// is true before you attempt to set it.
// This is here because the value gets set correctly and then changed
// to the wrong value when people use temp files and rename after writing
// and don't pay attention to what they are doing.
ON_ERROR("Attempt to change archive path.");
}
return;
}
if (m_archive_file_name.IsNotEmpty() || m_archive_full_path.IsNotEmpty())
{
// The first attempt wins!
if (false == m_archive_file_name.EqualOrdinal(local_file_name, false))
{
// You need to get this right on the first try.
// If you are hitting this error, figure out why you are attempting to change
// this value and/or and make sure ArchiveFullPath().IsEmpty()
// is true before you attempt to set it.
// This is here because the value gets set correctly and then changed
// to the wrong value when people use temp files and rename after writing
// and don't pay attention to what they are doing.
ON_ERROR("Attempt to change archive path.");
}
return;
}
m_archive_directory_name = local_directory_name;
m_archive_file_name = local_file_name;
if (m_archive_directory_name.IsNotEmpty() && m_archive_file_name.IsNotEmpty())
{
m_archive_full_path = ON_wString::EmptyString;
m_archive_full_path += ON_FileSystemPath::DirectorySeparator;
m_archive_full_path += m_archive_file_name;
}
else
m_archive_full_path = ON_wString::EmptyString;
switch (m_mode)
{
case ON::archive_mode::write3dm:
case ON::archive_mode::write:
m_archive_saved_as_full_path = m_archive_full_path;
break;
default:
break;
}
}
ON_BinaryArchive::eStorageDeviceError ON_BinaryArchive::StorageDeviceErrorFromUnsigned(
unsigned int storage_device_error_as_unsigned
)
{
switch (storage_device_error_as_unsigned)
{
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::None);
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::WriteFailed);
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringWriting);
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::ReadFailed);
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringReading);
ON_ENUM_FROM_UNSIGNED_CASE(ON_BinaryArchive::eStorageDeviceError::UnknownDeviceError);
}
ON_ERROR("Invalid storage_device_error_as_unsigned parmeter.");
return ON_BinaryArchive::eStorageDeviceError::UnknownDeviceError;
}
unsigned int ON_BinaryArchive::StorageDeviceError() const
{
return m_storage_device_error;
}
void ON_BinaryArchive::SetStorageDeviceError(
ON_BinaryArchive::eStorageDeviceError storage_device_error
)
{
const unsigned int u = static_cast<unsigned int>(storage_device_error);
SetStorageDeviceError(u);
}
void ON_BinaryArchive::SetStorageDeviceError(
unsigned int storage_device_error
)
{
if (0 != storage_device_error)
{
Internal_ReportCriticalError();
// A critical error terminates the read/write.
// The first one sets the code and subsequent attempts to modify
// the code fail.
if (0 == m_storage_device_error)
{
ON_ERROR("Damaged file and / or buggy code. Please investigate.");
m_storage_device_error = storage_device_error;
}
}
}
bool ON_BinaryArchive::ToggleByteOrder(
size_t count, // number of elements
size_t sizeof_element, // size of element (2,4, or 8)
const void* src, // source buffer
void* dst // destination buffer (can be same as source buffer)
)
{
bool rc = (0 == count || (count > 0 && sizeof_element > 0 && nullptr != src && nullptr != dst));
if ( rc && count > 0 )
{
unsigned char c[32];
const unsigned char* a = (const unsigned char*)src;
unsigned char* b = (unsigned char*)dst;
const unsigned char* b1 = b + (count*sizeof_element);
// loops are unrolled and a switch is used
// to speed things up a bit.
switch(sizeof_element)
{
case 2:
while(b < b1)
{
c[0] = *a++;
c[1] = *a++;
*b++ = c[1];
*b++ = c[0];
}
break;
case 4:
while(b < b1)
{
c[0] = *a++;
c[1] = *a++;
c[2] = *a++;
c[3] = *a++;
*b++ = c[3];
*b++ = c[2];
*b++ = c[1];
*b++ = c[0];
}
break;
case 8:
while(b < b1)
{
c[0] = *a++;
c[1] = *a++;
c[2] = *a++;
c[3] = *a++;
c[4] = *a++;
c[5] = *a++;
c[6] = *a++;
c[7] = *a++;
*b++ = c[7];
*b++ = c[6];
*b++ = c[5];
*b++ = c[4];
*b++ = c[3];
*b++ = c[2];
*b++ = c[1];
*b++ = c[0];
}
break;
default:
if ( sizeof_element < 32 )
{
// As of 2 May 2003, this case is never used
// by core opennurbs objects.
//
// This is here so that future code will work
// if and when 128 bit "ints"/"doubles" become common
// enough that they can be stored in 3dm files.
// It may also happen that third party applications
// on specialized CPUs need to toggle byte order
// for 128 bit ints/doubles stored as user data.
size_t i;
while(b < b1)
{
for (i = 0; i < sizeof_element; i++)
c[i] = *a++;
while(i--)
*b++ = c[i];
}
}
else
{
rc = false;
}
break;
}
}
return rc;
}
ON__UINT64 ON_BinaryArchive::CurrentPosition() const
{
return m_current_positionX;
}
bool ON_BinaryArchive::Internal_IncrementCurrentPosition(
ON__UINT64 delta
)
{
// TODO: Add error detection.
const ON__UINT64 new_pos = m_current_positionX + delta;
m_current_positionX = new_pos;
return true;
}
bool ON_BinaryArchive::Internal_DecrementCurrentPosition(
ON__UINT64 delta
)
{
if (m_current_positionX >= delta)
{
const ON__UINT64 new_pos = m_current_positionX - delta;
m_current_positionX = new_pos;
return true;
}
ON_ERROR("Attempt to set current position before start of archive.");
return false;
}
bool ON_BinaryArchive::SeekFromStart( ON__UINT64 bytes_from_start )
{
if (UnsetMode())
{
ON_ERROR("Invalid archive Mode().");
return false;
}
if (m_bChunkBoundaryCheck)
{
const ON_3DM_BIG_CHUNK* c = m_chunk.Last();
if (nullptr != c)
{
ON_ERROR("Attempt to seek before beginning of current chunk.");
return false;
}
}
if (0 != CurrentPosition() )
{
// Internal_SeekToStart() is a pure virutal function that must overridden.
if (!Internal_SeekToStartOverride())
{
ON_ERROR("Internal_SeekToStartOverride() failed.");
if (ReadMode())
SetStorageDeviceError(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringReading);
if (WriteMode())
SetStorageDeviceError(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringWriting);
return false;
}
m_current_positionX = 0;
}
return (bytes_from_start > 0) ? SeekForward(bytes_from_start) : true;
}
bool ON_BinaryArchive::SeekForward( ON__UINT64 bytes_forward )
{
return Internal_SeekCur(true, bytes_forward);
}
bool ON_BinaryArchive::SeekBackward( ON__UINT64 bytes_backward )
{
return Internal_SeekCur(false, bytes_backward);
}
bool ON_BinaryArchive::Internal_SeekCur( bool bForward, ON__UINT64 offset )
{
// Internal_SeekFromCurrentPosition() is a pure virutal function that must overridden.
// Some implementations may use signed 4 byte int in critical places.
// SeekForward() will work correctly in this worst case situation.
if (UnsetMode())
{
ON_ERROR("Invalid archive Mode().");
return false;
}
const ON__UINT64 current_pos = CurrentPosition();
if (false == bForward && offset > current_pos)
{
ON_ERROR("Attempt to seek before archive beginning.");
return false;
}
const ON__UINT64 new_pos
= bForward
? (current_pos + offset)
: (current_pos - offset);
if (m_bChunkBoundaryCheck)
{
const ON_3DM_BIG_CHUNK* c = m_chunk.Last();
if (nullptr != c && c->m_start_offset <= current_pos && current_pos <= c->m_end_offset )
{
if (new_pos > c->m_end_offset)
{
// The code that seeks 1 byte past the end of a level 1 chunk disables chunk boundary checking for that seek.)
ON_ERROR("Attempt to seek beyond end of current chunk.");
return false;
}
if (new_pos < c->m_start_offset)
{
// seeking before beginning of chunk's data
// (The code that seeks back to write a chunk length disables chunk boundary checking for that write.)
ON_ERROR("Attempt to seek before beginning of current chunk.");
return false;
}
}
}
const int dir = bForward ? 1 : -1;
int ioffset;
const ON__UINT64 max_internal_seek = 2147483632; // < maximum signed 4 bytes int = 2147483647
while ( offset > 0 )
{
const ON__UINT64 delta = (offset > max_internal_seek) ? max_internal_seek : offset;
ioffset = dir*((int)delta);
if (false == Internal_SeekFromCurrentPositionOverride(ioffset))
{
ON_ERROR("Internal_SeekFromCurrentPositionOverride(ioffset) failed.");
if (ReadMode())
SetStorageDeviceError(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringReading);
if (WriteMode())
SetStorageDeviceError(ON_BinaryArchive::eStorageDeviceError::SeekFailedDuringWriting);
return false;
}
if (bForward)
Internal_IncrementCurrentPosition(delta);
else
Internal_DecrementCurrentPosition(delta);
offset -= delta;
}
return true;
}
bool
ON_BinaryArchive::ReadChar( // Read an array of 8 bit chars
size_t count, // number of chars to read
char* p
)
{
return ReadByte(count, p);
}
bool
ON_BinaryArchive::ReadChar( // Read a single 8 bit char
char* p
)
{
return ReadByte(1, p);
}
bool
ON_BinaryArchive::ReadChar( // Read an array of 8 bit signed chars
size_t count, // number of chars to read
ON__INT8* p
)
{
return ReadByte( count, p );
}
bool
ON_BinaryArchive::ReadChar( // Read an array of 8 bit unsigned chars
size_t count, // number of unsigned chars to read
ON__UINT8* p
)
{
return ReadByte( count, p );
}
bool
ON_BinaryArchive::ReadChar( // Read a single 8 bit signed char
ON__INT8* p
)
{
return ReadByte( 1, p );
}
bool
ON_BinaryArchive::ReadChar( // Read a single 8 bit unsigned char
ON__UINT8* p
)
{
return ReadByte( 1, p );
}
bool
ON_BinaryArchive::ReadInt16( // Read an array of 16 bit integers
size_t count, // number of unsigned integers to read
ON__INT16* p
)
{
bool rc = ReadByte( count<<1, p );
if (rc && m_endian == ON::endian::big_endian)
{
// reverse byte order
unsigned char* b= (unsigned char*) (p);
unsigned char c;
while(count--) {
c = b[0]; b[0] = b[1]; b[1] = c;
b += 2;
}
}
return rc;
}
bool
ON_BinaryArchive::ReadShort( // Read an array of 16 bit shorts
size_t count, // number of signed chars to read
ON__INT16* p
)
{
#pragma ON_PRAGMA_WARNING_PUSH
// Disable the MSC /W4 "conditional expression is constant" warning
// about 2 == sizeof(*p). Since this code has to run on machines
// where sizeof(*p) can be 2, 4, or 8 bytes, the test is necessary.
#pragma ON_PRAGMA_WARNING_DISABLE_MSC(4127)
bool rc = true;
if ( 2 == sizeof(*p) )
{
rc = ReadInt16( count, (ON__INT16*)p );
}
else
{
size_t j;
ON__INT16 i16;
for ( j = 0; j < count && rc; j++ )
{
rc = ReadInt16( 1, &i16 );
*p++ = (short)i16;
}
}
return rc;
#pragma ON_PRAGMA_WARNING_POP
}
bool
ON_BinaryArchive::ReadShort( // Read an array of 16 bit unsigned shorts
size_t count, // number of unsigned chars to read
ON__UINT16* p
)
{
return ReadShort( count, (short*)p );
}
bool
ON_BinaryArchive::ReadShort( // Read a single 16 bit signed short
ON__INT16* p
)
{
return ReadShort( 1, p );
}
bool
ON_BinaryArchive::ReadShort( // Read a single 16 bit unsigned short
ON__UINT16* p
)
{
return ReadShort( 1, p );
}
bool
ON_BinaryArchive::ReadInt32( // Read an array of 32 bit signed integers
size_t count, // number of 32 bit signed integers to read
ON__INT32* p
)
{
bool rc = ReadByte( count<<2, p );
if (rc && m_endian == ON::endian::big_endian)
{
unsigned char* b= (unsigned char*)p;
unsigned char c;
while(count--) {
c = b[0]; b[0] = b[3]; b[3] = c;
c = b[1]; b[1] = b[2]; b[2] = c;
b += 4;
}
}
return rc;
}
bool
ON_BinaryArchive::ReadInt( // Read an array of signed integers
size_t count, // number of signed chars to read
ON__INT32* p
)
{
#if defined(ON_COMPILER_MSC)
#pragma ON_PRAGMA_WARNING_PUSH
// Disable the MSC /W4 "conditional expression is constant" warning