edit getCRC to inline
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "ZipDefine.h"
|
||||
#include "CRC.h"
|
||||
@@ -10,7 +11,6 @@
|
||||
#define OUTPUT_FILE_NAME "MyZip.zip"
|
||||
|
||||
bool ZipArchiver();
|
||||
uint32_t getCRC32(const char* pData, size_t length);
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -48,25 +48,22 @@ bool ZipArchiver()
|
||||
else
|
||||
{
|
||||
// 파일 크기 계산
|
||||
// seekg(offset, 목표점)
|
||||
fileIn.seekg(0, ios::end);
|
||||
streamsize fileInSize = fileIn.tellg();
|
||||
// beg == begin
|
||||
fileIn.seekg(0, ios::beg);
|
||||
|
||||
// TODO : Buffering 으로 구현. 현재는 파일 통짜로 읽음.
|
||||
fileIn.seekg(0, ios::end); // seekg(offset, 목표점)
|
||||
streamsize fileInSize = fileIn.tellg();
|
||||
fileIn.seekg(0, ios::beg); // beg == begin
|
||||
|
||||
// 메모리 할당
|
||||
// TODO : 자동으로 free 되는 구조로 변경
|
||||
auto pFile = (char *)malloc(fileInSize);
|
||||
vector<uint8_t> fileInBuffer(fileInSize);
|
||||
auto* pInBuffer = reinterpret_cast<char*>(fileInBuffer.data());
|
||||
|
||||
// Read
|
||||
fileIn.read(pFile, fileInSize);
|
||||
fileIn.read(pInBuffer, fileInSize);
|
||||
// ZIP 포맷 채워넣기
|
||||
// TODO : 여기있을 필요까진 없는듯. 코드 이관 필요
|
||||
// TODO : 클래스화 대상
|
||||
{
|
||||
// Get CRC value
|
||||
// TODO : 현재 streamsize 와 size_t 의 mismatch 존재. 해결필요.(long long <-> unsigned long long)
|
||||
uint32_t myCRC = getCRC32(pFile, fileInSize);
|
||||
uint32_t myCRC = getCRC32(pInBuffer, fileInSize);
|
||||
|
||||
// Put PK Signature
|
||||
LFHeader.signature = PK_SIGNATURE_LF_HEADER;
|
||||
@@ -142,7 +139,7 @@ bool ZipArchiver()
|
||||
//fileOut.write(NOT_USED, LFHeader.LFHeader.lengthOfExtraField);
|
||||
|
||||
// 2. File Data
|
||||
fileOut.write(pFile, fileInSize);
|
||||
fileOut.write(pInBuffer, fileInSize);
|
||||
|
||||
// 3. CentralDirectory, file name (var)
|
||||
EOCDR.offsetCentralDir = static_cast<uint32_t>(fileOut.tellp());
|
||||
@@ -159,33 +156,8 @@ bool ZipArchiver()
|
||||
}
|
||||
|
||||
// Free
|
||||
free(pFile);
|
||||
free(pInBuffer);
|
||||
}
|
||||
|
||||
return iRet;
|
||||
}
|
||||
|
||||
uint32_t getCRC32(const char* pData, size_t length)
|
||||
{
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
||||
// 1. 입력
|
||||
for (size_t i = 0; i < length ; i++)
|
||||
{
|
||||
// Index 추출
|
||||
// 입력과 현재 CRC를 1 Byte 씩 XOR + (1의 보수처리)
|
||||
uint8_t iTableIdx = (crc ^ pData[i]) & 0xFF;
|
||||
|
||||
// 1 Byte Shift
|
||||
crc = crc >> 8;
|
||||
|
||||
// Table 과 XOR 계산
|
||||
crc = crc ^ crcTable[iTableIdx];
|
||||
}
|
||||
|
||||
// TODO : CRC 최적화
|
||||
// "_mm_crc32_u16" => MSVC 자체적으로 지원하는 최적화된 CRC 가 있는듯?
|
||||
|
||||
// 1의 보수처리
|
||||
return crc ^ 0xFFFFFFFF;
|
||||
}
|
||||
Reference in New Issue
Block a user