1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsID.h"
#include "nsCRT.h"
#include "nscore.h"
#include "msgCore.h"
#include "nsMsgAppleDouble.h"
#include "nsMsgAppleCodes.h"
#include "nsFileSpec.h"
#include "nsMsgCompUtils.h"
#ifdef XP_MAC
#pragma warn_unusedarg off
#include "m_cvstrm.h"
#pragma cplusplus on
void
DecodingDone( appledouble_decode_object* p_ap_decode_obj )
{
FSSpec fspec;
fspec.vRefNum = p_ap_decode_obj->vRefNum;
fspec.parID = p_ap_decode_obj->dirId;
fspec.name[0] = nsCRT::strlen(p_ap_decode_obj->fname);
strcpy((char*)fspec.name+1, p_ap_decode_obj->fname);
CMimeMapper * mapper = CPrefs::sMimeTypes.FindMimeType(fspec);
if( mapper && (mapper->GetLoadAction() == CMimeMapper::Launch ) )
{
LFileBufferStream file( fspec );
LaunchFile( &file );
}
}
#pragma cplusplus reset
#endif /* the ifdef of XP_MAC */
/*
** The initial of the apple double decoder.
**
** Set up the next output stream based on the input.
*/
int ap_decode_init(
appledouble_decode_object* p_ap_decode_obj,
PRBool is_apple_single,
PRBool write_as_binhex,
void *closure)
{
memset(p_ap_decode_obj, 0, sizeof(appledouble_decode_object));
/* presume first buff starts a line */
p_ap_decode_obj->uu_starts_line = TRUE;
if (write_as_binhex)
{
p_ap_decode_obj->write_as_binhex = TRUE;
p_ap_decode_obj->binhex_stream = (NET_StreamClass*)closure;
p_ap_decode_obj->data_size = 0;
}
else
{
p_ap_decode_obj->write_as_binhex = FALSE;
p_ap_decode_obj->binhex_stream = NULL;
p_ap_decode_obj->context = (MWContext*)closure;
}
p_ap_decode_obj->is_apple_single = is_apple_single;
if (is_apple_single)
{
p_ap_decode_obj->encoding = kEncodeNone;
}
return NOERR;
}
static int ap_decode_state_machine(appledouble_decode_object* p_ap_decode_obj);
/*
* process the buffer
*/
int ap_decode_next(
appledouble_decode_object* p_ap_decode_obj,
char *in_buff,
PRInt32 buff_size)
{
/*
** install the buff to the decoder.
*/
p_ap_decode_obj->inbuff = in_buff;
p_ap_decode_obj->s_inbuff = buff_size;
p_ap_decode_obj->pos_inbuff = 0;
/*
** run off the decode state machine
*/
return ap_decode_state_machine(p_ap_decode_obj);
}
PRIVATE int ap_decode_state_machine(
appledouble_decode_object* p_ap_decode_obj)
{
int status = NOERR;
PRInt32 size;
switch (p_ap_decode_obj->state)
{
case kInit:
/*
** Make sure that there are stuff in the buff
** before we can parse the file head .
*/
if (p_ap_decode_obj->s_inbuff <=1 )
return NOERR;
if (p_ap_decode_obj->is_apple_single)
{
p_ap_decode_obj->state = kBeginHeaderPortion;
}
else
{
status = ap_seek_part_start(p_ap_decode_obj);
if (status != errDone)
return status;
p_ap_decode_obj->state = kBeginParseHeader;
}
status = ap_decode_state_machine(p_ap_decode_obj);
break;
case kBeginSeekBoundary:
p_ap_decode_obj->state = kSeekingBoundary;
status = ap_seek_to_boundary(p_ap_decode_obj, TRUE);
if (status == errDone)
{
p_ap_decode_obj->state = kBeginParseHeader;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kSeekingBoundary:
status = ap_seek_to_boundary(p_ap_decode_obj, FALSE);
if (status == errDone)
{
p_ap_decode_obj->state = kBeginParseHeader;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginParseHeader:
p_ap_decode_obj->state = kParsingHeader;
status = ap_parse_header(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->which_part == kDataPortion)
p_ap_decode_obj->state = kBeginDataPortion;
else if (p_ap_decode_obj->which_part == kHeaderPortion)
p_ap_decode_obj->state = kBeginHeaderPortion;
else
p_ap_decode_obj->state = kFinishing;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kParsingHeader:
status = ap_parse_header(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->which_part == kDataPortion)
p_ap_decode_obj->state = kBeginDataPortion;
else if (p_ap_decode_obj->which_part == kHeaderPortion)
p_ap_decode_obj->state = kBeginHeaderPortion;
else
p_ap_decode_obj->state = kFinishing;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginHeaderPortion:
p_ap_decode_obj->state = kProcessingHeaderPortion;
status = ap_decode_process_header(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kBeginDataPortion;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kProcessingHeaderPortion:
status = ap_decode_process_header(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kBeginDataPortion;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kBeginDataPortion:
p_ap_decode_obj->state = kProcessingDataPortion;
status = ap_decode_process_data(p_ap_decode_obj, TRUE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kFinishing;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kProcessingDataPortion:
status = ap_decode_process_data(p_ap_decode_obj, FALSE);
if (status == errDone)
{
if (p_ap_decode_obj->is_apple_single)
p_ap_decode_obj->state = kFinishing;
else
p_ap_decode_obj->state = kBeginSeekBoundary;
status = ap_decode_state_machine(p_ap_decode_obj);
}
break;
case kFinishing:
if (p_ap_decode_obj->write_as_binhex)
{
if (p_ap_decode_obj->tmpfd)
{
/*
** It is time to append the data fork to bin hex encoder.
**
** The reason behind this dirt work is resource fork is the last
** piece in the binhex, while it is the first piece in apple double.
*/
p_ap_decode_obj->tmpFileStream->seek(PR_SEEK_SET, 0);
while (p_ap_decode_obj->data_size > 0)
{
char buff[1024];
size = PR_MIN(1024, p_ap_decode_obj->data_size);
p_ap_decode_obj->tmpFileStream->read(buff, size);
status = (*p_ap_decode_obj->binhex_stream->put_block)
(p_ap_decode_obj->binhex_stream->data_object,
buff,
size);
p_ap_decode_obj->data_size -= size;
}
}
if (p_ap_decode_obj->data_size <= 0)
{
/* CALL put_block with size == 0 to close a part. */
status = (*p_ap_decode_obj->binhex_stream->put_block)
(p_ap_decode_obj->binhex_stream->data_object,
NULL,
0);
if (status != NOERR)
break;
/* and now we are really done. */
status = errDone;
}
else
status = NOERR;
}
break;
}
return (status == errEOB) ? NOERR : status;
}
int ap_decode_end(
appledouble_decode_object* p_ap_decode_obj,
PRBool is_aborting)
{
/*
** clear up the apple doubler object.
*/
if (p_ap_decode_obj == NULL)
return NOERR;
PR_FREEIF(p_ap_decode_obj->boundary0);
#ifdef XP_MAC
if (p_ap_decode_obj->fileId)
FSClose(p_ap_decode_obj->fileId);
if( p_ap_decode_obj->vRefNum )
FlushVol(nil, p_ap_decode_obj->vRefNum );
#endif
if (p_ap_decode_obj->write_as_binhex)
{
/*
** make sure close the binhex stream too.
*/
if (is_aborting)
{
(*p_ap_decode_obj->binhex_stream->abort)
(p_ap_decode_obj->binhex_stream->data_object, 0);
}
else
{
(*p_ap_decode_obj->binhex_stream->complete)
(p_ap_decode_obj->binhex_stream->data_object);
}
if (p_ap_decode_obj->tmpFileStream)
p_ap_decode_obj->tmpFileStream->close();
if (p_ap_decode_obj->tmpFileSpec)
{
p_ap_decode_obj->tmpFileSpec->Delete(PR_FALSE); /* remove tmp file if we used it */
delete p_ap_decode_obj->tmpFileSpec;
}
}
else if (p_ap_decode_obj->fileStream)
{
p_ap_decode_obj->fileStream->close();
}
#ifdef XP_MAC
if( !is_aborting )
DecodingDone( p_ap_decode_obj);
#endif
return NOERR;
}
|