summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkTasks/org/tianocore/framework/tasks/CompressSection.java
blob: 3e2a98f695c8f6cbbba8e3f4e31423c33b779b84 (plain)
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
/** @file
 CompressSection class.

 CompressSection indicate that all section which in it should be compressed.
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 **/


package org.tianocore.framework.tasks;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.tools.ant.BuildException;


/**
  CompressSection
  
  CompressSection indicate that all section which in it should be compressed. 
 
**/
public class CompressSection implements Section, FfsTypes {
    private int alignment = 0;
    //
    // The attribute of compressName.
    //
    private String compressName = "";
    //
    // The list contained the SectFile element.
    //
    private List<Section> sectList = new ArrayList<Section>();

    public static Object semaphore = new Object();
    /**
      toBuffer
      
      This function is to collect all sectFile and compress it , then output
      the result to buffer.
      
      @param Buffer     The point of output buffer
      
    **/
    public void toBuffer (DataOutputStream buffer){
        
        Section    sect;
    
        //
        //  Get section file in compress node.
        //
        try{
            
            ByteArrayOutputStream bo = new ByteArrayOutputStream ();
            DataOutputStream Do = new DataOutputStream (bo);
            
            //
            //  Get each section which under the compress {};
            //  And add it is contains to File;
            //
            Iterator SectionIter = sectList.iterator();
            while (SectionIter.hasNext()){
                sect = (Section)SectionIter.next();
                
                //
                //  Call each section class's toBuffer function.
                //
                try {
                    sect.toBuffer(Do);
                }
                catch (BuildException e) {
                    System.out.print(e.getMessage());
                    throw new BuildException ("Compress.toBuffer failed at section");
                }    
                            
            }
            Do.close();    
            
            synchronized (semaphore) {
            //
            //  Call compress
            //
            byte[] fileBuffer = bo.toByteArray();
            Compress myCompress = new Compress(fileBuffer, fileBuffer.length);            
            
            //
            //  Add Compress header
            //
            CompressHeader Ch        = new CompressHeader();
            Ch.SectionHeader.Size[0] = (byte)((myCompress.outputBuffer.length +
                                               Ch.GetSize()) &
                                               0xff
                                              );
            Ch.SectionHeader.Size[1] = (byte)(((myCompress.outputBuffer.length + 
                                                Ch.GetSize())&
                                               0xff00) >> 8
                                              );
            Ch.SectionHeader.Size[2] = (byte)(((myCompress.outputBuffer.length + 
                                                Ch.GetSize()) & 
                                               0xff0000) >> 16
                                             );
            Ch.SectionHeader.type    = (byte) EFI_SECTION_COMPRESSION;
            
            //
            //  Note: The compressName was not efsfective now. Using the
            //  EFI_STANDARD_COMPRSSION for compressType .
            //  That is follow old Genffsfile tools. Some code will be added for 
            //  the different compressName;
            //
            Ch.UncompressLen         = fileBuffer.length;
            Ch.CompressType          = EFI_STANDARD_COMPRESSION; 
            
            //
            //  Change header struct to byte buffer
            //
            byte [] headerBuffer = new byte[Ch.GetSize()];
            Ch.StructToBuffer(headerBuffer);
            
            //
            //  First add CompressHeader to Buffer, then add Compress data.
            //
            buffer.write (headerBuffer);
            buffer.write(myCompress.outputBuffer);            
                
            //
            //  Buffer 4 Byte aligment 
            //
            int size = Ch.GetSize() + myCompress.outputBuffer.length;
            
            while ((size & 0x03) != 0){
                size ++;
                buffer.writeByte(0);
            }
            //
            //  Delete temp file
            //
            //di.close();
            //compressOut.delete();
            }
                
        }
        catch (Exception e){
            throw new BuildException("compress.toBuffer failed!\n");
        }    
    }

    /**
      getCompressName
      
      This function is to get compressName.
      
      @return            The compressName.
    **/
    public String getCompressName() {
        return compressName;
    }

    /**
      setCompressName
      
      This function is to set compressName.
      
      @param compressName The string of compressName
    **/
    public void setCompressName(String compressName) {
        this.compressName = compressName;
    }

    /**
      addSectFile
      
      This function is to add sectFile element to SectList.
      
      @param sectFile    SectFile element which succeed from section class.
    **/
    public void addSectFile (SectFile sectFile) {
        sectList.add(sectFile);
            
    }    
    
    /**
      addTool
      
      This function is to add tool element to SectList.
      @param tool        Tool element which succeed from section class.
    **/
    public void addTool (Tool tool) {
        sectList.add(tool);
    }

    public int getAlignment() {
        return alignment;
    }

    public void setAlignment(int alignment) {
        if (alignment > 7) {
            this.alignment = 7;
        } else {
            this.alignment = alignment;
        }
    }
}