]> git.saurik.com Git - apple/ld64.git/blob - src/ld/code-sign-blobs/blob.cpp
5b02f329a49fa5b1d85faeebf67f1aea17bd8436
[apple/ld64.git] / src / ld / code-sign-blobs / blob.cpp
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // blob - generic extensible binary blob frame
27 //
28 #include "blob.h"
29
30 namespace Security {
31
32
33 //
34 // Content access and validation calls
35 //
36 char *BlobCore::stringAt(Offset offset)
37 {
38 char *s = at<char>(offset);
39 if (offset < this->length() && memchr(s, 0, this->length() - offset))
40 return s;
41 else
42 return NULL;
43 }
44
45 const char *BlobCore::stringAt(Offset offset) const
46 {
47 const char *s = at<const char>(offset);
48 if (offset < this->length() && memchr(s, 0, this->length() - offset))
49 return s;
50 else
51 return NULL;
52 }
53
54
55 //
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.
61 //
62 BlobCore *BlobCore::readBlob(int fd, size_t offset, uint32_t magic, size_t minSize, size_t maxSize)
63 {
64 BlobCore header;
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))
71 return blob;
72 free(blob);
73 errno = EINVAL;
74 }
75 return NULL;
76 }
77
78 BlobCore *BlobCore::readBlob(int fd, uint32_t magic, size_t minSize, size_t maxSize)
79 {
80 BlobCore header;
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))
87 return blob;
88 free(blob);
89 errno = EINVAL;
90 }
91 return NULL;
92 }
93
94 BlobCore *BlobCore::readBlob(std::FILE *file, uint32_t magic, size_t minSize, size_t maxSize)
95 {
96 BlobCore header;
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)
102 return blob;
103 free(blob);
104 errno = EINVAL;
105 }
106 return NULL;
107 }
108
109
110 //
111 // BlobWrappers
112 //
113 BlobWrapper *BlobWrapper::alloc(size_t length, Magic magic /* = _magic */)
114 {
115 size_t wrapLength = length + sizeof(BlobCore);
116 BlobWrapper *w = (BlobWrapper *)malloc(wrapLength);
117 w->BlobCore::initialize(magic, wrapLength);
118 return w;
119 }
120
121 BlobWrapper *BlobWrapper::alloc(const void *data, size_t length, Magic magic /* = _magic */)
122 {
123 BlobWrapper *w = alloc(length, magic);
124 memcpy(w->data(), data, w->length());
125 return w;
126 }
127
128
129 } // Security