forked from F8LEFT/SoFixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReader.h
74 lines (70 loc) · 1.84 KB
/
FileReader.h
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
//===------------------------------------------------------------*- C++ -*-===//
//
// Created by F8LEFT on 2021/1/5.
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#ifndef SOFIXER_FILEREADER_H
#define SOFIXER_FILEREADER_H
#include "macros.h"
#include "FDebug.h"
#include <cerrno>
#include <cstdio>
#include <cstring>
class FileReader {
public:
FileReader(const char* name): source(name){}
~FileReader() {
Close();
}
bool Open() {
if (IsValid()) {
return false;
}
fp = fopen(source, "rb");
if (fp == nullptr) {
return false;
}
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
return true;
}
bool Close() {
if (IsValid()) {
auto err = fclose(fp);
fp = nullptr;
return err == 0;
}
return false;
}
bool IsValid() {
return fp != nullptr;
}
const char* getSource() {
return source;
}
size_t Read(void *addr, size_t len, int offset = -1) {
if (offset >= 0) {
fseek(fp, offset, SEEK_SET);
}
auto rc = TEMP_FAILURE_RETRY(fread(addr, 1, len, fp));
if (rc < 0) {
FLOGE("can't read file \"%s\": %s", source, strerror(errno));
return rc;
}
if (rc != len) {
FLOGE("\"%s\" has no enough data at %x:%zx, not a valid file or you need to dump more data", source, offset, len);
return rc;
}
return rc;
}
long FileSize() {
return file_size;
}
private:
FILE* fp = nullptr;
const char* source = nullptr;
long file_size;
};
#endif //SOFIXER_FILEREADER_H