-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathopennurbs_embedded_file.cpp
1572 lines (1346 loc) · 41.8 KB
/
opennurbs_embedded_file.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-2022 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
class ON_EmbeddedFile::CImpl final
{
public:
class Data final
{
public:
Data() { }
Data(const Data& d) { *this = d; }
const Data& operator = (const Data& d);
void SetLength(size_t len);
public:
std::unique_ptr<ON__UINT8[]> m_buffer;
size_t m_length = 0;
size_t m_compressed_length = 0;
bool m_error = false;
};
bool LoadFile(const wchar_t* filename);
bool SaveFile(const wchar_t* filename) const;
public:
ON_wString m_orig_file;
Data m_data;
};
const ON_EmbeddedFile::CImpl::Data& ON_EmbeddedFile::CImpl::Data::operator = (const Data& d)
{
if (this == &d)
return *this;
SetLength(d.m_length);
m_compressed_length = d.m_compressed_length;
if (m_buffer)
{
memcpy(m_buffer.get(), d.m_buffer.get(), m_length);
}
else
{
m_length = m_compressed_length = 0;
}
m_error = d.m_error;
return *this;
}
void ON_EmbeddedFile::CImpl::Data::SetLength(size_t len)
{
if (0 != len)
{
m_buffer = std::unique_ptr<ON__UINT8[]>(new ON__UINT8[len]);
}
else
{
m_buffer = nullptr;
}
m_length = len;
}
bool ON_EmbeddedFile::CImpl::LoadFile(const wchar_t* filename)
{
auto& d = m_data;
// Open the file.
FILE* pFile = ON_FileStream::Open(filename, L"rb");
if (nullptr == pFile)
return false;
// Get the length of the file data.
ON_FileStream::SeekFromEnd(pFile, 0);
const size_t data_length = size_t(ON_FileStream::CurrentPosition(pFile));
ON_FileStream::SeekFromStart(pFile, 0);
// Allocate a buffer for the file data.
d.SetLength(data_length);
// Read the file data into the buffer.
const bool bOK = (ON_FileStream::Read(pFile, d.m_length, d.m_buffer.get()) == d.m_length);
d.m_error = !bOK;
// Close the file.
ON_FileStream::Close(pFile);
return bOK;
}
bool ON_EmbeddedFile::CImpl::SaveFile(const wchar_t* filename) const
{
if (m_data.m_error)
return false; // Can't save when in error state.
if (0 == m_data.m_length)
return false; // Not loaded.
// Open the file for writing.
FILE* pFile = ON_FileStream::Open(filename, L"wb");
if (nullptr == pFile)
return false;
// Write the buffer to the file.
if (ON_FileStream::Write(pFile, m_data.m_length, m_data.m_buffer.get()) != m_data.m_length)
return false;
// Close the file.
ON_FileStream::Close(pFile);
return true;
}
ON_OBJECT_IMPLEMENT(ON_EmbeddedFile, ON_ModelComponent, "E3BBE02E-F3D5-490D-9719-E21D8BF982EF");
ON_EmbeddedFile::ON_EmbeddedFile()
:
ON_ModelComponent(ON_ModelComponent::Type::EmbeddedFile)
{
m_impl = new CImpl;
}
ON_EmbeddedFile::ON_EmbeddedFile(const ON_EmbeddedFile& ef)
:
ON_ModelComponent(ON_ModelComponent::Type::EmbeddedFile, ef)
{
m_impl = new CImpl;
*this = ef;
}
ON_EmbeddedFile::~ON_EmbeddedFile()
{
Clear();
delete m_impl;
m_impl = nullptr;
}
const ON_EmbeddedFile& ON_EmbeddedFile::operator = (const ON_EmbeddedFile& ef)
{
if (&ef != this)
{
m_impl->m_orig_file = ef.m_impl->m_orig_file;
m_impl->m_data = ef.m_impl->m_data;
}
return *this;
}
ON_wString ON_EmbeddedFile::Filename(void) const
{
return m_impl->m_orig_file;
}
void ON_EmbeddedFile::SetFilename(const wchar_t* filename)
{
m_impl->m_orig_file = filename;
}
bool ON_EmbeddedFile::LoadFromFile(const wchar_t* filename)
{
Clear();
m_impl->m_orig_file = ON_FileSystemPath::CleanPath(filename);
if (m_impl->m_orig_file.IsEmpty())
return false;
if (!m_impl->LoadFile(m_impl->m_orig_file))
return false;
return true;
}
bool ON_EmbeddedFile::SaveToFile(const wchar_t* filename) const
{
const ON_wString file = ON_FileSystemPath::CleanPath(filename);
if (!m_impl->SaveFile(file))
return false;
return true;
}
bool ON_EmbeddedFile::LoadFromBuffer(ON_Buffer& buf)
{
Clear();
// Allocate a buffer for the data.
auto& d = m_impl->m_data;
d.SetLength(size_t(buf.Size()));
// Load the buffer from 'buf'.
if (buf.Read(d.m_length, d.m_buffer.get()) == d.m_length)
return true;
m_impl->m_data.m_error = true;
return false;
}
bool ON_EmbeddedFile::SaveToBuffer(ON_Buffer& buf) const
{
if (m_impl->m_data.m_error)
return false; // Can't save when in error state.
// Write the data to 'buf'.
buf.Write(m_impl->m_data.m_length, m_impl->m_data.m_buffer.get());
return true;
}
bool ON_EmbeddedFile::Read(ON_BinaryArchive& archive)
{
Clear();
// Read the full file path of the original file.
ON_wString filename;
if (!archive.ReadString(filename))
{
m_impl->m_data.m_error = true;
return false;
}
m_impl->m_orig_file = ON_FileSystemPath::CleanPath(filename);
// Read the original (uncompressed) size of the compressed buffer.
size_t uncompressed_size = 0;
if (!archive.ReadCompressedBufferSize(&uncompressed_size))
{
m_impl->m_data.m_error = true;
return false;
}
// Allocate a buffer for the uncompressed data.
auto& d = m_impl->m_data;
d.SetLength(uncompressed_size);
// Read the compressed buffer and uncompress it into uncompressed_buffer.
bool bFailedCRC = false;
const ON__UINT64 pos_before = archive.CurrentPosition();
if (!archive.ReadCompressedBuffer(uncompressed_size, d.m_buffer.get(), &bFailedCRC) && !bFailedCRC)
{
m_impl->m_data.m_error = true;
return false;
}
d.m_compressed_length = size_t(archive.CurrentPosition() - pos_before);
return true;
}
bool ON_EmbeddedFile::Write(ON_BinaryArchive& archive) const
{
auto& d = m_impl->m_data;
if (d.m_error)
return false; // Can't write when in error state.
// Write the original filename to the archive.
if (!archive.WriteString(m_impl->m_orig_file))
return false;
// Write the temp file data to the archive.
if (!archive.WriteCompressedBuffer(d.m_length, d.m_buffer.get()))
return false;
return true;
}
size_t ON_EmbeddedFile::Length(void) const
{
return m_impl->m_data.m_length;
}
size_t ON_EmbeddedFile::CompressedLength(void) const
{
return m_impl->m_data.m_compressed_length;
}
bool ON_EmbeddedFile::Error(void) const
{
return m_impl->m_data.m_error;
}
bool ON_EmbeddedFile::Clear(void)
{
m_impl->m_orig_file.Empty();
m_impl->m_data.SetLength(0);
m_impl->m_data.m_compressed_length = 0;
m_impl->m_data.m_error = false;
return true;
}
void* ON_EmbeddedFile::EVF(const wchar_t* func, void* data)
{
return nullptr;
}
const ON_EmbeddedFile* ON_EmbeddedFile::FromModelComponentRef(const ON_ModelComponentReference& ref,
const ON_EmbeddedFile* none_return_value) // Static.
{
const auto* ef = ON_EmbeddedFile::Cast(ref.ModelComponent());
if (nullptr != ef)
return ef;
return none_return_value;
}
// This ON_Buffer stuff was already in file opennurbs_embedded_file.cpp even though it has nothing
// to do with embedded files. I need this file for actual embedded file code so I'm hijacking it.
// TODO: This ON_Buffer stuff should be moved.
ON_Buffer::ON_Buffer()
: m_buffer_size(0)
, m_current_position(0)
, m_first_segment(0)
, m_last_segment(0)
, m_current_segment(0)
, m_error_handler(0)
, m_last_error(0)
{
memset(m_reserved,0,sizeof(m_reserved));
}
void ON_Buffer::Destroy()
{
ChangeSize(0);
}
void ON_Buffer::EmergencyDestroy()
{
m_buffer_size = 0;
m_current_position = 0;
m_first_segment = 0;
m_last_segment = 0;
m_current_segment = 0;
m_error_handler = 0;
m_last_error = 0;
}
struct ON_BUFFER_SEGMENT
{
struct ON_BUFFER_SEGMENT* m_prev_segment;
struct ON_BUFFER_SEGMENT* m_next_segment;
ON__UINT64 m_segment_position0; // position of first byte in this segment
ON__UINT64 m_segment_position1; // position of the first byte in the next segment
// When a segment is the last one in an ON_Buffer,
// is is common for m_segment_position1 > m_buffer_size.
unsigned char* m_segment_buffer; // null or an array of length (m_segment_position1 - m_segment_position0)
void* m_reserved;
};
int ON_Buffer::Compare( const ON_Buffer& a, const ON_Buffer& b )
{
if ( &a == &b )
return 0;
if ( a.m_buffer_size < b.m_buffer_size )
return -1;
if ( a.m_buffer_size > b.m_buffer_size )
return 1;
struct ON_BUFFER_SEGMENT* aseg = a.m_first_segment;
struct ON_BUFFER_SEGMENT* bseg = b.m_first_segment;
const ON__UINT64 buffer_size = a.m_buffer_size;
ON__UINT64 size = 0;
size_t aoffset = 0;
size_t boffset = 0;
size_t asegsize = 0;
size_t bsegsize = 0;
size_t asize = 0;
size_t bsize = 0;
size_t sz;
int rc = 0;
while ( 0 != aseg && 0 != bseg && size < buffer_size )
{
if ( 0 == asegsize )
{
if ( aseg->m_segment_position0 >= aseg->m_segment_position1 )
{
aseg = aseg->m_next_segment;
continue;
}
asegsize = (size_t)(aseg->m_segment_position1 - aseg->m_segment_position0);
aoffset = 0;
}
if ( 0 == bsegsize )
{
if ( bseg->m_segment_position0 >= bseg->m_segment_position1 )
{
bseg = bseg->m_next_segment;
continue;
}
bsegsize = (size_t)(bseg->m_segment_position1 - bseg->m_segment_position0);
boffset = 0;
}
if ( aoffset >= asegsize )
{
asegsize = 0;
aseg = aseg->m_next_segment;
continue;
}
if ( boffset >= bsegsize )
{
bsegsize = 0;
bseg = bseg->m_next_segment;
continue;
}
if ( 0 == aseg->m_segment_buffer )
{
return (0 == bseg->m_segment_buffer) ? 0 : -1;
}
if ( 0 == bseg->m_segment_buffer )
{
return 1;
}
asize = asegsize - aoffset;
bsize = bsegsize - boffset;
sz = (asize <= bsize) ? asize : bsize;
if ( size + sz > buffer_size )
sz = (size_t)(buffer_size - size);
rc = memcmp( aseg->m_segment_buffer + aoffset, bseg->m_segment_buffer + boffset, (size_t)sz );
if ( 0 != rc )
return ((rc<0)?-1:1);
aoffset += sz;
boffset += sz;
size += sz;
}
return 0;
}
ON_Buffer::~ON_Buffer()
{
ChangeSize(0); // frees all heap and zeros everything but m_current_position.
m_current_position = 0;
}
ON_Buffer::ON_Buffer( const ON_Buffer& src )
: m_buffer_size(0)
, m_current_position(0)
, m_first_segment(0)
, m_last_segment(0)
, m_current_segment(0)
, m_error_handler(0)
, m_last_error(0)
{
memset(m_reserved,0,sizeof(m_reserved));
Copy(src);
}
ON_Buffer& ON_Buffer::operator=( const ON_Buffer& src )
{
if ( this != &src )
{
ChangeSize(0); // frees all heap and zeros everything but m_current_position.
m_current_position = 0;
Copy(src);
}
return *this;
}
bool ON_Buffer::Seek( ON__INT64 offset, int origin )
{
ON__UINT64 pos0, pos1;
switch(origin)
{
case 0: // Seek from beginning of start.
pos0 = 0;
break;
case 1: // Seek from current position.
pos0 = m_current_position;
break;
case 2: // Seek from end.
pos0 = m_buffer_size;
break;
default:
{
ON_ERROR("Invalid origin parameter");
return false;
}
break;
}
if ( offset < 0 )
{
if ( pos0 < (ON__UINT64)(-offset) )
{
// current position cannot be negative
ON_ERROR("Attempt to seek before start of buffer.");
return false;
}
pos1 = pos0 - (ON__UINT64)(-offset); // overflow cannot happen in this operation
}
else if ( offset > 0 )
{
// current position can be >= m_buffer_size
pos1 = pos0 + (ON__UINT64)(offset); // overflow is possible in this operation
if ( pos1 <= pos0 )
{
// overflow
ON_ERROR("Attempt to seek to a position that is too large for 64-bit unsigned int storage.");
return false;
}
}
else
{
pos1 = pos0;
}
if ( pos1 != m_current_position )
{
m_current_position = pos1;
m_current_segment = 0;
}
return true;
}
bool ON_Buffer::SeekFromStart( ON__INT64 offset )
{
return Seek(offset,0);
}
bool ON_Buffer::SeekFromCurrentPosition( ON__INT64 offset )
{
return Seek(offset,1);
}
bool ON_Buffer::SeekFromEnd( ON__INT64 offset )
{
return Seek(offset,2);
}
bool ON_Buffer::Compact()
{
bool rc = false;
if ( 0 == m_buffer_size )
{
rc = ChangeSize(0); // frees all heap and zeros everything but m_current_position.
m_current_segment = 0;
}
else if ( 0 != m_last_segment
&& m_buffer_size > m_last_segment->m_segment_position0
&& m_buffer_size <= m_last_segment->m_segment_position1
)
{
if ( m_buffer_size == m_last_segment->m_segment_position1 )
rc = true;
else
{
ON__UINT64 sizeof_segment_buffer = m_buffer_size - m_last_segment->m_segment_position0;
struct ON_BUFFER_SEGMENT* prev_segment = m_last_segment->m_prev_segment;
void* last_buffer = ( 0 != m_last_segment->m_segment_buffer && m_last_segment->m_segment_buffer != (unsigned char*)(m_last_segment+1) )
? m_last_segment->m_segment_buffer
: 0;
struct ON_BUFFER_SEGMENT* new_last_segment = (struct ON_BUFFER_SEGMENT*)onrealloc(m_last_segment,sizeof(*m_last_segment) + ((size_t)sizeof_segment_buffer)); // sizeof_segment_buffer always < 0xFFFFFFFF
if ( 0 != new_last_segment )
{
if ( new_last_segment != m_last_segment || 0 != last_buffer )
{
new_last_segment->m_segment_buffer = (unsigned char*)(new_last_segment+1);
if ( 0 != last_buffer )
{
memcpy(new_last_segment->m_segment_buffer,last_buffer,(size_t)sizeof_segment_buffer);
onfree(last_buffer);
last_buffer = 0;
}
new_last_segment->m_prev_segment = prev_segment;
new_last_segment->m_next_segment = 0;
if ( m_first_segment == m_last_segment )
m_first_segment = new_last_segment;
if ( m_current_segment == m_last_segment )
m_current_segment = new_last_segment;
m_last_segment = new_last_segment;
if ( 0 != prev_segment )
{
prev_segment->m_next_segment = m_last_segment;
}
}
m_last_segment->m_segment_position1 = m_buffer_size;
rc = true;
}
}
}
return true;
}
bool ON_Buffer::ChangeSize(ON__UINT64 buffer_size)
{
if ( buffer_size <= 0 )
{
struct ON_BUFFER_SEGMENT* p0 = m_last_segment;
struct ON_BUFFER_SEGMENT* p1 = 0;
m_buffer_size = 0;
m_first_segment = 0;
m_last_segment = 0;
m_current_segment = 0;
// free in reverse order of allocation
while ( 0 != p0 )
{
p1 = p0->m_prev_segment;
if ( 0 != p0->m_segment_buffer && (void*)(p0->m_segment_buffer) != (void*)(p0+1) )
onfree(p0->m_segment_buffer);
onfree(p0);
p0 = p1;
}
}
else if ( buffer_size < m_buffer_size )
{
m_current_segment = 0;
if ( 0 == m_first_segment || 0 == m_last_segment )
{
ON_ERROR("Corrupt ON_Buffer");
return false;
}
while ( 0 != m_last_segment )
{
if ( m_last_segment->m_segment_position0 < buffer_size )
{
if ( buffer_size > m_last_segment->m_segment_position1 )
{
ON_ERROR("Corrupt ON_Buffer.");
// Set m_buffer_size and m_last_segment to valid values
// to prevent possible crashes if the return code is
// ignored.
if ( m_buffer_size > m_last_segment->m_segment_position1 )
m_buffer_size = m_last_segment->m_segment_position1;
m_last_segment->m_next_segment = 0;
if ( m_current_position > m_buffer_size )
m_current_position = m_buffer_size;
return false;
}
if ( 0 != m_last_segment->m_segment_buffer && m_last_segment->m_segment_position1 > buffer_size )
{
memset(m_last_segment->m_segment_buffer + (buffer_size - m_last_segment->m_segment_position0),
0,
(size_t)(m_last_segment->m_segment_position1 - buffer_size)
);
}
m_buffer_size = buffer_size;
break;
}
struct ON_BUFFER_SEGMENT* p = m_last_segment->m_prev_segment;
if ( 0 != p )
p->m_next_segment = 0;
if ( 0 != m_last_segment->m_segment_buffer && (void*)(m_last_segment->m_segment_buffer) != (void*)(m_last_segment+1) )
onfree(m_last_segment->m_segment_buffer);
onfree(m_last_segment);
m_last_segment = p;
}
}
else if ( buffer_size > m_buffer_size )
{
// save current position;
const ON__UINT64 saved_pos = CurrentPosition();
if ( SeekFromStart(buffer_size-1) )
{
// calling Write with the current position at buffer_size-1
// will pad with zeros from offset m_buffer_size to
// offset buffer_size-2, write a zero at offset buffer_size-1,
// and set m_buffer_size to buffer size.
const unsigned char zero_byte = 0;
Write(1,&zero_byte);
}
// restore current position.
SeekFromStart(saved_pos);
}
return (buffer_size == m_buffer_size);
}
void ON_Buffer::Copy( const ON_Buffer& src )
{
const struct ON_BUFFER_SEGMENT* src_seg;
struct ON_BUFFER_SEGMENT* dst_seg;
for ( src_seg = src.m_first_segment; 0 != src_seg; src_seg = src_seg->m_next_segment )
{
if ( m_buffer_size != src_seg->m_segment_position0
|| src_seg->m_segment_position0 >= src.m_buffer_size
)
{
ON_ERROR("Attempt to copy corrupt source.");
break;
}
if ( src_seg->m_segment_position0 >= src_seg->m_segment_position1
)
{
ON_ERROR("Attempt to copy corrupt source.");
continue;
}
ON__UINT64 segment_buffer_size = ( 0 != src_seg->m_segment_buffer)
? src_seg->m_segment_position1 - src_seg->m_segment_position0
: 0;
dst_seg = (struct ON_BUFFER_SEGMENT*)onmalloc(sizeof(*dst_seg) + ((size_t)segment_buffer_size) );
memset(dst_seg,0,sizeof(*dst_seg));
if ( segment_buffer_size > 0 )
{
dst_seg->m_segment_buffer = (unsigned char*)(dst_seg+1);
memcpy( dst_seg->m_segment_buffer, src_seg->m_segment_buffer, (size_t)segment_buffer_size ); // segment_buffer_size always < 0xFFFFFFFF
}
if ( 0 == m_first_segment )
m_first_segment = dst_seg;
dst_seg->m_prev_segment = m_last_segment;
if ( 0 != m_last_segment )
m_last_segment->m_next_segment = dst_seg;
m_last_segment = dst_seg;
dst_seg->m_segment_position0 = src_seg->m_segment_position0;
dst_seg->m_segment_position1 = src_seg->m_segment_position1;
m_buffer_size = (src.m_buffer_size < dst_seg->m_segment_position1)
? src.m_buffer_size
: dst_seg->m_segment_position1;
}
if ( src.m_current_position <= m_buffer_size )
m_current_position = src.m_current_position;
// 27 June, 2001 Dale Lear: Should this copy m_last_error and m_error_handler? Not sure.
}
static bool ON_Buffer_IsNotValid()
{
return false;
}
bool ON_Buffer::IsValid( const ON_TextLog* text_log ) const
{
// This function is primarily used to discover bugs
// in the ON_Buffer member function code.
if ( 0 == m_buffer_size )
{
if ( 0 != m_first_segment )
return ON_Buffer_IsNotValid();
if ( 0 != m_last_segment )
return ON_Buffer_IsNotValid();
if ( 0 != m_current_segment )
return ON_Buffer_IsNotValid();
return true;
}
if ( 0 == m_first_segment )
return ON_Buffer_IsNotValid();
if ( 0 != m_first_segment->m_prev_segment )
return ON_Buffer_IsNotValid();
if ( 0 == m_last_segment )
return ON_Buffer_IsNotValid();
if ( 0 != m_last_segment->m_next_segment )
return ON_Buffer_IsNotValid();
bool bCurrentSegInList = (0 == m_current_segment);
ON__UINT64 pos = 0;
ON__UINT64 u;
const struct ON_BUFFER_SEGMENT* prev_seg = 0;
const struct ON_BUFFER_SEGMENT* seg;
for ( seg = m_first_segment; seg != 0; seg = seg->m_next_segment )
{
if ( prev_seg != seg->m_prev_segment )
return ON_Buffer_IsNotValid();
if ( 0 != prev_seg && prev_seg->m_segment_position1 != seg->m_segment_position0 )
return ON_Buffer_IsNotValid();
if ( seg->m_segment_position1 <= seg->m_segment_position0 )
return ON_Buffer_IsNotValid();
if ( pos != seg->m_segment_position0 )
return ON_Buffer_IsNotValid();
if ( m_current_segment == seg )
bCurrentSegInList = true;
// pos checks prevent infinite loop when the linked list has a cycle;
u = pos + (seg->m_segment_position1 - seg->m_segment_position0);
if ( pos >= u )
return ON_Buffer_IsNotValid(); // addition wrapped value
pos = u;
prev_seg = seg;
}
if ( m_last_segment != prev_seg )
return ON_Buffer_IsNotValid();
if ( pos < m_buffer_size )
return ON_Buffer_IsNotValid();
if ( m_buffer_size <= m_last_segment->m_segment_position0
|| m_buffer_size > m_last_segment->m_segment_position1
)
return ON_Buffer_IsNotValid();
return true;
}
bool ON_Buffer::AtEnd() const
{
return (m_current_position == m_buffer_size);
}
ON__UINT64 ON_Buffer::Size() const
{
return m_buffer_size;
}
ON__UINT32 ON_Buffer::CRC32( ON__UINT32 current_remainder ) const
{
ON__UINT64 size, seg_size;
const struct ON_BUFFER_SEGMENT* prev_seg;
const struct ON_BUFFER_SEGMENT* seg;
const struct ON_BUFFER_SEGMENT* seg0 = 0;
size = 0;
for ( seg = m_first_segment; 0 != seg; seg = seg->m_next_segment )
{
// prev_seg is set this way so that the error handling
// code can use continue statements for non-fatal errors.
prev_seg = seg0;
seg0 = seg;
if ( seg->m_segment_position0 > seg->m_segment_position1 )
{
// This is really bad! If you can determine how the corruption occurs,
// please make a bug report and tell Dale Lear as soon as possible.
ON_ERROR("corrupt buffer - segment's position values are invalid.");
continue;
}
if ( 0 == prev_seg )
{
if ( 0 != seg->m_segment_position0 )
{
// The first segment should have seg->m_segment_position0 = 0.
// We'll keep going after the call to ON_ERROR.
//
// If you can determine how the corruption occurred, please
// make a bug report and assign it to Dale Lear.
ON_ERROR("corrupt buffer - first segment has non-zero value for position0.");
}
}
else if ( prev_seg->m_segment_position1 != seg->m_segment_position0 )
{
// Every segment after the first should have
// seg->m_segment_position0 = previous_segment->m_segment_position1.
// We'll keep going after the call to ON_ERROR.
//
// If you can determine how the corruption occurred, please
// make a bug report and assign it to Dale Lear.
ON_ERROR("corrupt buffer - previous segment's position1 !- segment's position0.");
}
seg_size = seg->m_segment_position1 - seg->m_segment_position0;
if ( 0 == seg_size )
{
// If you can determine how the corruption occurred, please
// make a bug report and assign it to Dale Lear.
ON_ERROR("corrupt buffer - empty segment buffer.");
continue;
}
if ( seg_size + size > m_buffer_size )
{
if ( seg != m_last_segment || seg->m_next_segment )
{
// If you can determine how the corruption occurred, please
// make a bug report and assign it to Dale Lear.
ON_ERROR("corrupt buffer - segments contain more bytes than m_buffer_size.");
}
seg_size = m_buffer_size - size;
}
current_remainder = ON_CRC32(current_remainder,(size_t)seg_size,seg->m_segment_buffer);
size += seg_size;
if ( size >= m_buffer_size )
{
if ( seg != m_last_segment || 0 != seg->m_next_segment || size > m_buffer_size )
{
// If you can determine how the corruption occurred, please
// make a bug report and assign it to Dale Lear.
ON_ERROR("corrupt buffer - list of segments is too long.");
}
break;
}
}
return current_remainder;
}
ON__UINT64 ON_Buffer::CurrentPosition() const
{
return m_current_position;
}
bool ON_Buffer::SetCurrentSegment( bool bWritePending )
{
// When ON_Buffer::Write() needs to write at least on byted, it
// calls ON_Buffer::SetCurrentSegment(true).
// In this case true is returned in all cases unless the information
// in the ON_Buffer class is corrupt.
// When ON_Buffer::Read() needs to read a at least one byte, it
// calls ON_Buffer::SetCurrentSegment(false).
// In this case, true is returned when m_current_position < m_buffer_size
// and false is returned in all other cases.
//
// If seeks have occurred since the last read or write, m_current_segment
// and m_current_segment_offset may need to be updated.
//
if ( 0 == m_current_segment )
m_current_segment = (m_current_position <= m_buffer_size/2) ? m_first_segment : m_last_segment;
if ( !bWritePending && m_current_position >= m_buffer_size )
{
m_current_segment = 0;
return false; // cannot read past end of buffer
}
if ( 0 != m_current_segment
&& m_current_segment->m_segment_position0 <= m_current_position
&& m_current_position < m_current_segment->m_segment_position1
)
{
// The current position is inside of m_current_segment.
// This happens most of the time which is why this code is at the top
// of this function.
return true;
}
if ( 0 == m_first_segment )
{
// m_current_position can be > 0 if we are writing
m_current_segment = 0;
return bWritePending;
}
if ( 0 == m_last_segment )
{
m_current_segment = 0;
ON_ERROR("Corrupt ON_Buffer");
return false;
}
if ( m_current_position >= m_last_segment->m_segment_position1 )
{
m_current_segment = 0;
return bWritePending;
}
while ( m_current_position < m_current_segment->m_segment_position0 )
{
m_current_segment = m_current_segment->m_prev_segment;
if ( 0 == m_current_segment )
{