File: ChunkedFile.cpp

package info (click to toggle)
nsis 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,496 kB
  • sloc: cpp: 39,326; ansic: 27,284; python: 1,386; asm: 712; xml: 409; pascal: 231; makefile: 225; javascript: 67
file content (98 lines) | stat: -rwxr-xr-x 3,835 bytes parent folder | download | duplicates (4)
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
//---------------------------------------------------------------------------
// ChunkedFile.cpp
//---------------------------------------------------------------------------
//                           -=* VPatch *=-
//---------------------------------------------------------------------------
// Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
//---------------------------------------------------------------------------
// Website: http://www.tibed.net/vpatch
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Unicode support by Jim Park -- 08/29/2007


#include "ChunkedFile.h"
#include "tchar.h"

using namespace std;

      ChunkedFile::ChunkedFile(bistream& f, TFileOffset fSize, TFileOffset chunkSize) :
        chunks(NULL) {

        chunkCount = fSize / chunkSize;
        tout << _T("[ChunkedFile] Filesize of ") << static_cast<unsigned int>(fSize) << _T(" gives ") << static_cast<unsigned int>(chunkCount) << _T(" chunks.\n");

        tout << _T("[ChunkedFile] Memory to be used by those chunks: ") << sizeof(FileChunk) * chunkCount << _T(" bytes...");
        if(chunkCount == 0) {
          chunks = NULL;
          return;
        }
        chunks = new FileChunk[chunkCount];
        tout << _T(" allocated.\n");

        unsigned char* data = new unsigned char[chunkSize];
        for(TFileOffset i = 0; i < chunkCount; i++) {
          f.read(reinterpret_cast<char*>(data),chunkSize);
          chunks[i].offset = i * chunkSize;
          calculateChecksum(data,chunkSize,chunks[i].checksum);
        }
        delete[] data;

        tout << _T("[ChunkedFile] Sorting chunks... ");
        std::sort(chunks,chunks + chunkCount);
        tout << _T("done.\n");
      }


bool ChunkedFile::search(TChunkChecksum key, TFileOffset* start) {
   // function:
   //   Searches sortedArray[first]..sortedArray[last] for key.
   // returns: index of the matching element if it finds key,
   //         otherwise  -(index where it could be inserted)-1.
   // parameters:
   //   sortedArray in  array of sorted (ascending) values.
   //   first, last in  lower and upper subscript bounds
   //   key         in  value to search for.
   // returns:
   //   index of key, or -insertion_position -1 if key is not
   //                 in the array. This value can easily be
   //                 transformed into the position to insert it.
  if(chunkCount == 0) return false;
  int first = 0;
  int last = chunkCount - 1;
  while (first <= last) {
    int mid = (first + last) / 2;  // compute mid point.
    if(key == chunks[mid].checksum) {
      while(true) {
        if(mid == 0) break;
        mid--;
        if(!(key == chunks[mid].checksum)) {
          mid++;
          break;
        }
      }
      *start = mid;
      return true;     // found it. return position
    }
    if (key < chunks[mid].checksum)
      last = mid - 1; // repeat search in bottom half.
    else
      first = mid + 1;  // repeat search in top half.
  }
  return false;    // failed to find key
}