Exemple #1
0
    bool load(std::istream &in, Memory& mem, addr_t img_base) {
        if (virt_sz > 0) {
            addr_t sect_addr_va = img_base + virt_addr;
            int prot = get_prot(charac);
            mem.alloc_protect(sect_addr_va , virt_sz, prot | Memory::Write);
            //TODO check for file_sz > virt_sz (explained in spec)
            if (file_sz > 0) {
                std::istream::streampos pos_orig = in.tellg(); 
                if (pos_orig == std::istream::pos_type(std::istream::off_type(-1)))
                    return false;
                in.seekg(file_pos, std::ios_base::beg);
                // TODO is "bad()" the right thing to check?
                if (in.bad())
                    return false;
                char *sect_data = new char[file_sz];
                in.read(sect_data, file_sz);
                if (std::size_t(in.gcount()) < file_sz) {
                    delete[] sect_data;
                    return false;
                }
                // perhaps change "write" interface to accept const char * to
                // avoid this copying madness?
                mem.write(sect_addr_va, std::string(sect_data, file_sz));
                delete[] sect_data;
                if (!(prot & Memory::Write))
                    mem.alloc_protect(sect_addr_va, virt_sz, prot);
                in.seekg(pos_orig);
                if (in.bad())
                    return false;
            }
        }

        return true;
    }