예제 #1
0
파일: gtm.c 프로젝트: alexbirkett/GPSBabel
static void write_trk_style(const route_head *trk)
{
	fwrite_string(file_out, trk->rte_name);
	fwrite_byte(file_out, 1);
	fwrite_long(file_out, 0);
	fwrite_single(file_out, 0);
	fwrite_byte(file_out, 0);
	fwrite_integer(file_out, 0);
}
예제 #2
0
파일: gtm.c 프로젝트: alexbirkett/GPSBabel
static void write_rte_waypt(const waypoint *wpt)
{
	fwrite_double(file_out, wpt->latitude);
	fwrite_double(file_out, wpt->longitude);
	fwrite_fixedstring(file_out, wpt->shortname, 10);
	fwrite_string(file_out, wpt->description);
	fwrite_string(file_out, rte_active->rte_name);
	fwrite_integer(file_out, icon_from_descr(wpt->icon_descr));
	fwrite_byte(file_out, 3);
	fwrite_byte(file_out, start_new);
	fwrite_long(file_out, 0);
	fwrite_integer(file_out, 0);
	if (wpt->altitude == unknown_alt)
		fwrite_single(file_out, unknown_alt_gtm);
	else
		fwrite_single(file_out, wpt->altitude);
	fwrite_integer(file_out, 0);
	start_new = 0;
}
예제 #3
0
파일: gtm.c 프로젝트: alexbirkett/GPSBabel
static void write_trk_waypt(const waypoint *wpt)
{
	fwrite_double(file_out, wpt->latitude);
	fwrite_double(file_out, wpt->longitude);
	fwrite_long(file_out, wpt->creation_time-EPOCH89DIFF);
	fwrite_byte(file_out, start_new);
	if (wpt->altitude == unknown_alt)
		fwrite_single(file_out, unknown_alt_gtm);
	else
		fwrite_single(file_out, wpt->altitude);
	start_new = 0;
}
예제 #4
0
파일: gtm.c 프로젝트: alexbirkett/GPSBabel
static void
gtm_wr_init(const char *fname)
{
	rt_count = tr_count = 0;
	track_disp_all(NULL, NULL, count_track_waypts);
	route_disp_all(NULL, NULL, count_route_waypts);

	file_out = gbfopen_le(fname, "wb", MYNAME);	/* little endian */

	/* Header */
	fwrite_integer(file_out, 211);
	fwrite_fixedstring(file_out, "TrackMaker", 10);
	fwrite_byte(file_out, 0);
	fwrite_byte(file_out, 0);
	fwrite_byte(file_out, 8);
	fwrite_byte(file_out, 0);
	fwrite_byte(file_out, 0);
	fwrite_byte(file_out, 0);
	fwrite_byte(file_out, 0);
	fwrite_long(file_out, 0);
	fwrite_long(file_out, 16777215);
	fwrite_long(file_out, waypt_count() ? 4 : 0); /* num waypoint styles */
	fwrite_long(file_out, 0);
	fwrite_long(file_out, waypt_count()); /* num waypoints */
	fwrite_long(file_out, tr_count);
	fwrite_long(file_out, rt_count);
	fwrite_single(file_out, 0); /* maxlon */
	fwrite_single(file_out, 0); /* minlon */
	fwrite_single(file_out, 0); /* maxlat */
	fwrite_single(file_out, 0); /* minlat */
	fwrite_long(file_out, 0);
	fwrite_long(file_out, track_count()); /* num tracklog styles */
	fwrite_single(file_out, 0);
	fwrite_single(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_bool(file_out, 0);
	fwrite_string(file_out, "Times New Roman");
	fwrite_string(file_out, "");
	fwrite_string(file_out, "");
	fwrite_string(file_out, "");

	/* User Grid and Datum */
	fwrite_null(file_out, 34);
	fwrite_integer(file_out, 217); /* WGS84 */
	fwrite_null(file_out, 22);
}
예제 #5
0
파일: gtm.c 프로젝트: alexbirkett/GPSBabel
static void write_waypt(const waypoint *wpt)
{
	fwrite_double(file_out, wpt->latitude);
	fwrite_double(file_out, wpt->longitude);
	fwrite_fixedstring(file_out, wpt->shortname, 10);
	fwrite_string(file_out, wpt->description);
	fwrite_integer(file_out, icon_from_descr(wpt->icon_descr));
	fwrite_byte(file_out, 3);
	if (wpt->creation_time)
		fwrite_long(file_out, wpt->creation_time-EPOCH89DIFF);
	else
		fwrite_long(file_out, 0);
	fwrite_integer(file_out, 0);
	if (wpt->altitude == unknown_alt)
		fwrite_single(file_out, unknown_alt_gtm);
	else
		fwrite_single(file_out, wpt->altitude);
	fwrite_integer(file_out, 0);
}
예제 #6
0
파일: files9x.c 프로젝트: TC01/tilibs
/**
 * ti9x_file_write_flash:
 * @filename: name of flash file where to write.
 * @content: the file content to write.
 *
 * Write content to a flash file (os or app).
 *
 * Return value: an error code, 0 otherwise.
 **/
int ti9x_file_write_flash(const char *fname, Ti9xFlash *head, char **real_fname)
{
	FILE *f;
	Ti9xFlash *content = head;
	char *filename;

	if (head == NULL)
	{
		tifiles_critical("%s: head is NULL", __FUNCTION__);
		return ERR_INVALID_FILE;
	}

	if (fname)
	{
		filename = g_strdup(fname);
		if (filename == NULL)
		{
			return ERR_MALLOC;
		}
	}
	else
	{
		VarEntry ve;

		for (content = head; content != NULL; content = content->next)
		{
			if (content->data_type == TI89_AMS || content->data_type == TI89_APPL)
			{
				break;
			}
		}
		if (content == NULL)
		{
			tifiles_critical("%s: content is NULL", __FUNCTION__);
			return ERR_BAD_FILE;
		}

		strncpy(ve.name, content->name, sizeof(ve.name) - 1);
		ve.name[sizeof(ve.name) - 1] = 0;
		ve.type = content->data_type;

		filename = tifiles_build_filename(content->model, &ve);
		if (real_fname != NULL)
		{
			*real_fname = g_strdup(filename);
		}
	}

	f = g_fopen(filename, "wb");
	if (f == NULL) 
	{
		tifiles_info("Unable to open this file: %s", filename);
		g_free(filename);
		return ERR_FILE_OPEN;
	}

	for (content = head; content != NULL; content = content->next) 
	{
		if (fwrite_8_chars(f, "**TIFL**") < 0) goto tfwf;
		if (fwrite_byte(f, content->revision_major) < 0) goto tfwf;
		if (fwrite_byte(f, content->revision_minor) < 0) goto tfwf;
		if (fwrite_byte(f, content->flags) < 0) goto tfwf;
		if (fwrite_byte(f, content->object_type) < 0) goto tfwf;
		if (fwrite_byte(f, content->revision_day) < 0) goto tfwf;
		if (fwrite_byte(f, content->revision_month) < 0) goto tfwf;
		if (fwrite_word(f, content->revision_year) < 0) goto tfwf;
		if (fwrite_byte(f, (uint8_t) strlen(content->name)) < 0) goto tfwf;
		if (fwrite_8_chars(f, content->name) < 0) goto tfwf;
		if (fwrite_n_chars(f, 23, "") < 0) goto tfwf;
		if (fwrite_byte(f, content->device_type) < 0) goto tfwf;
		if (fwrite_byte(f, content->data_type) < 0) goto tfwf;
		if (fwrite_n_chars(f, 23, "") < 0) goto tfwf;
		if (fwrite_byte(f, content->hw_id) < 0)  goto tfwf;
		if (fwrite_long(f, content->data_length) < 0) goto tfwf;
		if (fwrite(content->data_part, 1, content->data_length, f) < content->data_length) goto tfwf;
	}

	g_free(filename);
	fclose(f);
	return 0;

tfwf:	// release on exit
	tifiles_critical("%s: error writing file %s", __FUNCTION__, filename);
	g_free(filename);
	fclose(f);
	return ERR_FILE_IO;
}
예제 #7
0
파일: files9x.c 프로젝트: TC01/tilibs
/**
 * ti9x_file_write_regular:
 * @filename: name of single/group file where to write or NULL.
 * @content: the file content to write.
 * @real_filename: pointer address or NULL. Must be freed if needed when no longer needed.
 *
 * Write one (or several) variable(s) into a single (group) file. If filename is set to NULL,
 * the function build a filename from varname and allocates resulting filename in %real_fname.
 * %filename and %real_filename can be NULL but not both !
 *
 * %real_filename must be freed when no longer used.
 *
 * Return value: an error code, 0 otherwise.
 **/
int ti9x_file_write_regular(const char *fname, Ti9xRegular *content, char **real_fname)
{
	FILE *f;
	unsigned int i;
	char *filename = NULL;
	uint32_t offset = 0x52;
	int **table;
	unsigned int num_folders;
	char default_folder[FLDNAME_MAX];
	char fldname[FLDNAME_MAX], varname[VARNAME_MAX];

	if (content->entries == NULL)
	{
		tifiles_warning("%s: skipping content with NULL content->entries", __FUNCTION__);
		return 0;
	}

	if (fname != NULL)
	{
		filename = g_strdup(fname);
		if (filename == NULL)
		{
			return ERR_MALLOC;
		}
	} 
	else 
	{
		if (content->entries[0])
		{
			filename = tifiles_build_filename(content->model_dst, content->entries[0]);
		}
		else
		{
			tifiles_warning("%s: asked to build a filename from null content->entries[0], bailing out", __FUNCTION__);
			if (real_fname != NULL)
			{
				*real_fname = NULL;
			}
			return 0;
		}
		if (real_fname != NULL)
		{
			*real_fname = g_strdup(filename);
		}
	}

	// build the table of folder & variable entries
	table = tifiles_create_table_of_entries((FileContent *)content, &num_folders);
	if (table == NULL)
	{
		g_free(filename);
		return ERR_MALLOC;
	}

	f = g_fopen(filename, "wb");
	if (f == NULL) 
	{
		tifiles_info( "Unable to open this file: %s", filename);
		tifiles_free_table_of_entries(table);
		g_free(filename);
		return ERR_FILE_OPEN;
	}

	// write header
	if (fwrite_8_chars(f, tifiles_calctype2signature(content->model)) < 0) goto tfwr;
	if (fwrite(fsignature, 1, 2, f) < 2) goto tfwr;
	if (content->num_entries == 1)	// folder entry for single var is placed here
	{
		strncpy(content->default_folder, content->entries[0]->folder, sizeof(content->default_folder) - 1);
		content->default_folder[sizeof(content->default_folder) - 1] = 0;
	}
	ticonv_varname_to_tifile_sn(content->model, content->default_folder, default_folder, sizeof(default_folder), -1);
	if (fwrite_8_chars(f, default_folder) < 0) goto tfwr;
	if (fwrite_n_bytes(f, 40, (uint8_t *)content->comment) < 0) goto tfwr;
	if (content->num_entries > 1) 
	{
		if (fwrite_word(f, (uint16_t) (content->num_entries + num_folders)) < 0) goto tfwr;
		offset += 16 * (content->num_entries + num_folders - 1);
	} 
	else
	{
		if (fwrite_word(f, 1) < 0) goto tfwr;
	}

	// write table of entries
	for (i = 0; table[i] != NULL; i++) 
	{
		VarEntry *fentry;
		int j, idx = table[i][0];
		fentry = content->entries[idx];

		if (fentry == NULL)
		{
			tifiles_warning("%s: skipping null content entry %d", __FUNCTION__, i);
			continue;
		}

		if (content->num_entries > 1)	// single var does not have folder entry
		{
			if (fwrite_long(f, offset) < 0) goto tfwr;
			ticonv_varname_to_tifile_sn(content->model, fentry->folder, fldname, sizeof(fldname), -1);
			if (fwrite_8_chars(f, fldname) < 0) goto tfwr;
			if (fwrite_byte(f, (uint8_t)tifiles_folder_type(content->model)) < 0) goto tfwr;
			if (fwrite_byte(f, 0x00) < 0) goto tfwr;
			for (j = 0; table[i][j] != -1; j++);
			if (fwrite_word(f, (uint16_t) j) < 0) goto tfwr;
		}

		for (j = 0; table[i][j] != -1; j++) 
		{
			int idx2 = table[i][j];
			VarEntry *entry = content->entries[idx2];
			uint8_t attr = ATTRB_NONE;

			if (fwrite_long(f, offset) < 0) goto tfwr;
			ticonv_varname_to_tifile_sn(content->model, entry->name, varname, sizeof(varname), entry->type);
			if (fwrite_8_chars(f, varname) < 0) goto tfwr;
			if (fwrite_byte(f, entry->type) < 0) goto tfwr;
			attr = (entry->attr == ATTRB_ARCHIVED) ? 3 : entry->attr;
			if (fwrite_byte(f, attr) < 0) goto tfwr;
			if (fwrite_word(f, 0) < 0) goto tfwr;

			offset += entry->size + 4 + 2;
		}
	}

	if (fwrite_long(f, offset) < 0) goto tfwr;
	if (fwrite_word(f, 0x5aa5) < 0) goto tfwr;

	// write data
	for (i = 0; table[i] != NULL; i++) 
	{
		int j;

		for (j = 0; table[i][j] != -1; j++) 
		{
			int idx = table[i][j];
			VarEntry *entry = content->entries[idx];
			uint16_t sum;

			if (fwrite_long(f, 0) < 0) goto tfwr;
			if (fwrite(entry->data, 1, entry->size, f) < entry->size) goto tfwr;
			sum = tifiles_checksum(entry->data, entry->size);
			if (fwrite_word(f, sum) < 0) goto tfwr;
		}
	}

	tifiles_free_table_of_entries(table);
	g_free(filename);
	fclose(f);
	return 0;

tfwr:	// release on exit
	tifiles_critical("%s: error writing file %s", __FUNCTION__, filename);
	tifiles_free_table_of_entries(table);
	g_free(filename);
	fclose(f);
	return ERR_FILE_IO;
}
예제 #8
0
파일: bfc.cpp 프로젝트: naokikp/bfc
int bfc_ouptput_exe(FILE *ofp){
#ifdef _DEBUG
    {
        int size = BFCode.size();
        if(sizeof(NativeBuffer) + size <= ALIGNSIZE){
            int addsize = align(sizeof(NativeBuffer) + size, ALIGNSIZE) - sizeof(NativeBuffer) - size;
            BFCode.resize(BFCode.size() + addsize);
            memset(BFCode.data() + size, 0xdd, addsize);
        }
    }
#endif
    int size_codesec_raw = sizeof(NativeBuffer) + BFCode.size();
    int size_codesec = align(size_codesec_raw, ALIGNSIZE);
    int size_datasec = align(max(sizeof(InitBuffer), DATA_SEC_SIZE), ALIGNSIZE);
    int pos_codesec  = align(HEADER_SIZE, ALIGNSIZE);
    int pos_imptsec  = pos_codesec + size_codesec;

    int size_codesec_file = align(sizeof(NativeBuffer) + BFCode.size(), FILEALIGNSIZE);
    int size_datasec_file = align(sizeof(InitBuffer), FILEALIGNSIZE);
    int pos_codesec_file  = align(HEADER_SIZE, FILEALIGNSIZE);
    int pos_imptsec_file  = pos_codesec_file + size_codesec_file;

    if(sizeof(InitBuffer) == 1 && InitBuffer[0] == 0){
        size_datasec_file = 0;
    }

    int Import_DLL_Num = sizeof(ImportDllInfo) / sizeof(ImportDllInfo[0]);
    int Import_Proc_Num = 0;
    for(int i = 0; i < Import_DLL_Num; i++){
        const char **import_hint = ImportDllInfo[i].import_hint;
        for(int j = 0; ;j++){
            if(import_hint[j] != NULL) Import_Proc_Num++; else break;
        }
    }
    // メモリ確保
    int size_ImportDesc = sizeof(IMAGE_IMPORT_DESCRIPTOR) * (Import_DLL_Num + 1);
    IMAGE_IMPORT_DESCRIPTOR *ImportDesc = (IMAGE_IMPORT_DESCRIPTOR *)malloc(size_ImportDesc);
    memset(ImportDesc, 0, size_ImportDesc);

    int pos_LookupTable = pos_imptsec + size_ImportDesc;
    int size_LookupTable = sizeof(DWORD) * (Import_Proc_Num + Import_DLL_Num);
    DWORD *LookupTable = (DWORD *)malloc(size_LookupTable);
    memset(LookupTable, 0, size_LookupTable);
    int index_LookupTable = 0;

    int pos_HintTable = pos_LookupTable + size_LookupTable * 2;
    std::vector<char> HintTable;

    // インポートセクション生成
    for(int i = 0; i < Import_DLL_Num; i++){
        ImportDesc[i].OriginalFirstThunk    = pos_LookupTable + sizeof(DWORD) * index_LookupTable;
        ImportDesc[i].TimeDateStamp         = 0;
        ImportDesc[i].ForwarderChain        = 0;
        ImportDesc[i].Name                  = pos_HintTable + HintTable.size();
        ImportDesc[i].FirstThunk            = pos_LookupTable + sizeof(DWORD) * index_LookupTable + size_LookupTable;
        int htpos = HintTable.size();
        HintTable.resize(htpos + strlen(ImportDllInfo[i].dllname) + 1);
        strcpy(&(HintTable[htpos]), ImportDllInfo[i].dllname);
        if(HintTable.size() % 2) HintTable.resize(HintTable.size()+1);

        const char **import_hint = ImportDllInfo[i].import_hint;
        for(int j = 0; ;j++){
            if(import_hint[j] == NULL){
                LookupTable[index_LookupTable++] = 0;
                break;
            }
            int htpos = HintTable.size();
            LookupTable[index_LookupTable] = pos_HintTable + htpos;
            HintTable.resize(htpos + 2 + strlen(import_hint[j]) + 1);
            *(WORD*)&(HintTable[htpos]) = 0;
            strcpy(&(HintTable[htpos]) + 2, import_hint[j]);
            if(HintTable.size() % 2) HintTable.resize(HintTable.size()+1);

            DWORD IATAddr = EXE_IMAGE_BASE + pos_LookupTable + sizeof(DWORD) * index_LookupTable + size_LookupTable;
#ifdef _DEBUG
            printf("%s:%s IAT = 0x%08x\n", ImportDllInfo[i].dllname, import_hint[j], IATAddr);
#endif
            std::string procname = ImportDllInfo[i].dllname;
            procname += ":"; procname += import_hint[j];
            IAT.insert(make_pair(procname, IATAddr));

            index_LookupTable++;
        }
    }

    int size_imptsec_raw = size_ImportDesc + size_LookupTable * 2 + HintTable.size();
    int size_imptsec = align(size_imptsec_raw, ALIGNSIZE);
    int pos_datasec  = pos_imptsec + size_imptsec;

    int size_imptsec_file = align(size_imptsec_raw, FILEALIGNSIZE);;
    int pos_datasec_file  = pos_imptsec_file + size_imptsec_file;
    if(size_datasec_file == 0){
        pos_datasec_file = 0;
    }
    IAT.insert(make_pair(std::string(".data"), (DWORD)(EXE_IMAGE_BASE + pos_datasec)));

    // リロケーション
    for(int i = 0; i < sizeof(RelocationTable)/sizeof(RelocationTable[0]); i++){
        auto it = IAT.find(RelocationTable[i].procname);
        if(it != IAT.end()){
            *(DWORD *)(&NativeBuffer[RelocationTable[i].offset]) = it->second;
        }
    }

    IMAGE_DOS_HEADER ImageDosHeader;
    memset(&ImageDosHeader, 0, sizeof(ImageDosHeader));
    ImageDosHeader.e_magic      = 0x5A4D;
    ImageDosHeader.e_cblp       = 0x0040;
    ImageDosHeader.e_cp         = 0x0001;
    ImageDosHeader.e_crlc       = 0x0000;
    ImageDosHeader.e_cparhdr    = 0x0002;
    ImageDosHeader.e_minalloc   = 0x0000;
    ImageDosHeader.e_maxalloc   = 0xFFFF;
    ImageDosHeader.e_ss         = 0x0000;
    ImageDosHeader.e_sp         = 0x0000;
    ImageDosHeader.e_csum       = 0x0000;
    ImageDosHeader.e_ip         = 0x0000;
    ImageDosHeader.e_cs         = 0x0000;
    ImageDosHeader.e_lfarlc     = 0x0000;
    ImageDosHeader.e_ovno       = 0x0000;
    ImageDosHeader.e_lfanew     = sizeof(ImageDosHeader);
    memcpy(((char*)&ImageDosHeader) + 0x0020 + ImageDosHeader.e_ip, DosCodeBuffer, sizeof(DosCodeBuffer));

    IMAGE_NT_HEADERS ImagePeHeader;
    memset(&ImagePeHeader, 0, sizeof(ImagePeHeader));
    ImagePeHeader.Signature                         = IMAGE_NT_SIGNATURE;

    ImagePeHeader.FileHeader.Machine                = IMAGE_FILE_MACHINE_I386;
    ImagePeHeader.FileHeader.NumberOfSections       = 3;
    ImagePeHeader.FileHeader.TimeDateStamp          = (DWORD)time(NULL);
    ImagePeHeader.FileHeader.PointerToSymbolTable   = 0;
    ImagePeHeader.FileHeader.NumberOfSymbols        = 0;
    ImagePeHeader.FileHeader.SizeOfOptionalHeader   = sizeof(ImagePeHeader.OptionalHeader);
    ImagePeHeader.FileHeader.Characteristics        =
        IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_32BIT_MACHINE | IMAGE_FILE_RELOCS_STRIPPED;

    ImagePeHeader.OptionalHeader.Magic                          = 0x010B;
    ImagePeHeader.OptionalHeader.MajorLinkerVersion             = 10;
    ImagePeHeader.OptionalHeader.MinorLinkerVersion             = 0;
    ImagePeHeader.OptionalHeader.SizeOfCode                     = align(sizeof(NativeBuffer) + BFCode.size(), ALIGNSIZE);
    ImagePeHeader.OptionalHeader.SizeOfInitializedData          = size_imptsec + size_datasec;
    ImagePeHeader.OptionalHeader.SizeOfUninitializedData        = 0;
    ImagePeHeader.OptionalHeader.AddressOfEntryPoint            = pos_codesec;
    ImagePeHeader.OptionalHeader.BaseOfCode                     = pos_codesec;
    ImagePeHeader.OptionalHeader.BaseOfData                     = pos_imptsec;
    ImagePeHeader.OptionalHeader.ImageBase                      = EXE_IMAGE_BASE;
    ImagePeHeader.OptionalHeader.SectionAlignment               = ALIGNSIZE;
    ImagePeHeader.OptionalHeader.FileAlignment                  = FILEALIGNSIZE;
    ImagePeHeader.OptionalHeader.MajorOperatingSystemVersion    = 4;
    ImagePeHeader.OptionalHeader.MinorOperatingSystemVersion    = 0;
    ImagePeHeader.OptionalHeader.MajorImageVersion              = 0;
    ImagePeHeader.OptionalHeader.MinorImageVersion              = 0;
    ImagePeHeader.OptionalHeader.MajorSubsystemVersion          = 4;
    ImagePeHeader.OptionalHeader.MinorSubsystemVersion          = 0;
    ImagePeHeader.OptionalHeader.Win32VersionValue              = 0;
    ImagePeHeader.OptionalHeader.SizeOfImage                    = align(HEADER_SIZE, ALIGNSIZE) + size_codesec + size_imptsec + size_datasec;
    ImagePeHeader.OptionalHeader.SizeOfHeaders                  = HEADER_SIZE;
    ImagePeHeader.OptionalHeader.CheckSum                       = 0;
    ImagePeHeader.OptionalHeader.Subsystem                      = IMAGE_SUBSYSTEM_WINDOWS_CUI;
    ImagePeHeader.OptionalHeader.DllCharacteristics             = IMAGE_DLLCHARACTERISTICS_NX_COMPAT | IMAGE_DLLCHARACTERISTICS_NO_SEH | IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE;
    ImagePeHeader.OptionalHeader.SizeOfStackReserve             = 0x00100000;
    ImagePeHeader.OptionalHeader.SizeOfStackCommit              = 0x00001000;
    ImagePeHeader.OptionalHeader.SizeOfHeapReserve              = 0x00100000;
    ImagePeHeader.OptionalHeader.SizeOfHeapCommit               = 0x00001000;
    ImagePeHeader.OptionalHeader.LoaderFlags                    = 0;
    ImagePeHeader.OptionalHeader.NumberOfRvaAndSizes            = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;

    // データ ディクショナリ //
    //  [1]インポートテーブル
    ImagePeHeader.OptionalHeader.DataDirectory[1].VirtualAddress    = pos_imptsec;
    ImagePeHeader.OptionalHeader.DataDirectory[1].Size              = size_imptsec_raw;
    //  [12]インポートアドレステーブル
    ImagePeHeader.OptionalHeader.DataDirectory[12].VirtualAddress   = pos_LookupTable + size_LookupTable;
    ImagePeHeader.OptionalHeader.DataDirectory[12].Size             = size_LookupTable;

    // コードセクションヘッダ
    IMAGE_SECTION_HEADER CodeSectionHeader;
    memset(&CodeSectionHeader, 0, sizeof(CodeSectionHeader));
    strcpy((char *)CodeSectionHeader.Name, ".text");
    CodeSectionHeader.Misc.VirtualSize      = size_codesec_raw;                     // メモリ上のサイズ
    CodeSectionHeader.VirtualAddress        = pos_codesec;                          // メモリ上の開始アドレス
    CodeSectionHeader.SizeOfRawData         = align(sizeof(NativeBuffer) + BFCode.size(), FILEALIGNSIZE);
                                                                                    // ファイル上のサイズ
    CodeSectionHeader.PointerToRawData      = pos_codesec_file;                     // ファイル上の開始アドレス
    CodeSectionHeader.PointerToRelocations  = 0;
    CodeSectionHeader.PointerToLinenumbers  = 0;
    CodeSectionHeader.NumberOfRelocations   = 0;
    CodeSectionHeader.NumberOfLinenumbers   = 0;
    CodeSectionHeader.Characteristics       = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE;

    // インポートセクションヘッダ
    IMAGE_SECTION_HEADER ImptSectionHeader;
    memset(&ImptSectionHeader, 0, sizeof(ImptSectionHeader));
    strcpy((char *)ImptSectionHeader.Name, ".idata");
    ImptSectionHeader.Misc.VirtualSize      = size_imptsec_raw;
    ImptSectionHeader.VirtualAddress        = pos_imptsec;
    ImptSectionHeader.SizeOfRawData         = size_imptsec_file;
    ImptSectionHeader.PointerToRawData      = pos_imptsec_file;
    ImptSectionHeader.PointerToRelocations  = 0;
    ImptSectionHeader.PointerToLinenumbers  = 0;
    ImptSectionHeader.NumberOfRelocations   = 0;
    ImptSectionHeader.NumberOfLinenumbers   = 0;
    ImptSectionHeader.Characteristics       = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ;

    // データセクションヘッダ
    IMAGE_SECTION_HEADER DataSectionHeader;
    memset(&DataSectionHeader, 0, sizeof(DataSectionHeader));
    strcpy((char *)DataSectionHeader.Name, ".data");
    DataSectionHeader.Misc.VirtualSize      = DATA_SEC_SIZE;
    DataSectionHeader.VirtualAddress        = pos_datasec;
    DataSectionHeader.SizeOfRawData         = size_datasec_file;
    DataSectionHeader.PointerToRawData      = pos_datasec_file;
    DataSectionHeader.PointerToRelocations  = 0;
    DataSectionHeader.PointerToLinenumbers  = 0;
    DataSectionHeader.NumberOfRelocations   = 0;
    DataSectionHeader.NumberOfLinenumbers   = 0;
    DataSectionHeader.Characteristics       = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;

    int ofs = 0;
    // DOS Header
    ofs += fwrite(&ImageDosHeader,      1, sizeof(ImageDosHeader), ofp);
//  ofs += fwrite(&DosCodeBuffer,       1, sizeof(DosCodeBuffer), ofp);
    ofs += fwrite_byte(0x00,            ImageDosHeader.e_lfanew - ofs, ofp);
    // PE Header
    ofs += fwrite(&ImagePeHeader,       1, sizeof(ImagePeHeader), ofp);
    // Section Header
    ofs += fwrite(&CodeSectionHeader,   1, sizeof(CodeSectionHeader), ofp);
    ofs += fwrite(&ImptSectionHeader,   1, sizeof(ImptSectionHeader), ofp);
    ofs += fwrite(&DataSectionHeader,   1, sizeof(DataSectionHeader), ofp);
    ofs += fwrite_byte(0x00,            HEADER_SIZE - ofs, ofp);

    // Code Section
    ofs += fwrite(NativeBuffer,         1, sizeof(NativeBuffer), ofp);
    if(BFCode.size() > 0)
    ofs += fwrite(&(BFCode[0]),         1, BFCode.size(), ofp);
    ofs += fwrite_byte(0x00,            align(ofs, FILEALIGNSIZE) - ofs, ofp);

    // Import Section
    ofs += fwrite(ImportDesc,           1, size_ImportDesc, ofp);
    ofs += fwrite(LookupTable,          1, size_LookupTable, ofp);  // import name table
    ofs += fwrite(LookupTable,          1, size_LookupTable, ofp);  // import address table
    ofs += fwrite(&(HintTable[0]),      1, HintTable.size(), ofp);
    ofs += fwrite_byte(0x00,            align(ofs, FILEALIGNSIZE) - ofs, ofp);

    // Data Section
    if(size_datasec_file){
    ofs += fwrite(InitBuffer,           1, sizeof(InitBuffer), ofp);
    ofs += fwrite_byte(0x00,            align(ofs, FILEALIGNSIZE) - ofs, ofp);
    }

    return 0;
}