MMKV_OSX.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "MMKVPredef.h"
  21. #ifdef MMKV_APPLE
  22. # include "CodedInputData.h"
  23. # include "CodedOutputData.h"
  24. # include "InterProcessLock.h"
  25. # include "MMKV.h"
  26. # include "MemoryFile.h"
  27. # include "MiniPBCoder.h"
  28. # include "PBUtility.h"
  29. # include "ScopedLock.hpp"
  30. # include "ThreadLock.h"
  31. # include "aes/AESCrypt.h"
  32. # include <sys/utsname.h>
  33. # include <sys/sysctl.h>
  34. # include "MMKV_OSX.h"
  35. # include "MMKVLog.h"
  36. # ifdef MMKV_IOS
  37. # include <sys/mman.h>
  38. # endif
  39. # ifdef __aarch64__
  40. # include "crc32/Checksum.h"
  41. # endif
  42. # if __has_feature(objc_arc)
  43. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  44. # endif
  45. using namespace std;
  46. using namespace mmkv;
  47. extern ThreadLock *g_instanceLock;
  48. extern MMKVPath_t g_rootDir;
  49. MMKV_NAMESPACE_BEGIN
  50. HybridString::HybridString(string_view cpp) {
  51. if (cpp.empty()) {
  52. str = nil;
  53. } else {
  54. str = [[NSString alloc] initWithBytesNoCopy:(void*)cpp.data() length:cpp.length() encoding:NSUTF8StringEncoding freeWhenDone:NO];
  55. }
  56. }
  57. HybridString::~HybridString() {
  58. [str release];
  59. }
  60. HybridStringCP::HybridStringCP(string_view cpp) {
  61. if (cpp.empty()) {
  62. str = nil;
  63. } else {
  64. str = [[NSString alloc] initWithBytes:(void*)cpp.data() length:cpp.length() encoding:NSUTF8StringEncoding];
  65. }
  66. }
  67. HybridStringCP::~HybridStringCP() {
  68. [str release];
  69. }
  70. bool MMKV::set(bool value, std::string_view key) {
  71. return set(value, key, m_expiredInSeconds);
  72. }
  73. bool MMKV::set(bool value, std::string_view key, uint32_t expireDuration) {
  74. HybridStringCP hybridKey = key;
  75. return set(value, hybridKey.str, expireDuration);
  76. }
  77. bool MMKV::set(int32_t value, std::string_view key) {
  78. return set(value, key, m_expiredInSeconds);
  79. }
  80. bool MMKV::set(int32_t value, std::string_view key, uint32_t expireDuration) {
  81. HybridStringCP hybridKey = key;
  82. return set(value, hybridKey.str, expireDuration);
  83. }
  84. bool MMKV::set(uint32_t value, std::string_view key) {
  85. return set(value, key, m_expiredInSeconds);
  86. }
  87. bool MMKV::set(uint32_t value, std::string_view key, uint32_t expireDuration) {
  88. HybridStringCP hybridKey = key;
  89. return set(value, hybridKey.str, expireDuration);
  90. }
  91. bool MMKV::set(int64_t value, std::string_view key) {
  92. return set(value, key, m_expiredInSeconds);
  93. }
  94. bool MMKV::set(int64_t value, std::string_view key, uint32_t expireDuration) {
  95. HybridStringCP hybridKey = key;
  96. return set(value, hybridKey.str, expireDuration);
  97. }
  98. bool MMKV::set(uint64_t value, std::string_view key) {
  99. return set(value, key, m_expiredInSeconds);
  100. }
  101. bool MMKV::set(uint64_t value, std::string_view key, uint32_t expireDuration) {
  102. HybridStringCP hybridKey = key;
  103. return set(value, hybridKey.str, expireDuration);
  104. }
  105. bool MMKV::set(float value, std::string_view key) {
  106. return set(value, key, m_expiredInSeconds);
  107. }
  108. bool MMKV::set(float value, std::string_view key, uint32_t expireDuration) {
  109. HybridStringCP hybridKey = key;
  110. return set(value, hybridKey.str, expireDuration);
  111. }
  112. bool MMKV::set(double value, std::string_view key) {
  113. return set(value, key, m_expiredInSeconds);
  114. }
  115. bool MMKV::set(double value, std::string_view key, uint32_t expireDuration) {
  116. HybridStringCP hybridKey = key;
  117. return set(value, hybridKey.str, expireDuration);
  118. }
  119. bool MMKV::set(const char *value, std::string_view key) {
  120. return set(value, key, m_expiredInSeconds);
  121. }
  122. bool MMKV::set(const char *value, std::string_view key, uint32_t expireDuration) {
  123. HybridStringCP hybridKey = key;
  124. return set(value, hybridKey.str, expireDuration);
  125. }
  126. bool MMKV::set(const std::string &value, std::string_view key) {
  127. return set(value, key, m_expiredInSeconds);
  128. }
  129. bool MMKV::set(const std::string &value, std::string_view key, uint32_t expireDuration) {
  130. HybridStringCP hybridKey = key;
  131. return set(value, hybridKey.str, expireDuration);
  132. }
  133. bool MMKV::set(std::string_view value, std::string_view key) {
  134. return set(value, key, m_expiredInSeconds);
  135. }
  136. bool MMKV::set(std::string_view value, std::string_view key, uint32_t expireDuration) {
  137. HybridStringCP hybridKey = key;
  138. return set(value, hybridKey.str, expireDuration);
  139. }
  140. bool MMKV::set(const mmkv::MMBuffer &value, std::string_view key) {
  141. return set(value, key, m_expiredInSeconds);
  142. }
  143. bool MMKV::set(const mmkv::MMBuffer &value, std::string_view key, uint32_t expireDuration) {
  144. HybridStringCP hybridKey = key;
  145. return set(value, hybridKey.str, expireDuration);
  146. }
  147. bool MMKV::set(const std::vector<std::string> &vector, std::string_view key) {
  148. return set(vector, key, m_expiredInSeconds);
  149. }
  150. bool MMKV::set(const std::vector<std::string> &vector, std::string_view key, uint32_t expireDuration) {
  151. HybridStringCP hybridKey = key;
  152. return set(vector, hybridKey.str, expireDuration);
  153. }
  154. bool MMKV::containsKey(std::string_view key) {
  155. HybridString hybridKey = key;
  156. return containsKey(hybridKey.str);
  157. }
  158. bool MMKV::removeValueForKey(std::string_view key) {
  159. HybridString hybridKey = key;
  160. return removeValueForKey(hybridKey.str);
  161. }
  162. bool MMKV::getBool(std::string_view key, bool defaultValue, bool *hasValue) {
  163. HybridString hybridKey = key;
  164. return getBool(hybridKey.str, defaultValue, hasValue);
  165. }
  166. int32_t MMKV::getInt32(std::string_view key, int32_t defaultValue, bool *hasValue) {
  167. HybridString hybridKey = key;
  168. return getInt32(hybridKey.str, defaultValue, hasValue);
  169. }
  170. uint32_t MMKV::getUInt32(std::string_view key, uint32_t defaultValue, bool *hasValue) {
  171. HybridString hybridKey = key;
  172. return getUInt32(hybridKey.str, defaultValue, hasValue);
  173. }
  174. int64_t MMKV::getInt64(std::string_view key, int64_t defaultValue, bool *hasValue) {
  175. HybridString hybridKey = key;
  176. return getInt64(hybridKey.str, defaultValue, hasValue);
  177. }
  178. uint64_t MMKV::getUInt64(std::string_view key, uint64_t defaultValue, bool *hasValue) {
  179. HybridString hybridKey = key;
  180. return getUInt64(hybridKey.str, defaultValue, hasValue);
  181. }
  182. float MMKV::getFloat(std::string_view key, float defaultValue, bool *hasValue) {
  183. HybridString hybridKey = key;
  184. return getFloat(hybridKey.str, defaultValue, hasValue);
  185. }
  186. double MMKV::getDouble(std::string_view key, double defaultValue, bool *hasValue) {
  187. HybridString hybridKey = key;
  188. return getDouble(hybridKey.str, defaultValue, hasValue);
  189. }
  190. bool MMKV::getString(std::string_view key, std::string &result, bool inplaceModification) {
  191. HybridString hybridKey = key;
  192. return getString(hybridKey.str, result, inplaceModification);
  193. }
  194. mmkv::MMBuffer MMKV::getDataForKey(std::string_view key) {
  195. HybridString hybridKey = key;
  196. return getDataForKey(hybridKey.str);
  197. }
  198. bool MMKV::setDataForKey(mmkv::MMBuffer &&data, std::string_view key, bool isDataHolder) {
  199. HybridStringCP hybridKey = key;
  200. return setDataForKey(std::move(data), hybridKey.str, isDataHolder);
  201. }
  202. bool MMKV::getVector(std::string_view key, std::vector<std::string> &result) {
  203. HybridString hybridKey = key;
  204. return getVector(hybridKey.str, result);
  205. }
  206. # ifdef MMKV_IOS
  207. static bool g_isInBackground = false;
  208. void MMKV::setIsInBackground(bool isInBackground) {
  209. if (!g_instanceLock) {
  210. return;
  211. }
  212. SCOPED_LOCK(g_instanceLock);
  213. g_isInBackground = isInBackground;
  214. MMKVInfo("g_isInBackground:%d", g_isInBackground);
  215. }
  216. bool MMKV::isInBackground() {
  217. if (!g_instanceLock) {
  218. return true;
  219. }
  220. SCOPED_LOCK(g_instanceLock);
  221. return g_isInBackground;
  222. }
  223. # endif // MMKV_IOS
  224. bool MMKV::set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key) {
  225. return set(obj, key, m_expiredInSeconds);
  226. }
  227. bool MMKV::set(NSObject<NSCoding> *__unsafe_unretained obj, MMKVKey_t key, uint32_t expireDuration) {
  228. if (isKeyEmpty(key)) {
  229. return false;
  230. }
  231. if (!obj) {
  232. removeValueForKey(key);
  233. return true;
  234. }
  235. NSData *tmpData = nil;
  236. if ([obj isKindOfClass:NSString.class]) {
  237. auto str = (NSString *) obj;
  238. tmpData = [str dataUsingEncoding:NSUTF8StringEncoding];
  239. } else if ([obj isKindOfClass:NSData.class]) {
  240. tmpData = (NSData *) obj;
  241. }
  242. if (tmpData) {
  243. // delay write the size needed for encoding tmpData
  244. // avoid memory copying
  245. if (mmkv_likely(!m_enableKeyExpire)) {
  246. return setDataForKey(MMBuffer(tmpData, MMBufferNoCopy), key, true);
  247. } else {
  248. MMBuffer data(tmpData, MMBufferNoCopy);
  249. if (data.length() > 0) {
  250. auto tmp = MMBuffer(pbMMBufferSize(data) + Fixed32Size);
  251. CodedOutputData output(tmp.getPtr(), tmp.length());
  252. output.writeData(data);
  253. auto time = (expireDuration != 0) ? getCurrentTimeInSecond() + expireDuration : 0;
  254. output.writeRawLittleEndian32(UInt32ToInt32(time));
  255. data = std::move(tmp);
  256. }
  257. return setDataForKey(std::move(data), key);
  258. }
  259. } else if ([obj isKindOfClass:NSDate.class]) {
  260. NSDate *oDate = (NSDate *) obj;
  261. double time = oDate.timeIntervalSince1970;
  262. return set(time, key, expireDuration);
  263. } else {
  264. /*if ([object conformsToProtocol:@protocol(NSCoding)])*/ {
  265. @try {
  266. NSError *error = nil;
  267. auto archived = [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:&error];
  268. if (error) {
  269. MMKVError("fail to archive: %@", error);
  270. return false;
  271. }
  272. if (archived.length > 0) {
  273. if (mmkv_likely(!m_enableKeyExpire)) {
  274. return setDataForKey(MMBuffer(archived, MMBufferNoCopy), key);
  275. } else {
  276. MMBuffer data(archived, MMBufferNoCopy);
  277. if (data.length() > 0) {
  278. auto tmp = MMBuffer(data.length() + Fixed32Size);
  279. CodedOutputData output(tmp.getPtr(), tmp.length());
  280. output.writeRawData(data); // NSKeyedArchiver has its own size management
  281. auto time = (expireDuration != 0) ? getCurrentTimeInSecond() + expireDuration : 0;
  282. output.writeRawLittleEndian32(UInt32ToInt32(time));
  283. data = std::move(tmp);
  284. }
  285. return setDataForKey(std::move(data), key);
  286. }
  287. }
  288. } @catch (NSException *exception) {
  289. MMKVError("exception: %@", exception.reason);
  290. }
  291. }
  292. }
  293. return false;
  294. }
  295. static id unSecureUnArchiveObjectWithData(NSData *data) {
  296. @try {
  297. NSError *error = nil;
  298. auto unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
  299. if (error) {
  300. MMKVError("fail to init unarchiver %@", error);
  301. return nil;
  302. }
  303. unarchiver.requiresSecureCoding = NO;
  304. id result = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];
  305. [unarchiver release];
  306. return result;
  307. } @catch (NSException *exception) {
  308. MMKVError("exception: %@", exception.reason);
  309. }
  310. return nil;
  311. }
  312. NSObject *MMKV::getObject(MMKVKey_t key, Class cls) {
  313. if (isKeyEmpty(key) || !cls) {
  314. return nil;
  315. }
  316. SCOPED_LOCK(m_lock);
  317. SCOPED_LOCK(m_sharedProcessLock);
  318. auto data = getDataForKey(key);
  319. if (data.length() > 0) {
  320. if (MiniPBCoder::isCompatibleClass(cls)) {
  321. try {
  322. auto result = MiniPBCoder::decodeObject(data, cls);
  323. return result;
  324. } catch (std::exception &exception) {
  325. MMKVError("%s", exception.what());
  326. } catch (...) {
  327. MMKVError("decode fail");
  328. }
  329. } else {
  330. if ([cls conformsToProtocol:@protocol(NSCoding)]) {
  331. auto tmp = [NSData dataWithBytesNoCopy:data.getPtr() length:data.length() freeWhenDone:NO];
  332. return unSecureUnArchiveObjectWithData(tmp);
  333. }
  334. }
  335. }
  336. return nil;
  337. }
  338. # ifndef MMKV_DISABLE_CRYPT
  339. pair<bool, KeyValueHolder>
  340. MMKV::appendDataWithKey(const MMBuffer &data, MMKVKey_t key, const KeyValueHolderCrypt &kvHolder, bool isDataHolder) {
  341. if (kvHolder.type != KeyValueHolderType_Offset) {
  342. return appendDataWithKey(data, key, isDataHolder);
  343. }
  344. SCOPED_LOCK(m_exclusiveProcessLock);
  345. uint32_t keyLength = kvHolder.keySize;
  346. // size needed to encode the key
  347. size_t rawKeySize = keyLength + pbRawVarint32Size(keyLength);
  348. auto basePtr = (uint8_t *) m_file->getMemory() + Fixed32Size;
  349. MMBuffer keyData(rawKeySize);
  350. AESCrypt decrypter = m_crypter->cloneWithStatus(kvHolder.cryptStatus);
  351. decrypter.decrypt(basePtr + kvHolder.offset, keyData.getPtr(), rawKeySize);
  352. return doAppendDataWithKey(data, keyData, isDataHolder, keyLength);
  353. }
  354. pair<bool, KeyValueHolder>
  355. MMKV::overrideDataWithKey(const MMBuffer &data, MMKVKey_t key, const KeyValueHolderCrypt &kvHolder, bool isDataHolder) {
  356. if (kvHolder.type != KeyValueHolderType_Offset) {
  357. return overrideDataWithKey(data, key, isDataHolder);
  358. }
  359. SCOPED_LOCK(m_exclusiveProcessLock);
  360. uint32_t keyLength = kvHolder.keySize;
  361. // size needed to encode the key
  362. size_t rawKeySize = keyLength + pbRawVarint32Size(keyLength);
  363. auto basePtr = (uint8_t *) m_file->getMemory() + Fixed32Size;
  364. MMBuffer keyData(rawKeySize);
  365. AESCrypt decrypter = m_crypter->cloneWithStatus(kvHolder.cryptStatus);
  366. decrypter.decrypt(basePtr + kvHolder.offset, keyData.getPtr(), rawKeySize);
  367. return doOverrideDataWithKey(data, keyData, isDataHolder, keyLength);
  368. }
  369. # endif
  370. NSArray *MMKV::allKeysObjC(bool filterExpire) {
  371. SCOPED_LOCK(m_lock);
  372. checkLoadData();
  373. if (mmkv_unlikely(filterExpire && m_enableKeyExpire)) {
  374. SCOPED_LOCK(m_exclusiveProcessLock);
  375. fullWriteback(nullptr, true);
  376. }
  377. NSMutableArray *keys = [NSMutableArray array];
  378. if (m_crypter) {
  379. for (const auto &pair : *m_dicCrypt) {
  380. [keys addObject:pair.first];
  381. }
  382. } else {
  383. for (const auto &pair : *m_dic) {
  384. [keys addObject:pair.first];
  385. }
  386. }
  387. return keys;
  388. }
  389. std::vector<std::string> MMKV::allKeys(bool filterExpire) {
  390. @autoreleasepool {
  391. auto arrKeys = allKeysObjC(filterExpire);
  392. std::vector<std::string> vec;
  393. for (NSString* str in arrKeys) {
  394. vec.push_back(str.UTF8String);
  395. }
  396. return vec;
  397. }
  398. }
  399. bool MMKV::removeValuesForKeys(NSArray *arrKeys) {
  400. if (isReadOnly()) {
  401. MMKVWarning("[%s] file readonly", m_mmapID.c_str());
  402. return false;
  403. }
  404. if (arrKeys.count == 0) {
  405. return true;
  406. }
  407. if (arrKeys.count == 1) {
  408. return removeValueForKey(arrKeys[0]);
  409. }
  410. SCOPED_LOCK(m_lock);
  411. SCOPED_LOCK(m_exclusiveProcessLock);
  412. checkLoadData();
  413. size_t deleteCount = 0;
  414. if (m_crypter) {
  415. for (NSString *key in arrKeys) {
  416. auto itr = m_dicCrypt->find(key);
  417. if (itr != m_dicCrypt->end()) {
  418. auto oldKey = itr->first;
  419. m_dicCrypt->erase(itr);
  420. [oldKey release];
  421. deleteCount++;
  422. }
  423. }
  424. } else {
  425. for (NSString *key in arrKeys) {
  426. auto itr = m_dic->find(key);
  427. if (itr != m_dic->end()) {
  428. auto oldKey = itr->first;
  429. m_dic->erase(itr);
  430. [oldKey release];
  431. deleteCount++;
  432. }
  433. }
  434. }
  435. if (deleteCount > 0) {
  436. m_hasFullWriteback = false;
  437. return fullWriteback();
  438. }
  439. return true;
  440. }
  441. bool MMKV::removeValuesForKeys(const std::vector<std::string> &arrKeys) {
  442. if (arrKeys.empty()) {
  443. return true;
  444. }
  445. NSMutableArray* arr = [[NSMutableArray alloc] initWithCapacity:arrKeys.size()];
  446. for (auto& key : arrKeys) {
  447. [arr addObject:HybridString(key).str];
  448. }
  449. auto ret = removeValuesForKeys(arr);
  450. [arr release];
  451. return ret;
  452. }
  453. void MMKV::enumerateKeys(EnumerateBlock block) {
  454. if (block == nil) {
  455. return;
  456. }
  457. SCOPED_LOCK(m_lock);
  458. checkLoadData();
  459. MMKVInfo("enumerate [%s] begin", m_mmapID.c_str());
  460. if (m_crypter) {
  461. for (const auto &pair : *m_dicCrypt) {
  462. BOOL stop = NO;
  463. block(pair.first, &stop);
  464. if (stop) {
  465. break;
  466. }
  467. }
  468. } else {
  469. for (const auto &pair : *m_dic) {
  470. BOOL stop = NO;
  471. block(pair.first, &stop);
  472. if (stop) {
  473. break;
  474. }
  475. }
  476. }
  477. MMKVInfo("enumerate [%s] finish", m_mmapID.c_str());
  478. }
  479. void GetAppleMachineInfo(int &device, int &version) {
  480. device = UnKnown;
  481. version = 0;
  482. # if 0
  483. struct utsname systemInfo = {};
  484. uname(&systemInfo);
  485. std::string machine(systemInfo.machine);
  486. # else
  487. size_t size;
  488. sysctlbyname("hw.machine", nullptr, &size, nullptr, 0);
  489. char *answer = (char *) malloc(size);
  490. sysctlbyname("hw.machine", answer, &size, nullptr, 0);
  491. std::string machine(answer);
  492. free(answer);
  493. # endif
  494. if (machine.find("PowerMac") != std::string::npos || machine.find("Power Macintosh") != std::string::npos) {
  495. device = PowerMac;
  496. } else if (machine.find("Mac") != std::string::npos || machine.find("Macintosh") != std::string::npos) {
  497. device = Mac;
  498. } else if (machine.find("iPhone") != std::string::npos) {
  499. device = iPhone;
  500. } else if (machine.find("iPod") != std::string::npos) {
  501. device = iPod;
  502. } else if (machine.find("iPad") != std::string::npos) {
  503. device = iPad;
  504. } else if (machine.find("AppleTV") != std::string::npos) {
  505. device = AppleTV;
  506. } else if (machine.find("AppleWatch") != std::string::npos) {
  507. device = AppleWatch;
  508. }
  509. auto pos = machine.find_first_of("0123456789");
  510. if (pos != std::string::npos) {
  511. version = std::atoi(machine.substr(pos).c_str());
  512. }
  513. }
  514. MMKV_NAMESPACE_END
  515. #endif // MMKV_APPLE