Debian Codegraph @

src/af/xap/xp/xap_Resource.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Program Utilities
4 *
5 * Copyright (C) 2002 Francis James Franklin <[email protected]>
6 * Copyright (C) 2002 AbiSource, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA.
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "ut_assert.h"
28 #include "ut_debugmsg.h"
29 #include "ut_base64.h"
30
31 #include "xap_Resource.h"
32
33 XAP_Resource::XAP_Resource (const char * resource_name, bool resource_internal) :
34 bInternal(resource_internal),
35 Description(""),
36 m_resource_name(resource_name),
37 m_ref_count(1)
38 {
39 //
40 }
41
42 XAP_InternalResource::XAP_InternalResource (const char * resource_id) :
43 XAP_Resource(resource_id,true),
44 m_buffer(0),
45 m_buffer_length(0),
46 m_content_type("")
47 {
48 //
49 }
50
51 XAP_InternalResource::~XAP_InternalResource ()
52 {
53 clear ();
54 }
55
56 void XAP_InternalResource::clear ()
57 {
58 DELETEPV(m_buffer);
59 m_buffer_length = 0;
60 }
61
62 const char * XAP_InternalResource::buffer (const char * new_buffer, UT_uint32 new_buffer_length, bool base64_encoded)
63 {
64 clear ();
65
66 if ((new_buffer == 0) || (new_buffer_length == 0)) return 0;
67
68 UT_uint32 buffer_length = new_buffer_length;
69 if (base64_encoded) buffer_length -= buffer_length >> 2;
70
71 try
72 {
73 m_buffer = new char[buffer_length];
74 }
75 catch (...)
76 {
77 m_buffer = 0;
78 }
79 if (m_buffer == 0) return m_buffer;
80
81 if (!base64_encoded)
82 {
83 memcpy (m_buffer, new_buffer, buffer_length);
84 m_buffer_length = buffer_length;
85 return m_buffer;
86 }
87
88 const char * b64ptr = new_buffer;
89 char * binptr = m_buffer;
90
91 size_t b64len = new_buffer_length;
92 size_t binlen = buffer_length;
93
94 if (UT_UTF8_Base64Decode (binptr, binlen, b64ptr, b64len))
95 {
96 m_buffer_length = buffer_length - binlen;
97 }
98 else clear ();
99
100 return m_buffer;
101 }
102
103 UT_Error XAP_InternalResource::write_base64 (void * context, Writer & writer) // call's writer's write_base64() callback
104 {
105 UT_Error err = UT_OK;
106
107 char b64buf[73];
108
109 const char * binptr = m_buffer;
110 UT_uint32 buffer_length = m_buffer_length;
111
112 while (buffer_length >= 54)
113 {
114 char * b64ptr = b64buf;
115
116 size_t binlen = 54;
117 size_t b64len = 72;
118
119 if (!UT_UTF8_Base64Encode (b64ptr, b64len, binptr, binlen))
120 {
121 err = UT_ERROR;
122 break;
123 }
124 buffer_length -= 54;
125
126 b64buf[72] = 0;
127
128 err = writer.write_base64 (context, b64buf, 72, (buffer_length == 0));
129 if (err != UT_OK) break;
130 }
131 if (err != UT_OK) return err;
132
133 if (buffer_length)
134 {
135 char * b64ptr = b64buf;
136
137 size_t binlen = buffer_length;
138 size_t b64len = 72;
139
140 if (!UT_UTF8_Base64Encode (b64ptr, b64len, binptr, binlen))
141 {
142 err = UT_ERROR;
143 }
144 else
145 {
146 b64buf[72-b64len] = 0;
147 err = writer.write_base64 (context, b64buf, 72-b64len, true);
148 }
149 }
150 return err;
151 }
152
153 const UT_UTF8String & XAP_InternalResource::type (const UT_UTF8String & new_content_type)
154 {
155 m_content_type = new_content_type;
156 return m_content_type;
157 }
158
159 const UT_UTF8String & XAP_InternalResource::type (const char * new_content_type)
160 {
161 if (new_content_type)
162 m_content_type = new_content_type;
163 else
164 m_content_type = "";
165
166 return m_content_type;
167 }
168
169 XAP_ExternalResource::XAP_ExternalResource (const char * resource_id) :
170 XAP_Resource(resource_id,false),
171 m_url("")
172 {
173 //
174 }
175
176 XAP_ExternalResource::~XAP_ExternalResource ()
177 {
178 //
179 }
180
181 const UT_UTF8String & XAP_ExternalResource::URL (const UT_UTF8String & url)
182 {
183 m_url = url;
184 return m_url;
185 }