//这个函数的特点是可以指定输入数据的长度,注意,input的有效数据长度必须是16的整数倍,input的大小必须大于等于len。 //但是outbuf仅仅存储,有效的输出长度。 int Aes::DecryptEcb(Memory<char>& outbuf,const void* input,int len){ if(len<=0) return 0; if(outbuf.Length()<(uint)len){ if(!outbuf.SetLength(len)) return 0; } return DecryptEcb(outbuf.Handle(),len,input,len); }
//返回值是加密数据的长度,outbuf会自动设置大小,但是不一定是数据长度,可能大于它。 int Aes::EncryptEcb(Memory<char>& outbuf,const void* input,int len){ if(len<=0) return 0; int lastn = len&0xF; int outLen; if(lastn) outLen = len - lastn + 16; else outLen = len; if(outbuf.Length()<(uint)outLen){ if(!outbuf.SetLength(outLen)) return 0; } return EncryptEcb(outbuf.Handle(),outLen,input,len); }
int Image::Save(Memory<char>& mem,LPCWSTR type,long quality){ if(_image==0) return 0; CLSID clsid; //未知原因,如果不用一个变量转换一下,type是一个常量字串的话,如果把 type 直接传给 GetImageEncoderClsid 会引起程序崩溃。 //wchar_t tp[256]; //这样也不行。 //cs::WcsCopy(tp,type); cs::String tp = type; if(!GetImageEncoderClsid(tp,&clsid)) return 0; if(quality>100) quality = 100; if(quality<0) quality = 0; Gdiplus::EncoderParameters eps; eps.Count = 1; eps.Parameter[0].Guid = Gdiplus::EncoderQuality; eps.Parameter[0].NumberOfValues = 1; eps.Parameter[0].Type = Gdiplus::EncoderParameterValueTypeLong; eps.Parameter[0].Value = &quality; IStream* stream; if(FAILED(CreateStreamOnHGlobal(0,1,&stream))) return 0; if(((_Image*)_image)->Save(stream,&clsid,&eps)!=0) return 0; ULONG ws = 0; LARGE_INTEGER li; ULARGE_INTEGER npos; li.QuadPart = 0; stream->Seek(li,STREAM_SEEK_END,&npos); if(mem.Length()<npos.LowPart){ mem.SetUseSysMemory(1); if(!mem.SetLength(npos.LowPart)) return 0; } stream->Seek(li,STREAM_SEEK_SET,0); HRESULT ret = stream->Read(mem.Handle(),mem.Length(),&ws); stream->Release(); if(FAILED(ret)) return 0; return ws; }
int HttpRequest::Request(const void* data,int len,Memory<char>& html,String* retHead){ if(!hRequest) return -1; if(!::HttpSendRequest(hRequest,NULL,0,(LPVOID)data,len)) return -1; DWORD dwSize = 0; if(retHead){ HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL); if(dwSize!=0){ retHead->SetCubage(dwSize/2); if(HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, (LPVOID)retHead->Handle(), &dwSize, NULL)){ //安全起见,实际上数据已经是一个NULL结尾的字串。 (*retHead)[dwSize/2-1] = 0; retHead->Realize(); } } } int offset = 0; for(;;){ char szData[1024]; DWORD dwByteRead = 0; ::InternetReadFile(hRequest,szData,sizeof(szData),&dwByteRead); if(!dwByteRead) break; if(html.Length()<dwByteRead+offset+1){ if(!html.SetLength(dwByteRead+offset+1)){ Print(L"html alloc failed"); break; } } html.CopyFrom(szData,dwByteRead,offset); offset += dwByteRead; } if(offset!=0) html[offset] = 0; ::InternetCloseHandle(hConnect); hConnect = 0; ::InternetCloseHandle(hRequest); hRequest = 0; return offset; }