ERR ParsePFD( PKImageDecode* pID, size_t offPos, U16 cEntry) { ERR err = WMP_errSuccess; struct WMPStream* pWS = pID->pStream; U16 i = 0; for (i = 0; i < cEntry; ++i) { U16 uTag = 0; U16 uType = 0; U32 uCount = 0; U32 uValue = 0; Call(GetUShort(pWS, offPos, &uTag)); offPos += 2; Call(GetUShort(pWS, offPos, &uType)); offPos += 2; Call(GetULong(pWS, offPos, &uCount)); offPos += 4; Call(GetULong(pWS, offPos, &uValue)); offPos += 4; Call(ParsePFDEntry(pID, uTag, uType, uCount, uValue)); } pID->WMP.bHasAlpha = ((pID->WMP.bHasAlpha) && (pID->WMP.wmiDEMisc.uAlphaOffset != 0) && (pID->WMP.wmiDEMisc.uAlphaByteCount != 0));//has planar alpha Cleanup: return err; }
dis_handler_return JVMBrShort( dis_handle *h, void *d, dis_dec_ins *ins ) { ins->size += 3; ins->num_ops = 1; ins->op[0].type = DO_RELATIVE; ins->op[0].value = 0 | GetUShort( d, 1 ); return( DHR_DONE ); }
dis_handler_return JVMUShort( dis_handle *h, void *d, dis_dec_ins *ins ) { ins->num_ops = 1; ins->op[0].type = DO_IMMED; ins->op[0].value = 0 | GetUShort( d, ins->size + 1 ); ins->size += 3; return( DHR_DONE ); }
ERR ReadContainer( PKImageDecode* pID) { ERR err = WMP_errSuccess; struct WMPStream* pWS = pID->pStream; size_t offPos = 0; char szSig[2] = {0}; U16 uWmpID = 0; U32 offPFD = 0; U16 cPFDEntry = 0; U8 bVersion; //================================ Call(pWS->GetPos(pWS, &offPos)); FailIf(0 != offPos, WMP_errUnsupportedFormat); //================================ // Header Call(pWS->Read(pWS, szSig, sizeof(szSig))); offPos += 2; FailIf(szSig != strstr(szSig, "II"), WMP_errUnsupportedFormat); Call(GetUShort(pWS, offPos, &uWmpID)); offPos += 2; FailIf(WMP_valWMPhotoID != (0x00FF & uWmpID), WMP_errUnsupportedFormat); // We accept version 00 and version 01 bitstreams - all others rejected bVersion = (0xFF00 & uWmpID) >> 8; FailIf(bVersion != 0 && bVersion != 1, WMP_errUnsupportedFormat); Call(GetULong(pWS, offPos, &offPFD)); offPos += 4; //================================ // PFD offPos = (size_t)offPFD; Call(GetUShort(pWS, offPos, &cPFDEntry)); offPos += 2; FailIf(0 == cPFDEntry || USHRT_MAX == cPFDEntry, WMP_errUnsupportedFormat); Call(ParsePFD(pID, offPos, cPFDEntry)); //================================ Call(pWS->SetPos(pWS, pID->WMP.wmiDEMisc.uImageOffset)); Cleanup: return err; }
dis_handler_return JVMInterface( dis_handle *h, void *d, dis_dec_ins *ins ) { ins->size += 5; ins->num_ops = 2; ins->op[0].type = DO_MEMORY_ABS; ins->op[0].value = 0 | GetUShort( d, 1 ); ins->op[1].type = DO_IMMED; ins->op[1].value = 0 | GetUByte( d, 3 ); return( DHR_DONE ); }
dis_handler_return JVMIInc( dis_handle *h, void *d, dis_dec_ins *ins ) { ins->num_ops = 2; ins->op[0].type = DO_MEMORY_ABS; if( ins->flags & DIF_JVM_WIDE ) { ins->op[0].value = 0 | GetUShort( d, ins->size + 1 ); ins->size += 1; } else { ins->op[0].value = 0 | GetUByte( d, ins->size + 1 ); } ins->op[1].type = DO_IMMED; ins->op[1].value = GetSByte( d, ins->size + 2 ); ins->size += 3; return( DHR_DONE ); }
int do_init(int argc, char** argv) { FILE *list; char line[1024]; struct map_data map; char name[MAP_NAME_LENGTH_EXT]; grf_list_file = aStrdup("conf/grf-files.txt"); map_list_file = aStrdup("db/map_index.txt"); /* setup pre-defined, #define-dependant */ map_cache_file = aStrdup("db/map_cache.dat"); cmdline->exec(argc, argv, CMDLINE_OPT_PREINIT); cmdline->exec(argc, argv, CMDLINE_OPT_NORMAL); ShowStatus("Initializing grfio with %s\n", grf_list_file); grfio_init(grf_list_file); // Attempt to open the map cache file and force rebuild if not found ShowStatus("Opening map cache: %s\n", map_cache_file); if(!rebuild) { map_cache_fp = fopen(map_cache_file, "rb"); if(map_cache_fp == NULL) { ShowNotice("Existing map cache not found, forcing rebuild mode\n"); rebuild = 1; } else fclose(map_cache_fp); } if(rebuild) map_cache_fp = fopen(map_cache_file, "w+b"); else map_cache_fp = fopen(map_cache_file, "r+b"); if(map_cache_fp == NULL) { ShowError("Failure when opening map cache file %s\n", map_cache_file); exit(EXIT_FAILURE); } // Open the map list ShowStatus("Opening map list: %s\n", map_list_file); list = fopen(map_list_file, "r"); if(list == NULL) { ShowError("Failure when opening maps list file %s\n", map_list_file); exit(EXIT_FAILURE); } // Initialize the main header if(rebuild) { header.file_size = sizeof(struct main_header); header.map_count = 0; } else { if(fread(&header, sizeof(struct main_header), 1, map_cache_fp) != 1){ printf("An error as occured while reading map_cache_fp \n"); } header.file_size = GetULong((unsigned char *)&(header.file_size)); header.map_count = GetUShort((unsigned char *)&(header.map_count)); } // Read and process the map list while(fgets(line, sizeof(line), list)) { if(line[0] == '/' && line[1] == '/') continue; if(sscanf(line, "%15s", name) < 1) continue; if(strcmp("map:", name) == 0 && sscanf(line, "%*s %15s", name) < 1) continue; name[MAP_NAME_LENGTH_EXT-1] = '\0'; remove_extension(name); if (find_map(name)) { ShowInfo("Map '"CL_WHITE"%s"CL_RESET"' already in cache.\n", name); } else if(!read_map(name, &map)) { ShowError("Map '"CL_WHITE"%s"CL_RESET"' not found!\n", name); } else if (!cache_map(name, &map)) { ShowError("Map '"CL_WHITE"%s"CL_RESET"' failed to cache (write error).\n", name); } else { ShowInfo("Map '"CL_WHITE"%s"CL_RESET"' successfully cached.\n", name); } } ShowStatus("Closing map list: %s\n", map_list_file); fclose(list); // Write the main header and close the map cache ShowStatus("Closing map cache: %s\n", map_cache_file); fseek(map_cache_fp, 0, SEEK_SET); fwrite(&header, sizeof(struct main_header), 1, map_cache_fp); fclose(map_cache_fp); ShowStatus("Finalizing grfio\n"); grfio_final(); ShowInfo("%d maps now in cache\n", header.map_count); aFree(grf_list_file); aFree(map_list_file); aFree(map_cache_file); return 0; }
static int GetSShort( void *d, unsigned off ) { return( SEX( GetUShort( d, off ), 15 ) ); }
//================================================================ // PKImageDecode_WMP //================================================================ ERR ParsePFDEntry( PKImageDecode* pID, U16 uTag, U16 uType, U32 uCount, U32 uValue) { ERR err = WMP_errSuccess; PKPixelInfo PI; struct WMPStream* pWS = pID->pStream; size_t offPos = 0; union uf{ U32 uVal; Float fVal; }ufValue = {0}; //================================ switch (uTag) { case WMP_tagPixelFormat: { unsigned char *pGuid = (unsigned char *) &pID->guidPixFormat; /** following code is endian-agnostic **/ Call(GetULong(pWS, uValue, (unsigned long *)pGuid)); Call(GetUShort(pWS, uValue + 4, (unsigned short *)(pGuid + 4))); Call(GetUShort(pWS, uValue + 6, (unsigned short *)(pGuid + 6))); Call(pWS->Read(pWS, pGuid + 8, 8)); PI.pGUIDPixFmt = &pID->guidPixFormat; PixelFormatLookup(&PI, LOOKUP_FORWARD); pID->WMP.bHasAlpha = !!(PI.grBit & PK_pixfmtHasAlpha); pID->WMP.wmiI.cBitsPerUnit = PI.cbitUnit; pID->WMP.wmiI.bRGB = !(PI.grBit & PK_pixfmtBGR); break; } case WMP_tagImageWidth: FailIf(0 == uValue, WMP_errUnsupportedFormat); break; case WMP_tagImageHeight: FailIf(0 == uValue, WMP_errUnsupportedFormat); break; case WMP_tagImageOffset: FailIf(1 != uCount, WMP_errUnsupportedFormat); pID->WMP.wmiDEMisc.uImageOffset = uValue; break; case WMP_tagImageByteCount: FailIf(1 != uCount, WMP_errUnsupportedFormat); pID->WMP.wmiDEMisc.uImageByteCount = uValue; break; case WMP_tagAlphaOffset: FailIf(1 != uCount, WMP_errUnsupportedFormat); pID->WMP.wmiDEMisc.uAlphaOffset = uValue; break; case WMP_tagAlphaByteCount: FailIf(1 != uCount, WMP_errUnsupportedFormat); pID->WMP.wmiDEMisc.uAlphaByteCount = uValue; break; case WMP_tagWidthResolution: FailIf(1 != uCount, WMP_errUnsupportedFormat); ufValue.uVal = uValue; pID->fResX = ufValue.fVal; break; case WMP_tagHeightResolution: FailIf(1 != uCount, WMP_errUnsupportedFormat); ufValue.uVal = uValue; pID->fResY = ufValue.fVal; break; case WMP_tagXMPMetadata: case WMP_tagIccProfile: case WMP_tagEXIFMetadata: case WMP_tagTransformation: case WMP_tagCompression: case WMP_tagImageType: case WMP_tagImageDataDiscard: case WMP_tagAlphaDataDiscard: break; default: fprintf(stderr, "Unrecognized WMPTag: %d(%#x), %d, %d, %#x" CRLF, (int)uTag, (int)uTag, (int)uType, (int)uCount, (int)uValue); break; } Cleanup: return err; }
int do_init(int argc, char** argv) { /* setup pre-defined, #define-dependant */ map_cache_file = std::string(db_path) + "/" + std::string(DBPATH) + "map_cache.dat"; // Process the command-line arguments process_args(argc, argv); ShowStatus("Initializing grfio with %s\n", grf_list_file.c_str()); grfio_init(grf_list_file.c_str()); // Attempt to open the map cache file and force rebuild if not found ShowStatus("Opening map cache: %s\n", map_cache_file.c_str()); if(!rebuild) { map_cache_fp = fopen(map_cache_file.c_str(), "rb"); if(map_cache_fp == NULL) { ShowNotice("Existing map cache not found, forcing rebuild mode\n"); rebuild = 1; } else fclose(map_cache_fp); } if(rebuild) map_cache_fp = fopen(map_cache_file.c_str(), "w+b"); else map_cache_fp = fopen(map_cache_file.c_str(), "r+b"); if(map_cache_fp == NULL) { ShowError("Failure when opening map cache file %s\n", map_cache_file.c_str()); exit(EXIT_FAILURE); } // Open the map list FILE *list; std::vector<std::string> directories = { std::string(db_path) + "/", std::string(db_path) + "/" + std::string(DBIMPORT) + "/" }; for (const auto &directory : directories) { std::string filename = directory + map_list_file; ShowStatus("Opening map list: %s\n", filename.c_str()); list = fopen(filename.c_str(), "r"); if (list == NULL) { ShowError("Failure when opening maps list file %s\n", filename.c_str()); exit(EXIT_FAILURE); } // Initialize the main header if (rebuild) { header.file_size = sizeof(struct main_header); header.map_count = 0; } else { if (fread(&header, sizeof(struct main_header), 1, map_cache_fp) != 1) { printf("An error as occured while reading map_cache_fp \n"); } header.file_size = GetULong((unsigned char *)&(header.file_size)); header.map_count = GetUShort((unsigned char *)&(header.map_count)); } // Read and process the map list char line[1024]; while (fgets(line, sizeof(line), list)) { if (line[0] == '/' && line[1] == '/') continue; char name[MAP_NAME_LENGTH_EXT]; if (sscanf(line, "%15s", name) < 1) continue; if (strcmp("map:", name) == 0 && sscanf(line, "%*s %15s", name) < 1) continue; struct map_data map; name[MAP_NAME_LENGTH_EXT - 1] = '\0'; remove_extension(name); if (find_map(name)) ShowInfo("Map '" CL_WHITE "%s" CL_RESET "' already in cache.\n", name); else if (read_map(name, &map)) { cache_map(name, &map); ShowInfo("Map '" CL_WHITE "%s" CL_RESET "' successfully cached.\n", name); } else ShowError("Map '" CL_WHITE "%s" CL_RESET "' not found!\n", name); } ShowStatus("Closing map list: %s\n", filename.c_str()); fclose(list); } // Write the main header and close the map cache ShowStatus("Closing map cache: %s\n", map_cache_file.c_str()); fseek(map_cache_fp, 0, SEEK_SET); fwrite(&header, sizeof(struct main_header), 1, map_cache_fp); fclose(map_cache_fp); ShowStatus("Finalizing grfio\n"); grfio_final(); ShowInfo("%d maps now in cache\n", header.map_count); return 0; }