Example #1
0
//==============================================================================
bool PEFile::saveToFile(char* filePath) {
    commit();
    buildImportTable();

    // create the output file
    HANDLE fileHandle = CreateFile(filePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (fileHandle == INVALID_HANDLE_VALUE) {
        echo("Couldn't create file");
        return false;
    }

    DWORD bytesWritten;

    WriteFile(fileHandle, &dosHeader, sizeof(PE_DOS_HEADER), &bytesWritten, NULL);
    WriteFile(fileHandle, dosStub.RawData, dosStub.Size, &bytesWritten, NULL);
    writePadding(fileHandle, dosHeader.PEHeaderOffset - sizeof(PE_DOS_HEADER) - dosStub.Size);
    WriteFile(fileHandle, &peHeaders, sizeof(PE_NT_HEADERS), &bytesWritten, NULL);
    WriteFile(fileHandle, &sectionTable, peHeaders.FileHeader.NumberOfSections * sizeof(PE_SECTION_HEADER), &bytesWritten, NULL);
    WriteFile(fileHandle, reservedData.RawData, reservedData.Size, &bytesWritten, NULL);

    for	(int i = 0; i < peHeaders.FileHeader.NumberOfSections; i++) {
        writePadding(fileHandle, sectionTable[i].PointerToRawData - GetFileSize(fileHandle, NULL));
        WriteFile(fileHandle, sections[i].RawData, sections[i].Size, &bytesWritten, NULL);
    }

    CloseHandle(fileHandle);

    return true;
}
Example #2
0
	C_Log::C_Log(LOG_TYPE logType)
	{
		if (C_Log::_newLine) {
			//overwrite the log type only if the message is written on a new line
			//the type is the same if the message is not written on a new line
			C_Log::_logType = logType;
			writeHeader();
		}
		else {
			writePadding();
		}

		C_Log::_newLine = true;
	}
/*----------------------------------------------------------------------------*/
void sendViaEth(ethernet_frame_t* eth)
{
	uint8_t sendBuffer[max_frame_size]; // the byte array that will be sent
	uint8_t current_offset; // keeps track of current offset in the buffer
	uint8_t padding_len; // how much padding is required
	uint8_t message_len; // length of the message being sent
	
	createMac((uint8_t*)&(eth->eth_header.dest),dom0mac);
	createMac((uint8_t*)&(eth->eth_header.source),mymac);
	eth->eth_header.data_len = 0x0008; // 0x0008 means frame's content is IP

	writeEthernetHeader(sendBuffer, &(eth->eth_header), ethernet_header_offset);
	
	writeIPHeader(sendBuffer, &(eth->ip_header), ip_header_offset);
	
	writeUdpHeader(sendBuffer, &(eth->udp_header), udp_header_offset);
	
	writeMessage(sendBuffer, &eth->message, message_offset);

	message_len = eth->message.length;
	current_offset = message_offset+message_len;
	
	if(sum_headers_len + message_len < min_frame_size)
	{
		
		padding_len = min_frame_size - (sum_headers_len + message_len );
		writePadding(sendBuffer, current_offset, padding_len);
		current_offset += padding_len;
	}
	else
	{
		padding_len = 0;
	}
	
	writeChecksum(sendBuffer, current_offset);

	//netfront_xmit(xen_network_device,(unsigned char*) sendBuffer, current_offset+frame_checksum_length);
	netfront_xmit(net_dev,(unsigned char*) sendBuffer, current_offset+frame_checksum_length);//change it to linux version
	
}
Example #4
0
void TarWriter::writeFile(String path, FileStatus *status)
{
    Ref<StringList> headerFields = StringList::create();

    off_t contentSize = status->size();
    if (status->type() != File::Regular) contentSize = 0;

    if (status->type() == File::Directory) {
        if (path->count() > 0) {
            if (path->at(path->count() - 1) != '/') path = path + "/";
        }
    }

    String pathField(99, '\0');
    if (status == longPathStatus_ || status == longLinkStatus_)
        *pathField = *String("././@LongLink");
    else
        *pathField = *path;
    headerFields->append(pathField);
    headerFields->append(zero_);

    headerFields->append(oct(status->mode(), 7));
    headerFields->append(zero_);
    headerFields->append(oct(status->ownerId(), 7));
    headerFields->append(zero_);
    headerFields->append(oct(status->groupId(), 7));
    headerFields->append(zero_);
    if (status == longPathStatus_ || status == longLinkStatus_)
        headerFields->append(oct(path->count() + 1, 11));
    else
        headerFields->append(oct(contentSize, 11));
    headerFields->append(zero_);
    headerFields->append(oct(status->st_mtime, 11));
    headerFields->append(zero_);

    String checksumField(6, '0');
    headerFields->append(checksumField);
    headerFields->append(String("\0 ", 2));

    String typeField, linkTarget;
    if (status == longLinkStatus_ )                   typeField = "K";
    else if (status == longPathStatus_)               typeField = "L";
    else {
             if (status->type() == File::Regular)     ;
        else if (status->type() == File::Directory)   typeField = "5";
        else if (status->type() == File::Symlink)     typeField = "2";
        else if (status->type() == File::CharDevice)  typeField = "3";
        else if (status->type() == File::BlockDevice) typeField = "4";
        else if (status->type() == File::Fifo)        typeField = "6";
        if (status->numberOfHardLinks() > 1) {
            FileId fid(status);
            if (hardLinks_->lookup(fid, &linkTarget)) typeField = "1";
            else hardLinks_->insert(fid, path);
        }
        else if (status->type() == File::Symlink) {
            linkTarget = File::readlink(path);
        }
        if (typeField == "")                          typeField = "0";
        if (typeField != "0") contentSize = 0;
    }
    headerFields->append(typeField);

    String linkField(99, '\0');
    *linkField = *linkTarget;
    headerFields->append(linkField);
    headerFields->append(zero_);

    String gnuMagicField("ustar  ");
    headerFields->append(gnuMagicField);
    headerFields->append(zero_);

    String userField(31, '\0');
    *userField = *User::lookup(status->ownerId())->name();
    headerFields->append(userField);
    headerFields->append(zero_);

    String groupField(31, '\0');
    *groupField = *Group::lookup(status->groupId())->name();
    headerFields->append(groupField);
    headerFields->append(zero_);

    if (status != longPathStatus_ && status != longLinkStatus_) {
        if (path->count() > pathField->count()) writeFile(path, longPathStatus_);
        if (linkTarget->count() > linkField->count()) writeFile(linkTarget, longLinkStatus_);
    }

    String header = headerFields->join();
    FLUX_ASSERT(header->count() == 329);
    unsigned checksum = tarHeaderSum(header);
    *checksumField = *oct(checksum, 6);
    header = headerFields->join();
    sink_->write(header);
    writePadding(header->count());

    if (status == longPathStatus_ || status == longLinkStatus_) {
        sink_->write(path);
        sink_->write(zero_);
        writePadding(path->count() + 1);
    }
    else if (contentSize > 0) {
        File::open(path)->transfer(contentSize, sink_);
        writePadding(contentSize);
    }
}