MiniPBCoder_OSX.cpp 7.2 KB

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