• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "tlv_util.h"
17 
18 namespace OHOS {
19 namespace TLVUtil {
20 template<>
CountBufferSize(const std::shared_ptr<UnifiedRecord> & input,TLVObject & data)21 bool CountBufferSize(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data)
22 {
23     if (input == nullptr) {
24         return false;
25     }
26     data.Count(input->GetType());
27     data.Count(input->GetUid());
28     auto type = input->GetType();
29     switch (type) {
30         case UDType::TEXT: {
31             auto text = static_cast<Text *>(input.get());
32             if (text == nullptr) {
33                 return false;
34             }
35             data.Count(text->GetDetails());
36             break;
37         }
38         case UDType::PLAIN_TEXT: {
39             auto plainText = static_cast<PlainText *>(input.get());
40             if (plainText == nullptr) {
41                 return false;
42             }
43             data.Count(plainText->GetContent());
44             data.Count(plainText->GetAbstract());
45             auto text = static_cast<Text *>(input.get());
46             if (text == nullptr) {
47                 return false;
48             }
49             data.Count(text->GetDetails());
50             break;
51         }
52         case UDType::HTML: {
53             auto html = static_cast<Html *>(input.get());
54             if (html == nullptr) {
55                 return false;
56             }
57             data.Count(html->GetHtmlContent());
58             data.Count(html->GetPlainContent());
59             auto text = static_cast<Text *>(input.get());
60             if (text == nullptr) {
61                 return false;
62             }
63             data.Count(text->GetDetails());
64             break;
65         }
66         case UDType::HYPERLINK: {
67             auto link = static_cast<Link *>(input.get());
68             if (link == nullptr) {
69                 return false;
70             }
71             data.Count(link->GetUrl());
72             data.Count(link->GetDescription());
73             auto text = static_cast<Text *>(input.get());
74             if (text == nullptr) {
75                 return false;
76             }
77             data.Count(text->GetDetails());
78             break;
79         }
80         case UDType::FILE: {
81             auto file = static_cast<File *>(input.get());
82             if (file == nullptr) {
83                 return false;
84             }
85             data.Count(file->GetUri());
86             data.Count(file->GetRemoteUri());
87             data.Count(file->GetDetails());
88             break;
89         }
90         case UDType::IMAGE: {
91             auto image = static_cast<Image *>(input.get());
92             if (image == nullptr) {
93                 return false;
94             }
95             data.Count(image->GetUri());
96             data.Count(image->GetRemoteUri());
97             auto file = static_cast<File *>(input.get());
98             if (file == nullptr) {
99                 return false;
100             }
101             data.Count(file->GetDetails());
102             break;
103         }
104         case UDType::VIDEO: {
105             auto video = static_cast<Video *>(input.get());
106             if (video == nullptr) {
107                 return false;
108             }
109             data.Count(video->GetUri());
110             data.Count(video->GetRemoteUri());
111             auto file = static_cast<File *>(input.get());
112             if (file == nullptr) {
113                 return false;
114             }
115             data.Count(file->GetDetails());
116             break;
117         }
118         case UDType::AUDIO: {
119             auto audio = static_cast<Audio *>(input.get());
120             if (audio == nullptr) {
121                 return false;
122             }
123             data.Count(audio->GetUri());
124             data.Count(audio->GetRemoteUri());
125             auto file = static_cast<File *>(input.get());
126             if (file == nullptr) {
127                 return false;
128             }
129             data.Count(file->GetDetails());
130             break;
131         }
132         case UDType::FOLDER: {
133             auto folder = static_cast<Folder *>(input.get());
134             if (folder == nullptr) {
135                 return false;
136             }
137             data.Count(folder->GetUri());
138             data.Count(folder->GetRemoteUri());
139             auto file = static_cast<File *>(input.get());
140             if (file == nullptr) {
141                 return false;
142             }
143             data.Count(file->GetDetails());
144             break;
145         }
146         case UDType::SYSTEM_DEFINED_RECORD: {
147             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
148             if (sdRecord == nullptr) {
149                 return false;
150             }
151             data.Count(sdRecord->GetDetails());
152             break;
153         }
154         case UDType::SYSTEM_DEFINED_FORM: {
155             auto form = static_cast<SystemDefinedForm *>(input.get());
156             if (form == nullptr) {
157                 return false;
158             }
159             data.Count(form->GetFormId());
160             data.Count(form->GetFormName());
161             data.Count(form->GetBundleName());
162             data.Count(form->GetAbilityName());
163             data.Count(form->GetModule());
164             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
165             if (sdRecord == nullptr) {
166                 return false;
167             }
168             data.Count(sdRecord->GetDetails());
169             break;
170         }
171         case UDType::SYSTEM_DEFINED_APP_ITEM: {
172             auto appItem = static_cast<SystemDefinedAppItem *>(input.get());
173             if (appItem == nullptr) {
174                 return false;
175             }
176             data.Count(appItem->GetAppId());
177             data.Count(appItem->GetAppName());
178             data.Count(appItem->GetAppIconId());
179             data.Count(appItem->GetAppLabelId());
180             data.Count(appItem->GetBundleName());
181             data.Count(appItem->GetAbilityName());
182             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
183             if (sdRecord == nullptr) {
184                 return false;
185             }
186             data.Count(sdRecord->GetDetails());
187             break;
188         }
189         case UDType::SYSTEM_DEFINED_PIXEL_MAP: {
190             auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get());
191             if (pixelMap == nullptr) {
192                 return false;
193             }
194             data.Count(pixelMap->GetRawData());
195             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
196             if (sdRecord == nullptr) {
197                 return false;
198             }
199             data.Count(sdRecord->GetDetails());
200             break;
201         }
202         case UDType::APPLICATION_DEFINED_RECORD: {
203             auto record = static_cast<ApplicationDefinedRecord *>(input.get());
204             if (record == nullptr) {
205                 return false;
206             }
207             data.Count(record->GetApplicationDefinedType());
208             data.Count(record->GetRawData());
209             break;
210         }
211         default: {
212             return false;
213         }
214     }
215     return true;
216 }
217 
218 template<>
CountBufferSize(const Runtime & input,TLVObject & data)219 bool CountBufferSize(const Runtime &input, TLVObject &data)
220 {
221     data.Count(input.key);
222     data.Count(input.isPrivate);
223     uint32_t size = static_cast<uint32_t>(input.privileges.size());
224     data.Count(size);
225     for (uint32_t i = 0; i < size; ++i) {
226         data.Count(input.privileges[i]);
227     }
228     data.Count(static_cast<int64_t>(input.createTime));
229     data.Count(static_cast<int64_t>(input.lastModifiedTime));
230     data.Count(input.sourcePackage);
231     data.Count(static_cast<int32_t>(input.dataStatus));
232     data.Count(input.dataVersion);
233     data.Count(input.createPackage);
234     data.Count(input.deviceId);
235     data.Count(input.recordTotalNum);
236     return true;
237 }
238 
239 template<>
CountBufferSize(const UnifiedData & input,TLVObject & data)240 bool CountBufferSize(const UnifiedData &input, TLVObject &data)
241 {
242     int32_t size = static_cast<int32_t>(input.GetRecords().size());
243     data.Count(size);
244     for (auto record : input.GetRecords()) {
245         if (!CountBufferSize(record, data)) {
246             return false;
247         }
248     }
249     return true;
250 }
251 
252 template<>
CountBufferSize(const std::vector<UnifiedData> & input,TLVObject & data)253 bool CountBufferSize(const std::vector<UnifiedData> &input, TLVObject &data)
254 {
255     int32_t size = static_cast<int32_t>(input.size());
256     data.Count(size);
257     for (auto unifiedData : input) {
258         if (!CountBufferSize(unifiedData, data)) {
259             return false;
260         }
261     }
262     return true;
263 }
264 
265 template<typename T>
266 bool Writing(const T &input, TLVObject &data);
267 
268 template<typename T>
269 bool Reading(T &output, TLVObject &data);
270 
271 template<>
Writing(const int32_t & input,TLVObject & data)272 bool Writing(const int32_t &input, TLVObject &data)
273 {
274     return data.WriteBasic(TAG_INT32, input);
275 }
276 
277 template<>
Reading(int32_t & output,TLVObject & data)278 bool Reading(int32_t &output, TLVObject &data)
279 {
280     return data.ReadBasic(output);
281 }
282 
283 template<>
Writing(const int64_t & input,TLVObject & data)284 bool Writing(const int64_t &input, TLVObject &data)
285 {
286     return data.WriteBasic(TAG_INT64, input);
287 }
288 
289 template<>
Reading(int64_t & output,TLVObject & data)290 bool Reading(int64_t &output, TLVObject &data)
291 {
292     return data.ReadBasic(output);
293 }
294 
295 template<>
Reading(uint32_t & output,TLVObject & data)296 bool Reading(uint32_t &output, TLVObject &data)
297 {
298     return data.ReadBasic(output);
299 }
300 
301 template<>
Writing(const uint32_t & input,TLVObject & data)302 bool Writing(const uint32_t &input, TLVObject &data)
303 {
304     return data.WriteBasic(TAG_UINT32, input);
305 }
306 
307 template<>
Reading(uint64_t & output,TLVObject & data)308 bool Reading(uint64_t &output, TLVObject &data)
309 {
310     return data.ReadBasic(output);
311 }
312 
313 template<>
Writing(const uint64_t & input,TLVObject & data)314 bool Writing(const uint64_t &input, TLVObject &data)
315 {
316     return data.WriteBasic(TAG_UINT64, input);
317 }
318 
319 template<>
Writing(const bool & input,TLVObject & data)320 bool Writing(const bool &input, TLVObject &data)
321 {
322     return data.WriteBasic(TAG_BOOL, input);
323 }
324 
325 template<>
Reading(bool & output,TLVObject & data)326 bool Reading(bool &output, TLVObject &data)
327 {
328     return data.ReadBasic(output);
329 }
330 
331 template<>
Writing(const std::string & input,TLVObject & data)332 bool Writing(const std::string &input, TLVObject &data)
333 {
334     return data.WriteString(input);
335 }
336 
337 template<>
Reading(std::string & output,TLVObject & data)338 bool Reading(std::string &output, TLVObject &data)
339 {
340     return data.ReadString(output);
341 }
342 
343 template<>
Writing(const std::vector<uint8_t> & input,TLVObject & data)344 bool Writing(const std::vector<uint8_t> &input, TLVObject &data)
345 {
346     return data.WriteVector(input);
347 }
348 
349 template<>
Reading(std::vector<uint8_t> & output,TLVObject & data)350 bool Reading(std::vector<uint8_t> &output, TLVObject &data)
351 {
352     return data.ReadVector(output);
353 }
354 
355 template<>
Writing(const UDVariant & input,TLVObject & data)356 bool Writing(const UDVariant &input, TLVObject &data)
357 {
358     return data.WriteVariant(input);
359 }
360 
361 template<>
Reading(UDVariant & output,TLVObject & data)362 bool Reading(UDVariant &output, TLVObject &data)
363 {
364     return data.ReadVariant(output);
365 }
366 
367 template<>
Writing(const UDDetails & input,TLVObject & data)368 bool Writing(const UDDetails &input, TLVObject &data)
369 {
370     return data.WriteMap(input);
371 }
372 
373 template<>
Reading(UDDetails & output,TLVObject & data)374 bool Reading(UDDetails &output, TLVObject &data)
375 {
376     return data.ReadMap(output);
377 }
378 
379 template<>
Writing(const UDType & input,TLVObject & data)380 bool Writing(const UDType &input, TLVObject &data)
381 {
382     int32_t type = input;
383     return Writing(type, data);
384 }
385 
386 template<>
Reading(UDType & output,TLVObject & data)387 bool Reading(UDType &output, TLVObject &data)
388 {
389     int32_t type;
390     if (!Reading(type, data)) {
391         return false;
392     }
393     if (type < UDType::TEXT || type >= UDType::UD_BUTT) {
394         return false;
395     }
396     output = static_cast<UDType>(type);
397     return true;
398 }
399 
400 template<>
Writing(const Text & input,TLVObject & data)401 bool Writing(const Text &input, TLVObject &data)
402 {
403     return Writing(input.GetDetails(), data);
404 }
405 
406 template<>
Reading(Text & output,TLVObject & data)407 bool Reading(Text &output, TLVObject &data)
408 {
409     UDDetails details;
410     if (!Reading(details, data)) {
411         return false;
412     }
413     output.SetDetails(details);
414     return true;
415 }
416 
417 template<>
Writing(const PlainText & input,TLVObject & data)418 bool Writing(const PlainText &input, TLVObject &data)
419 {
420     if (!Writing(input.GetContent(), data)) {
421         return false;
422     }
423     if (!Writing(input.GetAbstract(), data)) {
424         return false;
425     }
426     return true;
427 }
428 
429 template<>
Reading(PlainText & output,TLVObject & data)430 bool Reading(PlainText &output, TLVObject &data)
431 {
432     std::string content;
433     std::string abstract;
434     UDDetails details;
435     if (!Reading(content, data)) {
436         return false;
437     }
438     if (!Reading(abstract, data)) {
439         return false;
440     }
441     if (!Reading(details, data)) {
442         return false;
443     }
444     output.SetContent(content);
445     output.SetAbstract(abstract);
446     output.SetDetails(details);
447     return true;
448 }
449 
450 template<>
Writing(const Html & input,TLVObject & data)451 bool Writing(const Html &input, TLVObject &data)
452 {
453     if (!Writing(input.GetHtmlContent(), data)) {
454         return false;
455     }
456     if (!Writing(input.GetPlainContent(), data)) {
457         return false;
458     }
459     return true;
460 }
461 
462 template<>
Reading(Html & output,TLVObject & data)463 bool Reading(Html &output, TLVObject &data)
464 {
465     std::string htmlContent;
466     std::string plainContent;
467     UDDetails details;
468     if (!Reading(htmlContent, data)) {
469         return false;
470     }
471     if (!Reading(plainContent, data)) {
472         return false;
473     }
474     if (!Reading(details, data)) {
475         return false;
476     }
477     output.SetHtmlContent(htmlContent);
478     output.SetPlainContent(plainContent);
479     output.SetDetails(details);
480     return true;
481 }
482 
483 template<>
Writing(const Link & input,TLVObject & data)484 bool Writing(const Link &input, TLVObject &data)
485 {
486     if (!Writing(input.GetUrl(), data)) {
487         return false;
488     }
489     if (!Writing(input.GetDescription(), data)) {
490         return false;
491     }
492     return true;
493 }
494 
495 template<>
Reading(Link & output,TLVObject & data)496 bool Reading(Link &output, TLVObject &data)
497 {
498     std::string url;
499     std::string description;
500     UDDetails details;
501     if (!Reading(url, data)) {
502         return false;
503     }
504     if (!Reading(description, data)) {
505         return false;
506     }
507     if (!Reading(details, data)) {
508         return false;
509     }
510     output.SetUrl(url);
511     output.SetDescription(description);
512     output.SetDetails(details);
513     return true;
514 }
515 
516 template<>
Writing(const File & input,TLVObject & data)517 bool Writing(const File &input, TLVObject &data)
518 {
519     if (!Writing(input.GetUri(), data)) {
520         return false;
521     }
522     if (!Writing(input.GetRemoteUri(), data)) {
523         return false;
524     }
525     if (!Writing(input.GetDetails(), data)) {
526         return false;
527     }
528     return true;
529 }
530 
531 template<>
Reading(File & output,TLVObject & data)532 bool Reading(File &output, TLVObject &data)
533 {
534     std::string uri;
535     std::string remoteUri;
536     UDDetails details;
537     if (!Reading(uri, data)) {
538         return false;
539     }
540     if (!Reading(remoteUri, data)) {
541         return false;
542     }
543     if (!Reading(details, data)) {
544         return false;
545     }
546     output.SetUri(uri);
547     output.SetRemoteUri(remoteUri);
548     output.SetDetails(details);
549     return true;
550 }
551 
552 template<>
Writing(const Image & input,TLVObject & data)553 bool Writing(const Image &input, TLVObject &data)
554 {
555     return true;
556 }
557 
558 template<>
Reading(Image & output,TLVObject & data)559 bool Reading(Image &output, TLVObject &data)
560 {
561     std::string uri;
562     std::string remoteUri;
563     UDDetails details;
564     if (!Reading(uri, data)) {
565         return false;
566     }
567     if (!Reading(remoteUri, data)) {
568         return false;
569     }
570     if (!Reading(details, data)) {
571         return false;
572     }
573     output.SetUri(uri);
574     output.SetRemoteUri(remoteUri);
575     output.SetDetails(details);
576     return true;
577 }
578 
579 template<>
Writing(const Video & input,TLVObject & data)580 bool Writing(const Video &input, TLVObject &data)
581 {
582     return true;
583 }
584 
585 template<>
Reading(Video & output,TLVObject & data)586 bool Reading(Video &output, TLVObject &data)
587 {
588     std::string uri;
589     std::string remoteUri;
590     UDDetails details;
591     if (!Reading(uri, data)) {
592         return false;
593     }
594     if (!Reading(remoteUri, data)) {
595         return false;
596     }
597     if (!Reading(details, data)) {
598         return false;
599     }
600     output.SetUri(uri);
601     output.SetRemoteUri(remoteUri);
602     output.SetDetails(details);
603     return true;
604 }
605 
606 template<>
Writing(const Audio & input,TLVObject & data)607 bool Writing(const Audio &input, TLVObject &data)
608 {
609     return true;
610 }
611 
612 template<>
Reading(Audio & output,TLVObject & data)613 bool Reading(Audio &output, TLVObject &data)
614 {
615     std::string uri;
616     std::string remoteUri;
617     UDDetails details;
618     if (!Reading(uri, data)) {
619         return false;
620     }
621     if (!Reading(remoteUri, data)) {
622         return false;
623     }
624     if (!Reading(details, data)) {
625         return false;
626     }
627     output.SetUri(uri);
628     output.SetRemoteUri(remoteUri);
629     output.SetDetails(details);
630     return true;
631 }
632 
633 template<>
Writing(const Folder & input,TLVObject & data)634 bool Writing(const Folder &input, TLVObject &data)
635 {
636     return true;
637 }
638 
639 template<>
Reading(Folder & output,TLVObject & data)640 bool Reading(Folder &output, TLVObject &data)
641 {
642     std::string uri;
643     std::string remoteUri;
644     UDDetails details;
645     if (!Reading(uri, data)) {
646         return false;
647     }
648     if (!Reading(remoteUri, data)) {
649         return false;
650     }
651     if (!Reading(details, data)) {
652         return false;
653     }
654     output.SetUri(uri);
655     output.SetRemoteUri(remoteUri);
656     output.SetDetails(details);
657     return true;
658 }
659 
660 template<>
Writing(const SystemDefinedRecord & input,TLVObject & data)661 bool Writing(const SystemDefinedRecord &input, TLVObject &data)
662 {
663     if (!Writing(input.GetDetails(), data)) {
664         return false;
665     }
666     return true;
667 }
668 
669 template<>
Reading(SystemDefinedRecord & output,TLVObject & data)670 bool Reading(SystemDefinedRecord &output, TLVObject &data)
671 {
672     UDDetails details;
673     if (!Reading(details, data)) {
674         return false;
675     }
676     output.SetDetails(details);
677     return true;
678 }
679 
680 template<>
Writing(const SystemDefinedForm & input,TLVObject & data)681 bool Writing(const SystemDefinedForm &input, TLVObject &data)
682 {
683     if (!Writing(input.GetFormId(), data)) {
684         return false;
685     }
686     if (!Writing(input.GetFormName(), data)) {
687         return false;
688     }
689     if (!Writing(input.GetBundleName(), data)) {
690         return false;
691     }
692     if (!Writing(input.GetAbilityName(), data)) {
693         return false;
694     }
695     if (!Writing(input.GetModule(), data)) {
696         return false;
697     }
698     return true;
699 }
700 
701 template<>
Reading(SystemDefinedForm & output,TLVObject & data)702 bool Reading(SystemDefinedForm &output, TLVObject &data)
703 {
704     int32_t formId;
705     std::string formName;
706     std::string bundleName;
707     std::string abilityName;
708     std::string module;
709     UDDetails details;
710     if (!Reading(formId, data)) {
711         return false;
712     }
713     if (!Reading(formName, data)) {
714         return false;
715     }
716     if (!Reading(bundleName, data)) {
717         return false;
718     }
719     if (!Reading(abilityName, data)) {
720         return false;
721     }
722     if (!Reading(module, data)) {
723         return false;
724     }
725     if (!Reading(details, data)) {
726         return false;
727     }
728     output.SetFormId(formId);
729     output.SetFormName(formName);
730     output.SetBundleName(bundleName);
731     output.SetAbilityName(abilityName);
732     output.SetModule(module);
733     output.SetDetails(details);
734     return true;
735 }
736 
737 template<>
Writing(const SystemDefinedAppItem & input,TLVObject & data)738 bool Writing(const SystemDefinedAppItem &input, TLVObject &data)
739 {
740     if (!Writing(input.GetAppId(), data)) {
741         return false;
742     }
743     if (!Writing(input.GetAppName(), data)) {
744         return false;
745     }
746     if (!Writing(input.GetAppIconId(), data)) {
747         return false;
748     }
749     if (!Writing(input.GetAppLabelId(), data)) {
750         return false;
751     }
752     if (!Writing(input.GetBundleName(), data)) {
753         return false;
754     }
755     if (!Writing(input.GetAbilityName(), data)) {
756         return false;
757     }
758     return true;
759 }
760 
761 template<>
Reading(SystemDefinedAppItem & output,TLVObject & data)762 bool Reading(SystemDefinedAppItem &output, TLVObject &data)
763 {
764     std::string appId;
765     std::string appName;
766     std::string appIconId;
767     std::string appLabelId;
768     std::string bundleName;
769     std::string abilityName;
770     UDDetails details;
771     if (!Reading(appId, data)) {
772         return false;
773     }
774     if (!Reading(appName, data)) {
775         return false;
776     }
777     if (!Reading(appIconId, data)) {
778         return false;
779     }
780     if (!Reading(appLabelId, data)) {
781         return false;
782     }
783     if (!Reading(bundleName, data)) {
784         return false;
785     }
786     if (!Reading(abilityName, data)) {
787         return false;
788     }
789     if (!Reading(details, data)) {
790         return false;
791     }
792     output.SetAppId(appId);
793     output.SetAppName(appName);
794     output.SetAppIconId(appIconId);
795     output.SetAppLabelId(appLabelId);
796     output.SetBundleName(bundleName);
797     output.SetAbilityName(abilityName);
798     output.SetDetails(details);
799     return true;
800 }
801 
802 template<>
Writing(const SystemDefinedPixelMap & input,TLVObject & data)803 bool Writing(const SystemDefinedPixelMap &input, TLVObject &data)
804 {
805     if (!Writing(input.GetRawData(), data)) {
806         return false;
807     }
808     return true;
809 }
810 
811 template<>
Reading(SystemDefinedPixelMap & output,TLVObject & data)812 bool Reading(SystemDefinedPixelMap &output, TLVObject &data)
813 {
814     std::vector<uint8_t> rawData;
815     UDDetails details;
816     if (!Reading(rawData, data)) {
817         return false;
818     }
819     if (!Reading(details, data)) {
820         return false;
821     }
822     output.SetRawData(rawData);
823     output.SetDetails(details);
824     return true;
825 }
826 
827 template<>
Writing(const ApplicationDefinedRecord & input,TLVObject & data)828 bool Writing(const ApplicationDefinedRecord &input, TLVObject &data)
829 {
830     if (!Writing(input.GetApplicationDefinedType(), data)) {
831         return false;
832     }
833     if (!Writing(input.GetRawData(), data)) {
834         return false;
835     }
836     return true;
837 }
838 
839 template<>
Reading(ApplicationDefinedRecord & output,TLVObject & data)840 bool Reading(ApplicationDefinedRecord &output, TLVObject &data)
841 {
842     std::string type;
843     std::vector<uint8_t> rawData;
844     if (!Reading(type, data)) {
845         return false;
846     }
847     if (!Reading(rawData, data)) {
848         return false;
849     }
850     output.SetApplicationDefinedType(type);
851     output.SetRawData(rawData);
852     return true;
853 }
854 
855 template<>
Writing(const std::shared_ptr<UnifiedRecord> & input,TLVObject & data)856 bool Writing(const std::shared_ptr<UnifiedRecord> &input, TLVObject &data)
857 {
858     if (data.GetBuffer().size() == 0) {
859         if (!CountBufferSize(input, data)) {
860             return false;
861         }
862         data.UpdateSize();
863     }
864 
865     if (!Writing(input->GetType(), data)) {
866         return false;
867     }
868     if (!Writing(input->GetUid(), data)) {
869         return false;
870     }
871     auto type = input->GetType();
872     switch (type) {
873         case UDType::TEXT: {
874             auto text = static_cast<Text *>(input.get());
875             if (text == nullptr) {
876                 return false;
877             }
878             return Writing(*text, data);
879         }
880         case UDType::PLAIN_TEXT: {
881             auto plainText = static_cast<PlainText *>(input.get());
882             if (plainText == nullptr) {
883                 return false;
884             }
885             if (!Writing(*plainText, data)) {
886                 return false;
887             }
888             auto text = static_cast<Text *>(input.get());
889             if (text == nullptr) {
890                 return false;
891             }
892             return Writing(*text, data);
893         }
894         case UDType::HTML: {
895             auto html = static_cast<Html *>(input.get());
896             if (html == nullptr) {
897                 return false;
898             }
899             if (!Writing(*html, data)) {
900                 return false;
901             }
902             auto text = static_cast<Text *>(input.get());
903             if (text == nullptr) {
904                 return false;
905             }
906             return Writing(*text, data);
907         }
908         case UDType::HYPERLINK: {
909             auto link = static_cast<Link *>(input.get());
910             if (link == nullptr) {
911                 return false;
912             }
913             if (!Writing(*link, data)) {
914                 return false;
915             }
916             auto text = static_cast<Text *>(input.get());
917             if (text == nullptr) {
918                 return false;
919             }
920             return Writing(*text, data);
921         }
922         case UDType::FILE: {
923             auto file = static_cast<File *>(input.get());
924             if (file == nullptr) {
925                 return false;
926             }
927             return Writing(*file, data);
928         }
929         case UDType::IMAGE: {
930             auto image = static_cast<Image *>(input.get());
931             if (image == nullptr) {
932                 return false;
933             }
934             if (!Writing(*image, data)) {
935                 return false;
936             }
937             auto file = static_cast<File *>(input.get());
938             if (file == nullptr) {
939                 return false;
940             }
941             return Writing(*file, data);
942         }
943         case UDType::VIDEO: {
944             auto video = static_cast<Video *>(input.get());
945             if (video == nullptr) {
946                 return false;
947             }
948             if (!Writing(*video, data)) {
949                 return false;
950             }
951             auto file = static_cast<File *>(input.get());
952             if (file == nullptr) {
953                 return false;
954             }
955             return Writing(*file, data);
956         }
957         case UDType::AUDIO: {
958             auto audio = static_cast<Audio *>(input.get());
959             if (audio == nullptr) {
960                 return false;
961             }
962             if (!Writing(*audio, data)) {
963                 return false;
964             }
965             auto file = static_cast<File *>(input.get());
966             if (file == nullptr) {
967                 return false;
968             }
969             return Writing(*file, data);
970         }
971         case UDType::FOLDER: {
972             auto folder = static_cast<Folder *>(input.get());
973             if (folder == nullptr) {
974                 return false;
975             }
976             if (!Writing(*folder, data)) {
977                 return false;
978             }
979             auto file = static_cast<File *>(input.get());
980             if (file == nullptr) {
981                 return false;
982             }
983             return Writing(*file, data);
984         }
985         case UDType::SYSTEM_DEFINED_RECORD: {
986             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
987             if (sdRecord == nullptr) {
988                 return false;
989             }
990             return Writing(*sdRecord, data);
991         }
992         case UDType::SYSTEM_DEFINED_FORM: {
993             auto form = static_cast<SystemDefinedForm *>(input.get());
994             if (form == nullptr) {
995                 return false;
996             }
997             if (!Writing(*form, data)) {
998                 return false;
999             }
1000             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
1001             if (sdRecord == nullptr) {
1002                 return false;
1003             }
1004             return Writing(*sdRecord, data);
1005         }
1006         case UDType::SYSTEM_DEFINED_APP_ITEM: {
1007             auto appItem = static_cast<SystemDefinedAppItem *>(input.get());
1008             if (appItem == nullptr) {
1009                 return false;
1010             }
1011             if (!Writing(*appItem, data)) {
1012                 return false;
1013             }
1014             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
1015             if (sdRecord == nullptr) {
1016                 return false;
1017             }
1018             return Writing(*sdRecord, data);
1019         }
1020         case UDType::SYSTEM_DEFINED_PIXEL_MAP: {
1021             auto pixelMap = static_cast<SystemDefinedPixelMap *>(input.get());
1022             if (pixelMap == nullptr) {
1023                 return false;
1024             }
1025             if (!Writing(*pixelMap, data)) {
1026                 return false;
1027             }
1028             auto sdRecord = static_cast<SystemDefinedRecord *>(input.get());
1029             if (sdRecord == nullptr) {
1030                 return false;
1031             }
1032             return Writing(*sdRecord, data);
1033         }
1034         case UDType::APPLICATION_DEFINED_RECORD: {
1035             auto record = static_cast<ApplicationDefinedRecord *>(input.get());
1036             if (record == nullptr) {
1037                 return false;
1038             }
1039             return Writing(*record, data);
1040         }
1041         default: {
1042             return false;
1043         }
1044     }
1045 }
1046 
1047 template<>
Reading(std::shared_ptr<UnifiedRecord> & output,TLVObject & data)1048 bool Reading(std::shared_ptr<UnifiedRecord> &output, TLVObject &data)
1049 {
1050     UDType type ;
1051     if (!Reading(type, data)) {
1052         return false;
1053     }
1054     std::string uid;
1055     if (!Reading(uid, data)) {
1056         return false;
1057     }
1058     switch (type) {
1059         case UDType::TEXT: {
1060             std::shared_ptr<Text> text = std::make_shared<Text>();
1061             if (!Reading(*text, data)) {
1062                 return false;
1063             }
1064             output = text;
1065             break;
1066         }
1067         case UDType::PLAIN_TEXT: {
1068             std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>();
1069             if (!Reading(*plainText, data)) {
1070                 return false;
1071             }
1072             output = plainText;
1073             break;
1074         }
1075         case UDType::HTML: {
1076             std::shared_ptr<Html> html = std::make_shared<Html>();
1077             if (!Reading(*html, data)) {
1078                 return false;
1079             }
1080             output = html;
1081             break;
1082         }
1083         case UDType::HYPERLINK: {
1084             std::shared_ptr<Link> link = std::make_shared<Link>();
1085             if (!Reading(*link, data)) {
1086                 return false;
1087             }
1088             output = link;
1089             break;
1090         }
1091         case UDType::FILE: {
1092             std::shared_ptr<File> file = std::make_shared<File>();
1093             if (!Reading(*file, data)) {
1094                 return false;
1095             }
1096             output = file;
1097             break;
1098         }
1099         case UDType::IMAGE: {
1100             std::shared_ptr<Image> image = std::make_shared<Image>();
1101             if (!Reading(*image, data)) {
1102                 return false;
1103             }
1104             output = image;
1105             break;
1106         }
1107         case UDType::VIDEO: {
1108             std::shared_ptr<Video> video = std::make_shared<Video>();
1109             if (!Reading(*video, data)) {
1110                 return false;
1111             }
1112             output = video;
1113             break;
1114         }
1115         case UDType::AUDIO: {
1116             std::shared_ptr<Audio> audio = std::make_shared<Audio>();
1117             if (!Reading(*audio, data)) {
1118                 return false;
1119             }
1120             output = audio;
1121             break;
1122         }
1123         case UDType::FOLDER: {
1124             std::shared_ptr<Folder> folder = std::make_shared<Folder>();
1125             if (!Reading(*folder, data)) {
1126                 return false;
1127             }
1128             output = folder;
1129             break;
1130         }
1131         case UDType::SYSTEM_DEFINED_RECORD: {
1132             std::shared_ptr<SystemDefinedRecord> sdRecord = std::make_shared<SystemDefinedRecord>();
1133             if (!Reading(*sdRecord, data)) {
1134                 return false;
1135             }
1136             output = sdRecord;
1137             break;
1138         }
1139         case UDType::SYSTEM_DEFINED_FORM: {
1140             std::shared_ptr<SystemDefinedForm> form = std::make_shared<SystemDefinedForm>();
1141             if (!Reading(*form, data)) {
1142                 return false;
1143             }
1144             output = form;
1145             break;
1146         }
1147         case UDType::SYSTEM_DEFINED_APP_ITEM: {
1148             std::shared_ptr<SystemDefinedAppItem> appItem = std::make_shared<SystemDefinedAppItem>();
1149             if (!Reading(*appItem, data)) {
1150                 return false;
1151             }
1152             output = appItem;
1153             break;
1154         }
1155         case UDType::SYSTEM_DEFINED_PIXEL_MAP: {
1156             std::shared_ptr<SystemDefinedPixelMap> pixelMap = std::make_shared<SystemDefinedPixelMap>();
1157             if (!Reading(*pixelMap, data)) {
1158                 return false;
1159             }
1160             output = pixelMap;
1161             break;
1162         }
1163         case UDType::APPLICATION_DEFINED_RECORD: {
1164             std::shared_ptr<ApplicationDefinedRecord> record = std::make_shared<ApplicationDefinedRecord>();
1165             if (!Reading(*record, data)) {
1166                 return false;
1167             }
1168             output = record;
1169             break;
1170         }
1171         default: {
1172             return false;
1173         }
1174     }
1175     output->SetUid(uid);
1176     output->SetType(type);
1177     return true;
1178 }
1179 
1180 template<>
Writing(const UnifiedData & input,TLVObject & data)1181 bool Writing(const UnifiedData &input, TLVObject &data)
1182 {
1183     if (data.GetBuffer().size() == 0) {
1184         if (!CountBufferSize(input, data)) {
1185             return false;
1186         }
1187         data.UpdateSize();
1188     }
1189 
1190     int32_t size = static_cast<int32_t>(input.GetRecords().size());
1191     if (!Writing(size, data)) {
1192         return false;
1193     }
1194 
1195     for (auto record : input.GetRecords()) {
1196         if (!Writing(record, data)) {
1197             return false;
1198         }
1199     }
1200     return true;
1201 }
1202 
1203 template<>
Reading(UnifiedData & output,TLVObject & data)1204 bool Reading(UnifiedData &output, TLVObject &data)
1205 {
1206     int32_t size;
1207     if (!Reading(size, data)) {
1208         return false;
1209     }
1210     while (size-- > 0) {
1211         std::shared_ptr<UnifiedRecord> record;
1212         if (!Reading(record, data)) {
1213             return false;
1214         }
1215         output.AddRecord(record);
1216     }
1217     return true;
1218 }
1219 
1220 template<>
Writing(const std::vector<UnifiedData> & input,TLVObject & data)1221 bool Writing(const std::vector<UnifiedData> &input, TLVObject &data)
1222 {
1223     if (!CountBufferSize(input, data)) {
1224         return false;
1225     }
1226     data.UpdateSize();
1227 
1228     int32_t size = static_cast<int32_t>(input.size());
1229     if (!Writing(size, data)) {
1230         return false;
1231     }
1232 
1233     for (auto unifiedData : input) {
1234         if (!Writing(unifiedData, data)) {
1235             return false;
1236         }
1237     }
1238     return true;
1239 }
1240 
1241 template<>
Reading(std::vector<UnifiedData> & output,TLVObject & data)1242 bool Reading(std::vector<UnifiedData> &output, TLVObject &data)
1243 {
1244     int32_t size;
1245     if (!Reading(size, data)) {
1246         return false;
1247     }
1248     while (size-- > 0) {
1249         UnifiedData unifiedData;
1250         if (!Reading(unifiedData, data)) {
1251             return false;
1252         }
1253         output.push_back(unifiedData);
1254     }
1255     return true;
1256 }
1257 
1258 template<>
Writing(const UnifiedKey & input,TLVObject & data)1259 bool Writing(const UnifiedKey &input, TLVObject &data)
1260 {
1261     if (!Writing(input.key, data)) {
1262         return false;
1263     }
1264     if (!Writing(input.intention, data)) {
1265         return false;
1266     }
1267     if (!Writing(input.bundleName, data)) {
1268         return false;
1269     }
1270     if (!Writing(input.groupId, data)) {
1271         return false;
1272     }
1273     return true;
1274 }
1275 
1276 template<>
Reading(UnifiedKey & output,TLVObject & data)1277 bool Reading(UnifiedKey &output, TLVObject &data)
1278 {
1279     std::string key;
1280     std::string intention;
1281     std::string bundleName;
1282     std::string groupId;
1283     if (!Reading(key, data)) {
1284         return false;
1285     }
1286     if (!Reading(intention, data)) {
1287         return false;
1288     }
1289     if (!Reading(bundleName, data)) {
1290         return false;
1291     }
1292     if (!Reading(groupId, data)) {
1293         return false;
1294     }
1295     output.key = key;
1296     output.intention = intention;
1297     output.bundleName = bundleName;
1298     output.groupId = groupId;
1299     return true;
1300 }
1301 
1302 template<>
Writing(const Privilege & input,TLVObject & data)1303 bool Writing(const Privilege &input, TLVObject &data)
1304 {
1305     if (!Writing(input.tokenId, data)) {
1306         return false;
1307     }
1308     if (!Writing(input.readPermission, data)) {
1309         return false;
1310     }
1311     if (!Writing(input.writePermission, data)) {
1312         return false;
1313     }
1314     return true;
1315 }
1316 
1317 template<>
Reading(Privilege & output,TLVObject & data)1318 bool Reading(Privilege &output, TLVObject &data)
1319 {
1320     uint32_t tokenId;
1321     std::string readPermission;
1322     std::string writePermission;
1323     if (!Reading(tokenId, data)) {
1324         return false;
1325     }
1326     if (!Reading(readPermission, data)) {
1327         return false;
1328     }
1329     if (!Reading(writePermission, data)) {
1330         return false;
1331     }
1332     output.tokenId = tokenId;
1333     output.readPermission = readPermission;
1334     output.writePermission = writePermission;
1335     return true;
1336 }
1337 
1338 template<>
Writing(const DataStatus & input,TLVObject & data)1339 bool Writing(const DataStatus &input, TLVObject &data)
1340 {
1341     int32_t status = input;
1342     return Writing(status, data);
1343 }
1344 
1345 template<>
Reading(DataStatus & output,TLVObject & data)1346 bool Reading(DataStatus &output, TLVObject &data)
1347 {
1348     int32_t status;
1349     if (!Reading(status, data)) {
1350         return false;
1351     }
1352     if (status < DataStatus::WORKING || status >= DataStatus::FADE) {
1353         return false;
1354     }
1355     output = static_cast<DataStatus>(status);
1356     return true;
1357 }
1358 
1359 template<>
Writing(const Runtime & input,TLVObject & data)1360 bool Writing(const Runtime &input, TLVObject &data)
1361 {
1362     (void)CountBufferSize(input, data);
1363     data.UpdateSize();
1364     if (!Writing(input.key, data)) {
1365         return false;
1366     }
1367     if (!Writing(input.isPrivate, data)) {
1368         return false;
1369     }
1370     uint32_t size = static_cast<uint32_t>(input.privileges.size());
1371     if (!Writing(size, data)) {
1372         return false;
1373     }
1374     for (uint32_t i = 0; i < size; ++i) {
1375         if (!Writing(input.privileges[i], data)) {
1376             return false;
1377         }
1378     }
1379     if (!Writing(static_cast<int64_t>(input.createTime), data)) {
1380         return false;
1381     }
1382     if (!Writing(input.sourcePackage, data)) {
1383         return false;
1384     }
1385     if (!Writing(input.dataStatus, data)) {
1386         return false;
1387     }
1388     if (!Writing(input.dataVersion, data)) {
1389         return false;
1390     }
1391     if (!Writing(static_cast<int64_t>(input.lastModifiedTime), data)) {
1392         return false;
1393     }
1394     if (!Writing(input.createPackage, data)) {
1395         return false;
1396     }
1397     if (!Writing(input.deviceId, data)) {
1398         return false;
1399     }
1400     if (!Writing(input.recordTotalNum, data)) {
1401         return false;
1402     }
1403     return true;
1404 }
1405 
1406 template<>
Reading(Runtime & output,TLVObject & data)1407 bool Reading(Runtime &output, TLVObject &data)
1408 {
1409     UnifiedKey key;
1410     bool isPrivate;
1411     uint32_t size;
1412     std::vector<Privilege> privileges;
1413     int64_t createTime;
1414     std::string sourcePackage;
1415     DataStatus dataStatus;
1416     int32_t dataVersion;
1417     int64_t lastModifiedTime;
1418     std::string createPackage;
1419     std::string deviceId;
1420     uint32_t recordTotalNum;
1421     if (!Reading(key, data)) {
1422         return false;
1423     }
1424     if (!Reading(isPrivate, data)) {
1425         return false;
1426     }
1427     if (!Reading(size, data)) {
1428         return false;
1429     }
1430     for (uint32_t i = 0; i < size; ++i) {
1431         Privilege privilege;
1432         if (!Reading(privilege, data)) {
1433             return false;
1434         }
1435         privileges.emplace_back(privilege);
1436     }
1437     if (!Reading(createTime, data)) {
1438         return false;
1439     }
1440     if (!Reading(sourcePackage, data)) {
1441         return false;
1442     }
1443     if (!Reading(dataStatus, data)) {
1444         return false;
1445     }
1446     if (!Reading(dataVersion, data)) {
1447         return false;
1448     }
1449     if (!Reading(lastModifiedTime, data)) {
1450         return false;
1451     }
1452     if (!Reading(createPackage, data)) {
1453         return false;
1454     }
1455     if (!Reading(deviceId, data)) {
1456         return false;
1457     }
1458     if (!Reading(recordTotalNum, data)) {
1459         return false;
1460     }
1461     output.key = key;
1462     output.isPrivate = isPrivate;
1463     output.privileges = privileges;
1464     output.createTime = createTime;
1465     output.sourcePackage = sourcePackage;
1466     output.dataStatus = dataStatus;
1467     output.dataVersion = dataVersion;
1468     output.lastModifiedTime = lastModifiedTime;
1469     output.createPackage = createPackage;
1470     output.deviceId = deviceId;
1471     output.recordTotalNum = recordTotalNum;
1472     return true;
1473 }
1474 } // namespace TLVUtil
1475 } // namespace OHOS