static int translate_file(const char *cpath, size_t offset, char *toString) { size_t base = 0; LIST_MEMBER *pentry = NULL; int res = 0; char *path, *dpath; dpath = path = convert_path(cpath); if (!path) return 1; // The path could be absolute: if (get_ImageBase(path, &base)) { pentry = entry_lookup(&cache, path); if (pentry) { path = pentry->path; base = pentry->ImageBase; if (base == INVALID_BASE) { l2l_dbg(1, "No, or invalid base address: %s\n", path); res = 2; } } else { l2l_dbg(1, "Not found in cache: %s\n", path); res = 3; } } if (!res) { res = process_file(path, offset, toString); } free(dpath); return res; }
int create_cache(int force, int skipImageBase) { FILE *fr, *fw; char Line[LINESIZE + 1], *Fname = NULL; int len, err; size_t ImageBase; if ((fw = fopen(tmp_name, "w")) == NULL) { l2l_dbg(1, "Apparently %s is not writable (mounted ISO?), using current dir\n", tmp_name); cache_name = basename(cache_name); tmp_name = basename(tmp_name); } else { l2l_dbg(3, "%s is writable\n", tmp_name); fclose(fw); remove(tmp_name); } if (force) { l2l_dbg(3, "Removing %s ...\n", cache_name); remove(cache_name); } else { if (file_exists(cache_name)) { l2l_dbg(3, "Cache %s already exists\n", cache_name); return 0; } } Line[LINESIZE] = '\0'; remove(tmp_name); l2l_dbg(0, "Scanning %s ...\n", opt_dir); snprintf(Line, LINESIZE, DIR_FMT, opt_dir, tmp_name); l2l_dbg(1, "Executing: %s\n", Line); if (system(Line) != 0) { l2l_dbg(0, "Cannot list directory %s\n", opt_dir); l2l_dbg(1, "Failed to execute: '%s'\n", Line); remove(tmp_name); return 2; } l2l_dbg(0, "Creating cache ..."); if ((fr = fopen(tmp_name, "r")) != NULL) { if ((fw = fopen(cache_name, "w")) != NULL) { while (fgets(Line, LINESIZE, fr) != NULL) { len = strlen(Line); if (!len) continue; Fname = Line + len - 1; if (*Fname == '\n') *Fname = '\0'; while (Fname > Line && *Fname != PATH_CHAR) Fname--; if (*Fname == PATH_CHAR) Fname++; if (*Fname && !skipImageBase) { if ((err = get_ImageBase(Line, &ImageBase)) == 0) fprintf(fw, "%s|%s|%0x\n", Fname, Line, (unsigned int)ImageBase); else l2l_dbg(3, "%s|%s|%0x, ERR=%d\n", Fname, Line, (unsigned int)ImageBase, err); } } fclose(fw); } l2l_dbg(0, "... done\n"); fclose(fr); } remove(tmp_name); return 0; }