Skip to content

Commit 39db50e

Browse files
committed
fixes
1 parent 09bb3ac commit 39db50e

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

cpp/MMapEncryptedFile.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ struct Header {
1717
uint8_t reserved[14];
1818
};
1919

20+
template <typename T> static inline T FixEndianness(T val) { return val; }
21+
22+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
23+
template <> static inline uint16_t FixEndianness(uint16_t val) { return __builtin_bswap16(val); }
24+
template <> static inline uint32_t FixEndianness(uint32_t val) { return __builtin_bswap32(val); }
25+
template <> static inline uint64_t FixEndianness(uint64_t val) { return __builtin_bswap64(val); }
26+
#endif
27+
2028
static const char* MMapEncryptedFileErrors[] = {
2129
"",
2230
"Encrypted file doesn't have a full header",
@@ -130,7 +138,7 @@ class MMapEncryptedFile
130138

131139
inline size_t size() const {
132140
file_.assertFileIsOpen();
133-
return reinterpret_cast<Header*>(file_.data())->size;
141+
return FixEndianness(reinterpret_cast<Header*>(file_.data())->size);
134142
}
135143

136144
inline size_t capacity() const { return file_.size() - sizeof(Header); }
@@ -146,13 +154,13 @@ class MMapEncryptedFile
146154
}
147155

148156
if (newSize < size()) {
149-
// the order is important here to avoid incosistencies
150-
reinterpret_cast<Header*>(file_.data())->size = newSize;
157+
// the order is important here to avoid inconsistencies
158+
reinterpret_cast<Header*>(file_.data())->size = FixEndianness((uint64_t)newSize);
151159
file_.resize(newSize + sizeof(Header), strictResize);
152160
} else {
153-
// the order is important here to avoid incosistencies
161+
// the order is important here to avoid inconsistencies
154162
file_.resize(newSize + sizeof(Header), strictResize);
155-
reinterpret_cast<Header*>(file_.data())->size = newSize;
163+
reinterpret_cast<Header*>(file_.data())->size = FixEndianness((uint64_t)newSize);
156164
}
157165
}
158166

@@ -175,21 +183,21 @@ class MMapEncryptedFile
175183
}
176184

177185
Header* header = reinterpret_cast<Header*>(file_.data());
178-
if (header->magic != MAGIC) {
186+
if (FixEndianness(header->magic) != MAGIC) {
179187
return 2;
180188
}
181-
if (header->version != 1) {
189+
if (FixEndianness(header->version) != 1) {
182190
return 3;
183191
}
184-
if (header->size + sizeof(Header) > file_.size()) {
192+
if (FixEndianness(header->size) + sizeof(Header) > file_.size()) {
185193
return 4;
186194
}
187195

188196
// decrypt data key
189197
uint8_t dataKey[16];
190198
aes_.decryptBlock(header->encryptedDataKey, dataKey);
191199

192-
if (header->keyHash != hashIVAndKey(header->iv, dataKey)) {
200+
if (FixEndianness(header->keyHash) != hashIVAndKey(header->iv, dataKey)) {
193201
return 5;
194202
}
195203

@@ -201,8 +209,8 @@ class MMapEncryptedFile
201209
file_.resize(sizeof(Header), true);
202210
uint8_t dataKey[16];
203211
Header* header = reinterpret_cast<Header*>(file_.data());
204-
header->magic = MAGIC;
205-
header->version = 1;
212+
header->magic = FixEndianness(MAGIC);
213+
header->version = FixEndianness((uint16_t)1);
206214
header->size = 0;
207215
// generate a random data key and encrypt it
208216
fillRandom(dataKey, 16);
@@ -211,7 +219,7 @@ class MMapEncryptedFile
211219
// generate a random IV
212220
fillRandom(header->iv, 16);
213221
// calculate the hash of the IV and the data key
214-
header->keyHash = hashIVAndKey(header->iv, dataKey);
222+
header->keyHash = FixEndianness(hashIVAndKey(header->iv, dataKey));
215223
}
216224
};
217225

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "NitroMmfileExample",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"private": true,
55
"scripts": {
66
"android": "react-native run-android",
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"react": "19.0.0",
1414
"react-native": "0.78.1",
15-
"react-native-mmfile": "file:react-native-mmfile-0.0.1.tgz",
15+
"react-native-mmfile": "1.0.0",
1616
"react-native-mmkv": "^3.2.0",
1717
"react-native-nitro-modules": "^0.25.2"
1818
},

package.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-mmfile",
3-
"version": "0.0.1",
4-
"description": "react-native-mmfile",
3+
"version": "1.0.0",
4+
"description": "Fastest storage with encryption for React Native.",
55
"main": "lib/index",
66
"module": "lib/index",
77
"types": "lib/index.d.ts",
@@ -41,18 +41,22 @@
4141
},
4242
"keywords": [
4343
"react-native",
44-
"nitro"
44+
"storage",
45+
"fast",
46+
"nitro",
47+
"ios",
48+
"android"
4549
],
4650
"repository": {
4751
"type": "git",
48-
"url": "git+https://github.com/mrousavy/nitro.git"
52+
"url": "git+http://github.com/weese/react-native-mmfile.git"
4953
},
50-
"author": "Marc Rousavy <[email protected]> (https://github.com/mrousavy)",
54+
"author": "David Weese <[email protected]> (https://github.com/weese)",
5155
"license": "MIT",
5256
"bugs": {
53-
"url": "https://github.com/mrousavy/nitro/issues"
57+
"url": "http://github.com/weese/react-native-mmfile/issues"
5458
},
55-
"homepage": "https://github.com/mrousavy/nitro#readme",
59+
"homepage": "http://github.com/weese/react-native-mmfile#readme",
5660
"publishConfig": {
5761
"registry": "https://registry.npmjs.org/"
5862
},

0 commit comments

Comments
 (0)