|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Collections.Generic; |
|
19 using System.Text; |
|
20 using System.IO; |
|
21 using SymbianUtils.Strings; |
|
22 using SymbianUtils.Streams; |
|
23 using SymbianImageLib.Common.Header; |
|
24 using SymbianImageLib.Common.Image; |
|
25 using SymbianStructuresLib.Compression.Common; |
|
26 using SymbianImageLib.ROFS.Structures; |
|
27 using SymbianImageLib.ROFS.Header.Types; |
|
28 |
|
29 namespace SymbianImageLib.ROFS.Header |
|
30 { |
|
31 internal abstract class SIHeaderROF : SIHeader |
|
32 { |
|
33 #region Static constructor |
|
34 public static SIHeaderROF New( SIImage aImage, Stream aStream ) |
|
35 { |
|
36 byte[] signature = new byte[ 4 ]; |
|
37 int readResult = aStream.Read( signature, 0, signature.Length ); |
|
38 if ( readResult != 4 ) |
|
39 { |
|
40 throw new Exception( "Unable to read ROF signature" ); |
|
41 } |
|
42 |
|
43 // Put us back where we were |
|
44 aStream.Seek( -signature.Length, SeekOrigin.Current ); |
|
45 |
|
46 // Convert signature to string and compare against known types. |
|
47 string headerText = StringParsingUtils.BytesToString( signature ); |
|
48 aImage.Trace( "[SymbianImageHeaderROF] New() - headerText: {0}", headerText ); |
|
49 // |
|
50 SIHeaderROF ret = null; |
|
51 switch ( headerText ) |
|
52 { |
|
53 case "ROFX": |
|
54 ret = new SIHeaderROFS( aImage, aStream ); |
|
55 break; |
|
56 case "ROFS": |
|
57 ret = new SIHeaderROFX( aImage, aStream ); |
|
58 break; |
|
59 default: |
|
60 throw new NotSupportedException( "Unsupported ROF type" ); |
|
61 } |
|
62 // |
|
63 return ret; |
|
64 } |
|
65 #endregion |
|
66 |
|
67 #region Constructors |
|
68 protected SIHeaderROF( SIImage aImage, Stream aStream ) |
|
69 : base( aImage ) |
|
70 { |
|
71 // Skip over identifier |
|
72 aStream.Seek( 4, SeekOrigin.Begin ); |
|
73 |
|
74 // Read header size and then put us back at the start of the stream |
|
75 // ready to read the entire header. |
|
76 int headerSize = aStream.ReadByte(); |
|
77 aStream.Seek( 0, SeekOrigin.Begin ); |
|
78 // |
|
79 iHeaderData = new byte[ headerSize ]; |
|
80 aStream.Read( iHeaderData, 0, iHeaderData.Length ); |
|
81 // |
|
82 ReadHeaderData( iHeaderData ); |
|
83 } |
|
84 #endregion |
|
85 |
|
86 #region API |
|
87 public static bool IsROFS( Stream aStream ) |
|
88 { |
|
89 using ( SymbianStreamReaderLE reader = SymbianStreamReaderLE.New( aStream, SymbianStreamReaderLE.TCloseOperation.EResetPosition ) ) |
|
90 { |
|
91 string signature = reader.ReadString( 4 ); |
|
92 bool ret = ( signature == KImageROFS || signature == KImageROFX ); |
|
93 return ret; |
|
94 } |
|
95 } |
|
96 #endregion |
|
97 |
|
98 #region Constants |
|
99 public const string KImageROFS = "ROFS"; |
|
100 public const string KImageROFX = "ROFX"; |
|
101 #endregion |
|
102 |
|
103 #region From SymbianImageHeader |
|
104 public override TSymbianCompressionType CompressionType |
|
105 { |
|
106 get |
|
107 { |
|
108 // ROFS image itself is not compressed - however, the individual ROFS entries |
|
109 // themselves are likely compressed. |
|
110 return TSymbianCompressionType.ENone; |
|
111 } |
|
112 } |
|
113 |
|
114 public override uint HeaderSize |
|
115 { |
|
116 get { return iRofsHdr.Size; } |
|
117 } |
|
118 #endregion |
|
119 |
|
120 #region API |
|
121 #endregion |
|
122 |
|
123 #region Properties |
|
124 internal TRofsHeader InternalHeader |
|
125 { |
|
126 get { return iRofsHdr; } |
|
127 } |
|
128 #endregion |
|
129 |
|
130 #region Internal methods |
|
131 private void ReadHeaderData( byte[] aBuffer ) |
|
132 { |
|
133 using ( MemoryStream stream = new MemoryStream( aBuffer ) ) |
|
134 { |
|
135 using ( BinaryReader reader = new BinaryReader( stream ) ) |
|
136 { |
|
137 iRofsHdr.Read( reader ); |
|
138 } |
|
139 } |
|
140 } |
|
141 #endregion |
|
142 |
|
143 #region Data members |
|
144 private TRofsHeader iRofsHdr = new TRofsHeader(); |
|
145 private readonly byte[] iHeaderData; |
|
146 #endregion |
|
147 } |
|
148 } |