Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

evatransfer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by yunfan                                          *
00003  *   yunfan_zg@163.com                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020  
00021 #include "evatransfer.h"
00022 #include "../evautil.h"
00023 #include <string.h>
00024 #ifdef _WIN32
00025 #include <winsock.h>
00026 #else
00027 #include <arpa/inet.h>
00028 #endif
00029 
00030 EvaPicTransferPacket::EvaPicTransferPacket() 
00031         : EvaPicOutPacket(QQ_05_CMD_TRANSFER, false), fragment(NULL)
00032 {
00033         cryptPosition = NO_CRYPTOGRAPH;
00034         requestSend = false;
00035         dataReply = false;
00036 }
00037 
00038 EvaPicTransferPacket::EvaPicTransferPacket(const bool isDataPacket, const bool isLastPacket)
00039         : EvaPicOutPacket(QQ_05_CMD_TRANSFER, isDataPacket?(isLastPacket?true:false):true)
00040         , fragment(NULL)
00041 {
00042         cryptPosition = NO_CRYPTOGRAPH;
00043         isData = isDataPacket;
00044         isLast = isLastPacket;
00045         requestSend = true;
00046 }
00047 
00048 EvaPicTransferPacket::EvaPicTransferPacket(const EvaPicTransferPacket &rhs)
00049         : EvaPicOutPacket(rhs)
00050 {
00051         *this = rhs;
00052 }
00053 
00054 EvaPicTransferPacket::~EvaPicTransferPacket()
00055 {
00056         if(fragment)
00057                 delete fragment;
00058 }
00059 
00060 EvaPicTransferPacket &EvaPicTransferPacket::operator=(const EvaPicTransferPacket &rhs)
00061 {
00062         *((EvaPicOutPacket*)this) = (EvaPicOutPacket)rhs;
00063         sessionID = rhs.getSessionID();
00064         
00065         memcpy(md5, rhs.getMD5(), 16); // always 16 bytes long
00066         imageLength = rhs.getImageLength();
00067         fileName = rhs.getFileName();
00068         
00069         fragLength = rhs.getFragmentLength();
00070         memcpy(fragment, rhs.getFragment(), fragLength);
00071         isData = rhs.isDataPacket();
00072         isLast = rhs.isLastPacket();
00073         
00074         requestSend = rhs.isRequestSend();
00075         dataReply = rhs.isDataReply();
00076         return *this;
00077 }
00078 
00079 void EvaPicTransferPacket::setMd5(const unsigned char *value)
00080 {
00081         memcpy(md5, value, 16);
00082 }
00083 
00084 void EvaPicTransferPacket::setFragment(const unsigned char *data, const unsigned int len)
00085 {
00086         if(!fragment)
00087                 fragment = new unsigned char[len];
00088         fragLength = len;
00089         memcpy(fragment, data, fragLength);
00090 }
00091 
00092 int EvaPicTransferPacket::putBody(unsigned char *buf)
00093 {
00094         int pos=0;
00095         unsigned int tmp4_1 = htonl(0x01000000), tmp4_2 = 0;
00096         if(!requestSend){
00097                 memcpy(buf+pos, &tmp4_1, 4);
00098                 pos+=4;
00099                 memcpy(buf+pos, &tmp4_2, 4);
00100                 pos+=4;                    // ignore 8 bytes
00101                 
00102                 tmp4_1 = htonl(sessionID);
00103                 memcpy(buf+pos, &tmp4_1, 4);
00104                 pos+=4;
00105                 
00106                 memset(buf+pos, 0, 4);
00107                 pos+=4;  // unknown 4 bytes, all 0s
00108                 
00109                 unsigned short tmp2;
00110                 if(dataReply){
00111                         tmp2 = (unsigned short)htons(1);
00112                         memcpy(buf+pos, &tmp2, 2);  //  the following length
00113                         pos+=2;
00114                         
00115                         buf[pos++] = 0x02;   // the contents
00116                 }else{
00117                         tmp2 = (unsigned short)htons(4);
00118                         memcpy(buf+pos, &tmp2, 2);     //  the following length
00119                         pos+=2;
00120                         
00121                         memset(buf+pos, 0, 4);     // the contents
00122                         pos+=4;
00123                 }
00124         }else{
00125                 if(isData){
00126                         if(isLast){ // if is last , should be random 8 bytes
00127                                 tmp4_1 = 1;
00128                                 tmp4_2 = 0;
00129                                 memcpy(buf+pos, &tmp4_1, 4); pos+=4;
00130                                 memcpy(buf+pos, &tmp4_2, 4); pos+=4;
00131                         }else{
00132                                 buf[pos++] = 0x01;
00133                                 memset(buf+pos, 0, 6); pos+=6;
00134                                 buf[pos++] = 0x01;
00135                         }
00136                         tmp4_1 = htonl(sessionID);
00137                         memcpy(buf+pos, &tmp4_1, 4); pos+=4;
00138                         
00139                         memset(buf+pos, 0, 4); pos+=4;
00140                         
00141                         unsigned short tmp2 = htons(fragLength);
00142                         memcpy(buf+pos, &tmp2, 2); pos+=2;
00143                         
00144                         memcpy(buf+pos, fragment, fragLength); pos+=fragLength;
00145                 }else{
00146                         tmp4_1 = 1;
00147                         tmp4_2 = 0;
00148                         memcpy(buf+pos, &tmp4_1, 4); pos+=4;
00149                         memcpy(buf+pos, &tmp4_2, 4); pos+=4; // unknown 8 bytes
00150                         
00151                         tmp4_1 = htonl(sessionID);
00152                         memcpy(buf+pos, &tmp4_1, 4); pos+=4;
00153                         
00154                         memset(buf+pos, 0, 4); pos+=4;
00155                         
00156                         pos+=2;
00157                         int start = pos;
00158                         pos+=2;
00159                         memcpy(buf+pos, md5, 16); pos+=16;
00160                         
00161                         memcpy(buf+pos, EvaUtil::doMd5((char *)fileName.c_str(), fileName.length()), 16); pos+=16;
00162                         
00163                         tmp4_1 = htonl(imageLength);
00164                         memcpy(buf+pos, &tmp4_1, 4); pos+=4;
00165                         
00166                         short tmp2 = htons(fileName.length());
00167                         memcpy(buf+pos, &tmp2, 2); pos+=2;
00168                         
00169                         memcpy(buf+pos, fileName.c_str(), fileName.length()); pos+=fileName.length();
00170                         
00171                         memset(buf+pos, 0, 8); pos+=8;
00172                         
00173                         tmp2 = htons((short)(pos - start));
00174                         memcpy(buf+start-2, &tmp2, 2);
00175                         memcpy(buf+start, &tmp2, 2);
00176                 }
00177         }
00178         return pos;
00179 }
00180 
00181 
00182 /************************************************************************************************************************************************/
00183 
00184 
00185 EvaPicTransferReplyPacket::EvaPicTransferReplyPacket(unsigned char *buf, int len)
00186         : EvaPicInPacket(buf, len, NO_CRYPTOGRAPH), data(NULL)
00187 {
00188 }
00189 
00190 EvaPicTransferReplyPacket::EvaPicTransferReplyPacket(const EvaPicTransferReplyPacket &rhs)
00191         : EvaPicInPacket(rhs)
00192 {
00193         *this = rhs;
00194 }
00195 
00196 EvaPicTransferReplyPacket::~EvaPicTransferReplyPacket()
00197 {
00198         if(data)
00199                 delete data;
00200 }
00201 
00202 EvaPicTransferReplyPacket &EvaPicTransferReplyPacket::operator=(const EvaPicTransferReplyPacket &rhs)
00203 {
00204         *((EvaPicInPacket *)this) = (EvaPicInPacket)rhs;
00205         sessionID = rhs.getSessionID();
00206         memcpy(md5, rhs.getMd5(), 16);
00207         memcpy(fileNameMd5, rhs.getFileNameMd5(), 16);
00208         fileName = rhs.getFileName();
00209         imageLength = rhs.getImageLength();
00210         
00211         dataLength = rhs.getDataLength();
00212         memcpy(data, rhs.getData(), dataLength);
00213         return *this;
00214 }
00215 
00216 void EvaPicTransferReplyPacket::parseBody()
00217 {
00218         int pos=0;
00219         pos+=8; // ignore unknown 8 bytes
00220         
00221         unsigned int tmp4;
00222         memcpy(&tmp4, decryptedBuf+pos, 4); pos+=4;
00223         sessionID = ntohl(tmp4);
00224         
00225         pos+=4;  // unknown 4 bytes
00226         
00227         unsigned short tmp2;
00228         memcpy(&tmp2, decryptedBuf+pos, 2); pos+=2;
00229         unsigned short len = ntohs(tmp2);
00230         
00231         if(source != QQ_CLIENT_VERSION && !sequence){
00232                 pos+=2;
00233                 memcpy(md5, decryptedBuf+pos, 16); pos+=16;
00234                 
00235                 memcpy(fileNameMd5, decryptedBuf+pos, 16); pos+=16;
00236                 
00237                 memcpy(&tmp4, decryptedBuf+pos, 4); pos+=4;
00238                 imageLength = ntohl(tmp4);
00239                 
00240                 unsigned short fileNameLength;
00241                 memcpy(&tmp2, decryptedBuf+pos, 2); pos+=2;
00242                 fileNameLength = ntohs(tmp2);
00243                 
00244                 char *str = new char[fileNameLength + 1];
00245                 memcpy(str, decryptedBuf+pos, fileNameLength); pos+= fileNameLength;
00246                 str[fileNameLength] = 0x00;
00247                 fileName.assign(str);
00248                 delete str;
00249         }else{
00250                 data = new unsigned char[len];
00251                 dataLength = len;
00252                 
00253                 memcpy(data, decryptedBuf+pos, len); pos+= len;
00254         }
00255 }
00256 
00257 
00258 
00259 

Generated on Mon May 15 20:48:42 2006 for libeva by  doxygen 1.4.4