MiniPBCoder_OSX.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "MiniPBCoder.h"
  21. #include "MMKVLog.h"
  22. #ifdef MMKV_APPLE
  23. # include "CodedInputData.h"
  24. # include "CodedInputDataCrypt.h"
  25. # include "CodedOutputData.h"
  26. # include "MMBuffer.h"
  27. # include "PBEncodeItem.hpp"
  28. # include "PBUtility.h"
  29. # include <string>
  30. # include <vector>
  31. # if __has_feature(objc_arc)
  32. # error This file must be compiled with MRC. Use -fno-objc-arc flag.
  33. # endif
  34. using namespace std;
  35. namespace mmkv {
  36. size_t MiniPBCoder::prepareObjectForEncode(__unsafe_unretained NSObject *obj) {
  37. if (!obj) {
  38. return m_encodeItems->size();
  39. }
  40. m_encodeItems->push_back(PBEncodeItem());
  41. PBEncodeItem *encodeItem = &(m_encodeItems->back());
  42. size_t index = m_encodeItems->size() - 1;
  43. if ([obj isKindOfClass:[NSString class]]) {
  44. NSString *str = (NSString *) obj;
  45. encodeItem->type = PBEncodeItemType_NSString;
  46. NSData *buffer = [[str dataUsingEncoding:NSUTF8StringEncoding] retain];
  47. encodeItem->value.tmpObjectValue = (__bridge void *) buffer;
  48. encodeItem->valueSize = static_cast<uint32_t>(buffer.length);
  49. } else if ([obj isKindOfClass:[NSDate class]]) {
  50. NSDate *oDate = (NSDate *) obj;
  51. encodeItem->type = PBEncodeItemType_NSDate;
  52. encodeItem->value.objectValue = (__bridge void *) oDate;
  53. encodeItem->valueSize = pbDoubleSize();
  54. encodeItem->compiledSize = encodeItem->valueSize;
  55. return index; // double has fixed compilesize
  56. } else if ([obj isKindOfClass:[NSData class]]) {
  57. NSData *oData = (NSData *) obj;
  58. encodeItem->type = PBEncodeItemType_NSData;
  59. encodeItem->value.objectValue = (__bridge void *) oData;
  60. encodeItem->valueSize = static_cast<uint32_t>(oData.length);
  61. } else {
  62. m_encodeItems->pop_back();
  63. MMKVError("%@ not recognized", NSStringFromClass(obj.class));
  64. return m_encodeItems->size();
  65. }
  66. encodeItem->compiledSize = pbRawVarint32Size(encodeItem->valueSize) + encodeItem->valueSize;
  67. return index;
  68. }
  69. void MiniPBCoder::decodeOneMap(MMKVMap &dic, size_t position, bool greedy) {
  70. auto block = [position, this](MMKVMap &dictionary) {
  71. if (position) {
  72. m_inputData->seek(position);
  73. } else {
  74. m_inputData->readInt32();
  75. }
  76. while (!m_inputData->isAtEnd()) {
  77. KeyValueHolder kvHolder;
  78. const auto &key = m_inputData->readNSString(kvHolder);
  79. if (key.length > 0) {
  80. m_inputData->readData(kvHolder);
  81. auto itr = dictionary.find(key);
  82. if (itr != dictionary.end()) {
  83. if (kvHolder.valueSize > 0) {
  84. itr->second = std::move(kvHolder);
  85. } else {
  86. auto oldKey = itr->first;
  87. dictionary.erase(itr);
  88. [oldKey release];
  89. }
  90. } else {
  91. if (kvHolder.valueSize > 0) {
  92. dictionary.emplace(key, std::move(kvHolder));
  93. [key retain];
  94. }
  95. }
  96. }
  97. }
  98. };
  99. if (greedy) {
  100. try {
  101. block(dic);
  102. } catch (std::exception &exception) {
  103. MMKVError("%s", exception.what());
  104. } catch (...) {
  105. MMKVError("decode fail");
  106. }
  107. } else {
  108. try {
  109. MMKVMap tmpDic;
  110. block(tmpDic);
  111. dic.swap(tmpDic);
  112. for (auto &pair : tmpDic) {
  113. [pair.first release];
  114. }
  115. } catch (std::exception &exception) {
  116. MMKVError("%s", exception.what());
  117. } catch (...) {
  118. MMKVError("decode fail");
  119. }
  120. }
  121. }
  122. # ifndef MMKV_DISABLE_CRYPT
  123. void MiniPBCoder::decodeOneMap(MMKVMapCrypt &dic, size_t position, bool greedy) {
  124. auto block = [position, this](MMKVMapCrypt &dictionary) {
  125. if (position) {
  126. m_inputDataDecrpt->seek(position);
  127. } else {
  128. m_inputDataDecrpt->readInt32();
  129. }
  130. while (!m_inputDataDecrpt->isAtEnd()) {
  131. KeyValueHolderCrypt kvHolder;
  132. const auto &key = m_inputDataDecrpt->readNSString(kvHolder);
  133. if (key.length > 0) {
  134. m_inputDataDecrpt->readData(kvHolder);
  135. auto itr = dictionary.find(key);
  136. if (itr != dictionary.end()) {
  137. if (kvHolder.realValueSize() > 0) {
  138. itr->second = std::move(kvHolder);
  139. } else {
  140. auto oldKey = itr->first;
  141. dictionary.erase(itr);
  142. [oldKey release];
  143. }
  144. } else {
  145. if (kvHolder.realValueSize() > 0) {
  146. dictionary.emplace(key, std::move(kvHolder));
  147. [key retain];
  148. }
  149. }
  150. }
  151. }
  152. };
  153. if (greedy) {
  154. try {
  155. block(dic);
  156. } catch (std::exception &exception) {
  157. MMKVError("%s", exception.what());
  158. } catch (...) {
  159. MMKVError("decode fail");
  160. }
  161. } else {
  162. try {
  163. MMKVMapCrypt tmpDic;
  164. block(tmpDic);
  165. dic.swap(tmpDic);
  166. for (auto &pair : tmpDic) {
  167. [pair.first release];
  168. }
  169. } catch (std::exception &exception) {
  170. MMKVError("%s", exception.what());
  171. } catch (...) {
  172. MMKVError("decode fail");
  173. }
  174. }
  175. }
  176. # endif // MMKV_DISABLE_CRYPT
  177. NSObject *MiniPBCoder::decodeObject(const MMBuffer &oData, Class cls) {
  178. if (!cls || oData.length() == 0) {
  179. return nil;
  180. }
  181. CodedInputData input(oData.getPtr(), oData.length());
  182. if (cls == [NSString class]) {
  183. return input.readNSString();
  184. } else if (cls == [NSMutableString class]) {
  185. return [NSMutableString stringWithString:input.readNSString()];
  186. } else if (cls == [NSData class]) {
  187. return input.readNSData();
  188. } else if (cls == [NSMutableData class]) {
  189. return [NSMutableData dataWithData:input.readNSData()];
  190. } else if (cls == [NSDate class]) {
  191. return [NSDate dateWithTimeIntervalSince1970:input.readDouble()];
  192. } else {
  193. MMKVError("%@ not recognized", NSStringFromClass(cls));
  194. }
  195. return nil;
  196. }
  197. bool MiniPBCoder::isCompatibleClass(Class cls) {
  198. if (cls == [NSString class]) {
  199. return true;
  200. }
  201. if (cls == [NSMutableString class]) {
  202. return true;
  203. }
  204. if (cls == [NSData class]) {
  205. return true;
  206. }
  207. if (cls == [NSMutableData class]) {
  208. return true;
  209. }
  210. if (cls == [NSDate class]) {
  211. return true;
  212. }
  213. return false;
  214. }
  215. } // namespace mmkv
  216. #endif // MMKV_APPLE