1/*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions, and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32// FlatMessageIO.cpp
33// e.moon 6jul99
34
35#include "FlatMessageIO.h"
36//#include "xml_export_utils.h"
37
38#include "ExportContext.h"
39
40#include <Debug.h>
41
42// base64 encoding tools
43#include <E-mail.h>
44
45#include <cstdlib>
46#include <cstring>
47
48__USE_CORTEX_NAMESPACE
49
50// -------------------------------------------------------- //
51// *** constants
52// -------------------------------------------------------- //
53
54const char* const FlatMessageIO::s_element 				= "flat-BMessage";
55
56// -------------------------------------------------------- //
57// *** ctor/dtor/accessor
58// -------------------------------------------------------- //
59
60FlatMessageIO::~FlatMessageIO() {
61	if(m_ownMessage && m_message)
62		delete m_message;
63}
64FlatMessageIO::FlatMessageIO() :
65	m_ownMessage(true),
66	m_message(0) {}
67FlatMessageIO::FlatMessageIO(const BMessage* message) :
68	m_ownMessage(false),
69	m_message(const_cast<BMessage*>(message)) {}
70
71void FlatMessageIO::setMessage(BMessage* message) {
72	if(m_ownMessage && m_message)
73		delete m_message;
74	m_ownMessage = false;
75	m_message = message;
76}
77
78// -------------------------------------------------------- //
79// *** static setup method
80// -------------------------------------------------------- //
81
82// call this method to install hooks for the tags needed by
83// FlatMessageIO into the given document type
84/*static*/
85void FlatMessageIO::AddTo(XML::DocumentType* pDocType) {
86	pDocType->addMapping(new Mapping<FlatMessageIO>(s_element));
87}
88
89// -------------------------------------------------------- //
90// *** IPersistent impl.
91// -------------------------------------------------------- //
92
93
94void FlatMessageIO::xmlExportBegin(
95	ExportContext& context) const {
96
97	context.beginElement(s_element);
98}
99
100void FlatMessageIO::xmlExportAttributes(
101	ExportContext& context) const {
102
103	context.writeAttr("encoding", "base64");
104}
105
106void FlatMessageIO::xmlExportContent(
107	ExportContext& context) const {
108
109	context.beginContent();
110
111	// convert message to base64
112	ASSERT(m_message);
113	ssize_t flatSize = m_message->FlattenedSize();
114	ASSERT(flatSize);
115	char* flatData = new char[flatSize];
116	status_t err = m_message->Flatten(flatData, flatSize);
117	ASSERT(err == B_OK);
118
119	// make plenty of room for encoded content (encode_base64 adds newlines)
120	ssize_t base64Size = ((flatSize * 3) / 2);
121	char* base64Data = new char[base64Size];
122	ssize_t base64Used = encode_base64(base64Data, flatData, flatSize);
123	base64Data[base64Used] = '\0';
124
125	// write the data
126
127	const char* pos = base64Data;
128	while(*pos) {
129		ssize_t chunk = 0;
130		const char* nextBreak = strchr(pos, '\n');
131		if(!nextBreak)
132			chunk = strlen(pos);
133		else
134			chunk = nextBreak - pos;
135
136		context.writeString(context.indentString());
137		context.writeString(pos, chunk);
138		context.writeString("\n");
139
140		pos += chunk;
141		if(*pos == '\n')
142			++pos;
143	}
144
145	// clean up
146	delete [] flatData;
147	delete [] base64Data;
148}
149
150void FlatMessageIO::xmlExportEnd(
151	ExportContext& context) const {
152	context.endElement();
153}
154
155// -------------------------------------------------------- //
156
157void FlatMessageIO::xmlImportBegin(
158	ImportContext&		context) {
159
160	m_base64Data = "";
161}
162
163void FlatMessageIO::xmlImportAttribute(
164	const char*					key,
165	const char*					value,
166	ImportContext&		context) {
167	if(!strcmp(key, "encoding")) {
168		if(strcmp(value, "base64") != 0)
169			context.reportError("Unexpected value of 'encoding'.");
170	}
171}
172
173void FlatMessageIO::xmlImportContent(
174	const char*					data,
175	uint32						length,
176	ImportContext&		context) {
177	m_base64Data.Append(data, length);
178}
179
180void FlatMessageIO::xmlImportChild(
181	IPersistent*			child,
182	ImportContext&		context) {
183	delete child;
184	context.reportError("Unexpected child object.");
185}
186
187void FlatMessageIO::xmlImportComplete(
188	ImportContext&		context) {
189
190	if(m_ownMessage && m_message)
191		delete m_message;
192
193	// decode the message
194	ssize_t decodedSize = m_base64Data.Length();
195	char* decodedData = new char[decodedSize];
196	decodedSize = decode_base64(
197		decodedData, const_cast<char*>(m_base64Data.String()),
198		m_base64Data.Length(), false);
199
200	m_message = new BMessage();
201	m_ownMessage = true;
202
203	status_t err = m_message->Unflatten(decodedData);
204	if(err < B_OK) {
205		// decode failed; report error & clean up
206		BString error = "Unflatten(): ";
207		error << strerror(err);
208		context.reportError(error.String());
209
210		delete m_message;
211		m_message = 0;
212	}
213
214	m_base64Data = "";
215	delete [] decodedData;
216}
217
218// END -- FlatMessageIO.cpp --
219