1. Adjust might be 64bit, so we need to typecast it to UINT32 firstly.
[mirror_edk2.git] / Tools / Source / FrameworkTasks / org / tianocore / framework / tasks / CompressSection.java
CommitLineData
878ddf1f 1/** @file\r
2 CompressSection class.\r
3\r
4 CompressSection indicate that all section which in it should be compressed.\r
5 \r
6 Copyright (c) 2006, Intel Corporation\r
7 All rights reserved. This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11 \r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15 **/\r
16\r
17\r
18package org.tianocore.framework.tasks;\r
19\r
20import java.io.DataInputStream;\r
21import java.io.DataOutputStream;\r
22import java.io.File;\r
23import java.io.FileInputStream;\r
24import java.io.FileOutputStream;\r
25import java.util.ArrayList;\r
26import java.util.Iterator;\r
27import java.util.List;\r
28\r
29import org.apache.tools.ant.BuildException;\r
30\r
31\r
32/**\r
33 CompressSection\r
34 \r
35 CompressSection indicate that all section which in it should be compressed. \r
36 \r
37**/\r
38public class CompressSection implements Section, FfsTypes{\r
39 //\r
40 // The attribute of compressName.\r
41 //\r
42 String compressName = "";\r
43 //\r
44 // The list contained the SectFile element.\r
45 //\r
46 List<Object> SectList = new ArrayList<Object>();\r
47\r
48 /**\r
49 toBuffer\r
50 \r
51 This function is to collect all sectFile and compress it , then output\r
52 the result to buffer.\r
53 \r
54 @param Buffer The point of output buffer\r
55 \r
56 **/\r
2da8968b 57 public void toBuffer (DataOutputStream buffer, DataOutputStream orgBuffer){\r
878ddf1f 58 \r
59 Section sect;\r
60 File compressOut;\r
61 \r
62 //\r
63 // Get section file in compress node.\r
64 //\r
65 try{\r
66 compressOut = new File ("Compress.temp");\r
67 FileOutputStream fo = new FileOutputStream (compressOut.getName());\r
68 DataOutputStream Do = new DataOutputStream (fo);\r
69 \r
70 //\r
71 // Get each section which under the compress {};\r
72 // And add it is contains to File;\r
73 //\r
74 Iterator SectionIter = SectList.iterator();\r
75 while (SectionIter.hasNext()){\r
76 sect = (Section)SectionIter.next();\r
77 \r
78 //\r
79 // Call each section class's toBuffer function.\r
80 //\r
81 try {\r
2da8968b 82 sect.toBuffer(Do, orgBuffer);\r
878ddf1f 83 }\r
84 catch (BuildException e) {\r
85 System.out.print(e.getMessage());\r
86 throw new BuildException ("Compress.toBuffer failed at section");\r
87 } \r
88 \r
89 }\r
90 Do.close(); \r
91 \r
92 //\r
93 // Get contain to Buffer\r
94 //\r
95 FileInputStream fi = new FileInputStream (compressOut.getName());\r
96 DataInputStream di = new DataInputStream (fi);\r
97 byte[] fileBuffer = new byte[(int)compressOut.length()];\r
98 di.read(fileBuffer);\r
99 \r
100 //\r
101 // Call compress\r
102 //\r
103 Compress myCompress = new Compress(fileBuffer, fileBuffer.length); \r
104 \r
105 //\r
106 // Add Compress header\r
107 //\r
108 CompressHeader Ch = new CompressHeader();\r
109 Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +\r
110 Ch.GetSize()) &\r
111 0xff\r
112 );\r
113 Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length + \r
114 Ch.GetSize())&\r
115 0xff00) >> 8\r
116 );\r
117 Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length + \r
118 Ch.GetSize()) & \r
119 0xff0000) >> 16\r
120 );\r
121 Ch.SectionHeader.type = (byte) EFI_SECTION_COMPRESSION;\r
122 \r
123 //\r
2da8968b 124 // Note: The compressName was not efsfective now. Using the\r
878ddf1f 125 // EFI_STANDARD_COMPRSSION for compressType .\r
126 // That is follow old Genffsfile tools. Some code will be added for \r
127 // the different compressName;\r
128 //\r
129 Ch.UncompressLen = fileBuffer.length;\r
130 Ch.CompressType = EFI_STANDARD_COMPRESSION; \r
131 \r
132 //\r
133 // Change header struct to byte buffer\r
134 //\r
135 byte [] headerBuffer = new byte[Ch.GetSize()];\r
136 Ch.StructToBuffer(headerBuffer);\r
137 \r
138 //\r
139 // First add CompressHeader to Buffer, then add Compress data.\r
140 //\r
2da8968b 141 buffer.write (headerBuffer);\r
142 buffer.write(myCompress.outputBuffer); \r
878ddf1f 143 \r
144 //\r
2da8968b 145 // Buffer 4 Byte aligment \r
878ddf1f 146 //\r
147 int size = Ch.GetSize() + myCompress.outputBuffer.length;\r
148 \r
149 while ((size & 0x03) != 0){\r
150 size ++;\r
2da8968b 151 buffer.writeByte(0);\r
152 }\r
153 //\r
154 // orgBuffer 4 Byte aligment\r
155 //\r
156 size = (int)compressOut.length();\r
157 while ((size & 0x03) != 0){\r
158 size ++;\r
159 orgBuffer.writeByte(0);\r
878ddf1f 160 }\r
878ddf1f 161 //\r
162 // Delete temp file\r
163 //\r
164 di.close();\r
165 compressOut.delete();\r
166 \r
167 }\r
168 catch (Exception e){\r
169 throw new BuildException("compress.toBuffer failed!\n");\r
170 } \r
171 }\r
172\r
173 /**\r
174 getCompressName\r
175 \r
176 This function is to get compressName.\r
177 \r
178 @return The compressName.\r
179 **/\r
180 public String getCompressName() {\r
181 return compressName;\r
182 }\r
183\r
184 /**\r
185 setCompressName\r
186 \r
187 This function is to set compressName.\r
188 \r
189 @param compressName The string of compressName\r
190 **/\r
191 public void setCompressName(String compressName) {\r
192 this.compressName = compressName;\r
193 }\r
194\r
195 /**\r
196 addSectFile\r
197 \r
198 This function is to add sectFile element to SectList.\r
199 \r
200 @param sectFile SectFile element which succeed from section class.\r
201 **/\r
202 public void addSectFile (SectFile sectFile) {\r
203 SectList.add(sectFile);\r
204 \r
205 } \r
206 \r
207 /**\r
208 addTool\r
209 \r
210 This function is to add tool element to SectList.\r
211 @param tool Tool element which succeed from section class.\r
212 **/\r
213 public void addTool (Tool tool) {\r
214 SectList.add(tool);\r
215 }\r
216}