BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / Key.cpp
blobffe5374c30e9c9c1cc1f7216c725612d84f53e52
1 /*
2 * Copyright 2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <Key.h>
9 #include <stdio.h>
12 #if 0
13 // TODO: move this to the KeyStore or the registrar backend if needed
14 static bool
15 CompareLists(BObjectList<BString> a, BObjectList<BString> b)
17 if (a.CountItems() != b.CountItems())
18 return false;
20 for (int32 i = 0; i < a.CountItems(); i++) {
21 if (*a.ItemAt(i) != *b.ItemAt(i))
22 return false;
25 return true;
27 #endif
30 // #pragma mark - Generic BKey
33 BKey::BKey()
35 Unset();
39 BKey::BKey(BKeyPurpose purpose, const char* identifier,
40 const char* secondaryIdentifier, const uint8* data, size_t length)
42 SetTo(purpose, identifier, secondaryIdentifier, data, length);
46 BKey::BKey(BKey& other)
48 *this = other;
52 BKey::~BKey()
57 void
58 BKey::Unset()
60 SetTo(B_KEY_PURPOSE_GENERIC, "", "", NULL, 0);
64 status_t
65 BKey::SetTo(BKeyPurpose purpose, const char* identifier,
66 const char* secondaryIdentifier, const uint8* data, size_t length)
68 fCreationTime = 0;
69 SetPurpose(purpose);
70 SetIdentifier(identifier);
71 SetSecondaryIdentifier(secondaryIdentifier);
72 return SetData(data, length);
76 void
77 BKey::SetPurpose(BKeyPurpose purpose)
79 fPurpose = purpose;
83 BKeyPurpose
84 BKey::Purpose() const
86 return fPurpose;
90 void
91 BKey::SetIdentifier(const char* identifier)
93 fIdentifier = identifier;
97 const char*
98 BKey::Identifier() const
100 return fIdentifier.String();
104 void
105 BKey::SetSecondaryIdentifier(const char* identifier)
107 fSecondaryIdentifier = identifier;
111 const char*
112 BKey::SecondaryIdentifier() const
114 return fSecondaryIdentifier.String();
118 status_t
119 BKey::SetData(const uint8* data, size_t length)
121 fData.SetSize(0);
122 ssize_t bytesWritten = fData.WriteAt(0, data, length);
123 if (bytesWritten < 0)
124 return (status_t)bytesWritten;
126 return (size_t)bytesWritten == length ? B_OK : B_NO_MEMORY;
130 size_t
131 BKey::DataLength() const
133 return fData.BufferLength();
137 const uint8*
138 BKey::Data() const
140 return (const uint8*)fData.Buffer();
144 status_t
145 BKey::GetData(uint8* buffer, size_t bufferSize) const
147 ssize_t bytesRead = fData.ReadAt(0, buffer, bufferSize);
148 if (bytesRead < 0)
149 return (status_t)bytesRead;
151 return B_OK;
156 const char*
157 BKey::Owner() const
159 return fOwner.String();
163 bigtime_t
164 BKey::CreationTime() const
166 return fCreationTime;
170 status_t
171 BKey::Flatten(BMessage& message) const
173 if (message.MakeEmpty() != B_OK
174 || message.AddUInt32("type", Type()) != B_OK
175 || message.AddUInt32("purpose", fPurpose) != B_OK
176 || message.AddString("identifier", fIdentifier) != B_OK
177 || message.AddString("secondaryIdentifier", fSecondaryIdentifier)
178 != B_OK
179 || message.AddString("owner", fOwner) != B_OK
180 || message.AddInt64("creationTime", fCreationTime) != B_OK
181 || message.AddData("data", B_RAW_TYPE, fData.Buffer(),
182 fData.BufferLength()) != B_OK) {
183 return B_ERROR;
186 return B_OK;
190 status_t
191 BKey::Unflatten(const BMessage& message)
193 BKeyType type;
194 if (message.FindUInt32("type", (uint32*)&type) != B_OK || type != Type())
195 return B_BAD_VALUE;
197 const void* data = NULL;
198 ssize_t dataLength = 0;
199 if (message.FindUInt32("purpose", (uint32*)&fPurpose) != B_OK
200 || message.FindString("identifier", &fIdentifier) != B_OK
201 || message.FindString("secondaryIdentifier", &fSecondaryIdentifier)
202 != B_OK
203 || message.FindString("owner", &fOwner) != B_OK
204 || message.FindInt64("creationTime", &fCreationTime) != B_OK
205 || message.FindData("data", B_RAW_TYPE, &data, &dataLength) != B_OK
206 || dataLength < 0) {
207 return B_ERROR;
210 return SetData((const uint8*)data, (size_t)dataLength);
214 BKey&
215 BKey::operator=(const BKey& other)
217 SetPurpose(other.Purpose());
218 SetData((const uint8*)other.Data(), other.DataLength());
220 fIdentifier = other.fIdentifier;
221 fSecondaryIdentifier = other.fSecondaryIdentifier;
222 fOwner = other.fOwner;
223 fCreationTime = other.fCreationTime;
225 return *this;
229 bool
230 BKey::operator==(const BKey& other) const
232 return Type() == other.Type()
233 && DataLength() == other.DataLength()
234 && Purpose() == other.Purpose()
235 && fOwner == other.fOwner
236 && fIdentifier == other.fIdentifier
237 && fSecondaryIdentifier == other.fSecondaryIdentifier
238 && memcmp(Data(), other.Data(), DataLength()) == 0;
242 bool
243 BKey::operator!=(const BKey& other) const
245 return !(*this == other);
249 void
250 BKey::PrintToStream()
252 if (Type() == B_KEY_TYPE_GENERIC)
253 printf("generic key:\n");
255 const char* purposeString = "unknown";
256 switch (fPurpose) {
257 case B_KEY_PURPOSE_ANY:
258 purposeString = "any";
259 break;
260 case B_KEY_PURPOSE_GENERIC:
261 purposeString = "generic";
262 break;
263 case B_KEY_PURPOSE_KEYRING:
264 purposeString = "keyring";
265 break;
266 case B_KEY_PURPOSE_WEB:
267 purposeString = "web";
268 break;
269 case B_KEY_PURPOSE_NETWORK:
270 purposeString = "network";
271 break;
272 case B_KEY_PURPOSE_VOLUME:
273 purposeString = "volume";
274 break;
277 printf("\tpurpose: %s\n", purposeString);
278 printf("\tidentifier: \"%s\"\n", fIdentifier.String());
279 printf("\tsecondary identifier: \"%s\"\n", fSecondaryIdentifier.String());
280 printf("\towner: \"%s\"\n", fOwner.String());
281 printf("\tcreation time: %" B_PRIu64 "\n", fCreationTime);
282 printf("\traw data length: %" B_PRIuSIZE "\n", fData.BufferLength());
286 // #pragma mark - BPasswordKey
289 BPasswordKey::BPasswordKey()
294 BPasswordKey::BPasswordKey(const char* password, BKeyPurpose purpose,
295 const char* identifier, const char* secondaryIdentifier)
297 BKey(purpose, identifier, secondaryIdentifier, (const uint8*)password,
298 strlen(password) + 1)
303 BPasswordKey::BPasswordKey(BPasswordKey& other)
308 BPasswordKey::~BPasswordKey()
313 status_t
314 BPasswordKey::SetTo(const char* password, BKeyPurpose purpose,
315 const char* identifier, const char* secondaryIdentifier)
317 return BKey::SetTo(purpose, identifier, secondaryIdentifier,
318 (const uint8*)password, strlen(password) + 1);
322 status_t
323 BPasswordKey::SetPassword(const char* password)
325 return SetData((const uint8*)password, strlen(password) + 1);
329 const char*
330 BPasswordKey::Password() const
332 return (const char*)Data();
336 void
337 BPasswordKey::PrintToStream()
339 printf("password key:\n");
340 BKey::PrintToStream();
341 printf("\tpassword: \"%s\"\n", Password());