/* function: initfile_filereader * Opens file for reading. * * The caller must free all out variables even in case of error ! */ static int initfile_filereader(/*out*/file_t * fd, /*out*/off_t * filesize, const char * filepath, const struct directory_t * relative_to/*0 => current working dir*/) { int err; err = init_file(fd, filepath, accessmode_READ, relative_to); if (err) return err; err = size_file(*fd, filesize); if (err) return err; err = advisereadahead_file(*fd, 0, *filesize); if (err) return err; return 0; }
int main(int argc, char *argv[]) { if (argc < 2 || argc > 3) { std::cerr << "usage: " << argv[0] << " infile outfile\n"; exit(1); } std::string infile_prefix(argv[1]); std::string outfile_name; if (argc == 3) { outfile_name = argv[2]; } else { outfile_name = argv[1]; } std::vector<bool> code_content; read(infile_prefix+".huff.code", code_content); Hx::huffman_tree<char, size_t> huff; read(infile_prefix+".huff.dict", huff); std::cout << "huffman dictionary:\n"; huff.print(std::cerr); std::vector<char> file_content; huff.decode(code_content, file_content); std::ifstream size_file(infile_prefix+".huff.size"); size_t size; if (!size_file.good()) { std::cerr << "open " << infile_prefix+".huff.size" << " fault!\n"; size = file_content.size(); } size_file >> size; size_file.close(); file_content.resize(size); write(outfile_name, file_content); std::cout << '\n'; std::cout << "huffman decode ok!\n"; return 0; }
/** * @brief Open up an empty section cache entry and add the filename * * @param section_type The type of the section * @param filename The name of the file * * @returns 0 if successful, -1 otherwise */ int section_cache_entry_open(const uint32_t section_type, const char * filename) { int linux_style_status = -1; /* assume failure */ section_cache_entry * section = NULL; if (current_section < MAX_TFTF_SECTION_CACHE) { /** * If the args are such that we go directly from one section to the * next, we need to close the previous section before opening this * one */ if (section_window_open) { section_cache_entry_close(); } /* * Open the window and save the filename and type. * Note: we don't allocate and load the file into * section_cache[current_section].blob at this point. We will load * the file later. */ section = §ion_cache[current_section]; section_window_open = true; section->blob = NULL; section->section.section_type = section_type; if (filename != NULL) { section->filename = filename; section->section.section_length = section->section.section_expanded_length = size_file(filename); } /* By default, each section's load address is contiguous with * the previous section. Set the default load address here - we * will override it in section_cache_entry_set_load_address, and * section_cache_entry_close will advance the running * section_load_address to the section's load_address + load_length. */ section->section.section_load_address = section_load_address; linux_style_status = 0; } return linux_style_status; }