/** * @name ParseHeader * Parse a Patch file header * @note The current implementation is far from complete! * * @param Patch * Buffer pointing to the raw patch data * * @param Header * The result of the parsed header * * @return STATUS_SUCCESS on success, an error code otherwise */ DWORD ParseHeader(SAFE_READ* Patch, PATCH_HEADER* Header) { DWORD Crc, Unknown; int Delta; ZeroMemory(Header, sizeof(*Header)); /* Validate the patch */ Crc = RtlComputeCrc32(0, Patch->Root, Patch->Size); if (Crc != ~0) return ERROR_PATCH_CORRUPT; if (ReadDWord(Patch) != '91AP') return ERROR_PATCH_DECODE_FAILURE; /* Read the flags, warn about an unknown combination */ Header->Flags = ReadDWord(Patch); if (Header->Flags ^ UNKNOWN_FLAGS_COMBINATION) ERR("Unknown flags: 0x%x, patch will most likely fail\n", Header->Flags ^ UNKNOWN_FLAGS_COMBINATION); /* 0x5bb3284e, 0x5bb33562, 0x5bb357b1 */ Unknown = ReadDWord(Patch); TRACE("Unknown: 0x%x\n", Unknown); Header->OutputSize = DecodeDWord(Patch); Header->OutputCrc = ReadDWord(Patch); Unknown = ReadByte(Patch); if (Unknown != 1) ERR("Field after CRC is not 1 but %u\n", Unknown); Delta = DecodeInt(Patch); Header->OldSize = Header->OutputSize + Delta; Header->OldCrc = ReadDWord(Patch); Unknown = ReadUShort(Patch); if (Unknown != 0) ERR("Field1 after OldCrc is not 0 but %u\n", Unknown); Unknown = DecodeDWord(Patch); if (Unknown != 0) ERR("Field2 after OldCrc is not 0 but %u\n", Unknown); Header->DataSize = DecodeDWord(Patch); /* Remaining data, minus the CRC appended */ if (Header->DataSize != (Patch->Size - (Patch->Ptr - Patch->Root) - sizeof(DWORD))) { ERR("Unable to read header, check previous logging!\n"); return ERROR_PATCH_DECODE_FAILURE; } return STATUS_SUCCESS; }
string TorrentFile::ParseNext(unsigned short * buf, int * pos) { int cur_pos = *pos; unsigned short type = buf[cur_pos]; while(type > 0 && type < 255 && !isdigit(type)) { if(type == 'i') { int i = DecodeInt(buf, &cur_pos); } else (cur_pos)++; type = buf[cur_pos]; } if(type < 0) { //WriteFile(buf, m_size); } *pos = cur_pos; if(cur_pos > m_size) return "break"; return DecodeString(buf,pos); }
// Function processes command line parameters int ProcessCommandLine (int argc, char* argv[], Config& settings, char*& pInFile, char*& pOutFile, char*& pConfig) { int cmdCount = 0; char* cmdRead; while (cmdCount < argc-1) { // command line error if (cmdCount < 0) return cmdCount; // next command to process! cmdCount++; cmdRead = argv[cmdCount]; // this is a command directive if (cmdRead[0] == '-') { // upcase the command parameter StrUpr (cmdRead); cmdRead++; // miscellaneous flags, "sort +1" DecodeFlg ("BCL", settings.braceLoc, False); DecodeFlg ("BNL", settings.braceLoc, True); DecodeInt ("CC", settings.posOfCommentsWC); DecodeInt ("F", settings.numOfLineFunc); DecodeStr ("FI", pInFile); DecodeStr ("FNC", pConfig); DecodeStr ("FO", pOutFile); DecodeInt ("I", settings.tabSpaceSize); DecodeFlg ("LG", settings.deleteHighChars, 3); DecodeInt ("NC", settings.posOfCommentsNC); DecodeInt ("QB", settings.queueBuffer); DecodeFlg ("S", settings.useTabs, False); DecodeFlg ("T", settings.useTabs, True); DecodeFlg ("TBCL", settings.topBraceLoc, False); DecodeFlg ("TBNL", settings.topBraceLoc, True); // "No" flags DecodeFlg ("NA", settings.deleteHighChars, 0); DecodeFlg ("NB", settings.backUp, False); DecodeFlg ("NBBI", settings.braceIndent2, False); DecodeFlg ("NBI", settings.braceIndent, False); DecodeFlg ("NLCNC", settings.leaveCommentsNC, False); DecodeFlg ("NO", settings.output, False); DecodeFlg ("NQ", settings.quoteChars, False); // "Yes" flags DecodeFlg ("YA", settings.deleteHighChars, 1); DecodeFlg ("YB", settings.backUp, True); DecodeFlg ("YBBI", settings.braceIndent2, True); DecodeFlg ("YBI", settings.braceIndent, True); DecodeFlg ("YLCNC", settings.leaveCommentsNC, True); DecodeFlg ("YO", settings.output, True); DecodeFlg ("YQ", settings.quoteChars, True); // ### display help ### if( (strcmp ("?", cmdRead) == 0) || (strcmp ("H", cmdRead) == 0) ) { verbose ("*** Displaying Brief Help ***\n"); PrintProgramHelp (); return -1; } warning ("Unknown Command Directive %s \n", cmdRead); PrintProgramHelp (); return -1; } else if (pInFile == NULL) pInFile = argv [cmdCount]; else if (pOutFile == NULL) pOutFile = argv [cmdCount]; else { warning ("Command Line Error : Expected Command Directive, Not %s\n", argv[cmdCount]); PrintProgramHelp (); return -1; } } if (settings.queueBuffer < 2) settings.queueBuffer = 2; return 0; }
bool TorrentFile::ParseTorrentFile(unsigned short * buf, int size) { string tempstring; int pos = 1; if(buf[0] != 'd') { tempstring = ""; strcpy(m_url,tempstring.c_str()); return false; } int len = size; if(m_data == NULL) m_data = new unsigned short[len+1]; memcpy(m_data, buf, len); for(int i = 0; i < len; i++) { if(m_data[i] != buf[i]) { m_data[i] = buf[i]; } } m_data[len] = 0; m_size = size; info_start = 0; int info_end = 0; bool havePieces = false; while(true) { tempstring = ParseNext(buf, &pos); // The URL if(tempstring.compare("announce") == 0) { tempstring = DecodeString(buf, &pos); strcpy(m_url,tempstring.c_str()); } else if(tempstring.compare("announce-list") == 0) { AtomList al = DecodeList(buf, &pos); v_announce_list = al.v_elements; } else if(tempstring.compare("info") == 0) { info_start = pos; } // Creation date else if(tempstring.compare("creation date") == 0) { m_creationdate = DecodeInt(buf,&pos); } // Length else if(tempstring.compare("length") == 0) { m_length += DecodeInt(buf,&pos); } // Name else if(tempstring.compare("name") == 0) { tempstring = DecodeString(buf, &pos); strcpy(m_name,tempstring.c_str()); } // Piece Length else if(tempstring.compare("piece length") == 0) { m_piecelength = DecodeInt(buf,&pos); } // Pieces else if(tempstring.compare("pieces") == 0) { v_pieces = DecodePieces(buf, &pos); m_numpieces = (int)v_pieces.size(); havePieces = true; } if(havePieces && (buf[pos] == 'e') || tempstring.compare("break") == 0) { info_end = pos; info_len = info_end-info_start+1; break; } } b_is_valid = true; return true; }