int unpack_rom(const char* filepath, const char* dstfile) { struct _rkfw_header rom_header; FILE *fp = fopen(filepath, "rb"); if (!fp) { fprintf(stderr, "Can't open file %s\n, reason: %s\n", filepath, strerror(errno)); goto unpack_fail; } fseek(fp, 0, SEEK_SET); if (1 != fread(&rom_header, sizeof(rom_header), 1, fp)) goto unpack_fail; if (strncmp(RK_ROM_HEADER_CODE, rom_header.head_code, sizeof(rom_header.head_code)) != 0) { fprintf(stderr, "Invalid rom file: %s\n", filepath); goto unpack_fail; } printf("rom version: %x.%x.%x\n", (rom_header.version >> 24) & 0xFF, (rom_header.version >> 16) & 0xFF, (rom_header.version) & 0xFFFF); printf("build time: %d-%02d-%02d %02d:%02d:%02d\n", rom_header.year, rom_header.month, rom_header.day, rom_header.hour, rom_header.minute, rom_header.second); printf("chip: %x\n", rom_header.chip); printf("checking md5sum...."); fflush(stdout); if (check_md5sum(fp, rom_header.image_offset + rom_header.image_length) != 0) { printf("Not match!\n"); goto unpack_fail; } printf("OK\n"); //export_data(loader_filename, rom_header.loader_offset, rom_header.loader_length, fp); export_data(dstfile, rom_header.image_offset, rom_header.image_length, fp); fclose(fp); return 0; unpack_fail: if (fp) fclose(fp); return -1; }
static bool export_list(int _iH5File, int *_piVar, char* _pstName, int _iVarType) { int iRet = 0; bool bReturn = false; int iItemNumber = 0; SciErr sciErr = getListItemNumber(pvApiCtx, _piVar, &iItemNumber); if (sciErr.iErr) { printError(&sciErr, 0); return false; } //create groupe name char* pstGroupName = createGroupName(_pstName); char pstMsg[256]; sprintf(pstMsg, "list (%d)", iItemNumber); print_type(pstMsg); iLevel++; //open list void *pvList = openList(_iH5File, pstGroupName, iItemNumber); for (int i = 0 ; i < iItemNumber ; i++) { int *piNewVar = NULL; getListItemAddress(pvApiCtx, _piVar, i + 1, &piNewVar);//1 indexed char* pstPathName = createPathName(pstGroupName, i); if (piNewVar == NULL) { //undefined item bReturn = export_undefined(_iH5File, piNewVar, pstPathName); } else { bReturn = export_data(_iH5File, piNewVar, pstPathName); } iRet = addItemInList(_iH5File, pvList, i, pstPathName); FREE(pstPathName); if (bReturn == false || iRet) { return false; } } iLevel--; closeList(_iH5File, pvList, _pstName, iItemNumber, _iVarType); FREE(pstGroupName); //close list return true; }
int example_main(int argc, char **argv) { db_t hdb = create_database( EXAMPLE_DATABASE, &db_schema ); int rc = EXIT_FAILURE; if( hdb ) { rc = populate_data( hdb ); if ( EXIT_SUCCESS == rc ) { rc = export_data( hdb, STORAGE_TABLE, 0, 0 ); } } return rc; }
//---------------------------------------------------------------------------// // Search the domain with the range entity centroids and construct the // graph. This will update the state of the object. void ParallelSearch::search( const EntityIterator& range_iterator, const Teuchos::RCP<EntityLocalMap>& range_local_map, const Teuchos::ParameterList& parameters ) { // Set the parameters with the local map. range_local_map->setParameters( parameters ); // Empty range flag. d_empty_range = ( 0 == range_iterator.size() ); // Reset the state of the object. d_range_owner_ranks.clear(); d_domain_to_range_map.clear(); d_range_to_domain_map.clear(); d_parametric_coords.clear(); // Perform a coarse global search to redistribute the range entities. Teuchos::Array<EntityId> range_entity_ids; Teuchos::Array<int> range_owner_ranks; Teuchos::Array<double> range_centroids; d_coarse_global_search->search( range_iterator, range_local_map, parameters, range_entity_ids, range_owner_ranks, range_centroids ); // If needed, extract the range entities that were missed during the // coarse global search. Teuchos::Array<EntityId> found_range_entity_ids; Teuchos::Array<int> found_range_ranks; Teuchos::Array<EntityId> missed_range_entity_ids; Teuchos::Array<int> missed_range_ranks; if ( d_track_missed_range_entities ) { missed_range_entity_ids = Teuchos::Array<EntityId>( d_coarse_global_search->getMissedRangeEntityIds() ); missed_range_ranks.assign( missed_range_entity_ids.size(), d_comm->getRank() ); } // Only do the local search if there are local domain entities. Teuchos::Array<int> export_range_ranks; Teuchos::Array<EntityId> export_data; if ( !d_empty_domain ) { // For each range centroid, perform a local search. int num_range = range_entity_ids.size(); Teuchos::Array<Entity> domain_neighbors; Teuchos::Array<Entity> domain_parents; Teuchos::Array<double> reference_coordinates; Teuchos::Array<double> local_coords( d_physical_dim ); int num_parents = 0; for ( int n = 0; n < num_range; ++n ) { // Perform a coarse local search to get the nearest domain // entities to the point. d_coarse_local_search->search( range_centroids(d_physical_dim*n,d_physical_dim), parameters, domain_neighbors ); // Perform a fine local search to get the entities the point maps // to. d_fine_local_search->search( domain_neighbors, range_centroids(d_physical_dim*n,d_physical_dim), parameters, domain_parents, reference_coordinates ); // Store the potentially multiple parametric realizations of the // point. std::unordered_map<EntityId,Teuchos::Array<double> > ref_map; num_parents = domain_parents.size(); for ( int p = 0; p < num_parents; ++p ) { // Store the range data in the domain parallel decomposition. local_coords().assign( reference_coordinates(d_physical_dim*p,d_physical_dim) ); d_range_owner_ranks.emplace( range_entity_ids[n], range_owner_ranks[n] ); d_domain_to_range_map.emplace( domain_parents[p].id(), range_entity_ids[n] ); ref_map.emplace( domain_parents[p].id(), local_coords ); // Extract the data to communicate back to the range parallel // decomposition. export_range_ranks.push_back( range_owner_ranks[n] ); export_data.push_back( range_entity_ids[n] ); export_data.push_back( domain_parents[p].id() ); export_data.push_back( Teuchos::as<EntityId>(d_comm->getRank()) ); } // If we found parents for the point, store them. if ( num_parents > 0 ) { d_parametric_coords.emplace( range_entity_ids[n], ref_map ); // If we are tracking missed entities, also track those that // we found so we can determine if an entity was found after // being sent to multiple destinations. if ( d_track_missed_range_entities ) { found_range_entity_ids.push_back( range_entity_ids[n] ); found_range_ranks.push_back( range_owner_ranks[n] ); } } // Otherwise, if we are tracking missed entities report this. else if ( d_track_missed_range_entities ) { missed_range_entity_ids.push_back( range_entity_ids[n] ); missed_range_ranks.push_back( range_owner_ranks[n] ); } } } // Back-communicate the domain entities in which we found each range // entity to complete the mapping. Tpetra::Distributor domain_to_range_dist( d_comm ); int num_import = domain_to_range_dist.createFromSends( export_range_ranks() ); Teuchos::Array<EntityId> domain_data( 3*num_import ); Teuchos::ArrayView<const EntityId> export_data_view = export_data(); domain_to_range_dist.doPostsAndWaits( export_data_view, 3, domain_data() ); // Store the domain data in the range parallel decomposition. for ( int i = 0; i < num_import; ++i ) { d_domain_owner_ranks.emplace( domain_data[3*i+1], domain_data[3*i+2] ); d_range_to_domain_map.emplace( domain_data[3*i], domain_data[3*i+1] ); } // If we are tracking missed entities, back-communicate the missing entities // and found entities to determine which entities are actually missing. if ( d_track_missed_range_entities ) { // Back-communicate the missing entities. Tpetra::Distributor missed_range_dist( d_comm ); int num_import_missed = missed_range_dist.createFromSends( missed_range_ranks() ); Teuchos::Array<EntityId> import_missed( num_import_missed ); Teuchos::ArrayView<const EntityId> missed_view = missed_range_entity_ids(); missed_range_dist.doPostsAndWaits( missed_view, 1, import_missed() ); // Back-communicate the found entities. Tpetra::Distributor found_range_dist( d_comm ); int num_import_found = found_range_dist.createFromSends( found_range_ranks() ); Teuchos::Array<EntityId> import_found( num_import_found ); Teuchos::ArrayView<const EntityId> found_view = found_range_entity_ids(); found_range_dist.doPostsAndWaits( found_view, 1, import_found() ); // Intersect the found and missed entities to determine if there are any // that were found on one process but missed on another. std::sort( import_missed.begin(), import_missed.end() ); std::sort( import_found.begin(), import_found.end() ); Teuchos::Array<EntityId> false_positive_missed( import_missed.size() + import_found.size() ); auto false_positive_end = std::set_intersection( import_missed.begin(), import_missed.end(), import_found.begin(), import_found.end(), false_positive_missed.begin() ); // Create a list of missed entities without the false positives. d_missed_range_entity_ids.resize( num_import_missed ); auto missed_range_end = std::set_difference( import_missed.begin(), import_missed.end(), false_positive_missed.begin(), false_positive_end, d_missed_range_entity_ids.begin() ); // Create a unique list of missed entities without the false positives. std::sort( d_missed_range_entity_ids.begin(), missed_range_end ); auto missed_range_unique_end = std::unique( d_missed_range_entity_ids.begin(), missed_range_end ); d_missed_range_entity_ids.resize( std::distance(d_missed_range_entity_ids.begin(), missed_range_unique_end) ); } }
//Function main----------------------------------------------------------------- int main(int argc, char *argv[]) { //This message is used as a 'template' for messages sent to the tree. //This can't be constructed as an argument because the argument must be //a non-const reference. comm_message Message; if (argc < 2 || argc > 3) //Argument check-------------------------------------------------------- { std::cout << argv[0] << " [input file] (output file)\n"; return 0; } //---------------------------------------------------------------------- //Objects needed for parsing-------------------------------------------- BASE_TYPE tree_base; /* specific class for each example */ FileInterface FileTransfer; DataDisplay Display; //---------------------------------------------------------------------- if (argc > 1) //Initial file parsing-------------------------------------------------- { std::cout << "### DATA PARSED FROM " << argv[1] << " ###\n"; //set the file to transfer from if (!FileTransfer.set_file(argv[1])) std::cout << "SET FILE ERROR!\n"; //load the file into the tree base if (!load_file(&tree_base, &FileTransfer)) std::cout << "INPUT ERROR!\n"; std::cin.get(); //update the tree data in case of parsing errors tree_base.global_update(); //set the output mode of the tree to 'display' tree_base.send_message(Message = "DISPLAY"); std::cout << "### RECONSTRUCTED FROM STORED DATA ###\n"; //display the tree data to the display if (!export_data(&tree_base, &Display)) std::cout << "DISPLAY ERROR!\n"; std::cin.get(); } //---------------------------------------------------------------------- if (argc > 2) //File reconstruction--------------------------------------------------- { //set the output mode of the tree to 'output' tree_base.send_message(Message = "OUTPUT"); std::cout << "### RECONSTRUCTED DATA SENT TO " << argv[2] << " ###\n"; //set the file to transfer to if (!FileTransfer.set_file(argv[2])) std::cout << "SET FILE ERROR!\n"; //save the data from the tree base into the second file if (!save_file(&tree_base, &FileTransfer)) std::cout << "OUTPUT ERROR!\n"; std::cin.get(); } //---------------------------------------------------------------------- return 0; } //END-------------------------------------------------------------------------
/*--------------------------------------------------------------------------*/ int sci_export_to_hdf5(char *fname, unsigned long fname_len) { int iNbVar = 0; int** piAddrList = NULL; char** pstNameList = NULL; char *pstFileName = NULL; bool bExport = false; bool bAppendMode = false; SciErr sciErr; int iRhs = nbInputArgument(pvApiCtx); CheckInputArgumentAtLeast(pvApiCtx, 1); CheckOutputArgument(pvApiCtx, 0, 1); pstNameList = (char**)MALLOC(sizeof(char*) * iRhs); iNbVar = extractVarNameList(1, iRhs, pstNameList); if (iNbVar == 0) { FREE(pstNameList); return 1; } piAddrList = (int**)MALLOC(sizeof(int*) * (iNbVar)); for (int i = 1 ; i < iRhs ; i++) { if (strcmp(pstNameList[i], "-append") == 0) { bAppendMode = true; } else { sciErr = getVarAddressFromName(pvApiCtx, pstNameList[i], &piAddrList[i]); if (sciErr.iErr) { Scierror(999, _("%s: Wrong value for input argument #%d: Defined variable expected.\n"), fname, i + 1); printError(&sciErr, 0); return 1; } } } iLevel = 0; // open hdf5 file pstFileName = expandPathVariable(pstNameList[0]); int iH5File = 0; if (bAppendMode) { iH5File = openHDF5File(pstFileName, bAppendMode); if (iH5File < 0) { iH5File = createHDF5File(pstFileName); } } else { iH5File = createHDF5File(pstFileName); } if (iH5File < 0) { FREE(pstFileName); if (iH5File == -2) { Scierror(999, _("%s: Wrong value for input argument #%d: \"%s\" is a directory"), fname, 1, pstNameList[0]); } else { Scierror(999, _("%s: Cannot open file %s.\n"), fname, pstNameList[0]); } return 1; } if (bAppendMode) { int iVersion = getSODFormatAttribute(iH5File); if (iVersion != -1 && iVersion != SOD_FILE_VERSION) { //to update version must be the same closeHDF5File(iH5File); Scierror(999, _("%s: Wrong SOD file format version. Expected: %d Found: %d\n"), fname, SOD_FILE_VERSION, iVersion); return 1; } } // export data for (int i = 1 ; i < iRhs ; i++) { if (strcmp(pstNameList[i], "-append") == 0) { continue; } if (isVarExist(iH5File, pstNameList[i])) { if (bAppendMode) { if (deleteHDF5Var(iH5File, pstNameList[i])) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to delete existing variable \"%s\"."), fname, pstNameList[i]); return 1; } } else { closeHDF5File(iH5File); Scierror(999, _("%s: Variable \'%s\' already exists in file \'%s\'\nUse -append option to replace existing variable\n."), fname, pstNameList[i], pstNameList[0]); return 1; } } bExport = export_data(iH5File, piAddrList[i], pstNameList[i]); if (bExport == false) { break; } } if (bExport && iRhs != 1) { //add or update scilab version and file version in hdf5 file if (updateScilabVersion(iH5File) < 0) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to update Scilab version in \"%s\"."), fname, pstNameList[0]); return 1; } if (updateFileVersion(iH5File) < 0) { closeHDF5File(iH5File); Scierror(999, _("%s: Unable to update HDF5 format version in \"%s\"."), fname, pstNameList[0]); return 1; } } //close hdf5 file closeHDF5File(iH5File); //delete file in case of error but nor in append mode if (bExport == false && bAppendMode == false && iRhs != 1) { //remove file deleteafile(pstFileName); } FREE(pstFileName); //create boolean return value int *piReturn = NULL; sciErr = allocMatrixOfBoolean(pvApiCtx, iRhs + 1, 1, 1, &piReturn); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (bExport == true || iRhs == 1) { piReturn[0] = 1; } else { piReturn[0] = 0; } //free memory for (int i = 0 ; i < iRhs ; i++) { FREE(pstNameList[i]); } FREE(pstNameList); FREE(piAddrList); LhsVar(1) = iRhs + 1; PutLhsVar(); return 0; }
static int export_registry_data(HANDLE hFile, HKEY key, WCHAR *path) { LONG rc; DWORD max_value_len = 256, value_len; DWORD max_data_bytes = 2048, data_size; DWORD subkey_len; DWORD i, type, path_len; WCHAR *value_name, *subkey_name, *subkey_path; BYTE *data; HKEY subkey; export_key_name(hFile, path); value_name = heap_xalloc(max_value_len * sizeof(WCHAR)); data = heap_xalloc(max_data_bytes); i = 0; for (;;) { value_len = max_value_len; data_size = max_data_bytes; rc = RegEnumValueW(key, i, value_name, &value_len, NULL, &type, data, &data_size); if (rc == ERROR_SUCCESS) { export_data(hFile, value_name, value_len, type, data, data_size); i++; } else if (rc == ERROR_MORE_DATA) { if (data_size > max_data_bytes) { max_data_bytes = data_size; data = heap_xrealloc(data, max_data_bytes); } else { max_value_len *= 2; value_name = heap_xrealloc(value_name, max_value_len * sizeof(WCHAR)); } } else break; } heap_free(data); heap_free(value_name); subkey_name = heap_xalloc(MAX_SUBKEY_LEN * sizeof(WCHAR)); path_len = lstrlenW(path); i = 0; for (;;) { subkey_len = MAX_SUBKEY_LEN; rc = RegEnumKeyExW(key, i, subkey_name, &subkey_len, NULL, NULL, NULL, NULL); if (rc == ERROR_SUCCESS) { subkey_path = build_subkey_path(path, path_len, subkey_name, subkey_len); if (!RegOpenKeyExW(key, subkey_name, 0, KEY_READ, &subkey)) { export_registry_data(hFile, subkey, subkey_path); RegCloseKey(subkey); } heap_free(subkey_path); i++; } else break; } heap_free(subkey_name); return 0; }