STDMETHODIMP CFolderInStream::Read(void *data, UInt32 size, UInt32 *processedSize) { UInt32 realProcessedSize = 0; while ((_curIndex < _refItem.NumItems || _fileIsOpen) && size > 0) { if (_fileIsOpen) { UInt32 localProcessedSize; RINOK(_stream->Read( ((Byte *)data) + realProcessedSize, size, &localProcessedSize)); _crc = CrcUpdate(_crc, ((Byte *)data) + realProcessedSize, localProcessedSize); if (localProcessedSize == 0) { RINOK(CloseStream()); continue; } realProcessedSize += localProcessedSize; size -= localProcessedSize; break; } else { RINOK(OpenStream()); } } if (processedSize != 0) *processedSize = realProcessedSize; return S_OK; }
value ml_crc32_update (value custom, value string) { CAMLparam2 (custom, string); u_int32_t *context = crc32_custom_val(custom); CrcUpdate (context, String_val(string), caml_string_length(string)); CAMLreturn (Val_unit); }
static i64 unzip_literal(rzip_control *control, void *ss, i64 len, uint32 *cksum) { i64 stream_read; uchar *buf; if (unlikely(len < 0)) failure_return(("len %lld is negative in unzip_literal!\n",len), -1); buf = (uchar *)malloc(len); if (unlikely(!buf)) fatal_return(("Failed to malloc literal buffer of size %lld\n", len), -1); stream_read = read_stream(control, ss, 1, buf, len); if (unlikely(stream_read == -1 )) { free(buf); fatal_return(("Failed to read_stream in unzip_literal\n"), -1); } if (unlikely(write_1g(control, buf, (size_t)stream_read) != (ssize_t)stream_read)) { free(buf); fatal_return(("Failed to write literal buffer of size %lld\n", stream_read), -1); } if (!HAS_MD5) *cksum = CrcUpdate(*cksum, buf, stream_read); if (!NO_MD5) md5_process_bytes(buf, stream_read, &control->ctx); free(buf); return stream_read; }
void XzCheck_Update(CXzCheck *p, const void *data, size_t size) { switch (p->mode) { case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break; case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break; case XZ_CHECK_SHA256: Sha256_Update(&p->sha, (const Byte *)data, size); break; } }
STDMETHODIMP COutStreamWithCRC::Write(const void *data, UInt32 size, UInt32 *processedSize) { HRESULT result = S_OK; if (_stream) result = _stream->Write(data, size, &size); if (_calculate) _crc = CrcUpdate(_crc, data, size); _size += size; if (processedSize != NULL) *processedSize = size; return result; }
STDMETHODIMP CSequentialInStreamWithCRC::Read(void *data, UInt32 size, UInt32 *processedSize) { UInt32 realProcessedSize; HRESULT result = _stream->Read(data, size, &realProcessedSize); _size += realProcessedSize; if (size > 0 && realProcessedSize == 0) _wasFinished = true; _crc = CrcUpdate(_crc, data, realProcessedSize); if(processedSize != NULL) *processedSize = realProcessedSize; return result; }
void XzCheck_Update(CXzCheck *p, const void *data, size_t size) { switch (p->mode) { case XZ_CHECK_CRC32: p->crc = CrcUpdate(p->crc, data, size); break; case XZ_CHECK_CRC64: p->crc64 = Crc64Update(p->crc64, data, size); break; case XZ_CHECK_SHA256: if ((p->sha)) cl_update_hash(p->sha, (void *)data, size); break; } }
void COutArchive::WriteBytes(const void *data, size_t size) { if (_countMode) _countSize += size; else if (_writeToStream) { _outByte.WriteBytes(data, size); _crc = CrcUpdate(_crc, data, size); } else _outByte2.WriteBytes(data, size); }
static i64 unzip_match(rzip_control *control, void *ss, i64 len, uint32 *cksum, int chunk_bytes) { i64 offset, n, total, cur_pos; uchar *buf, *off_buf; if (unlikely(len < 0)) failure_return(("len %lld is negative in unzip_match!\n",len), -1); total = 0; cur_pos = seekcur_fdout(control); if (unlikely(cur_pos == -1)) fatal_return(("Seek failed on out file in unzip_match.\n"), -1); /* Note the offset is in a different format v0.40+ */ offset = read_vchars(control, ss, 0, chunk_bytes); if (unlikely(offset == -1)) return -1; if (unlikely(seekto_fdhist(control, cur_pos - offset) == -1)) fatal_return(("Seek failed by %d from %d on history file in unzip_match\n", offset, cur_pos), -1); buf = (uchar *)malloc(len); if (unlikely(!buf)) fatal_return(("Failed to malloc match buffer of size %lld\n", len), -1); off_buf = buf; while (len) { n = MIN(len, offset); if (unlikely(read_fdhist(control, off_buf, (size_t)n) != (ssize_t)n)) { free(buf); fatal_return(("Failed to read %d bytes in unzip_match\n", n), -1); } if (unlikely(write_1g(control, off_buf, (size_t)n) != (ssize_t)n)) { free(buf); fatal_return(("Failed to write %d bytes in unzip_match\n", n), -1); } if (!HAS_MD5) *cksum = CrcUpdate(*cksum, off_buf, n); if (!NO_MD5) md5_process_bytes(off_buf, n, &control->ctx); len -= n; off_buf += n; total += n; } free(buf); return total; }
void cli_event_fastdata(cli_events_t *ctx, unsigned id, const void *data, uint32_t len) { struct cli_event *ev = get_event(ctx, id); if (!ev) return; if (ev->type != ev_data_fast) { cli_event_error_str(ctx, "cli_event_fastdata must be called with ev_data_fast"); return; } ev->u.v_int = CrcUpdate(ev->u.v_int, data, len); ev->count += len; /* when we are done we should invert all bits, but since we are just * comparing it doesn't matter */ }
STDMETHODIMP CInStreamWithCRC::Read(void *data, UInt32 size, UInt32 *processedSize) { UInt32 realProcessed = 0; HRESULT result = S_OK; if (_stream) result = _stream->Read(data, size, &realProcessed); _size += realProcessed; /* if (size != 0 && realProcessed == 0) _wasFinished = true; */ _crc = CrcUpdate(_crc, data, realProcessed); if (processedSize) *processedSize = realProcessed; return result; }
static HRESULT GetStreamCRC(ISequentialInStream *inStream, UInt32 &resultCRC) { UInt32 crc = CRC_INIT_VAL; const UInt32 kBufferSize = (1 << 14); Byte buffer[kBufferSize]; for (;;) { UInt32 realProcessedSize; RINOK(inStream->Read(buffer, kBufferSize, &realProcessedSize)); if (realProcessedSize == 0) { resultCRC = CRC_GET_DIGEST(crc); return S_OK; } crc = CrcUpdate(crc, buffer, (size_t)realProcessedSize); } }
HRESULT COutArchive::WriteBytes(const void *data, size_t size) { if (_mainMode) { if (_dynamicMode) _dynamicBuffer.Write(data, size); else _outByte.WriteBytes(data, size); _crc = CrcUpdate(_crc, data, size); } else { if (_countMode) _countSize += size; else RINOK(_outByte2.Write(data, size)); } return S_OK; }
UInt32 MY_FAST_CALL CrcCalc(const void *data, size_t size) { return CrcUpdate(CRC_INIT_VAL, data, size) ^ 0xFFFFFFFF; }
static size_t FolderOutStream_Write2(CFolderOutStream *p, const void *data, size_t size) { SizeT processedSize = 0; UInt16 *fullPath = NULL; SizeT pathLen = 0; SizeT curSize = 0; SRes res = SZ_OK; UInt32 i; SizeT dirLen = 0; if(p->outputDir) dirLen = wcslen(p->outputDir); while(size != 0) { if(p->file.handle == INVALID_HANDLE_VALUE) { UInt16 *filename; UInt32 index = p->startIndex + p->currentIndex; const CSzFileItem *fileItem = p->ar->db.Files + index; SizeT len = SzArEx_GetFileNameUtf16(p->ar, index, NULL); if(len + dirLen > pathLen) { free(fullPath); pathLen = len + dirLen; fullPath = (UInt16*)malloc(pathLen * sizeof(fullPath[0])); if(fullPath == 0) { res = SZ_ERROR_MEM; break; } if(p->outputDir) wcscpy_s(fullPath,pathLen,p->outputDir); } filename = fullPath + dirLen; SzArEx_GetFileNameUtf16(p->ar, index, filename); for (i = 0; fullPath[i] != 0 && i<pathLen; i++) if (fullPath[i] == '/') { fullPath[i] = 0; CreateDirectoryW(fullPath,NULL); fullPath[i] = CHAR_PATH_SEPARATOR; } if (fileItem->IsDir) { CreateDirectoryW(fullPath,NULL); continue; } else if(OutFile_OpenW(&p->file, fullPath)) { res = SZ_ERROR_FAIL; break; } #ifdef _SZ_ALLOC_DEBUG wprintf(L"\nExtract: %s", fullPath); #endif p->fileSize = fileItem->Size; p->checkCrc = fileItem->CrcDefined; p->crc = CRC_INIT_VAL; } curSize = size < p->fileSize ? size : (SizeT)p->fileSize; if(S_OK != File_Write(&p->file, data, &curSize)) { File_Close(&p->file); res = SZ_ERROR_FAIL; break; } if(p->checkCrc) p->crc = CrcUpdate(p->crc,data,curSize); data = (const Byte*)data + curSize; size -= curSize; p->fileSize -= curSize; p->folderSize -= curSize; processedSize += curSize; if(p->fileSize == 0) { UInt32 index = p->startIndex + p->currentIndex; const CSzFileItem *fileItem = p->ar->db.Files + index; p->currentIndex += 1; if(fileItem->MTimeDefined) SetFileTime(p->file.handle,NULL,NULL,(const FILETIME*)&fileItem->MTime); File_Close(&p->file); if(fileItem->AttribDefined) SetFileAttributesW(fullPath, fileItem->Attrib); if(fileItem->CrcDefined && CRC_GET_DIGEST(p->crc) != fileItem->Crc) { res = SZ_ERROR_CRC; break; } } } free(fullPath); return processedSize; }
static SRes WriteBytesAndCrc(ISeqOutStream *s, const void *buf, UInt32 size, UInt32 *crc) { *crc = CrcUpdate(*crc, buf, size); return WriteBytes(s, buf, size); }
int elzma_decompress_run(elzma_decompress_handle hand, elzma_read_callback inputStream, void * inputContext, elzma_write_callback outputStream, void * outputContext, elzma_file_format format) { unsigned long long int totalRead = 0; /* total amount read from stream */ unsigned int crc32 = CRC_INIT_VAL; /* running crc32 (lzip case) */ CLzmaDec dec; unsigned int errorCode = ELZMA_E_OK; struct elzma_format_handler formatHandler; struct elzma_file_header h; struct elzma_file_footer f; /* switch between supported formats */ if (format == ELZMA_lzma) { initializeLZMAFormatHandler(&formatHandler); } else if (format == ELZMA_lzip) { CrcGenerateTable(); initializeLZIPFormatHandler(&formatHandler); } else { return ELZMA_E_BAD_PARAMS; } /* initialize footer */ f.crc32 = 0; f.uncompressedSize = 0; /* initialize decoder memory */ memset((void *) &dec, 0, sizeof(dec)); LzmaDec_Init(&dec); /* decode the header. */ { unsigned char * hdr = hand->allocStruct.Alloc(&(hand->allocStruct), formatHandler.header_size); size_t sz = formatHandler.header_size; formatHandler.init_header(&h); if (inputStream(inputContext, hdr, &sz) != 0 || sz != formatHandler.header_size) { hand->allocStruct.Free(&(hand->allocStruct), hdr); return ELZMA_E_INPUT_ERROR; } if (0 != formatHandler.parse_header(hdr, &h)) { hand->allocStruct.Free(&(hand->allocStruct), hdr); return ELZMA_E_CORRUPT_HEADER; } /* the LzmaDec_Allocate call requires 5 bytes which have * compression properties encoded in them. In the case of * lzip, the header format does not already contain what * LzmaDec_Allocate expects, so we must craft it, silly */ { unsigned char propsBuf[13]; const unsigned char * propsPtr = hdr; if (format == ELZMA_lzip) { struct elzma_format_handler lzmaHand; initializeLZMAFormatHandler(&lzmaHand); lzmaHand.serialize_header(propsBuf, &h); propsPtr = propsBuf; } /* now we're ready to allocate the decoder */ LzmaDec_Allocate(&dec, propsPtr, 5, (ISzAlloc *) &(hand->allocStruct)); } hand->allocStruct.Free(&(hand->allocStruct), hdr); } /* perform the decoding */ for (;;) { size_t dstLen = ELZMA_DECOMPRESS_OUTPUT_BUFSIZE; size_t srcLen = ELZMA_DECOMPRESS_INPUT_BUFSIZE; size_t amt = 0; size_t bufOff = 0; ELzmaStatus stat; if (0 != inputStream(inputContext, hand->inbuf, &srcLen)) { errorCode = ELZMA_E_INPUT_ERROR; goto decompressEnd; } /* handle the case where the input prematurely finishes */ if (srcLen == 0) { errorCode = ELZMA_E_INSUFFICIENT_INPUT; goto decompressEnd; } amt = srcLen; /* handle the case where a single read buffer of compressed bytes * will translate into multiple buffers of uncompressed bytes, * with this inner loop */ stat = LZMA_STATUS_NOT_SPECIFIED; while (bufOff < srcLen) { SRes r = LzmaDec_DecodeToBuf(&dec, (Byte *) hand->outbuf, &dstLen, ((Byte *) hand->inbuf + bufOff), &amt, LZMA_FINISH_ANY, &stat); /* XXX deal with result code more granularly*/ if (r != SZ_OK) { errorCode = ELZMA_E_DECOMPRESS_ERROR; goto decompressEnd; } /* write what we've read */ { size_t wt; /* if decoding lzip, update our crc32 value */ if (format == ELZMA_lzip && dstLen > 0) { crc32 = CrcUpdate(crc32, hand->outbuf, dstLen); } totalRead += dstLen; wt = outputStream(outputContext, hand->outbuf, dstLen); if (wt != dstLen) { errorCode = ELZMA_E_OUTPUT_ERROR; goto decompressEnd; } } /* do we have more data on the input buffer? */ bufOff += amt; assert( bufOff <= srcLen ); if (bufOff >= srcLen) break; amt = srcLen - bufOff; /* with lzip, we will have the footer left on the buffer! */ if (stat == LZMA_STATUS_FINISHED_WITH_MARK) { break; } } /* now check status */ if (stat == LZMA_STATUS_FINISHED_WITH_MARK) { /* read a footer if one is expected and * present */ if (formatHandler.footer_size > 0 && amt >= formatHandler.footer_size && formatHandler.parse_footer != NULL) { formatHandler.parse_footer( (unsigned char *) hand->inbuf + bufOff, &f); } break; } /* for LZMA utils, we don't always have a finished mark */ if (!h.isStreamed && totalRead >= h.uncompressedSize) { break; } } /* finish the calculated crc32 */ crc32 ^= 0xFFFFFFFF; /* if we have a footer, check that the calculated crc32 matches * the encoded crc32, and that the sizes match */ if (formatHandler.footer_size) { if (f.crc32 != crc32) { errorCode = ELZMA_E_CRC32_MISMATCH; } else if (f.uncompressedSize != totalRead) { errorCode = ELZMA_E_SIZE_MISMATCH; } } else if (!h.isStreamed) { /* if the format does not support a footer and has an uncompressed * size in the header, let's compare that with how much we actually * read */ if (h.uncompressedSize != totalRead) { errorCode = ELZMA_E_SIZE_MISMATCH; } } decompressEnd: LzmaDec_Free(&dec, (ISzAlloc *) &(hand->allocStruct)); return errorCode; }