summaryrefslogtreecommitdiff
path: root/Database/internals/database-segment.h
blob: de8e8504158aa25775c6d8f4bbd4995c82beefd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#ifndef __DATABASE_SEGMENT_H__
#define __DATABASE_SEGMENT_H__

#include <Handle.h>
#include "database-types.h"

class SegmentRefData;

/**
 * The Segment class that holds the memory and patches for a memory segment.
 *
 * This class will passively hold the memory for a segment memory. The tags
 * corresponding to each byte are also stored there. The patches are optionnal,
 * and will only be allocated if a patch is actually done.
 *
 * TODO: do in-memory hollow structure for the tags.
 */

class Segment : public Base {
  public:
      /**
       * Main constructor.
       *
       * @param size is the real data size of this segment.
       * @param id is the static id for that data segment.
       * @param cpu_base is the base absolute physical address for that segment in the cpu's flat memory.
       * @param extra_size gives the size of the uninitialized memory at the end of the segment, typically the bss part.
       * @param data optionally holds the memory to use for this segment. If it's provided, the caller MUST NOT free it, as it's going to be used directly.
       */
      Segment(Uint32 size, Uint32 id, Uint64 cpu_base, Uint32 extra_size = 0, const Byte * data = 0);
      ~Segment();
    Uint32 getId() { return id; }
    /** Will return the byte for that pointer in the segment. May be patched.
     * @param ptr is the pointer for the byte to be retrieved.
     * @return the byte read, or -1 if it's inside the BSS part, or outside the whole segment.
     */
    short Read(Uint32 ptr);
    /** Will return the byte for that pointer in the segment. Will NOT be patched.
     * @param ptr is the pointer for the byte to be retrieved.
     * @return the byte read, or -1 if it's inside the BSS part, or outside the whole segment.
     */
    short RawRead(Uint32 ptr);
    /** In case you need to read the (unpatched) memory block.
     * @return the memory block of the segment.
     */
    const Byte * getPristineMemory() { return plainmemory; }
    const memory_tags_t * getMemoryTags() { return tags; }
    void Patch(Uint32 ptr, Byte val);
    void Restore(Uint32 ptr);
    bool IsPatched(Uint32 ptr);
    bool IsLoaded() { return loaded; }
    /** Will load the memory segment with the provided Handle; will
     * only work if you didn't provide data during the constructor.
     * @see Segment()
     */
    void LoadMemory(Handle * src);
    void setTag(Uint32 ptr, memory_tags_t tag);
    memory_tags_t getTag(Uint32 ptr);
    void setBasicTag(Uint32 ptr, basic_tags_t tag) {
        memory_tags_t t = getTag(ptr);
        t.basic_tag = tag;
        setTag(ptr, t);
    }
    basic_tags_t getBasicTag(Uint32 ptr) {
        return (basic_tags_t) getTag(ptr).basic_tag;
    }
    void setCrawled(Uint32 ptr, bool crawled) {
        memory_tags_t t = getTag(ptr);
        t.crawled = crawled ? 1 : 0;
        setTag(ptr, t);
    }
    bool getCrawled(Uint32 ptr) {
        return getTag(ptr).crawled;
    }
    void setCertitude(Uint32 ptr, int certitude) {
        if (certitude < 0)
            certitude = -1;
        if (certitude > 15)
            certitude = 15;
        memory_tags_t t = getTag(ptr);
        t.certitude = certitude + 1;
    }
    int getCertitude(Uint32 ptr) {
        return getTag(ptr).certitude - 1;
    }
    void ResetCrawled() {
        for (Uint32 ptr = 0; ptr < size; ptr++) {
            tags[ptr].crawled = 0;
        }
    }
    Uint32 getSize() { return size; }
    Uint64 getFullSize() { return size + extra_size; }
    Uint64 getCpuBase() { return cpu_base; }
    SegmentRefData * getSegmentRefData(Uint32 ptr);
    void setSegmentRefData(Uint32 ptr, SegmentRefData * data);
  private:
    Byte * plainmemory, * patches, * patchesmap;
    SegmentRefData ** refData;
    memory_tags_t * tags;
    bool loaded, allocated;
    Uint32 size, extra_size;
    Uint32 id;
    Uint64 cpu_base;
};

#endif