private static void WriteApplefileHeader(List <MimeAppleTranscoder.EntryDescriptor> descriptors, bool isAppleDouble, Stream outStream) { int num = 26 + 12 * descriptors.Count; byte[] array = new byte[num]; int num2 = 0; MimeAppleTranscoder.WriteUIntData(isAppleDouble ? 333319 : 333312, array, ref num2); MimeAppleTranscoder.WriteUIntData(131072, array, ref num2); num2 += 16; MimeAppleTranscoder.WriteUShortData(descriptors.Count, array, ref num2); int num3 = num; foreach (MimeAppleTranscoder.EntryDescriptor entryDescriptor in descriptors) { MimeAppleTranscoder.WriteUIntData(entryDescriptor.EntryId, array, ref num2); MimeAppleTranscoder.WriteUIntData(num3, array, ref num2); MimeAppleTranscoder.WriteUIntData(entryDescriptor.EntryLength, array, ref num2); num3 += entryDescriptor.EntryLength; } outStream.Write(array, 0, num); }
private static void Transcode(Stream applefileStream, Stream dataForkStream, Stream outAttachMacInfo, Stream outMacBinStream, ref Stream outDataForkStream, out string fileName, out byte[] additionalInfo) { if (applefileStream.Length < 26L) { throw new MimeException(EmailMessageStrings.UnexpectedEndOfStream); } int num = MimeAppleTranscoder.ReadUIntFromStream(applefileStream); if (num != 333319 && num != 333312) { if (dataForkStream != null) { throw new MimeException(EmailMessageStrings.WrongAppleMagicNumber); } applefileStream.Position = 0L; MimeAppleTranscoder.MacBinToApplefile(applefileStream, outAttachMacInfo, out fileName, out additionalInfo); applefileStream.Position = 0L; if (outMacBinStream == null) { return; } MimeAppleTranscoder.CopyStreamData(applefileStream, outMacBinStream); return; } else { fileName = null; byte[] array = new byte[128]; array[0] = 0; int num2 = 0; if (dataForkStream != null) { num2 = 83; MimeAppleTranscoder.WriteIntData((int)dataForkStream.Length, array, ref num2); } byte[] bytes = MimeAppleTranscoder.macEncoding.GetBytes("mBIN"); for (int i = 0; i < 4; i++) { array[102 + i] = bytes[i]; } array[122] = 130; array[123] = 129; int inputOffset = 0; MimeAppleTranscoder.EntryDescriptor entryDescriptor = null; int inputOffset2 = 0; List <MimeAppleTranscoder.EntryDescriptor> list = MimeAppleTranscoder.ReadAppleFileHeaderEntries(applefileStream); foreach (MimeAppleTranscoder.EntryDescriptor entryDescriptor2 in list) { switch (entryDescriptor2.EntryId) { case 1: entryDescriptor = entryDescriptor2; num2 = 83; MimeAppleTranscoder.WriteIntData(entryDescriptor2.EntryLength, array, ref num2); break; case 2: inputOffset = entryDescriptor2.EntryOffset; num2 = 87; MimeAppleTranscoder.WriteIntData(entryDescriptor2.EntryLength, array, ref num2); break; case 4: inputOffset2 = entryDescriptor2.EntryOffset; num2 = 99; MimeAppleTranscoder.WriteShortData(entryDescriptor2.EntryLength, array, ref num2); break; } } if (entryDescriptor != null) { list.Remove(entryDescriptor); } if (outAttachMacInfo != null) { MimeAppleTranscoder.WriteApplefileHeader(list, true, outAttachMacInfo); } int num3 = (int)applefileStream.Position; foreach (MimeAppleTranscoder.EntryDescriptor entryDescriptor3 in list) { if (entryDescriptor3.EntryOffset < num3) { throw new MimeException(EmailMessageStrings.WrongOffsetsInApplefile); } if (entryDescriptor3.EntryOffset > num3) { if (applefileStream.Length < (long)entryDescriptor3.EntryOffset) { throw new MimeException(EmailMessageStrings.WrongOffsetsInApplefile); } applefileStream.Position = (long)entryDescriptor3.EntryOffset; } if (entryDescriptor3.EntryLength < 0) { throw new MimeException(EmailMessageStrings.EntryLengthTooBigInApplefile((long)((ulong)entryDescriptor3.EntryLength))); } int num4 = 0; byte[] array2 = null; int entryId = entryDescriptor3.EntryId; if (entryId != 3) { switch (entryId) { case 8: num4 = Math.Min(8, entryDescriptor3.EntryLength); array2 = new byte[num4]; MimeAppleTranscoder.ReadFixed(applefileStream, array2, 0, num4); for (int j = 0; j < num4; j++) { array[91 + j] = array2[j]; } break; case 9: num4 = Math.Min(28, entryDescriptor3.EntryLength); array2 = new byte[num4]; MimeAppleTranscoder.ReadFixed(applefileStream, array2, 0, num4); if (num4 >= 16) { for (int k = 0; k < 16; k++) { array[65 + k] = array2[k]; } array[101] = array[73]; array[73] = array[74]; array[74] = 0; if (num4 >= 22) { array[106] = array2[20]; array[107] = array2[21]; } } break; case 10: num4 = Math.Min(4, entryDescriptor3.EntryLength); array2 = new byte[num4]; MimeAppleTranscoder.ReadFixed(applefileStream, array2, 0, num4); if (num4 == 4) { array[81] = (byte)((array2[3] & 2) >> 1); } break; } } else { num4 = Math.Min(63, entryDescriptor3.EntryLength); array2 = new byte[num4]; MimeAppleTranscoder.ReadFixed(applefileStream, array2, 0, num4); fileName = MimeAppleTranscoder.macEncoding.GetString(array2, 0, num4); array[1] = (byte)num4; for (int l = 0; l < num4; l++) { array[2 + l] = array2[l]; } } if (outAttachMacInfo != null) { if (array2 != null) { outAttachMacInfo.Write(array2, 0, num4); } if (num4 != entryDescriptor3.EntryLength) { MimeAppleTranscoder.CopyStreamData(applefileStream, outAttachMacInfo, new int?(entryDescriptor3.EntryLength - num4)); } } num3 = (int)applefileStream.Position; } num2 = 124; MimeAppleTranscoder.WriteUShortData((int)MimeAppleTranscoder.CalcCRC16(array, 126), array, ref num2); MacBinaryHeader macBinaryHeader = new MacBinaryHeader(array); if (outMacBinStream == null) { additionalInfo = null; if (outDataForkStream != null) { ReadableDataStorageOnStream readableDataStorageOnStream = new ReadableDataStorageOnStream(applefileStream, false); if (entryDescriptor != null) { outDataForkStream = readableDataStorageOnStream.OpenReadStream((long)entryDescriptor.EntryOffset, (long)(entryDescriptor.EntryOffset + (int)macBinaryHeader.DataForkLength)); return; } outDataForkStream = readableDataStorageOnStream.OpenReadStream(0L, 0L); } return; } additionalInfo = MimeAppleTranscoder.EncodeAdditionalInfo(macBinaryHeader.FileCreator, macBinaryHeader.FileType); outMacBinStream.Write(array, 0, array.Length); if (macBinaryHeader.DataForkLength > 0L) { if (dataForkStream != null) { MimeAppleTranscoder.CopyStreamData(dataForkStream, outMacBinStream); MimeAppleTranscoder.WriteMacBinPadding(outMacBinStream, (int)macBinaryHeader.DataForkLength); } else if (entryDescriptor != null) { MimeAppleTranscoder.CopyDataWithPadding(applefileStream, outMacBinStream, entryDescriptor.EntryOffset, (int)macBinaryHeader.DataForkLength); } } if (macBinaryHeader.ResourceForkLength > 0L) { MimeAppleTranscoder.CopyDataWithPadding(applefileStream, outMacBinStream, inputOffset, (int)macBinaryHeader.ResourceForkLength); } if (macBinaryHeader.GetInfoLength > 0) { MimeAppleTranscoder.CopyDataWithPadding(applefileStream, outMacBinStream, inputOffset2, macBinaryHeader.GetInfoLength); } return; } }