/* image.hh */ #ifndef _IMAGE_HH #define _IMAGE_HH #include #include #include #include "endian.hh" #include "types.hh" class image { public: struct annotation { std::string pre_text; std::string post_text; }; class mapping { public: mapping() : _addr(0), _size(0), _file(0), _read(false), _write(false), _big_endian(false), _data(0), _data_offset(0), _mmap_size(0) {} mapping(da_addr_t addr, uint_t size, FILE* file, uint_t offset, bool read, bool write, bool big_endian); ~mapping(); da_addr_t addr() const { return this->_addr; } uint_t size() const { return this->_size; } const uint8_t* data() const { uint8_t* bytesrc = static_cast(this->_data); return &bytesrc[this->_data_offset - this->_addr]; } bool has_file() const { return this->_file != NULL; } bool big_endian() const { return this->_big_endian; } private: da_addr_t _addr; uint_t _size; FILE* _file; bool _read; bool _write; bool _big_endian; void* _data; uint_t _data_offset; uint_t _mmap_size; }; image() : _mappings(), _annot_map() {} ~image(); void create_mapping(da_addr_t addr, uint_t size, const char *filename, uint_t offset, bool read, bool write, bool big_endian); void remove_mapping(da_addr_t addr); bool is_addr_mapped(da_addr_t addr) const; uint_t read_swap(da_addr_t addr, uint8_t dest[], uint_t size) const; template uint_t read(da_addr_t addr, typename uint_type::type& dest) const { const image::mapping* src_mapping = this->find_mapping(addr); if (src_mapping == NULL) return 0; if ((addr + sizeof(typename uint_type::type)) > (src_mapping->addr() + src_mapping->size())) return 0; dest = *reinterpret_cast::type*> (&src_mapping->data()[addr]); if (src_mapping->big_endian()) { dest = endian_swap::swap(dest); } else { dest = endian_swap::swap(dest); } return sizeof(typename uint_type::type); } uint_t read(da_addr_t addr, uint8_t dest[], uint_t size) const; void add_annotation(da_addr_t addr, const std::string& text, bool before); void add_annotations_from_file(FILE* f); std::map annotations() { return this->_annot_map; } const std::map annotations() const { return this->_annot_map; } private: image::mapping* find_mapping(da_addr_t addr); const image::mapping* find_mapping(da_addr_t) const; std::map _mappings; std::map _annot_map; }; #endif /* _IMAGE_HH */