KUNAI Static Analyzer
Kunai is a library for doing static binary analysis of Dalvik.
Loading...
Searching...
No Matches
kunaistream.hpp
1//--------------------------------------------------------------------*- C++ -*-
2// Kunai-static-analyzer: library for doing analysis of dalvik files
3// @author Farenain <kunai.static.analysis@gmail.com>
4//
5// @file kunaistream.hpp
6// @brief Manage the possible streams for reading and writing, as well as
7// possibly reading data types that need special handling.
8#ifndef KUNAI_UTILS_KUNAISTREAM_HPP
9#define KUNAI_UTILS_KUNAISTREAM_HPP
10
11#include <fstream>
12#include "Kunai/Exceptions/stream_exception.hpp"
13
14namespace KUNAI
15{
16 namespace stream
17 {
20 {
22 std::ifstream& input_file;
23
25 std::size_t file_size;
26
28 void initialize();
29
30 public:
32 const std::int32_t MAX_ANSII_STR_SIZE = 256;
33
36 KunaiStream(std::ifstream& input_file) : input_file(input_file)
37 {
38 if (!input_file.is_open())
39 throw exceptions::StreamException("KunaiStream: error input_file not open");
40 initialize();
41 }
42
45 ~KunaiStream() = default;
46
49 std::size_t get_size() const
50 {
51 return file_size;
52 }
53
59 template <typename T>
60 void read_data(T& buffer, std::int32_t read_size)
61 {
62 if (read_size < 0)
63 throw exceptions::StreamException("read_data(): read_size given incorrect");
64 // read the data
65 input_file.read(reinterpret_cast<char*>(&buffer), read_size);
66
67 if(!input_file)
68 throw exceptions::StreamException("read_data(): error reading input file");
69 }
70
73 std::streampos tellg() const
74 {
75 return input_file.tellg();
76 }
77
81 void seekg(std::streamoff off, std::ios_base::seekdir dir)
82 {
83 if (off >= file_size)
84 throw exceptions::StreamException("seekg(): offset provided is out of bound");
85 input_file.seekg(off, dir);
86 }
87
91 std::string read_ansii_string(std::int64_t offset);
92
97 std::string read_dex_string(std::int64_t offset);
98
101 std::uint64_t read_uleb128();
102
105 std::int64_t read_sleb128();
106 };
107 }
108} // namespace KUNAI
109
110
111#endif // KUNAI_UTILS_KUNAISTREAM_HPP
Class to manage an input file stream given for the analysis.
Definition kunaistream.hpp:20
void seekg(std::streamoff off, std::ios_base::seekdir dir)
Move the pointer from the input file.
Definition kunaistream.hpp:81
std::size_t get_size() const
Obtain the size of the file.
Definition kunaistream.hpp:49
std::string read_dex_string(std::int64_t offset)
Read a DEX string, the dex string contains the next format: <size in uleb128><string with size>
KunaiStream(std::ifstream &input_file)
Constructor from KunaiStream class.
Definition kunaistream.hpp:36
std::streampos tellg() const
Obtain the current pointer of the file.
Definition kunaistream.hpp:73
std::string read_ansii_string(std::int64_t offset)
Read a string as an array of char finished in a 0 byte.
void read_data(T &buffer, std::int32_t read_size)
Read data given a buffer of a T data type and with a size specified by the user.
Definition kunaistream.hpp:60
std::int64_t read_sleb128()
Read a number in sleb128 format.
std::uint64_t read_uleb128()
Read a number in uleb128 format.
~KunaiStream()=default
Destructor from KunaiStream, nothing done here for the moment.
const std::int32_t MAX_ANSII_STR_SIZE
maximum size for ansii strings
Definition kunaistream.hpp:32
Exception raised in stream error.
Definition stream_exception.hpp:15
utilities
Definition analysis.hpp:23