Пример #1
0
int ReadKDPipe(HANDLE hPipe, kd_packet_t *pktBuffer){
	DWORD numBytesRead = 0;
	BOOL result;
	UINT8 firstCMD = 0x00;
	do{
		firstCMD = Get8Pipe(hPipe);
	} while (firstCMD != 0x69 && firstCMD != 0x30 && firstCMD != 0x62);

	if (firstCMD == 0x62){ //Fast-Break !!!
		return FASTBREAK_PKT; //TODO: return FAST-BREAK !
	}
	UINT32 leader = (firstCMD << 24) | (Get16Pipe(hPipe) << 8) | Get8Pipe(hPipe);
	if (leader == 0x69696969
		|| leader == 0x30303030){
		UINT16 type = Get16Pipe(hPipe);
		UINT16 length = Get16Pipe(hPipe);
		UINT32 id = Get32Pipe(hPipe);
		UINT32 checksum = Get32Pipe(hPipe);

		pktBuffer->leader = leader;
		pktBuffer->type = type;
		pktBuffer->length = length;
		pktBuffer->id = id;
		pktBuffer->checksum = checksum;

		//TODO: function !
		UINT16 bytesToRead = length;
		UINT16 bytesAlreadyRead = 0;
		while (bytesToRead > 0){
			//printf("bytesToRead %d\n", bytesToRead);
			result = ReadFile(hPipe, pktBuffer->data + bytesAlreadyRead, bytesToRead, &numBytesRead, NULL);
			bytesToRead = bytesToRead - numBytesRead;
			bytesAlreadyRead = bytesAlreadyRead + numBytesRead;
			//printf("%d/%d\n", bytesAlreadyRead, length);
		}

		//END_OF_DATA
		if (length > 0){
			char endOfData;
			ReadFile(hPipe, &endOfData, 1, NULL, NULL);
		}

		return KD_PKT;
	}else{
		UINT16 type = Get16Pipe(hPipe);
		printf("Unknown Leader %08x\n", leader);
		printf("type: %04x\n", type);
		//system("pause");
	}
	return ERR_PKT;
}
Пример #2
0
/*
 * Read a pkt from a Named Pipe
 *
 * @param hPipe the Named Pipe to read from
 * @param pktBuffer the destination buffer, the pkt will be in this buffer
 *
 * @return Type of pkt (ERR, KD_PKT, FASTBREAK_PKT)
 */
KdPacketType ReadKDPipe(HANDLE hPipe, KD_PACKET_T *pktBuffer)
{
    DWORD numBytesRead = 0;
    BOOL result;
    UINT8 firstCMD = 0x00;
    do{
        bool dataRead = GetPipeTry(hPipe, &firstCMD, sizeof(uint8_t), true);
        if (dataRead == false){
            return KdNoPacket;
        }
    } while (firstCMD != TYPE1_SHORTLEADER && firstCMD != TYPE2_SHORTLEADER && firstCMD != BREAKIN_SHORTLEADER);

    if (firstCMD == BREAKIN_SHORTLEADER){
        //This is a BreakIn short packet
        return KdBreakinPacket;
    }

    //Read the end of the Leader
    uint32_t LeaderEnd = 0;
    GetPipe(hPipe, (uint8_t*)&LeaderEnd, 3);
    uint32_t u32Leader = (firstCMD << 24) | LeaderEnd;
    if (u32Leader == TYPE1_LONGLEADER
        || u32Leader == TYPE2_LONGLEADER){

        pktBuffer->Leader = u32Leader;

        //Read header
        GetPipe(hPipe, (uint8_t*)&(pktBuffer->Type), 12);

        //TODO: function !
        uint32_t bytesToRead = pktBuffer->Length;
        uint32_t bytesAlreadyRead = 0;
        while (bytesToRead > 0){
            result = ReadFile(hPipe, pktBuffer->data + bytesAlreadyRead, bytesToRead, &numBytesRead, NULL);
            bytesToRead = bytesToRead - numBytesRead;
            bytesAlreadyRead = bytesAlreadyRead + numBytesRead;
        }

        //END_OF_DATA
        if (pktBuffer->Length > 0){
            //Trick to avoid segfault on Windows 7 ???
            uint8_t tmpBuffer[1024];
            DWORD test = 0;
            if (ReadFile(hPipe, tmpBuffer, 1, &test, NULL) == false){
                printf("Error while reading file !\n");
            }
        }
        if (DEBUG_PKT){
            ParseKDPkt(pktBuffer);
        }
        return KdKdPacket;
    }else{
        UINT16 type = Get16Pipe(hPipe);
        printf("Unknown Leader %08x\n", u32Leader);
        printf("type: %04x\n", type);
    }

    return KdErrorPacket;
}