admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of xlslib -- A multiplatform, C/C++ library
 * for dynamic generation of Excel(TM) files.
 *
 * Copyright 2004 Yeico S. A. de C. V. All Rights Reserved.
 * Copyright 2008-2013 David Hoerl All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''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 David Hoerl OR
 * CONTRIBUTORS 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.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 
#ifdef __BCPLUSPLUS__
#include <memory.h>
// string.h needed for memcpy(). RLN 111215
// This may be applicable to other compilers as well.
#endif
 
#include "../xlslib/record.h"
#include "../xlslib/unit.h"
#include "../xlslib/globalrec.h"
#include "../xlslib/rectypes.h"
#include "../xlslib/datast.h"
 
using namespace xlslib_core;
using namespace xlslib_strings;
 
/*
 *********************************************************************************
 *  CUnit class implementation
 *********************************************************************************
 */
 
// Default constructor
CUnit::CUnit(CDataStorage &datastore) :
    m_Store(datastore),
    m_Index(INVALID_STORE_INDEX),
    m_Backpatching_Level(0),
    m_AlreadyContinued(false)
{
    datastore.Push(this);
}
 
CUnit::CUnit(const CUnit& orig) :
    m_Store(orig.m_Store),
    m_Index(INVALID_STORE_INDEX),
    m_Backpatching_Level(0)
{
    XL_ASSERT(m_Index == INVALID_STORE_INDEX);
    if (orig.m_Index != INVALID_STORE_INDEX) {
        m_Index = m_Store.RequestIndex(orig.GetDataSize());
        if (m_Index != INVALID_STORE_INDEX)    {
            XL_ASSERT(m_Index >= 0);
            XL_ASSERT(m_Store[m_Index].GetSize() >= orig.GetDataSize());
            memcpy(m_Store[m_Index].GetBuffer(), orig.GetBuffer(), orig.GetDataSize());
        } else {
            // TODO: mark error. Should we throw an exception from this constructor?
        }
    }
}
 
CUnit& CUnit::operator=(const CUnit& right)
{
    if(this == &right) {
        return *this;
    }
 
    size_t len = right.GetDataSize();
    if (m_Index == INVALID_STORE_INDEX && right.m_Index != INVALID_STORE_INDEX)    {
        m_Index = m_Store.RequestIndex(len);
        if (m_Index == INVALID_STORE_INDEX)    {
            // TODO: mark error.
        }
    } else
    if (right.m_Index != INVALID_STORE_INDEX)    {
        signed8_t ret = m_Store[m_Index].Resize(len);
        if (ret != NO_ERRORS) {
            // TODO mark error
        }
        XL_ASSERT(ret == NO_ERRORS);
    }
    XL_ASSERT(right.m_Index != INVALID_STORE_INDEX && m_Index != INVALID_STORE_INDEX);
    if (right.m_Index != INVALID_STORE_INDEX && m_Index != INVALID_STORE_INDEX)    {
        XL_ASSERT(m_Store[m_Index].GetSize() >= len);
        memcpy(m_Store[m_Index].GetBuffer(), right.GetBuffer(), len);
        m_Store[m_Index].SetDataSize(len);
    }
 
    return *this;
}
 
// Default destructor
CUnit::~CUnit()
{
    ResetDataStorage();
}
 
void CUnit::ResetDataStorage(void)
{
    if (m_Index != INVALID_STORE_INDEX)    {
        XL_ASSERT(m_Index >= 0 ? !m_Store[m_Index].IsSticky() : 1);
        XL_ASSERT(m_Index < 0 ? m_Store[m_Index].IsSticky() : 1);
        if (m_Index >= 0) {
            m_Store[m_Index].Reset();
        }
    }
    m_Index = INVALID_STORE_INDEX;
}
 
const size_t CUnit::DefaultInflateSize = 10;
 
signed8_t CUnit::SetValueAt8(unsigned8_t newval, unsigned32_t index)
{
    signed8_t errcode = NO_ERRORS;
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
 
    if(data != NULL) {
        XL_ASSERT(m_Store[m_Index].GetSize() >= datasize);
        if (index < datasize) {
            data[index] = newval;
        } else {
            errcode = ERR_INVALID_INDEX;
        }
    } else {
        errcode = ERR_DATASTORAGE_EMPTY;
    }
 
    return errcode;
}
 
signed8_t CUnit::AddValue16(unsigned16_t newval)
{
    signed8_t errcode = NO_ERRORS;
 
    if(AddValue8(BYTE_0(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_1(newval))) { errcode = GENERAL_ERROR; }
 
    return errcode;
}
 
signed8_t CUnit::AddValue32(unsigned32_t newval)
{
    signed8_t errcode = NO_ERRORS;
 
    if(AddValue8(BYTE_0(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_1(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_2(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_3(newval))) { errcode = GENERAL_ERROR; }
 
    return errcode;
}
 
signed8_t CUnit::AddValue64(unsigned64_t newval)
{
    signed8_t errcode = NO_ERRORS;
 
    if(AddValue8(BYTE_0(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_1(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_2(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_3(newval))) { errcode = GENERAL_ERROR; }
 
    if(AddValue8(BYTE_4(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_5(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_6(newval))) { errcode = GENERAL_ERROR; }
    if(AddValue8(BYTE_7(newval))) { errcode = GENERAL_ERROR; }
 
    return errcode;
}
 
unsigned64_t CUnit::EncodeFP2I64(double newval)
{
//#include "common/xls_pshpack1.h"
 
    union
    {
        double f;
        unsigned64_t i;
        unsigned8_t b[8];
    } v;
 
//#include "common/xls_poppack.h"
 
    XL_ASSERT(sizeof(v.f) == sizeof(v.i));
    XL_ASSERT(sizeof(v.f) == sizeof(v.b));
    XL_ASSERT(sizeof(v) == sizeof(v.f));
 
    v.f = newval;
 
    return v.i;
}
 
signed8_t CUnit::AddValue64FP(double newval)
{
    unsigned64_t i = EncodeFP2I64(newval);
 
    return AddValue64(i);
}
 
signed8_t CUnit::SetValueAt16(unsigned16_t newval, unsigned32_t index)
{
    signed8_t errcode = NO_ERRORS;
 
    if(SetValueAt8(BYTE_0(newval), index  )) {errcode = GENERAL_ERROR; }
    if(SetValueAt8(BYTE_1(newval), index+1)) { errcode = GENERAL_ERROR; }
 
    return errcode;
}
 
signed8_t CUnit::SetValueAt32(unsigned32_t newval, unsigned32_t index)
{
    signed8_t errcode = NO_ERRORS;
 
    if(SetValueAt8(BYTE_0(newval), index  )) {errcode = GENERAL_ERROR; }
    if(SetValueAt8(BYTE_1(newval), index+1)) { errcode = GENERAL_ERROR; }
    if(SetValueAt8(BYTE_2(newval), index+2)) { errcode = GENERAL_ERROR; }
    if(SetValueAt8(BYTE_3(newval), index+3)) { errcode = GENERAL_ERROR; }
 
    return errcode;
}
 
signed8_t CUnit::GetValue16From(unsigned16_t* val, unsigned32_t index) const
{
    signed8_t errcode = NO_ERRORS;
 
    *val = (unsigned16_t)(operator[](index) +
                          operator[](index+1)*0x0100U);
 
    return errcode;
}
 
signed8_t CUnit::GetValue32From(unsigned32_t* val, unsigned32_t index) const
{
    signed8_t errcode = NO_ERRORS;
    // Yikes! this was signed16_t - DFH
    *val = (unsigned32_t)(operator[](index)*0x00000001U +
                          operator[](index+1)*0x00000100U +
                          operator[](index+2)*0x00010000U +
                          operator[](index+3)*0x01000000U); // Yikes again, it was
    return errcode;
}
 
signed8_t CUnit::GetValue8From(unsigned8_t* dst, unsigned32_t index) const
{
    signed8_t errcode = NO_ERRORS;
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
 
    if(dst != NULL) {
        XL_ASSERT(m_Store[m_Index].GetSize() >= datasize);
        if (index < datasize) {
            *dst = data[index];
        } else {
            errcode = ERR_INVALID_INDEX;
        }
    } else {
        errcode = ERR_DATASTORAGE_EMPTY;
    }
 
    return errcode;
}
 
signed8_t CUnit::AddDataArray(const unsigned8_t* newdata, size_t size)
{
    signed8_t errcode = NO_ERRORS;
 
    if (m_Index == INVALID_STORE_INDEX)    {
        m_Index = m_Store.RequestIndex(size);
        if (m_Index == INVALID_STORE_INDEX)    {
            return GENERAL_ERROR;
        }
    }
 
    XL_ASSERT(GetSize() >= GetDataSize());
    size_t spaceleft = GetSize() - GetDataSize();
 
    if(spaceleft < size) { // allocate more space if new to-be-added array won't fit
        errcode = Inflate(size + GetDataSize());
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
 
    if(newdata != NULL) {
        // TODO: memmove() (not memcpy() as can be called from Append() <-- this += *this; fringe case where memcpy() would possibly fail!
        for(size_t i=0; i<size; i++) {
            XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
            data[datasize++] = newdata[i];
        }
 
        m_Store[m_Index].SetDataSize(datasize);
    } else {
        //No data to add. Do nothing
        if (size != 0) {
            return GENERAL_ERROR; // [i_a] at least report this very suspicious situation
        }
    }
 
    return errcode;
}
 
signed8_t CUnit::AddFixedDataArray(const unsigned8_t value, size_t size)
{
    signed8_t errcode = NO_ERRORS;
 
    if (m_Index == INVALID_STORE_INDEX)    {
        m_Index = m_Store.RequestIndex(size);
        if (m_Index == INVALID_STORE_INDEX)    {
            return GENERAL_ERROR;
        }
    }
 
    XL_ASSERT(GetSize() >= GetDataSize());
    size_t spaceleft = GetSize() - GetDataSize();
 
    if(spaceleft < size) { // allocate more space if new to-be-added array won't fit
        errcode = Inflate(size + GetDataSize());
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
 
    // The following can be a memset
    for(size_t i=0; i<size; i++) {
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = value;
    }
 
    m_Store[m_Index].SetDataSize(datasize);
 
    return errcode;
}
 
//    signed8_t AddDataArray (const unsigned16_t* newdata, size_t size);
//    signed8_t AddFixedDataArray (const unsigned16_t value, size_t size);
 
signed8_t CUnit::SetArrayAt(const unsigned8_t* newdata, size_t size, unsigned32_t index)
{
    signed8_t errcode = NO_ERRORS;
    size_t space = GetSize();
 
    if(space < size + index) { // allocate more space if new to-be-added array won't fit
        errcode = Inflate(size + index);
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    //size_t datasize = m_Store[m_Index].GetDataSize();
 
    if (newdata != NULL) {
        for (size_t i=0; i<size; i++) {
            XL_ASSERT(m_Store[m_Index].GetSize() > index);
            data[index++] = newdata[i];
        }
    }
 
    return errcode;
}
 
signed8_t CUnit::AddValue8(unsigned8_t newdata)
{
    XL_ASSERT(GetSize() >= GetDataSize());
    if(GetDataSize() >= GetSize()) {
        signed8_t errcode = Inflate(GetDataSize() + 1);
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
 
    data[datasize++] = newdata;
 
    m_Store[m_Index].SetDataSize(datasize);
 
    return NO_ERRORS;
}
 
signed8_t CUnit::AddUnicodeString(CGlobalRecords& gRecords, const std::string& str, XlsUnicodeStringFormat_t fmt)
{
    std::string::const_iterator    cBegin, cEnd;
    signed8_t errcode = NO_ERRORS;
    size_t strSize = 0;
    size_t strLen;
    size_t spaceleft;
    bool isASCII = CGlobalRecords::IsASCII(str);
 
    if (!isASCII) {
        u16string s16;
 
        XL_ASSERTS("Should never happen!");
 
        gRecords.char2str16(str, s16);
        return AddUnicodeString(s16, fmt);
    }
 
    strLen = str.length();
 
    switch (fmt) {
    case LEN2_FLAGS_UNICODE:            // RECTYPE_FORMAT, RECTYPE_LABEL -- 'regular'
        strSize = 2;
        strSize += 1;   // flags byte
        break;
 
    case LEN1_FLAGS_UNICODE:            // RECTYPE_BOUNDSHEET
        strSize = 1;
        strSize += 1;   // flags byte
        break;
 
    case NOLEN_FLAGS_UNICODE:            // RECTYPE_NAME
        strSize = 0;
        strSize += 1;   // flags byte
        break;
 
    default:
        XL_ASSERTS("should never go here!");
        break;
    }
    strSize += strLen;
 
    XL_ASSERT(GetSize() >= GetDataSize());
    spaceleft = GetSize() - GetDataSize();
    if(spaceleft < strSize)    { // allocate more space if new to-be-added array won't fit
        errcode = Inflate(strSize + GetDataSize());
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
    XL_ASSERT(data);
    //XL_ASSERT(datasize > strSize);
 
    switch (fmt) {
    case LEN2_FLAGS_UNICODE: // RECTYPE_FORMAT, RECTYPE_LABEL -- 'regular'
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = strLen & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (strLen >> 8) & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = 0x00; // ASCII
        break;
 
    case LEN1_FLAGS_UNICODE: // RECTYPE_BOUNDSHEET
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = strLen & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = 0x00; // ASCII
        break;
 
    case NOLEN_FLAGS_UNICODE: // RECTYPE_NAME
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = 0x00; // ASCII
        break;
 
    default:
        XL_ASSERTS("should never go here!");
        break;
    }
 
    cBegin     = str.begin();
    cEnd = str.end();
 
    while(cBegin != cEnd) {
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (unsigned8_t)*cBegin++;
    }
 
    m_Store[m_Index].SetDataSize(datasize);
 
    return errcode;
}
 
signed8_t CUnit::AddUnicodeString(const u16string& str16, XlsUnicodeStringFormat_t fmt)
{
    u16string::const_iterator cBegin, cEnd;
    signed8_t errcode = NO_ERRORS;
    size_t spaceleft;
    size_t strLen;
    bool isASCII;
    size_t strSize = UnicodeStringLength(str16, strLen, isASCII, fmt /* = LEN2_FLAGS_UNICODE */ );
 
    XL_ASSERT(GetSize() >= GetDataSize());
    spaceleft = GetSize() - GetDataSize();
    if(spaceleft < strSize)    { // allocate more space if new to-be-added array won't fit
        errcode = Inflate(strSize + GetDataSize());
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    size_t datasize = m_Store[m_Index].GetDataSize();
    XL_ASSERT(data);
    //XL_ASSERT(datasize > strSize);
 
    switch (fmt) {
    case LEN2_FLAGS_UNICODE: // RECTYPE_FORMAT, RECTYPE_LABEL -- 'regular'
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = strLen & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (strLen >> 8) & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (isASCII ? 0x00 : 0x01); // ASCII or UTF-16
        break;
 
    case LEN1_FLAGS_UNICODE: // RECTYPE_BOUNDSHEET
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = strLen & 0xFF;
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (isASCII ? 0x00 : 0x01); // ASCII or UTF-16
        break;
 
    case NOLEN_FLAGS_UNICODE: // RECTYPE_NAME
        XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
        data[datasize++] = (isASCII ? 0x00 : 0x01); // ASCII or UTF-16
        break;
 
    default:
        XL_ASSERTS("should never go here!");
        break;
    }
 
    cBegin    = str16.begin();
    cEnd    = str16.end();
 
    if (isASCII) {
        while(cBegin != cEnd) {
            unsigned16_t c = *cBegin++;
 
            XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
            data[datasize++] = static_cast<unsigned8_t>(c);
        }
    } else {
        while(cBegin != cEnd) {
            unsigned16_t c = *cBegin++;
 
            XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
            data[datasize++] = c & 0xFF;
            XL_ASSERT(m_Store[m_Index].GetSize() > datasize);
            data[datasize++] = (c >> 8) & 0xFF;
        }
    }
    m_Store[m_Index].SetDataSize(datasize);
 
    return errcode;
}
 
size_t CUnit::UnicodeStringLength(const u16string& str16, size_t& strLen, bool& isASCII, XlsUnicodeStringFormat_t fmt /* = LEN2_FLAGS_UNICODE */ )
{
    strLen = str16.length();
    isASCII = CGlobalRecords::IsASCII(str16);
    size_t strSize = strLen;
 
    switch (fmt) {
    case LEN2_FLAGS_UNICODE:            // RECTYPE_FORMAT, RECTYPE_LABEL -- 'regular'
        strSize += 2;
        strSize += 1;   // flags byte
        if (!isASCII) {
            strSize += strLen;  // UTF16 takes 2 bytes per char
        }
        break;
 
    case LEN1_FLAGS_UNICODE:            // RECTYPE_BOUNDSHEET
        strSize += 1;
        strSize += 1;   // flags byte
        if (!isASCII) {
            strSize += strLen;  // UTF16 takes 2 bytes per char
        }
        break;
 
    case NOLEN_FLAGS_UNICODE:            // RECTYPE_NAME
        //strSize = 0;
        strSize += 1;   // flags byte
        if (!isASCII) {
            strSize += strLen;  // UTF16 takes 2 bytes per char
        }
        break;
 
    default:
        XL_ASSERTS("should never go here!");
        break;
    }
 
    return strSize;
}
 
signed8_t CUnit::Inflate(size_t newsize)
{
    signed8_t errcode = NO_ERRORS;
 
    if (m_Index == INVALID_STORE_INDEX)    {
        XL_ASSERT(newsize > 0);
        m_Index = m_Store.RequestIndex(newsize);
        if (m_Index == INVALID_STORE_INDEX)    {
            return GENERAL_ERROR;
        }
    } else {
        XL_ASSERT(newsize > 0);
#if 0
        {
            size_t oldlen = m_Store[m_Index].GetSize();
            if (oldlen < 64) {
                increase = CUnit::DefaultInflateSize;
            } else {
                // bigger units grow faster: save on the number of realloc redimension operations...
                increase = oldlen / 2;
            }
        }
#endif
        XL_ASSERT(m_Index != INVALID_STORE_INDEX);
        errcode = m_Store[m_Index].Resize(newsize);
    }
 
    return errcode;
}
 
unsigned8_t& CUnit::operator[](const size_t index) const
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    unsigned8_t *data = m_Store[m_Index].GetBuffer();
    //size_t datasize = m_Store[m_Index].GetDataSize();
 
    XL_ASSERT(index < GetSize());       // DFH: need to read ahead when setting bits in 32bit words
    XL_ASSERT(index < GetDataSize());   // [i_a]
    //if(index >= datasize) printf("ERROR: Short read!! \n");
 
    return data[index];
}
 
CUnit& CUnit::operator +=(const CUnit& from)
{
    XL_VERIFY(NO_ERRORS == Append(from));
 
    return *this;
}
 
CUnit& CUnit::operator +=(unsigned8_t from)
{
    XL_VERIFY(NO_ERRORS == AddValue8(from));
 
    return *this;
}
 
signed8_t CUnit::Init(const unsigned8_t* data, const size_t size, const unsigned32_t datasz)
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    signed8_t ret = m_Store[m_Index].Init(data, size, datasz);
    return ret;
}
 
signed8_t CUnit::Append(const CUnit& newunit)
{
    XL_ASSERT(GetSize() >= GetDataSize());
    size_t spaceleft = GetSize() - GetDataSize();
    if(spaceleft < newunit.GetDataSize()) { // allocate more space if new to-be-added array won't fit
        signed8_t errcode = Inflate(GetDataSize() + newunit.GetDataSize());
        if (errcode != NO_ERRORS) {
            return errcode;
        }
    }
 
    return AddDataArray(newunit.GetBuffer(), newunit.GetDataSize());
}
 
signed8_t CUnit::InitFill(unsigned8_t data, size_t size)
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    return m_Store[m_Index].InitWithValue(data, size);
}
 
size_t CUnit::GetSize(void) const
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    return m_Store[m_Index].GetSize();
}
 
size_t CUnit::GetDataSize(void) const
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    return m_Store[m_Index].GetDataSize();
}
 
const unsigned8_t* CUnit::GetBuffer(void) const
{
    XL_ASSERT(m_Index != INVALID_STORE_INDEX);
    return m_Store[m_Index].GetBuffer();
}