Beispiel #1
0
void zStr::getCompressedText(long block, long entry, char **buf) const {

	__u32 size = 0;

	if (cacheBlockIndex != block) {
		__u32 start = 0;

		zdxfd->seek(block * ZDXENTRYSIZE, SEEK_SET);
		zdxfd->read(&start, 4);
		zdxfd->read(&size, 4);
		start = swordtoarch32(start);
		size = swordtoarch32(size);

		SWBuf buf;
		buf.setSize(size + 5);
		zdtfd->seek(start, SEEK_SET);
		zdtfd->read(buf.getRawData(), size);

		flushCache();

		unsigned long len = size;
		buf.setSize(size);
		rawZFilter(buf, 0); // 0 = decipher

		compressor->zBuf(&len, buf.getRawData());
		char *rawBuf = compressor->Buf(0, &len);
		cacheBlock = new EntriesBlock(rawBuf, len);
		cacheBlockIndex = block;
	}
	size = cacheBlock->getEntrySize(entry);
	*buf = (*buf) ? (char *)realloc(*buf, size*2 + 1) : (char *)malloc(size*2 + 1);
	strcpy(*buf, cacheBlock->getEntry(entry));
}
Beispiel #2
0
void zVerse::flushCache() const {
	if (dirtyCache) {
		__u32 idxoff;
		__u32 start, outstart;
		__u32 size, outsize;
		__u32 zsize, outzsize;

		idxoff = cacheBufIdx * 12;
		if (cacheBuf) {
			size = outsize = zsize = outzsize = strlen(cacheBuf);
			if (size) {
	//			if (compressor) {
	//				delete compressor;
	//				compressor = new LZSSCompress();
	//			}
				compressor->Buf(cacheBuf);
				unsigned long tmpSize;
				compressor->zBuf(&tmpSize);
				outzsize = zsize = tmpSize;

				SWBuf buf;
				buf.setSize(zsize + 5);
				memcpy(buf.getRawData(), compressor->zBuf(&tmpSize), tmpSize);
				outzsize = zsize = tmpSize;
				buf.setSize(zsize);
				rawZFilter(buf, 1); // 1 = encipher

				start = outstart = textfp[cacheTestament-1]->seek(0, SEEK_END);

				outstart  = archtosword32(start);
				outsize   = archtosword32(size);
				outzsize  = archtosword32(zsize);

				textfp[cacheTestament-1]->write(buf, zsize);

				idxfp[cacheTestament-1]->seek(idxoff, SEEK_SET);
				idxfp[cacheTestament-1]->write(&outstart, 4);
				idxfp[cacheTestament-1]->write(&outzsize, 4);
				idxfp[cacheTestament-1]->write(&outsize, 4);
			}
			free(cacheBuf);
			cacheBuf = 0;
		}
		dirtyCache = false;
	}
}
Beispiel #3
0
void zVerse::zReadText(char testmt, long start, unsigned short size, unsigned long ulBuffNum, SWBuf &inBuf) const {
	__u32 ulCompOffset = 0;	       // compressed buffer start
	__u32 ulCompSize   = 0;	             // buffer size compressed
	__u32 ulUnCompSize = 0;	          // buffer size uncompressed

	if (!testmt) {
		testmt = ((idxfp[0]) ? 1:2);
	}
	
	// assert we have and valid file descriptor
	if (compfp[testmt-1]->getFd() < 1)
		return;
	
	if (size && 
		!(((long) ulBuffNum == cacheBufIdx) && (testmt == cacheTestament) && (cacheBuf))) {
		//fprintf(stderr, "Got buffer number{%ld} versestart{%ld} versesize{%d}\n", ulBuffNum, ulVerseStart, usVerseSize);

		if (idxfp[testmt-1]->seek(ulBuffNum*12, SEEK_SET)!=(long) ulBuffNum*12)
		{
			fprintf(stderr, "Error seeking compressed file index\n");
			return;
		}
		if (idxfp[testmt-1]->read(&ulCompOffset, 4)<4)
		{
			fprintf(stderr, "Error reading ulCompOffset\n");
			return;
		}
		if (idxfp[testmt-1]->read(&ulCompSize, 4)<4)
		{
			fprintf(stderr, "Error reading ulCompSize\n");
			return;
		}
		if (idxfp[testmt-1]->read(&ulUnCompSize, 4)<4)
		{
			fprintf(stderr, "Error reading ulUnCompSize\n");
			return;
		}

		ulCompOffset  = swordtoarch32(ulCompOffset);
		ulCompSize  = swordtoarch32(ulCompSize);
		ulUnCompSize  = swordtoarch32(ulUnCompSize);

		if (textfp[testmt-1]->seek(ulCompOffset, SEEK_SET)!=(long)ulCompOffset)
		{
			fprintf(stderr, "Error: could not seek to right place in compressed text\n");
			return;
		}
		SWBuf pcCompText;
		pcCompText.setSize(ulCompSize+5);

		if (textfp[testmt-1]->read(pcCompText.getRawData(), ulCompSize)<(long)ulCompSize) {
			fprintf(stderr, "Error reading compressed text\n");
			return;
		}
		pcCompText.setSize(ulCompSize);
		rawZFilter(pcCompText, 0); // 0 = decipher
		
		unsigned long bufSize = ulCompSize;
		compressor->zBuf(&bufSize, pcCompText.getRawData());

		if (cacheBuf) {
			flushCache();
			free(cacheBuf);
		}
		
		unsigned long len = 0;
		compressor->Buf(0, &len);
		cacheBuf = (char *)calloc(len + 1, 1);
		memcpy(cacheBuf, compressor->Buf(), len);
		cacheBufSize = strlen(cacheBuf);  // TODO: can we just use len?
		cacheTestament = testmt;
		cacheBufIdx = ulBuffNum;
	}	
	
	inBuf = "";
	if ((size > 0) && cacheBuf && ((unsigned)start < cacheBufSize)) {
		inBuf.setFillByte(0);
		inBuf.setSize(size+1);
		strncpy(inBuf.getRawData(), &(cacheBuf[start]), size);
		inBuf.setSize(strlen(inBuf.c_str()));
	}
}
Beispiel #4
0
void zStr::flushCache() const {

	static const char nl[] = {13, 10};

	if (cacheBlock) {
		if (cacheDirty) {
			__u32 start = 0;
			unsigned long size = 0;
			__u32 outstart = 0, outsize = 0;

			const char *rawBuf = cacheBlock->getRawData(&size);
			compressor->Buf(rawBuf, &size);
			compressor->zBuf(&size);

			SWBuf buf;
			buf.setSize(size + 5);
			memcpy(buf.getRawData(), compressor->zBuf(&size), size); // 1 = encipher
			buf.setSize(size);
			rawZFilter(buf, 1); // 1 = encipher

			long zdxSize = zdxfd->seek(0, SEEK_END);
			unsigned long zdtSize = zdtfd->seek(0, SEEK_END);

			if ((cacheBlockIndex * ZDXENTRYSIZE) > (zdxSize - ZDXENTRYSIZE)) {	// New Block
				start = zdtSize;
			}
			else {
				zdxfd->seek(cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
				zdxfd->read(&start, 4);
				zdxfd->read(&outsize, 4);
				start = swordtoarch32(start);
				outsize = swordtoarch32(outsize);
				if (start + outsize >= zdtSize) {	// last entry, just overwrite
					// start is already set
				}
				else	if (size < outsize) {	// middle entry, but smaller, that's fine and let's preserve bigger size
					size = outsize;
				}
				else {	// middle and bigger-- we have serious problems, for now let's put it at the end = lots of wasted space
					start = zdtSize;
				}
			}



			outstart = archtosword32(start);
			outsize  = archtosword32((__u32)size);

			zdxfd->seek(cacheBlockIndex * ZDXENTRYSIZE, SEEK_SET);
			zdtfd->seek(start, SEEK_SET);
			zdtfd->write(buf, size);

			// add a new line to make data file easier to read in an editor
			zdtfd->write(&nl, 2);
			
			zdxfd->write(&outstart, 4);
			zdxfd->write(&outsize, 4);
		}
		delete cacheBlock;
		cacheBlock = 0;
	}
	cacheBlockIndex = -1;
	cacheDirty = false;
}