]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_utilities/lib/blob.cpp
2 * Copyright (c) 2006,2011,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // blob - generic extensible binary blob frame
29 #include <security_utilities/unix++.h>
35 // Content access and validation calls
37 char *BlobCore::stringAt(Offset offset
)
39 char *s
= at
<char>(offset
);
40 if (offset
< this->length() && memchr(s
, 0, this->length() - offset
))
46 const char *BlobCore::stringAt(Offset offset
) const
48 const char *s
= at
<const char>(offset
);
49 if (offset
< this->length() && memchr(s
, 0, this->length() - offset
))
57 // Read a blob from a standard file stream.
58 // Reads in one pass, so it's suitable for transmission over pipes and networks.
59 // The blob is allocated with malloc(3).
60 // On error, sets errno and returns NULL; in which case the input stream may
61 // be partially consumed.
63 BlobCore
*BlobCore::readBlob(int fd
, size_t offset
, uint32_t magic
, size_t minSize
, size_t maxSize
)
66 if (::pread(fd
, &header
, sizeof(header
), offset
) == sizeof(header
))
67 if (header
.validateBlob(magic
, minSize
, maxSize
))
68 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
69 memcpy(blob
, &header
, sizeof(header
));
70 size_t remainder
= header
.length() - sizeof(header
);
71 if (::pread(fd
, blob
+1, remainder
, offset
+ sizeof(header
)) == ssize_t(remainder
))
79 BlobCore
*BlobCore::readBlob(int fd
, uint32_t magic
, size_t minSize
, size_t maxSize
)
82 if (::read(fd
, &header
, sizeof(header
)) == sizeof(header
))
83 if (header
.validateBlob(magic
, minSize
, maxSize
))
84 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
85 memcpy(blob
, &header
, sizeof(header
));
86 size_t remainder
= header
.length() - sizeof(header
);
87 if (::read(fd
, blob
+1, remainder
) == ssize_t(remainder
))
95 BlobCore
*BlobCore::readBlob(std::FILE *file
, uint32_t magic
, size_t minSize
, size_t maxSize
)
98 if (::fread(&header
, sizeof(header
), 1, file
) == 1)
99 if (header
.validateBlob(magic
, minSize
, maxSize
))
100 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
101 memcpy(blob
, &header
, sizeof(header
));
102 if (::fread(blob
+1, header
.length() - sizeof(header
), 1, file
) == 1)
114 BlobWrapper
*BlobWrapper::alloc(size_t length
, Magic magic
/* = _magic */)
116 size_t wrapLength
= length
+ sizeof(BlobCore
);
117 if (wrapLength
< length
) // overflow
119 BlobWrapper
*w
= (BlobWrapper
*)malloc(wrapLength
);
121 w
->BlobCore::initialize(magic
, wrapLength
);
125 BlobWrapper
*BlobWrapper::alloc(const void *data
, size_t length
, Magic magic
/* = _magic */)
127 BlobWrapper
*w
= alloc(length
, magic
);
129 memcpy(w
->data(), data
, w
->length());