1 /* 2 * Copyright 2019-2020, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #include "Captcha.h" 6 7 #include <DataIO.h> 8 9 #include "Logger.h" 10 11 // These are keys that are used to store this object's data into a BMessage 12 // instance. 13 14 #define KEY_TOKEN "token" 15 #define KEY_PNG_IMAGE_DATA "pngImageData" 16 17 18 Captcha::Captcha(BMessage* from) 19 : 20 fToken(""), 21 fPngImageData(NULL) 22 { 23 if (from->FindString(KEY_TOKEN, &fToken) != B_OK) { 24 HDERROR("expected key [%s] in the message data when creating a " 25 "captcha", KEY_TOKEN); 26 } 27 28 const void* data; 29 ssize_t len; 30 31 if (from->FindData(KEY_PNG_IMAGE_DATA, B_ANY_TYPE, &data, &len) != B_OK) 32 HDERROR("expected key [%s] in the message data", KEY_PNG_IMAGE_DATA); 33 else 34 SetPngImageData(data, len); 35 } 36 37 38 Captcha::Captcha() 39 : 40 fToken(""), 41 fPngImageData(NULL) 42 { 43 } 44 45 46 Captcha::~Captcha() 47 { 48 delete fPngImageData; 49 } 50 51 52 const BString& 53 Captcha::Token() const 54 { 55 return fToken; 56 } 57 58 59 BPositionIO* 60 Captcha::PngImageData() const 61 { 62 return fPngImageData; 63 } 64 65 66 void 67 Captcha::SetToken(const BString& value) 68 { 69 fToken = value; 70 } 71 72 73 void 74 Captcha::SetPngImageData(const void* data, size_t len) 75 { 76 if (fPngImageData != NULL) 77 delete fPngImageData; 78 fPngImageData = NULL; 79 if (data != NULL) { 80 fPngImageData = new BMallocIO(); 81 fPngImageData->Write(data, len); 82 } 83 } 84 85 86 status_t 87 Captcha::Archive(BMessage* into, bool deep) const 88 { 89 status_t result = B_OK; 90 if (result == B_OK && into == NULL) 91 result = B_ERROR; 92 if (result == B_OK) 93 result = into->AddString(KEY_TOKEN, fToken); 94 if (result == B_OK && fPngImageData != NULL) { 95 result = into->AddData(KEY_PNG_IMAGE_DATA, B_ANY_TYPE, 96 fPngImageData->Buffer(), fPngImageData->BufferLength()); 97 } 98 return result; 99 }