]>
git.saurik.com Git - apple/ld64.git/blob - src/ld/code-sign-blobs/blob.cpp
5b02f329a49fa5b1d85faeebf67f1aea17bd8436
2 * Copyright (c) 2006 Apple Computer, 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
34 // Content access and validation calls
36 char *BlobCore::stringAt(Offset offset
)
38 char *s
= at
<char>(offset
);
39 if (offset
< this->length() && memchr(s
, 0, this->length() - offset
))
45 const char *BlobCore::stringAt(Offset offset
) const
47 const char *s
= at
<const char>(offset
);
48 if (offset
< this->length() && memchr(s
, 0, this->length() - offset
))
56 // Read a blob from a standard file stream.
57 // Reads in one pass, so it's suitable for transmission over pipes and networks.
58 // The blob is allocated with malloc(3).
59 // On error, sets errno and returns NULL; in which case the input stream may
60 // be partially consumed.
62 BlobCore
*BlobCore::readBlob(int fd
, size_t offset
, uint32_t magic
, size_t minSize
, size_t maxSize
)
65 if (::pread(fd
, &header
, sizeof(header
), offset
) == sizeof(header
))
66 if (header
.validateBlob(magic
, minSize
, maxSize
))
67 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
68 memcpy(blob
, &header
, sizeof(header
));
69 size_t remainder
= header
.length() - sizeof(header
);
70 if (::pread(fd
, blob
+1, remainder
, offset
+ sizeof(header
)) == ssize_t(remainder
))
78 BlobCore
*BlobCore::readBlob(int fd
, uint32_t magic
, size_t minSize
, size_t maxSize
)
81 if (::read(fd
, &header
, sizeof(header
)) == sizeof(header
))
82 if (header
.validateBlob(magic
, minSize
, maxSize
))
83 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
84 memcpy(blob
, &header
, sizeof(header
));
85 size_t remainder
= header
.length() - sizeof(header
);
86 if (::read(fd
, blob
+1, remainder
) == ssize_t(remainder
))
94 BlobCore
*BlobCore::readBlob(std::FILE *file
, uint32_t magic
, size_t minSize
, size_t maxSize
)
97 if (::fread(&header
, sizeof(header
), 1, file
) == 1)
98 if (header
.validateBlob(magic
, minSize
, maxSize
))
99 if (BlobCore
*blob
= (BlobCore
*)malloc(header
.length())) {
100 memcpy(blob
, &header
, sizeof(header
));
101 if (::fread(blob
+1, header
.length() - sizeof(header
), 1, file
) == 1)
113 BlobWrapper
*BlobWrapper::alloc(size_t length
, Magic magic
/* = _magic */)
115 size_t wrapLength
= length
+ sizeof(BlobCore
);
116 BlobWrapper
*w
= (BlobWrapper
*)malloc(wrapLength
);
117 w
->BlobCore::initialize(magic
, wrapLength
);
121 BlobWrapper
*BlobWrapper::alloc(const void *data
, size_t length
, Magic magic
/* = _magic */)
123 BlobWrapper
*w
= alloc(length
, magic
);
124 memcpy(w
->data(), data
, w
->length());