使用soui开发的mbc,只支持windows版本
w1146869587
2022-01-24 0408576e9da10015ffa9da0079b8c985113ce4b3
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
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2011 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////
 
/**
* \file ZipStorage.h
* Includes the CZipStorage class.    
*
*/
 
#if !defined(ZIPARCHIVE_ZIPSTORAGE_DOT_H)
#define ZIPARCHIVE_ZIPSTORAGE_DOT_H
 
#if _MSC_VER > 1000
    #pragma once
    #if defined ZIP_HAS_DLL
        #pragma warning (push)
        #pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients of class
    #endif
#endif
 
#include "ZipFile.h"    
#include "ZipAutoBuffer.h"
#include "ZipString.h"
#include "ZipMemFile.h"
#include "ZipExport.h"
#include "ZipCallback.h"
#include "BitFlag.h"
#include "ZipSplitNamesHandler.h"
#include "ZipException.h"
#include "ZipCollections.h"
 
/**
    Represents the storage layer for an archive.
*/
class ZIP_API CZipStorage
{
    friend class CZipArchive;
    friend class CZipCentralDir;
public:
    /**
        Storage state.
    */
    enum State
    {
        stateOpened            = 0x0001,                    ///< The storage file is opened.
        stateReadOnly        = 0x0002,                    ///< The storage file is opened as read-only.
        stateAutoClose        = 0x0004,                    ///< The storage file will be closed when the storage is closed.
        stateExisting        = 0x0008,                    ///< The storage file existed before opening.
        stateSegmented        = 0x0010,                    ///< The current archive is segmented.
        stateSplit            = stateSegmented | 0x0020,    ///< The current archive is split.
        stateBinarySplit    = stateSplit     | 0x0040,    ///< The current archive is binary split.
        stateSpan            = stateSegmented | 0x0080    ///< The current archive is spanned.
    };    
    
    /**
        The direction of the seeking operation.
 
        \see
            CZipStorage::Seek
    */
    enum SeekType
    {
        seekFromBeginning, ///< Start seeking from the beginning of a file.
        seekFromEnd, ///< Start seeking from the end of a file.
        /**
            Start seeking from the current position in the archive.
            This value can cause a volume change when a segmented archive is opened for reading.
        */
        seekCurrent
    };
    CZipStorage();
    virtual ~CZipStorage();
 
    void Initialize();
    /**
        Opens a new or existing archive in memory.
        The meaning for the parameters is the same as in the CZipArchive::Open(CZipAbstractFile& , int, bool) method.
    */
    void Open(CZipAbstractFile& af, int iMode, bool bAutoClose);
 
    /**
        Opens or creates an archive.
 
        The meaning for the parameters is the same as in the CZipArchive::Open(LPCTSTR, int, ZIP_SIZE_TYPE) method.
    */
    void Open(LPCTSTR lpszPathName, int iMode, ZIP_SIZE_TYPE uVolumeSize);
 
 
    /**
        Closes a segmented archive in creation and reopens it as an existing segmented archive.
        No modifications are allowed afterwards.
        The archive may also turn out to be a not segmented archive.
    */
    void FinalizeSegm();
 
    
    /**
        Called only by CZipCentralDir::Read when opening an existing archive.
 
        \param    uLastVolume
            The number of the volume the central directory is on.
    */
    void UpdateSegmMode(ZIP_VOLUME_TYPE uLastVolume);
 
    /**
        Ensures than in a segmented archive, there is enough free space on the current volume.
 
        \param uNeeded
            The size of the required free space in bytes.
 
        \return
            The number of free bytes on the current volume.
 
    */
    ZIP_SIZE_TYPE AssureFree(ZIP_SIZE_TYPE uNeeded);
 
    /**
        Writes a chunk of data to the archive.
 
        \param    pBuf
            The buffer with data.
 
        \param    iSize
            The number of bytes to write.
 
        \param    bAtOnce
            If \c true, the whole chunk must fit in the current volume.
            If there is not enough free space, a volume change is performed.
 
    */
    void Write(const void *pBuf, DWORD iSize, bool bAtOnce);
 
    /** 
        Returns the total size currently occupied by the archive.
 
        \return
            The length of the current archive file increased by the number of bytes in the write buffer.    
    */
    ZIP_SIZE_TYPE GetOccupiedSpace() const
    {
        return ZIP_SIZE_TYPE(m_pFile->GetLength() + m_uBytesInWriteBuffer);
    }
 
    /**
        The same as the CZipArchive::IsClosed method.
    */
    bool IsClosed(bool bArchive) const 
    {
        if (bArchive)
            return !m_state.IsSetAny(stateOpened);
        else
            // assume not auto-close files always opened
            return !m_pFile || m_state.IsSetAny(stateAutoClose) && m_pFile->IsClosed();
    }
 
    /**
        Reads a chunk of data from the archive.
 
        \param    pBuf
            The buffer to receive the data.
 
        \param    iSize
            The number of bytes to read.
 
        \param    bAtOnce
            If \c true, no volume change is allowed during reading. 
            If the requested number of bytes cannot be read from a single volume, an exception is thrown.
 
    */
    DWORD Read(void* pBuf, DWORD iSize, bool bAtOnce);
 
    /**
        Returns the position in the file, taking into account the number of bytes in the write buffer 
        and the number of bytes before the archive.         
 
        \return 
            The position in the file.
 
        \note
            For binary split archives, it returns the position from the beginning of the first part.
    */
    ZIP_SIZE_TYPE GetPosition() const
    {
        ZIP_SIZE_TYPE uPos = (ZIP_SIZE_TYPE)(m_pFile->GetPosition()) + m_uBytesInWriteBuffer;
        if (m_uCurrentVolume == 0)
            uPos -= m_uBytesBeforeZip;
        else if (IsBinarySplit()) // not for the first volume
        {
            ZIP_VOLUME_TYPE uVolume = m_uCurrentVolume;
            ASSERT(m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)(uVolume - 1));
            do
            {
                uVolume--;
                uPos += (ZIP_SIZE_TYPE)m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
            }
            while (uVolume > 0);
        }
        return uPos;
    }
 
 
    /**
        Flushes the data from the read buffer to the disk.
 
    */
    void Flush();
 
 
    /**
        Forces any data remaining in the file buffer to be written to the disk.
    */
    void FlushFile()
    {
        if (!IsReadOnly())
            m_pFile->Flush();
    }
 
    void FlushBuffers()
    {
        Flush();
        FlushFile();
    }
 
    /**
        Changes volumes during writing to a segmented archive.
 
        \param    uNeeded
            The number of bytes needed in the volume.
 
    */
    void NextVolume(ZIP_SIZE_TYPE uNeeded);
 
 
    /**
        Returns a zero-based number of the current volume.
    */
    ZIP_VOLUME_TYPE GetCurrentVolume() const {return m_uCurrentVolume;}
 
 
    /**
        Changes the volume during extract operations.
 
        \param    uNumber
            A zero-based number of the requested volume.
    */
    void ChangeVolume(ZIP_VOLUME_TYPE uNumber);
 
    /**
        Changes the current volume to the next volume during extract operations.
    */
    void ChangeVolume()
    {
        ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1));
    }
 
    /**
        Changes the current volume to the previous volume during extract operations.
    */
    void ChangeVolumeDec()
    {
        if (m_uCurrentVolume == 0)
            ThrowError(CZipException::badZipFile);
        ChangeVolume((ZIP_VOLUME_TYPE)(m_uCurrentVolume - 1));
    }
    
    /**
        Returns the value indicating whether the archive is a split archive (binary or regular).
 
        \return
            \c true, if the archive is a split archive; \c false otherwise.
    */
    bool IsSplit() const
    {
        return m_state.IsSetAll(stateSplit);
    }
 
    /**
        Returns the value indicating whether the archive is a binary split archive.
 
        \return
            \c true, if the archive is a binary split archive; \c false otherwise.
    */
    bool IsBinarySplit() const
    {
        return m_state.IsSetAll(stateBinarySplit);
    }
 
    /**
        Returns the value indicating whether the archive is a regular split archive (not binary).
 
        \return
            \c true, if the archive is a regular split archive; \c false otherwise.
    */
    bool IsRegularSplit() const
    {
        return m_state.IsSetAll(stateSplit) && !m_state.IsSetAll(stateBinarySplit);
    }
 
    /**
        Returns the value indicating whether the archive is a spanned archive.
 
        \return
            \c true, if the archive is a spanned archive; \c false otherwise.
    */
    bool IsSpanned() const
    {
        return m_state.IsSetAll(stateSpan);
    }
 
    /**
        The same as the CZipArchive::IsReadOnly method.
    */
    bool IsReadOnly() const
    {
        return m_state.IsSetAny(stateReadOnly) || IsExistingSegmented();
    }
    
    /**
        Returns the value indicating whether the archive is an existing segmented archive.
 
        \return
            \c true, if the archive is an existing segmented archive; \c false otherwise.
    */
    bool IsExistingSegmented() const
    {
        return m_state.IsSetAll(stateSegmented | stateExisting);
    }
 
    /**
        Returns the value indicating whether the archive is a new segmented archive.
 
        \return
            \c true, if the archive is a new segmented archive; \c false otherwise.
    */
    bool IsNewSegmented() const
    {
        return m_state.IsSetAny(stateSegmented) && !IsExisting();
    }
 
    /**
        Returns the value indicating whether the archive is a segmented archive.
 
        \return
            \c true, if the archive is a segmented archive; \c false otherwise.
    */
    bool IsSegmented() const
    {
        return m_state.IsSetAny(stateSegmented);
    }
 
    /**
        Returns the value indicating whether the archive is an existing archive.
 
        \return
            \c true, if the archive is an existing archive; \c false, if the archive is a new archive.
    */
    bool IsExisting() const
    {
        return m_state.IsSetAny(stateExisting);
    }
 
    /**
        Sets the split names handler.
 
        \see
            CZipArchive::SetSplitNamesHandler(CZipSplitNamesHandler*, bool)
        \see
            CZipSplitNamesHandler
    */
    bool SetSplitNamesHandler(CZipSplitNamesHandler* pNames, bool bAutoDelete)
    {
        if (m_state != 0)
        {
            ZIPTRACE("%s(%i) : The archive is already opened.\n");
            return false;
        }
        ClearSplitNames();
        m_pSplitNames = pNames;
        m_bAutoDeleteSplitNames = bAutoDelete;
        return true;
    }
 
    /**
        Returns the current split names handler.
 
        \return
            The current split names handler.
        \see
            CZipSplitNamesHandler
    */
    CZipSplitNamesHandler* GetSplitNamesHandler()
    {
        return m_pSplitNames;
    }
 
    /**
        Returns the current split names handler (const).
 
        \return
            The current split names handler.
        \see
            CZipSplitNamesHandler
    */
    const CZipSplitNamesHandler* GetSplitNamesHandler() const
    {
        return m_pSplitNames;
    }
    
    /**
        Performs the seeking operation on the #m_pFile.
 
        \param lOff
            The new position in the file.
 
        \param iSeekType
            The direction of the seek operation.
            It can be one of the #SeekType values.
    */
    ULONGLONG Seek(ULONGLONG lOff, SeekType iSeekType = seekFromBeginning);    
 
    /**
        Performs the seeking operation in a binary split archive.
 
        \param lOff
            The offset to move the file pointer.
 
        \param bSeekToBegin
            If \c true, the file pointer is moved to the beginning before seeking.
            If \c false, the file pointer is moved relatively to the current position.
    */
    void SeekInBinary(ZIP_FILE_SIZE lOff, bool bSeekToBegin = false);    
 
    /**
        Returns the number of free bytes on the current volume.    
 
        \return 
            The number of free bytes on the current volume.
    */
    ZIP_SIZE_TYPE VolumeLeft() const;
    
    /**    
        Closes the storage.
 
        \param    bWrite
            Set to \c false, if the storage should not perform any write operations.
        \param bGetLastVolumeName
            Set to \c true, if the storage should return the path.
 
        \return
            The file path of the archive or of the last volume in the archive.
            Only if \a bGetLastVolumeName is set to \c true.
 
    */
    CZipString Close(bool bWrite, bool bGetLastVolumeName = false);
 
    /**
        Represents the physical storage for the archive (or the current archive segment in segmented archives).
    */
    CZipAbstractFile* m_pFile;
 
    /**
        The signature of the extended header.
    */
    static char m_gszExtHeaderSignat[];
 
    ZipArchiveLib::CBitFlag& GetState()
    {
        return m_state;
    }
 
protected:
 
    /**
        Returns the file offset after the last data byte in the archive.
 
        \return 
            The file offset after the last data byte in the archive.
    */
    ZIP_SIZE_TYPE GetLastDataOffset()
    {
        return (ZIP_SIZE_TYPE)m_pFile->GetLength() - m_uBytesBeforeZip;
    }
    
    /**
        Reverse-finds the location of the given signature starting from the current position in file.
 
        \param szSignature
            The signature to locate.
 
        \param uMaxDepth
            The maximum number of bytes to search for \a szSignature.
 
        \return
            The location of the signature.
 
    */
    ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth);
        
 
    /**
        Flushes without writing. It can be used only on not segmented archives.
    */
    void EmptyWriteBuffer()
    {
        m_uBytesInWriteBuffer = 0;
    }
 
    /**
        Opens a physical file.
 
        \param    lpszName
            The name of the file to open.
 
        \param    uFlags
            The file open flags.
 
        \param    bThrow
            If \c true, throw an exception in case of failure.
 
        \return
            \c true if successful; \c false otherwise.
    */
    bool OpenFile(LPCTSTR lpszName, UINT uFlags, bool bThrow = true);
 
    /**
        Renames the last segment file in a split archive when finalizing the archive.
 
        \return
            The name of the last segment.
    */
    CZipString RenameLastFileInSplitArchive();
 
    /**
        Writes data to the internal buffer.
 
        \param    *pBuf
            The buffer to copy the data from.
 
        \param    uSize
            The number of bytes to write.
 
    */
    void WriteInternalBuffer(const char *pBuf, DWORD uSize);
 
    /**
        Returns the free space size on the current removable disk.
 
        \return
            The free space in bytes.
    */
    ZIP_SIZE_TYPE GetFreeVolumeSpace() const;
 
    /**
        Calls the segmented callback object.
        Throws an exception if the callback method returns \c false.
 
        \param uNeeded
            The minimum number of free bytes required on the disk.
 
        \param    iCode
            The code to be passed to the callback method.
 
        \param    szTemp
            The string to be used as a filename (as an argument
            in the CZipException::Throw method) when an exception must be thrown.
 
                \see
            CZipArchive::SetSegmCallback
    */
    void CallCallback(ZIP_SIZE_TYPE uNeeded, int iCode, CZipString szTemp);
 
    /**
        Changes a file when processing a split archive.
    */
    CZipString ChangeSplitRead();
 
    /**
        Changes a disk when processing a spanned archive.
    */
    CZipString ChangeSpannedRead();
 
    /**
        Returns the free space left in the write buffer.
 
        \return
            The free space in bytes.
    */
    DWORD GetFreeInBuffer() const {return m_pWriteBuffer.GetSize() - m_uBytesInWriteBuffer;}    
 
    /**
        The value it holds, depends on the current mode:        
        - An opened existing split archive: the number of the last volume ( usually the one with the "zip" extension).
        - A split archive in creation: the size of the volume.
 
        This method is used only when processing split archives.
    */
    ZIP_SIZE_TYPE m_uSplitData;
    
    /**
        The number of bytes available in the write buffer.        
    */
    DWORD m_uBytesInWriteBuffer;
 
    /**
        The value it holds depends on the segmentation mode:
        - A split archive: the total size of the current volume.
        - A spanned archive: the free space on the current volume.
    */
    ZIP_SIZE_TYPE m_uCurrentVolSize;
 
    /**
        The write buffer caching data.
    */
    CZipAutoBuffer m_pWriteBuffer;
 
    /**
        Stores the number of bytes that have been written physically to the current segment.
        Used only when processing a segmented archive in creation.
    */
    ZIP_SIZE_TYPE m_uBytesWritten;    
 
    /**
        The current volume number in a segmented archive.
        The value is zero-based.
    */
    ZIP_VOLUME_TYPE m_uCurrentVolume;
    
    /**
        The number of bytes before the actual zip archive in a file.
        \see
            CZipArchive::GetBytesBeforeZip
    */
    ZIP_SIZE_TYPE m_uBytesBeforeZip;
 
 
    /**
        The size of the write buffer.         
 
        \see
            CZipArchive::SetAdvanced
    */
    int m_iWriteBufferSize;
 
    /**
        The size of the buffer used in searching for the central directory.
 
        \see
            CZipArchive::SetAdvanced
    */
    int m_iLocateBufferSize;        
 
    /**
        A callback object called when there is a need for a volume change
        in a spanned archive.
 
        \see
            CZipArchive::SetSegmCallback
    */
    CZipSegmCallback* m_pSpanChangeVolumeFunc;
 
    /**
        A callback object called when there is a need for a volume change
        in a split archive.
 
        \see
            CZipArchive::SetSegmCallback
    */
    CZipSegmCallback* m_pSplitChangeVolumeFunc;
    
private:
    ZIP_FILE_USIZE LocateSignature(char* szSignature, ZIP_SIZE_TYPE uMaxDepth, int& leftToFind, bool& found, ZIP_FILE_USIZE uFileLength);
    CZipString GetSplitVolumeName(bool bLast)
    {
        if (m_pSplitNames == NULL)
        {
            ThrowError(CZipException::genericError);
        }
        int flags = bLast ? CZipSplitNamesHandler::flLast : CZipSplitNamesHandler::flNone;
        if (IsExisting())
            flags |= CZipSplitNamesHandler::flExisting;
        return m_pSplitNames->GetVolumeName(m_szArchiveName, (ZIP_VOLUME_TYPE)(m_uCurrentVolume + 1), flags);
    }
 
    void ClearSplitNames()
    {
        if (m_pSplitNames)
        {
            if (m_bAutoDeleteSplitNames)
                delete m_pSplitNames;
            m_pSplitNames = NULL;
            m_bAutoDeleteSplitNames = false;
        }
    }
 
    void ClearCachedSizes()
    {
        if (m_pCachedSizes)
        {
            delete m_pCachedSizes;
            m_pCachedSizes = NULL;
        }
    }
 
    void EnsureSplitNames()
    {
        if (IsSplit())
        {
            if (m_pSplitNames == NULL)
            {
                m_bAutoDeleteSplitNames = true;
                if (m_state.IsSetAll(stateBinarySplit))
                    m_pSplitNames = new CZipBinSplitNamesHandler();
                else
                    m_pSplitNames = new CZipRegularSplitNamesHandler();
            }
            m_pSplitNames->Initialize(m_szArchiveName);
        }
    }
 
    ZIP_FILE_USIZE GetCachedSize(ZIP_VOLUME_TYPE uVolume)
    {
        ASSERT(m_pCachedSizes);
        if (m_pCachedSizes->GetSize() > (ZIP_ARRAY_SIZE_TYPE)uVolume)
            return m_pCachedSizes->GetAt((ZIP_ARRAY_SIZE_TYPE)uVolume);
        ThrowError(CZipException::genericError);
        // for a compiler
        return 0;
    }
 
    void CacheSizes();
 
    ZipArchiveLib::CBitFlag m_state;
    CZipSegmCallback* m_pChangeVolumeFunc;
    CZipString m_szArchiveName;
    CZipFile m_internalfile;
    CZipSplitNamesHandler* m_pSplitNames;
    CZipArray<ZIP_FILE_USIZE>* m_pCachedSizes;
    bool m_bAutoDeleteSplitNames;
    static const ZIP_FILE_USIZE SignatureNotFound;
    void ThrowError(int err) const;
};
 
#if (_MSC_VER > 1000) && (defined ZIP_HAS_DLL)
    #pragma warning (pop)    
#endif
 
 
#endif // !defined(ZIPARCHIVE_ZIPSTORAGE_DOT_H)