//--------------------------------------------------------------------------- AWPRESULT awp4BitLBP(awpImage* pImage, awpRect* pRect, AWPBYTE* pCode) { AWPINT x,y,w,h,ww; AWPRESULT res = AWP_OK; AWPDOUBLE tarea, area, total; AWPDOUBLE* pix; AWPINT iv[4]; if (pImage == NULL) return AWP_BADARG; if (pImage->dwType != AWP_DOUBLE) return AWP_BADARG; if (pRect == NULL) return AWP_BADARG; if (awpRectInImage(pImage, pRect) != AWP_OK) return AWP_BADARG; x = pRect->left; y = pRect->top; w = pRect->right - x; h = pRect->bottom - y; ww = pImage->sSizeX; tarea = 1.0/((double)w*(double)h); area = ((double)w*(double)h)/ 4.0; area = 1/area; pix = (double*)pImage->pPixels; total = CalcSum(pix, x, y, w, h, ww)*tarea; memset(iv, 0, sizeof(iv)); w /=2; h /=2; iv[0] = CalcSum(pix, x, y, w, h, ww) * area > total?1:0; iv[1] = CalcSum(pix, x + w, y, w, h, ww) * area> total?1:0; iv[2] = CalcSum(pix, x +w, y + h, w, h, ww) * area> total?1:0; iv[3] = CalcSum(pix, x +w, y + h, w, h, ww) * area> total?1:0; *pCode = 0; *pCode |= iv[0]; *pCode = *pCode << 1; *pCode |= iv[1]; *pCode = *pCode << 1; *pCode |= iv[2]; *pCode = *pCode << 1; *pCode |= iv[3]; return res; }
AWPRESULT awp2BitLBP(awpImage* pImage, awpRect* pRect, AWPBYTE* pCode) { AWPRESULT res = AWP_OK; AWPFLOAT x,y,w,h,ww; AWPDOUBLE* pix; AWPDOUBLE area, iv[4]; AWPBYTE c1; AWPBYTE c2; if (pImage == NULL) return AWP_BADARG; if (pImage->dwType != AWP_DOUBLE) return AWP_BADARG; if (pRect == NULL) return AWP_BADARG; if (awpRectInImage(pImage, pRect) != AWP_OK) return AWP_BADARG; x = pRect->left; y = pRect->top; w = pRect->right - x; h = pRect->bottom - y; ww = pImage->sSizeX; area = ((double)w*(double)h)/ 2.0; area = 1/area; pix = (double*)pImage->pPixels; memset(iv, 0, sizeof(iv)); iv[0] = (AWPDOUBLE)(CalcSum(pix, (AWPINT)x, (AWPINT)y, (AWPINT)w, (AWPINT)(h / 2), (AWPINT)ww)*area); iv[1] = (AWPDOUBLE)(CalcSum(pix, (AWPINT)x, (AWPINT)y + (AWPINT)(h / 2), (AWPINT)w, (AWPINT)(h / 2), (AWPINT)ww)*area); iv[2] = (AWPDOUBLE)(CalcSum(pix, (AWPINT)x, (AWPINT)y, (AWPINT)(w / 2), (AWPINT)h, (AWPINT)ww)*area); iv[3] = (AWPDOUBLE)(CalcSum(pix, (AWPINT)x + (AWPINT)(w / 2), (AWPINT)y, (AWPINT)(w / 2), (AWPINT)h, (AWPINT)ww)*area); c1 = iv[0] > iv[1] ? 1:0; c2 = iv[2] > iv[3] ? 1:0; *pCode = 0; *pCode |= c1; *pCode = *pCode << 1; *pCode |= c2; return res; }
STDMETHODIMP Ccomputation2::CalcAvg(double* dResult) { // TODO: Add your implementation code here CalcSum(dResult); (*dResult) /= problist->size(); return S_OK; }
AWPRESULT awpIntegralGrid(const awpImage* pImage, const awpRect* pRect, awpImage* pDst) { AWPRESULT res = AWP_OK; AWPSHORT rwidth,rheight; AWPDOUBLE x_cell_size, y_cell_size, cell_square; AWPDOUBLE* in; AWPDOUBLE* out; awpRect r; AWPWORD x,y; if (pImage == NULL) return AWP_BADARG; if (pDst == NULL) return AWP_BADARG; if (pImage->dwType != AWP_DOUBLE) return AWP_BADARG; if (pDst->dwType != AWP_DOUBLE) return AWP_BADARG; if (pRect == NULL) return AWP_BADARG; if (awpRectInImage(pImage, pRect) != AWP_OK) return AWP_BADARG; rwidth = pRect->right - pRect->left; rheight = pRect->bottom - pRect->top; x_cell_size = (AWPDOUBLE)rwidth / (AWPDOUBLE)pDst->sSizeX; y_cell_size = (AWPDOUBLE)rheight / (AWPDOUBLE)pDst->sSizeY; cell_square = x_cell_size*y_cell_size; cell_square = 1 / cell_square; if (x_cell_size < 2 || y_cell_size < 2) return AWP_BADARG; out = (AWPDOUBLE*)pDst->pPixels; in = (AWPDOUBLE*)pImage->pPixels; for ( y = 0; y < pDst->sSizeY; y++) { r.top = (AWPSHORT)ceil(pRect->top + y*y_cell_size + 0.5); r.bottom = (AWPSHORT)ceil(r.top + y_cell_size + 0.5); for ( x = 0; x < pDst->sSizeX; x++) { r.left = (AWPSHORT)ceil(pRect->left + x*x_cell_size + 0.5); r.right = (AWPSHORT)ceil(r.left + x_cell_size + 0.5); out[y*pDst->sSizeX + x] = CalcSum(in, r.left, r.top, (AWPINT)x_cell_size, (AWPINT)y_cell_size, pImage->sSizeX); out[y*pDst->sSizeX + x] *= cell_square; } } return res; }
/************************************************************** 函数名称: GetDataFromHsm 函数功能: 连接加密机从其获取数据 入口参数: pMsgBuffer[in/out]:发送或返回的数据中的MSG iLen[in/out]:发送或返回数据的长度 相关调用: 返 回 值: true成功, false失败 **************************************************************/ bool EnCryptComm::GetDataFromHsm(unsigned char *pMsgBuffer, unsigned & iLen) { unsigned char cmd = pMsgBuffer[0], Buffer[1024]={0}; unsigned sLen;//发送数据长度 sLen = iLen + 7; memcpy(Buffer, &sLen, 2);//LC Buffer[2]=0xF1;//INS Buffer[3]=0;//SEQ //CR默认为0 memcpy(&Buffer[6], pMsgBuffer, iLen);//MSG Buffer[sLen-1] = CalcSum(Buffer, sLen - 1);//CRC char errDesc[512]={0}; unsigned char cRecBuf[1024]={0}; int recLen=0; SocketClient client; int iErrorCode=client.SendToHost(Buffer,sLen,cRecBuf,recLen,g_Cfg.EncHost.Host, g_Cfg.EncHost.Port,20,errDesc); if(0==iErrorCode) { trace_log(DBG, "recv from enchost"); trace_mem(cRecBuf, recLen); if(cRecBuf[recLen-1] == CalcSum(cRecBuf, recLen - 1) && cRecBuf[6] == cmd && cRecBuf[8] == CMD_OK) {//校验值正确,与原命令字相同,返回码为成功 if(memcmp(pMsgBuffer+2, cRecBuf+9, LEN_MERCHNO+LEN_TERMINO)!=0)//+LEN_TERMINO {//比较商户号与终端号 trace_log(DBG, "The return data from encryptor is not match!!"); return false; } memcpy(pMsgBuffer, cRecBuf+6, recLen-7);//去掉LC,INS,SEQ,CR,CRC iLen = recLen-7; trace_log(DBG,"GetDataFromHsm->iLen=%d",iLen); return true; } else { trace_log(ERR, "GetDataFromHsm else CalcSum %d", recLen); return false ; } }else{ trace_log(ERR,errDesc); return false; } }
void SendDataToSever(char * Buffer,char * pMsgBuffer,int iLen) { int sLen;//发送数据长度 sLen = iLen + 7; memcpy(&Buffer[0], &sLen, 2);//LC Buffer[2]=0xF1;//INS Buffer[3]=0;//SEQ //CR默认为0 memcpy(&Buffer[6], pMsgBuffer, iLen);//MSG Buffer[sLen-1] = CalcSum((const unsigned char*)Buffer, sLen - 1);//CRC }
void Start() { // Here we would create temp Base object from argument '5', pass it to // function CalcSum(), and after return destroy temp object. int sum = CalcSum(5, 4); std::cout << "Sum is " << sum << std::endl; // Here we would use RVO compiler optimisation. No temp objects would be // created, only one - result Base result = InitBaseRVO(10); std::cout << "Inited Base object have value " << result.m_value << std::endl; Base resultNRVO = InitBaseNRVO(15); std::cout << "NRVO-Inited Base object have value " << resultNRVO.m_value << std::endl; }
int main() { try { Boss::Loader Ldr("Registry.xml", MAKE_MODULE_PATH MAKE_MODULE_NAME("service_registry"), MAKE_MODULE_PATH MAKE_MODULE_NAME("class_factory")); int stub = 0; int stub1 = 1; int stub2 = 2; auto Obj = Boss::CreateObject<MyNs::ISum>(MyNs::Service::Id::Sum); int Res = 0; if (Obj->CalcSum(10, 20, &Res)) std::cout << "Failed to calc sum." << std::endl; std::cout << "Sum: " << Res << std::endl; } catch (std::exception const &e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; }
CFeatureValues* CLBPFeatures::GetFeatures(awpImage* img, awpRect* r) { if (awpRectInImage(img, r) != AWP_OK) return NULL; awpRect r1 = *r; int w = r->right - r->left; int h = r->bottom - r->top; r1.top += h; r1.bottom += h; if (awpRectInImage(img, &r1) != AWP_OK) return NULL; if (r->right - r->left < 32) return NULL; CFeatureValues* v = new CFeatureValues(2*m_num); float scale = (r->right - r->left) / 24.; int count = 0; for (int i = 0; i < this->m_patches.GetCount(); i++) { TLFRect* rr = (TLFRect*)this->m_patches.Get(i); awpRect r1 = rr->GetRect(); int w = rr->Width(); int h = rr->Height(); w = (int)(w*scale + 0.5); h = (int)(h*scale + 0.5); r1.left = (int)(r1.left*scale + 0.5); r1.top = (int)(r1.top*scale + 0.5); r1.left += r->left; r1.top += r->top; r1.bottom = r1.top + h; r1.right = r1.left + w; AWPBYTE code = 1; double v1 = CalcSum((double*)img->pPixels, r1.left, r1.top, w / 2, h, img->sSizeX ); double v2 = CalcSum((double*)img->pPixels, r1.left + w/2, r1.top, w / 2, h, img->sSizeX ); if (v1 > v2) code = 1; else code = 0; v->SetValue(2*count, code); v1 = CalcSum((double*)img->pPixels, r1.left, r1.top, w , h/2, img->sSizeX ); v2 = CalcSum((double*)img->pPixels, r1.left, r1.top + h/2, w, h/2, img->sSizeX ); if (v1 > v2) code = 1; else code = 0; v->SetValue(2*count+1, code); count++; } //memcpy(r, &r1, sizeof(awpRect)); awpRect r3 = r1; for (int i = 0; i < this->m_patches.GetCount(); i++) { TLFRect* rr = (TLFRect*)this->m_patches.Get(i); awpRect r1 = rr->GetRect(); int w = rr->Width(); int h = rr->Height(); w = (int)(w*scale + 0.5); h = (int)(h*scale + 0.5); r1.left = (int)(r1.left*scale + 0.5); r1.top = (int)(r1.top*scale + 0.5); r1.left += r3.left; r1.top += r3.top; r1.bottom = r1.top + h; r1.right = r1.left + w; AWPBYTE code = 1; double v1 = CalcSum((double*)img->pPixels, r1.left, r1.top, w / 2, h, img->sSizeX ); double v2 = CalcSum((double*)img->pPixels, r1.left + w/2, r1.top, w / 2, h, img->sSizeX ); if (v1 > v2) code = 1; else code = 0; v->SetValue(2*count, code); v1 = CalcSum((double*)img->pPixels, r1.left, r1.top, w , h/2, img->sSizeX ); v2 = CalcSum((double*)img->pPixels, r1.left, r1.top + h/2, w, h/2, img->sSizeX ); if (v1 > v2) code = 1; else code = 0; v->SetValue(2*count+1, code); count++; } return v; }
/* bCompress: 0 - выполнять распаковку (avc->ave2) 1 - выполнять упаковку (ave2->avc) */ int TransformBase(SMemFile mfInBase, SMemFile *pmfOutBase, BOOL bCompress) { int i; AVP_BaseHeader BaseHeader; SPtrArray BlockHeaderArray; AVP_BlockHeader * bhp; int rCount=0; DWORD bread; int ret = ERR_SUCCESS; // NO ERROR PtrArrayReset(&BlockHeaderArray); MemFileSeekToBegin(&mfInBase); //BaseHeader read bread = MemFileRead(&mfInBase, &BaseHeader, sizeof(BaseHeader)); if(*(DWORD*)BaseHeader.Authenticity==AVP_MAGIC) { MemFileSeekToBegin(&mfInBase); bread = MemFileRead(&mfInBase, BaseHeader.Authenticity, sizeof(BaseHeader)-0x40); } if(BaseHeader.Magic!=AVP_MAGIC) { ret = ERR_BAD_FORMAT; } else { //char timeBuf[0x200]; //SYSTEMTIME st; BOOL bBaseCompressed = 0; //BlockHeaderTable read MemFileSeek(&mfInBase, BaseHeader.BlockHeaderTableFO, SEEK_SET); for(i=0; i<(int)BaseHeader.BlockHeaderTableSize; i++) { bhp=(AVP_BlockHeader*)malloc(sizeof(AVP_BlockHeader)); bread = MemFileRead(&mfInBase, bhp, sizeof(AVP_BlockHeader)); if(bread!=sizeof(AVP_BlockHeader)) { free(bhp); ret = ERR_FILE_CORRUPTED; goto abort_; } PtrArrayAdd(&BlockHeaderArray,bhp); if (bhp->Compression) bBaseCompressed = 1; } if ( (!bBaseCompressed && !bCompress) || (bBaseCompressed && bCompress)) { ret = ERR_BAD_FORMAT; // WRONG COMPRESSION STATUS goto abort_; } MemFileReset(pmfOutBase); /*GetLocalTime2(&st); memset(BaseHeader.Text,0,0x40); sprintf((char *)BaseHeader.Text,"AVP Antiviral Database. (c)Kaspersky Lab 1997-%d.",st.wYear); *(DWORD*)(BaseHeader.Text+0x3E)='\xd\xa'; memset(BaseHeader.Authenticity,0,0x40); strcpy((char *)(BaseHeader.Authenticity), TimeFormat(timeBuf,0x200,"Kaspersky Lab. %d %b %Y %H:%M:%S")); *(DWORD*)(BaseHeader.Authenticity+0x3C)='\xd\xa\xd\xa'; BaseHeader.CreationTime=st; BaseHeader.ModificationTime=st;*/ //BaseHeader write MemFileWrite(pmfOutBase, &BaseHeader, sizeof(BaseHeader)); //BlockTableHeader write BaseHeader.BlockHeaderTableFO = MemFileGetPosition(pmfOutBase); bhp=(AVP_BlockHeader *)malloc( sizeof(AVP_BlockHeader) ); memset(bhp,0,sizeof(AVP_BlockHeader)); for(i=0; i<(int)BaseHeader.BlockHeaderTableSize; i++) MemFileWrite(pmfOutBase, bhp, sizeof(AVP_BlockHeader)); free(bhp); //Records & Links write for(i=0; i<(int)BaseHeader.BlockHeaderTableSize; i++) { UINT ib; BYTE* buf = 0; bhp=(AVP_BlockHeader*)BlockHeaderArray.pp[i]; if (bhp->CompressedSize == 0 && bhp->UncompressedSize == 0) continue; if (bhp->CompressedSize == 0 || bhp->UncompressedSize == 0) { ret = ERR_AVP_BLOCKHEADER_CORRUPTED; // 16-битные линки надо лучше чистить !!! goto abort_; } // read block MemFileSeek(&mfInBase, bhp->BlockFO, SEEK_SET); buf = (BYTE*)malloc(bhp->CompressedSize); bread = MemFileRead(&mfInBase, buf, bhp->CompressedSize); if(bread!=bhp->CompressedSize || bhp->CRC!=CalcSum(buf,bhp->CompressedSize)) { free(buf); ret = ERR_FILE_CORRUPTED; goto abort_; } if(bhp->Compression) { // decompress block BYTE *out_buf = (BYTE*)malloc(bhp->UncompressedSize+1); for(ib=0; ib<bhp->CompressedSize; ib++) buf[ib]^=(BYTE)ib; bread = unsqu( buf, out_buf); if (bread != bhp->UncompressedSize) { ret = ERR_FILE_CORRUPTED; goto abort_; } free(buf); buf = out_buf; out_buf = 0; bhp->Compression = 0; bhp->CompressedSize = bhp->UncompressedSize; } if (bCompress) { // compress block SMemFile mfBlock; SMemFile mfOutBlock; MemFileReset(&mfOutBlock); MemFileAttach(&mfBlock, buf, bhp->UncompressedSize); bhp->Compression = 1; bhp->CompressedSize = squeeze(&mfBlock, &mfOutBlock, bhp->UncompressedSize, 0); MemFileDelete(&mfBlock); buf = MemFileDetach(&mfOutBlock); for(ib=0; ib<bhp->CompressedSize; ib++) buf[ib]^=(BYTE)ib; } bhp->BlockFO = MemFileGetPosition(pmfOutBase); bhp->CRC = CalcSum( buf, bhp->CompressedSize); MemFileWrite(pmfOutBase, buf, bhp->CompressedSize); } //BaseHeader Again write BaseHeader.FileSize = MemFileGetLength(pmfOutBase) + ((bCompress)?64:0); BaseHeader.BlockHeaderTableSize = PtrArrayGetSize( &BlockHeaderArray ); BaseHeader.AuthenticityCRC=(CalcSum(BaseHeader.Authenticity,0x40)); BaseHeader.HeaderCRC=(CalcSum((BYTE*)&BaseHeader.Magic,sizeof(BaseHeader)-0x84)); MemFileSeekToBegin(pmfOutBase); MemFileWrite(pmfOutBase,&BaseHeader,sizeof(BaseHeader)); //BlockTableHeader Again write MemFileSeek(pmfOutBase, BaseHeader.BlockHeaderTableFO, SEEK_SET); for(i=0; i<(int)BaseHeader.BlockHeaderTableSize; i++) MemFileWrite(pmfOutBase, BlockHeaderArray.pp[i], sizeof(AVP_BlockHeader)); } abort_: PtrArrayFreeAllPtr(&BlockHeaderArray); return ret; }
int CompressBase( const BYTE *pAveBuffer, DWORD dwAveBufferSize, const char *pszAvcFileName) { int NameCnt=0; AVP_BaseHeader BaseHeader; int ret=ERR_SUCCESS; FILE *fp=0; memset(mfRecordsArray,0,NUMBER_OF_RECORD_BLOCK_TYPES*NUMBER_OF_RECORD_TYPES*sizeof(SMemFile)); memset(RNumTable, 0,NUMBER_OF_RECORD_BLOCK_TYPES*NUMBER_OF_RECORD_TYPES*sizeof(int)); MemFileReset(&mfNames); NameIdx=0; memcpy(&BaseHeader, pAveBuffer, sizeof(BaseHeader)); if(BaseHeader.Magic==AVP_MAGIC) { if(!LoadBase(pAveBuffer,dwAveBufferSize)) { ret=ERR_BAD_FORMAT; goto err; } } else { ret=ERR_BAD_FORMAT; goto err; } if((fp=fopen(pszAvcFileName,"wb")) != NULL) { //char timeBuf[0x200]; //SYSTEMTIME st; SPtrArray BlockHeaderArray; AVP_BlockHeader* bhp; int i,j; int nbh; PtrArrayReset(&BlockHeaderArray); /*GetLocalTime2(&st); memset(BaseHeader.Text,0,0x40); sprintf((char *)BaseHeader.Text,"AVP Antiviral Database. (c)Kaspersky Lab 1997-%d.",st.wYear); *(DWORD*)(BaseHeader.Text+0x3E)='\xd\xa'; memset(BaseHeader.Authenticity,0,0x40); strcpy((char *)(BaseHeader.Authenticity), TimeFormat(timeBuf,0x200,"Kaspersky Lab. %d %b %Y %H:%M:%S")); *(DWORD*)(BaseHeader.Authenticity+0x3C)='\xd\xa\xd\xa';*/ BaseHeader.VXs.BXs.VerH= 0x03; BaseHeader.VXs.BXs.VerL= 0x00; BaseHeader.Flags=0x0001; BaseHeader.NumberOfRecordBlockTypes=NUMBER_OF_RECORD_BLOCK_TYPES; BaseHeader.NumberOfRecordTypes=NUMBER_OF_RECORD_TYPES; /*BaseHeader.CreationTime=st; BaseHeader.ModificationTime=st;*/ BaseHeader.Flags &=~1; //BaseHeader write fwrite(&BaseHeader,sizeof(BaseHeader),1,fp); //BlockTableHeader write BaseHeader.BlockHeaderTableFO = ftell(fp); nbh=NameIdx?1:0; for (i=0; i<NUMBER_OF_RECORD_BLOCK_TYPES; i++) for (j=0; j<NUMBER_OF_RECORD_TYPES; j++) if(RNumTable[i][j])nbh++; bhp=(AVP_BlockHeader *)malloc( sizeof(AVP_BlockHeader) ); memset(bhp,0,sizeof(AVP_BlockHeader)); for(i=0;i<nbh;i++) fwrite(bhp,sizeof(AVP_BlockHeader),1,fp); free(bhp); //Records & Links write for (i=0; i<NUMBER_OF_RECORD_BLOCK_TYPES; i++) for (j=0; j<NUMBER_OF_RECORD_TYPES; j++) { SMemFile cmf; UINT ib; BYTE *mb; if(0==RNumTable[i][j])continue; bhp=(AVP_BlockHeader *)malloc(sizeof(AVP_BlockHeader)); memset(bhp,0,sizeof(AVP_BlockHeader)); PtrArrayAdd(&BlockHeaderArray, bhp); bhp->BlockFO=ftell(fp); bhp->BlockType=i; //HERE define TYPES bhp->RecordType=j; bhp->NumRecords=RNumTable[i][j]; if(i==BT_RECORD){ switch(j){ case RT_KERNEL: bhp->RecordSize=sizeof(R_Kernel); break; case RT_JUMP: bhp->RecordSize=sizeof(R_Jump); break; case RT_MEMORY: bhp->RecordSize=sizeof(R_Memory); break; case RT_SECTOR: bhp->RecordSize=sizeof(R_Sector); break; case RT_FILE: bhp->RecordSize=sizeof(R_File); break; case RT_CA: bhp->RecordSize=sizeof(R_CA); break; case RT_UNPACK: bhp->RecordSize=sizeof(R_Unpack); break; case RT_EXTRACT:bhp->RecordSize=sizeof(R_Extract); break; default: bhp->RecordSize=0; break; } } else bhp->RecordSize=0; bhp->UncompressedSize=MemFileGetLength( &mfRecordsArray[i][j] ); bhp->Compression=1; MemFileReset(&cmf); MemFileSeekToBegin( &mfRecordsArray[i][j] ); bhp->CompressedSize=squeeze(&mfRecordsArray[i][j],&cmf,bhp->UncompressedSize,0); /* BYTE* */mb = MemFileDetach(&cmf); for(ib=0; ib<bhp->CompressedSize; ib++) mb[ib]^=(BYTE)ib; bhp->CRC=CalcSum(mb, bhp->CompressedSize); fwrite(mb,bhp->CompressedSize,1,fp); free(mb); } //Names write if(NameIdx) { SMemFile *mf; SMemFile cmf; UINT ib; BYTE *mb; bhp=(AVP_BlockHeader *)malloc(sizeof(AVP_BlockHeader)); memset(bhp,0,sizeof(AVP_BlockHeader)); PtrArrayAdd( &BlockHeaderArray, bhp ); bhp->BlockFO=ftell(fp); bhp->BlockType=BT_NAME; bhp->NumRecords=NameIdx; /* CMemFile* */mf=&mfNames; bhp->UncompressedSize=MemFileGetLength(mf); bhp->Compression=1; //CMemFile cmf(0x1000); MemFileReset(&cmf); MemFileSeekToBegin(mf); bhp->CompressedSize=squeeze(mf,&cmf,bhp->UncompressedSize,0); /* BYTE* */mb=MemFileDetach( &cmf ); for(ib=0; ib<bhp->CompressedSize; ib++) mb[ib]^=(BYTE)ib; bhp->CRC=CalcSum(mb,bhp->CompressedSize); fwrite(mb,bhp->CompressedSize,1,fp); free(mb); mb=MemFileDetach(mf); free(mb); } BaseHeader.FileSize = ftell(fp)+64; BaseHeader.BlockHeaderTableSize = PtrArrayGetSize( &BlockHeaderArray ); BaseHeader.AuthenticityCRC=(CalcSum(BaseHeader.Authenticity,0x40)); BaseHeader.HeaderCRC=(CalcSum((BYTE*)&BaseHeader.Magic,sizeof(BaseHeader)-0x84)); fseek(fp,0,SEEK_SET); fwrite(&BaseHeader,sizeof(BaseHeader),1,fp); //BlockTableHeader Again write fseek(fp, BaseHeader.BlockHeaderTableFO, SEEK_SET); for(i=0; i<(int)BaseHeader.BlockHeaderTableSize; i++) { fwrite(BlockHeaderArray.pp[i],sizeof(AVP_BlockHeader),1,fp); } fclose(fp); PtrArrayFreeAllPtr(&BlockHeaderArray); for (i=0; i<NUMBER_OF_RECORD_BLOCK_TYPES; i++) { for (j=0; j<NUMBER_OF_RECORD_TYPES; j++) MemFileDelete(&mfRecordsArray[i][j]); } MemFileDelete(&mfNames); } else { ret = ERR_FILE_IO_ERROR; } err:; return ret; }
int LoadBase(const BYTE *pAveBuffer, DWORD dwAveBufferSize) { int rCount=0; AVP_BaseHeader BaseHeader; SPtrArray BlockHeaderArray; AVP_BlockHeader * bhp; PtrArrayReset(&BlockHeaderArray); //BaseHeader read memcpy(&BaseHeader, pAveBuffer, sizeof(BaseHeader)); if(*(DWORD*)BaseHeader.Authenticity==AVP_MAGIC) memcpy(BaseHeader.Authenticity, pAveBuffer, sizeof(BaseHeader)-0x40); if(BaseHeader.Magic==AVP_MAGIC) { BYTE* nbuf=NULL; UINT i; UINT j; BOOL names_done; //BlockHeaderTable read bhp = (AVP_BlockHeader*)&pAveBuffer[BaseHeader.BlockHeaderTableFO]; for(i=0; i<BaseHeader.BlockHeaderTableSize; i++) { PtrArrayAdd(&BlockHeaderArray,bhp); bhp++; } names_done=0; lab_names_done: for(i=0; i<BaseHeader.BlockHeaderTableSize; i++) { BYTE* buf = 0; DWORD npos; DWORD k; bhp=(AVP_BlockHeader*)BlockHeaderArray.pp[i]; if(bhp->Compression) goto abort_; // Fix LINK16 bug if (bhp->CompressedSize == 0 || bhp->UncompressedSize == 0) continue; switch(bhp->BlockType) { case BT_NAME: break; case BT_LINK16: //if(!add_16bit_links) //continue; case BT_RECORD: case BT_LINK32: if(names_done) break; case BT_EDIT: case BT_COMMENT: default: continue; } /*BYTE* */ buf=malloc(bhp->CompressedSize); if(buf==NULL) goto abort_; memcpy(buf,&pAveBuffer[bhp->BlockFO], bhp->CompressedSize); if( bhp->CRC!=CalcSum(buf,bhp->CompressedSize) ) { free(buf); goto abort_; } switch(bhp->BlockType) { case BT_NAME: if(names_done) { MemFileWrite( &mfNames, buf, bhp->CompressedSize ); } else { if(nbuf) free(nbuf); nbuf=malloc(bhp->CompressedSize+1); memcpy(nbuf+1,buf,bhp->CompressedSize); nbuf[0]='\n'; for(k=1; k<=bhp->CompressedSize; k++) if(nbuf[k]=='\n') nbuf[k]=0; free(buf); buf=0; names_done=1; goto lab_names_done; } break; case BT_RECORD: rCount+=bhp->NumRecords; npos=MemFileGetPosition(&mfNames); switch(bhp->RecordType) { case RT_MEMORY: { R_Memory* p=(R_Memory*)buf; for( j=0; j<bhp->NumRecords; j++,p++ ) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_SECTOR: { R_Sector* p=(R_Sector*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_FILE: { R_File* p=(R_File*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_CA: { R_CA* p=(R_CA*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_UNPACK: { R_Unpack* p=(R_Unpack*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_EXTRACT: { R_Extract* p=(R_Extract*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { p->NameIdx +=npos; if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_KERNEL: { R_Kernel* p=(R_Kernel*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } case RT_JUMP: { R_Jump* p=(R_Jump*)buf; for(j=0;j<bhp->NumRecords;j++,p++) { if(p->LinkIdx!=(WORD)-1) p->LinkIdx +=RNumTable[BT_LINK32][bhp->RecordType]; } break; } default: break; } MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],buf,bhp->CompressedSize); break; case BT_LINK32: case BT_LINK16: { BYTE* b=buf; UINT k; for(k=0; k<bhp->NumRecords; k++) { DWORD* dptr=(DWORD*)b; DWORD q=0; DWORD l=dptr[0]; b+=l; l-=dptr[1]; if(BT_LINK32==bhp->BlockType) StripComments(((BYTE*)dptr)+dptr[1]+8); MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],&l,4); MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],&q,4); MemFileWrite(&mfRecordsArray[bhp->BlockType][bhp->RecordType],((BYTE*)dptr)+dptr[1]+8,l-8); } } break; default: break; } if(buf) free(buf); } if(nbuf) free(nbuf); for(i=0; i<BaseHeader.BlockHeaderTableSize; i++) { bhp=(AVP_BlockHeader*)BlockHeaderArray.pp[i]; switch(bhp->BlockType) { case BT_NAME: NameIdx+=bhp->NumRecords; break; case BT_RECORD: case BT_LINK32: case BT_LINK16: RNumTable[bhp->BlockType][bhp->RecordType]+=bhp->NumRecords; default: break; } } } abort_:; free(BlockHeaderArray.pp); return rCount; }
//static DWORD LoadOldKey(char* filename,CPtrArray* array,DWORD appID) //static DWORD LoadOldKey(char* filename, HDATA& hRootData, DWORD appID) static DWORD LoadOldKey(DWORD appID, const char* filename, DWORD* level, BOOL* valid, HDATA& hRootData) { DWORD ret=0; AVP_KeyHeader avpKeyHeader; int handle; unsigned bytes; unsigned char *cbuf; DWORD Level=0; BOOL Valid=TRUE; BOOL platf=FALSE; if(_dos_open(filename,O_RDONLY,&handle)) return ret; if(_dos_read(handle,(void far *)&avpKeyHeader,sizeof(avpKeyHeader),&bytes) ||avpKeyHeader.Magic!=AVP_KEY_MAGIC||avpKeyHeader.Version!=3 ||((cbuf=(unsigned char *)Malloc(avpKeyHeader.CompressedSize))==0)) { _dos_close(handle); return ret; } //if(KeyHeader.CompressedCRC==CalcSum((BYTE*)cbuf,KeyHeader.CompressedSize)) if(((avpKey=(struct _AVP_Key *)Malloc(avpKeyHeader.UncompressedSize))==NULL) ||_dos_read(handle,(void *)cbuf,avpKeyHeader.CompressedSize,&bytes)) { _dos_close(handle); Free(cbuf); return ret; } _dos_close(handle); if(avpKeyHeader.CompressedCRC==CalcSum((BYTE*)cbuf,avpKeyHeader.CompressedSize)) { for(int i=0;i<avpKeyHeader.CompressedSize;i++) cbuf[i]^=(BYTE)i; if(avpKeyHeader.UncompressedSize==unsqu(cbuf,(unsigned char *)avpKey) )//&&!FFake) { avp_key_present=1; switch(appID) { case AVP_APID_SCANNER_WIN_95: platf=!!(avpKey->Platforms & KEY_P_WIN95); break; case AVP_APID_SCANNER_WIN_NTWS: platf=!!(avpKey->Platforms & KEY_P_WINNT); break; case AVP_APID_MONITOR_WIN_95: platf=!!(avpKey->Platforms & KEY_P_WIN95) && !!(avpKey->Options & KEY_O_MONITOR); break; case AVP_APID_MONITOR_WIN_NTWS: platf=!!(avpKey->Platforms & KEY_P_WINNT) && !!(avpKey->Options & KEY_O_MONITOR); break; case AVP_APID_OS_2: platf=!!(avpKey->Platforms & KEY_P_OS2); break; case AVP_APID_SCANNER_DOS_LITE: platf=!!(avpKey->Platforms & KEY_P_DOSLITE); break; case AVP_APID_NOVELL: platf=!!(avpKey->Platforms & KEY_P_NOVELL); break; case AVP_APID_SCANNER_DOS_32: case AVP_APID_SCANNER_DOS_16: platf=!!(avpKey->Platforms & KEY_P_DOS); break; case AVP_APID_CONTROLCENTER: case AVP_APID_UPDATOR: platf=!!(avpKey->Platforms & KEY_P_WIN95) || !!(avpKey->Platforms & KEY_P_WINNT); break; case AVP_APID_INSPECTOR: case AVP_APID_WEBINSPECTOR: case AVP_APID_EXCHANGE: case AVP_APID_NT_SERVER: default: break; } if(platf) { ret=FN_MINIMAL; ret|=FN_OPTIONS; if(avpKey->Options & KEY_O_REMOTELAUNCH)ret|=FN_NETWORK; if(avpKey->Options & KEY_O_REMOTESCAN) ret|=FN_NETWORK; if(!(avpKey->Options & KEY_O_DISINFECT))ret&=~FN_DISINFECT; if(!(avpKey->Options & KEY_O_UNPACK)) ret&=~FN_UNPACK; if(!(avpKey->Options & KEY_O_EXTRACT)) ret&=~FN_EXTRACT; struct dosdate_t stCurrent; //SYSTEMTIME stCurrent; SYSTEMTIME stExpir; _dos_getdate(&stCurrent); //GetSystemTime(&stCurrent); memset(&stExpir,0,sizeof(struct dosdate_t));//SYSTEMTIME)); stExpir.wYear =avpKey->ExpirYear; stExpir.wMonth=(unsigned char)avpKey->ExpirMonth; stExpir.wDay =(unsigned char)avpKey->ExpirDay; if(stCurrent.year > stExpir.wYear) Valid=FALSE; else if(stCurrent.year < stExpir.wYear) ; else if(stCurrent.month > stExpir.wMonth) Valid=FALSE; else if(stCurrent.month < stExpir.wMonth) ; else if(stCurrent.day > stExpir.wDay) Valid=FALSE; //if(stCurrent.year >stExpir.wYear) goto time_bad; //if(stCurrent.year <stExpir.wYear) goto time_ok; //if(stCurrent.month>stExpir.wMonth)goto time_bad; //if(stCurrent.month<stExpir.wMonth)goto time_ok; //if(stCurrent.day >stExpir.wDay) goto time_bad; //if(stCurrent.day <stExpir.wDay) goto time_ok; //time_bad: // if(!!(avpKey->Flags & KEY_F_INFOEVERYLAUNCH) // || !(avpKey->Flags & KEY_F_REGISTERED) // || !CheckSelfExpir(stExpir)) // goto clo; // ret=0; // else // ret &= ~FN_UPDATES; //time_ok: if(!CheckSelfExpir(stExpir)) Valid=FALSE; else { if(avpKey->Options & KEY_O_DISINFECT) { Level=10; if((avpKey->Options & KEY_O_UNPACK)||(avpKey->Options & KEY_O_EXTRACT)) Level=20; if((avpKey->Options & KEY_O_REMOTELAUNCH)||(avpKey->Options & KEY_O_REMOTESCAN)) Level=30; } else { if(avpKey->Flags & KEY_F_INFOEVERYLAUNCH) Valid=FALSE; else Level=5; } } if(DATA_Init_Library((void*(*)(unsigned int))Malloc,Free)) { hRootData=DATA_Add(0,0,AVP_PID_KEYROOT,0,0); if(hRootData) { DATA_Add(hRootData,0,PID_FILENAME,(DWORD)filename,0); HDATA hInfo; hInfo=DATA_Add(hRootData,0,AVP_PID_KEYINFO,0,0); //OemToChar(buf+Key->RegNumberOffs, buf+Key->RegNumberOffs); //OemToChar(buf+Key->CopyInfoOffs, buf+Key->CopyInfoOffs); //OemToChar(buf+Key->SupportOffs, buf+Key->SupportOffs); DATA_Add(hInfo,0, PID_SERIALSTR,(DWORD)avpKey+avpKey->RegNumberOffs,0); DATA_Add(hInfo,0, AVP_PID_KEYPLPOSNAME, (DWORD)"AVP Key v1.0", 0); DATA_Add(hInfo,0, AVP_PID_KEYLICENCEINFO,(DWORD)avpKey+avpKey->CopyInfoOffs,0); DATA_Add(hInfo,0, AVP_PID_KEYSUPPORTINFO,(DWORD)avpKey+avpKey->SupportOffs,0); if(stExpir.wDay!=0) DATA_Add(hInfo,0, AVP_PID_KEYEXPDATE, (DWORD)&stExpir, sizeof(AVP_date)); } } } } else ret=0; if(!(avpKey->Flags&KEY_F_REGISTERED)||!(avpKey->Platforms&KEY_P_DOS)) ret=0; } else ret=0; //clo: Free(cbuf); if(ret) { if(level) *level=MAX(*level, Level); if(valid) *valid|=Valid; } return ret; }
HRESULT CInArchive::GetNextItem(bool &filled, CItemEx &item) { filled = false; UInt32 processedSize; Byte startHeader[2]; RINOK(ReadBytes(startHeader, 2, processedSize)) if (processedSize == 0) return S_OK; if (processedSize == 1) return (startHeader[0] == 0) ? S_OK: S_FALSE; if (startHeader[0] == 0 && startHeader[1] == 0) return S_OK; Byte header[256]; const UInt32 kBasicPartSize = 22; RINOK(ReadBytes(header, kBasicPartSize, processedSize)); if (processedSize != kBasicPartSize) return (startHeader[0] == 0) ? S_OK: S_FALSE; const Byte *p = header; memmove(item.Method, p, kMethodIdSize); if (!item.IsValidMethod()) return S_OK; p += kMethodIdSize; p = ReadUInt32(p, item.PackSize); p = ReadUInt32(p, item.Size); p = ReadUInt32(p, item.ModifiedTime); item.Attributes = *p++; item.Level = *p++; if (item.Level > 2) return S_FALSE; UInt32 headerSize; if (item.Level < 2) { headerSize = startHeader[0]; if (headerSize < kBasicPartSize) return S_FALSE; UInt32 remain = headerSize - kBasicPartSize; RINOK(CheckReadBytes(header + kBasicPartSize, remain)); if (startHeader[1] != CalcSum(header, headerSize)) return S_FALSE; size_t nameLength = *p++; if ((p - header) + nameLength + 2 > headerSize) return S_FALSE; p = ReadString(p, nameLength, item.Name); } else headerSize = startHeader[0] | ((UInt32)startHeader[1] << 8); p = ReadUInt16(p, item.CRC); if (item.Level != 0) { if (item.Level == 2) { RINOK(CheckReadBytes(header + kBasicPartSize, 2)); } if ((size_t)(p - header) + 3 > headerSize) return S_FALSE; item.OsId = *p++; UInt16 nextSize; p = ReadUInt16(p, nextSize); while (nextSize != 0) { if (nextSize < 3) return S_FALSE; if (item.Level == 1) { if (item.PackSize < nextSize) return S_FALSE; item.PackSize -= nextSize; } CExtension ext; RINOK(CheckReadBytes(&ext.Type, 1)) nextSize -= 3; ext.Data.SetCapacity(nextSize); RINOK(CheckReadBytes((Byte *)ext.Data, nextSize)) item.Extensions.Add(ext); Byte hdr2[2]; RINOK(CheckReadBytes(hdr2, 2)); ReadUInt16(hdr2, nextSize); } } item.DataPosition = m_Position; filled = true; return S_OK; }