int XzCheck_Final(CXzCheck *p, Byte *digest) { switch (p->mode) { case XZ_CHECK_CRC32: SetUi32(digest, CRC_GET_DIGEST(p->crc)); break; case XZ_CHECK_CRC64: { int i; UInt64 v = CRC64_GET_DIGEST(p->crc64); for (i = 0; i < 8; i++, v >>= 8) digest[i] = (Byte)(v & 0xFF); break; } case XZ_CHECK_SHA256: if (!(p->sha)) return 0; cl_finish_hash(p->sha, digest); break; default: return 0; } return 1; }
HRESULT CFolderInStream::CloseStream() { CRCs.Add(CRC_GET_DIGEST(_crc)); _stream.Release(); _fileIsOpen = false; return S_OK; }
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); } }
bool CInArchive::ReadMarkerAndArchiveHeader(const UInt64 *searchHeaderSizeLimit) { if (!FindAndReadMarker(searchHeaderSizeLimit)) return false; Byte buf[NHeader::NArchive::kArchiveHeaderSize]; UInt32 processedSize; ReadBytes(buf, sizeof(buf), &processedSize); if (processedSize != sizeof(buf)) return false; m_CurData = buf; m_CurPos = 0; m_PosLimit = sizeof(buf); m_ArchiveHeader.CRC = ReadUInt16(); m_ArchiveHeader.Type = ReadByte(); m_ArchiveHeader.Flags = ReadUInt16(); m_ArchiveHeader.Size = ReadUInt16(); m_ArchiveHeader.Reserved1 = ReadUInt16(); m_ArchiveHeader.Reserved2 = ReadUInt32(); m_ArchiveHeader.EncryptVersion = 0; UInt32 crc = CRC_INIT_VAL; crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.Type); crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Flags); crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Size); crc = CrcUpdateUInt16(crc, m_ArchiveHeader.Reserved1); crc = CrcUpdateUInt32(crc, m_ArchiveHeader.Reserved2); if (m_ArchiveHeader.IsThereEncryptVer() && m_ArchiveHeader.Size > NHeader::NArchive::kArchiveHeaderSize) { ReadBytes(&m_ArchiveHeader.EncryptVersion, 1, &processedSize); if (processedSize != 1) return false; crc = CRC_UPDATE_BYTE(crc, m_ArchiveHeader.EncryptVersion); } if(m_ArchiveHeader.CRC != (CRC_GET_DIGEST(crc) & 0xFFFF)) ThrowExceptionWithCode(CInArchiveException::kArchiveHeaderCRCError); if (m_ArchiveHeader.Type != NHeader::NBlockType::kArchiveHeader) return false; m_ArchiveCommentPosition = m_Position; m_SeekOnArchiveComment = true; return true; }
SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s) { Byte buf[32]; UInt64 globalPos; { UInt32 crc = CRC_INIT_VAL; unsigned pos = 1 + Xz_WriteVarInt(buf + 1, p->numBlocks); size_t i; globalPos = pos; buf[0] = 0; RINOK(WriteBytesAndCrc(s, buf, pos, &crc)); for (i = 0; i < p->numBlocks; i++) { const CXzBlockSizes *block = &p->blocks[i]; pos = Xz_WriteVarInt(buf, block->totalSize); pos += Xz_WriteVarInt(buf + pos, block->unpackSize); globalPos += pos; RINOK(WriteBytesAndCrc(s, buf, pos, &crc)); } pos = ((unsigned)globalPos & 3); if (pos != 0) { buf[0] = buf[1] = buf[2] = 0; RINOK(WriteBytesAndCrc(s, buf, 4 - pos, &crc)); globalPos += 4 - pos; } { SetUi32(buf, CRC_GET_DIGEST(crc)); RINOK(WriteBytes(s, buf, 4)); globalPos += 4; } } { UInt32 indexSize = (UInt32)((globalPos >> 2) - 1); SetUi32(buf + 4, indexSize); buf[8] = (Byte)(p->flags >> 8); buf[9] = (Byte)(p->flags & 0xFF); SetUi32(buf, CrcCalc(buf + 4, 6)); memcpy(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE); return WriteBytes(s, buf, 12); } }
STDMETHODIMP_(void) CCrcHasher::Final(Byte *digest) { UInt32 val = CRC_GET_DIGEST(_crc); SetUi32(digest, val); }
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; }