2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-07 07:09:11 +03:00
|
|
|
|
|
|
|
#include "nsDecodeAppleFile.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
|
2013-07-19 06:24:15 +04:00
|
|
|
NS_IMPL_ADDREF(nsDecodeAppleFile)
|
|
|
|
NS_IMPL_RELEASE(nsDecodeAppleFile)
|
2001-02-07 07:09:11 +03:00
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDecodeAppleFile)
|
2001-02-07 07:09:11 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIOutputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIOutputStream)
|
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
nsDecodeAppleFile::nsDecodeAppleFile()
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
m_state = parseHeaders;
|
|
|
|
m_dataBufferLength = 0;
|
|
|
|
m_dataBuffer = (unsigned char*) PR_MALLOC(MAX_BUFFERSIZE);
|
2012-07-30 18:20:58 +04:00
|
|
|
m_entries = nullptr;
|
2001-02-07 07:09:11 +03:00
|
|
|
m_rfRefNum = -1;
|
|
|
|
m_totalDataForkWritten = 0;
|
|
|
|
m_totalResourceForkWritten = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
m_headerOk = false;
|
2001-02-07 07:09:11 +03:00
|
|
|
|
|
|
|
m_comment[0] = 0;
|
2002-01-30 05:55:57 +03:00
|
|
|
memset(&m_dates, 0, sizeof(m_dates));
|
|
|
|
memset(&m_finderInfo, 0, sizeof(m_dates));
|
|
|
|
memset(&m_finderExtraInfo, 0, sizeof(m_dates));
|
2001-02-07 07:09:11 +03:00
|
|
|
}
|
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
nsDecodeAppleFile::~nsDecodeAppleFile()
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
PR_FREEIF(m_dataBuffer);
|
|
|
|
if (m_entries)
|
|
|
|
delete [] m_entries;
|
|
|
|
}
|
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::Initialize(nsIOutputStream *output, nsIFile *file)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
m_output = output;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFileMac> macFile = do_QueryInterface(file);
|
|
|
|
macFile->GetTargetFSSpec(&m_fsFileSpec);
|
|
|
|
|
|
|
|
m_offset = 0;
|
|
|
|
m_dataForkOffset = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::Close(void)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = m_output->Close();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2001-02-07 07:09:11 +03:00
|
|
|
|
|
|
|
if (m_rfRefNum != -1)
|
|
|
|
FSClose(m_rfRefNum);
|
|
|
|
|
|
|
|
/* Check if the file is complete and if it's the case, write file attributes */
|
|
|
|
if (m_headerOk)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool dataOk = true; /* It's ok if the file doesn't have a datafork, therefore set it to true by default. */
|
2001-02-07 07:09:11 +03:00
|
|
|
if (m_headers.magic == APPLESINGLE_MAGIC)
|
|
|
|
{
|
|
|
|
for (i = 0; i < m_headers.entriesCount; i ++)
|
|
|
|
if (ENT_DFORK == m_entries[i].id)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
dataOk = (bool)(m_totalDataForkWritten == m_entries[i].length);
|
2001-02-07 07:09:11 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool resourceOk = FALSE;
|
2001-02-07 07:09:11 +03:00
|
|
|
for (i = 0; i < m_headers.entriesCount; i ++)
|
|
|
|
if (ENT_RFORK == m_entries[i].id)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
resourceOk = (bool)(m_totalResourceForkWritten == m_entries[i].length);
|
2001-02-07 07:09:11 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataOk && resourceOk)
|
|
|
|
{
|
|
|
|
HFileInfo *fpb;
|
|
|
|
CInfoPBRec cipbr;
|
|
|
|
|
|
|
|
fpb = (HFileInfo *) &cipbr;
|
|
|
|
fpb->ioVRefNum = m_fsFileSpec.vRefNum;
|
|
|
|
fpb->ioDirID = m_fsFileSpec.parID;
|
|
|
|
fpb->ioNamePtr = m_fsFileSpec.name;
|
|
|
|
fpb->ioFDirIndex = 0;
|
|
|
|
PBGetCatInfoSync(&cipbr);
|
|
|
|
|
|
|
|
/* set finder info */
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&fpb->ioFlFndrInfo, &m_finderInfo, sizeof (FInfo));
|
|
|
|
memcpy(&fpb->ioFlXFndrInfo, &m_finderExtraInfo, sizeof (FXInfo));
|
2001-02-07 07:09:11 +03:00
|
|
|
fpb->ioFlFndrInfo.fdFlags &= 0xfc00; /* clear flags maintained by finder */
|
|
|
|
|
|
|
|
/* set file dates */
|
|
|
|
fpb->ioFlCrDat = m_dates.create - CONVERT_TIME;
|
|
|
|
fpb->ioFlMdDat = m_dates.modify - CONVERT_TIME;
|
|
|
|
fpb->ioFlBkDat = m_dates.backup - CONVERT_TIME;
|
|
|
|
|
|
|
|
/* update file info */
|
|
|
|
fpb->ioDirID = fpb->ioFlParID;
|
|
|
|
PBSetCatInfoSync(&cipbr);
|
|
|
|
|
|
|
|
/* set comment */
|
|
|
|
IOParam vinfo;
|
|
|
|
GetVolParmsInfoBuffer vp;
|
|
|
|
DTPBRec dtp;
|
|
|
|
|
2002-01-30 05:55:57 +03:00
|
|
|
memset((void *) &vinfo, 0, sizeof (vinfo));
|
2001-02-07 07:09:11 +03:00
|
|
|
vinfo.ioVRefNum = fpb->ioVRefNum;
|
|
|
|
vinfo.ioBuffer = (Ptr) &vp;
|
|
|
|
vinfo.ioReqCount = sizeof (vp);
|
|
|
|
if (PBHGetVolParmsSync((HParmBlkPtr) &vinfo) == noErr && ((vp.vMAttrib >> bHasDesktopMgr) & 1))
|
|
|
|
{
|
2002-01-30 05:55:57 +03:00
|
|
|
memset((void *) &dtp, 0, sizeof (dtp));
|
2001-02-07 07:09:11 +03:00
|
|
|
dtp.ioVRefNum = fpb->ioVRefNum;
|
|
|
|
if (PBDTGetPath(&dtp) == noErr)
|
|
|
|
{
|
|
|
|
dtp.ioDTBuffer = (Ptr) &m_comment[1];
|
|
|
|
dtp.ioNamePtr = fpb->ioNamePtr;
|
|
|
|
dtp.ioDirID = fpb->ioDirID;
|
|
|
|
dtp.ioDTReqCount = m_comment[0];
|
|
|
|
if (PBDTSetCommentSync(&dtp) == noErr)
|
|
|
|
PBDTFlushSync(&dtp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-07 09:44:07 +03:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::Flush(void)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
return m_output->Flush();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::WriteFrom(nsIInputStream *inStr, uint32_t count, uint32_t *_retval)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
return m_output->WriteFrom(inStr, count, _retval);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::WriteSegments(nsReadSegmentFun reader, void * closure, uint32_t count, uint32_t *_retval)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
return m_output->WriteSegments(reader, closure, count, _retval);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::IsNonBlocking(bool *aNonBlocking)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
2002-03-12 03:59:06 +03:00
|
|
|
return m_output->IsNonBlocking(aNonBlocking);
|
2001-02-07 07:09:11 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsDecodeAppleFile::Write(const char *buffer, uint32_t bufferSize, uint32_t* writeCount)
|
2001-02-07 07:09:11 +03:00
|
|
|
{
|
|
|
|
/* WARNING: to simplify my life, I presume that I should get all appledouble headers in the first block,
|
|
|
|
else I would have to implement a buffer */
|
|
|
|
|
|
|
|
const char * buffPtr = buffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataCount;
|
|
|
|
int32_t i;
|
2001-02-07 07:09:11 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
*writeCount = 0;
|
|
|
|
|
|
|
|
while (bufferSize > 0 && NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
switch (m_state)
|
|
|
|
{
|
|
|
|
case parseHeaders :
|
|
|
|
dataCount = sizeof(ap_header) - m_dataBufferLength;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_dataBuffer[m_dataBufferLength], buffPtr, dataCount);
|
2001-02-07 07:09:11 +03:00
|
|
|
m_dataBufferLength += dataCount;
|
|
|
|
|
|
|
|
if (m_dataBufferLength == sizeof(ap_header))
|
|
|
|
{
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_headers, m_dataBuffer, sizeof(ap_header));
|
2001-02-07 07:09:11 +03:00
|
|
|
|
|
|
|
/* Check header to be sure we are dealing with the right kind of data, else just write it to the data fork. */
|
|
|
|
if ((m_headers.magic == APPLEDOUBLE_MAGIC || m_headers.magic == APPLESINGLE_MAGIC) &&
|
|
|
|
m_headers.version == VERSION && m_headers.entriesCount)
|
|
|
|
{
|
|
|
|
/* Just to be sure, the filler must contains only 0 */
|
|
|
|
for (i = 0; i < 4 && m_headers.fill[i] == 0L; i ++)
|
|
|
|
;
|
|
|
|
if (i == 4)
|
|
|
|
m_state = parseEntries;
|
|
|
|
}
|
|
|
|
m_dataBufferLength = 0;
|
|
|
|
|
|
|
|
if (m_state == parseHeaders)
|
|
|
|
{
|
|
|
|
dataCount = 0;
|
|
|
|
m_state = parseWriteThrough;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseEntries :
|
|
|
|
if (!m_entries)
|
|
|
|
{
|
|
|
|
m_entries = new ap_entry[m_headers.entriesCount];
|
|
|
|
if (!m_entries)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t entriesSize = sizeof(ap_entry) * m_headers.entriesCount;
|
2001-02-07 07:09:11 +03:00
|
|
|
dataCount = entriesSize - m_dataBufferLength;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_dataBuffer[m_dataBufferLength], buffPtr, dataCount);
|
2001-02-07 07:09:11 +03:00
|
|
|
m_dataBufferLength += dataCount;
|
|
|
|
|
|
|
|
if (m_dataBufferLength == entriesSize)
|
|
|
|
{
|
|
|
|
for (i = 0; i < m_headers.entriesCount; i ++)
|
|
|
|
{
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_entries[i], &m_dataBuffer[i * sizeof(ap_entry)], sizeof(ap_entry));
|
2001-02-07 07:09:11 +03:00
|
|
|
if (m_headers.magic == APPLEDOUBLE_MAGIC)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t offset = m_entries[i].offset + m_entries[i].length;
|
2001-02-07 07:09:11 +03:00
|
|
|
if (offset > m_dataForkOffset)
|
|
|
|
m_dataForkOffset = offset;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
m_headerOk = true;
|
2001-02-07 07:09:11 +03:00
|
|
|
m_state = parseLookupPart;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseLookupPart :
|
|
|
|
/* which part are we parsing? */
|
|
|
|
m_currentPartID = -1;
|
|
|
|
for (i = 0; i < m_headers.entriesCount; i ++)
|
|
|
|
if (m_offset == m_entries[i].offset && m_entries[i].length)
|
|
|
|
{
|
|
|
|
m_currentPartID = m_entries[i].id;
|
|
|
|
m_currentPartLength = m_entries[i].length;
|
|
|
|
m_currentPartCount = 0;
|
|
|
|
|
|
|
|
switch (m_currentPartID)
|
|
|
|
{
|
|
|
|
case ENT_DFORK : m_state = parseDataFork; break;
|
|
|
|
case ENT_RFORK : m_state = parseResourceFork; break;
|
|
|
|
|
|
|
|
case ENT_COMMENT :
|
|
|
|
case ENT_DATES :
|
|
|
|
case ENT_FINFO :
|
|
|
|
m_dataBufferLength = 0;
|
|
|
|
m_state = parsePart;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default : m_state = parseSkipPart; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_currentPartID == -1)
|
|
|
|
{
|
|
|
|
/* maybe is the datafork of an appledouble file? */
|
|
|
|
if (m_offset == m_dataForkOffset)
|
|
|
|
{
|
|
|
|
m_currentPartID = ENT_DFORK;
|
|
|
|
m_currentPartLength = -1;
|
|
|
|
m_currentPartCount = 0;
|
|
|
|
m_state = parseDataFork;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dataCount = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parsePart :
|
|
|
|
dataCount = m_currentPartLength - m_dataBufferLength;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_dataBuffer[m_dataBufferLength], buffPtr, dataCount);
|
2001-02-07 07:09:11 +03:00
|
|
|
m_dataBufferLength += dataCount;
|
|
|
|
|
|
|
|
if (m_dataBufferLength == m_currentPartLength)
|
|
|
|
{
|
|
|
|
switch (m_currentPartID)
|
|
|
|
{
|
|
|
|
case ENT_COMMENT :
|
|
|
|
m_comment[0] = m_currentPartLength > 255 ? 255 : m_currentPartLength;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_comment[1], buffPtr, m_comment[0]);
|
2001-02-07 07:09:11 +03:00
|
|
|
break;
|
|
|
|
case ENT_DATES :
|
|
|
|
if (m_currentPartLength == sizeof(m_dates))
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_dates, buffPtr, m_currentPartLength);
|
2001-02-07 07:09:11 +03:00
|
|
|
break;
|
|
|
|
case ENT_FINFO :
|
|
|
|
if (m_currentPartLength == (sizeof(m_finderInfo) + sizeof(m_finderExtraInfo)))
|
|
|
|
{
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&m_finderInfo, buffPtr, sizeof(m_finderInfo));
|
|
|
|
memcpy(&m_finderExtraInfo, buffPtr + sizeof(m_finderInfo), sizeof(m_finderExtraInfo));
|
2001-02-07 07:09:11 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_state = parseLookupPart;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseSkipPart :
|
|
|
|
dataCount = m_currentPartLength - m_currentPartCount;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
|
|
|
else
|
|
|
|
m_state = parseLookupPart;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseDataFork :
|
|
|
|
if (m_headers.magic == APPLEDOUBLE_MAGIC)
|
|
|
|
dataCount = bufferSize;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dataCount = m_currentPartLength - m_currentPartCount;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
|
|
|
else
|
|
|
|
m_state = parseLookupPart;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_output)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t writeCount;
|
2001-02-07 07:09:11 +03:00
|
|
|
rv = m_output->Write((const char *)buffPtr, dataCount, &writeCount);
|
|
|
|
if (dataCount != writeCount)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
m_totalDataForkWritten += dataCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseResourceFork :
|
|
|
|
dataCount = m_currentPartLength - m_currentPartCount;
|
|
|
|
if (dataCount > bufferSize)
|
|
|
|
dataCount = bufferSize;
|
|
|
|
else
|
|
|
|
m_state = parseLookupPart;
|
|
|
|
|
|
|
|
if (m_rfRefNum == -1)
|
|
|
|
{
|
|
|
|
if (noErr != FSpOpenRF(&m_fsFileSpec, fsWrPerm, &m_rfRefNum))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
long count = dataCount;
|
|
|
|
if (noErr != FSWrite(m_rfRefNum, &count, buffPtr) || count != dataCount)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
m_totalResourceForkWritten += dataCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case parseWriteThrough :
|
|
|
|
dataCount = bufferSize;
|
|
|
|
if (m_output)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t writeCount;
|
2001-02-07 07:09:11 +03:00
|
|
|
rv = m_output->Write((const char *)buffPtr, dataCount, &writeCount);
|
|
|
|
if (dataCount != writeCount)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataCount)
|
|
|
|
{
|
|
|
|
*writeCount += dataCount;
|
|
|
|
bufferSize -= dataCount;
|
|
|
|
buffPtr += dataCount;
|
|
|
|
m_currentPartCount += dataCount;
|
|
|
|
m_offset += dataCount;
|
|
|
|
dataCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|