1 //===-- CFCData.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CFCData.h"
10 
11 //----------------------------------------------------------------------
12 // CFCData constructor
13 //----------------------------------------------------------------------
14 CFCData::CFCData(CFDataRef data) : CFCReleaser<CFDataRef>(data) {}
15 
16 //----------------------------------------------------------------------
17 // CFCData copy constructor
18 //----------------------------------------------------------------------
19 CFCData::CFCData(const CFCData &rhs) : CFCReleaser<CFDataRef>(rhs) {}
20 
21 //----------------------------------------------------------------------
22 // CFCData copy constructor
23 //----------------------------------------------------------------------
24 CFCData &CFCData::operator=(const CFCData &rhs)
25 
26 {
27   if (this != &rhs)
28     *this = rhs;
29   return *this;
30 }
31 
32 //----------------------------------------------------------------------
33 // Destructor
34 //----------------------------------------------------------------------
35 CFCData::~CFCData() {}
36 
37 CFIndex CFCData::GetLength() const {
38   CFDataRef data = get();
39   if (data)
40     return CFDataGetLength(data);
41   return 0;
42 }
43 
44 const uint8_t *CFCData::GetBytePtr() const {
45   CFDataRef data = get();
46   if (data)
47     return CFDataGetBytePtr(data);
48   return NULL;
49 }
50 
51 CFDataRef CFCData::Serialize(CFPropertyListRef plist,
52                              CFPropertyListFormat format) {
53   CFAllocatorRef alloc = kCFAllocatorDefault;
54   reset();
55   CFCReleaser<CFWriteStreamRef> stream(
56       ::CFWriteStreamCreateWithAllocatedBuffers(alloc, alloc));
57   ::CFWriteStreamOpen(stream.get());
58   CFIndex len =
59       ::CFPropertyListWriteToStream(plist, stream.get(), format, NULL);
60   if (len > 0)
61     reset((CFDataRef)::CFWriteStreamCopyProperty(stream.get(),
62                                                  kCFStreamPropertyDataWritten));
63   ::CFWriteStreamClose(stream.get());
64   return get();
65 }
66