View Javadoc

1   package org.millscript.office.excel.versions;
2   
3   import org.millscript.office.excel.alerts.BIFFAlert;
4   
5   
6   /**
7    * This enumeration represents the set of available BIFF versions. Not all the
8    * versions in this enumeration are supported.
9    */
10  public enum BIFFVersion {
11  
12      BIFF2, BIFF3, BIFF4S, BIFF4W, BIFF5, BIFF7,
13  
14      BIFF8 {
15          @Override
16          public void checkRecordSize( final byte[] recordData ) {
17              // BIFF8 allows records to contain 8224 bytes of data excluding
18              // the record header
19              if ( recordData.length > 8224 ) {
20                  throw new BIFFAlert(
21                      "Record size exceeds allowed size"
22                  ).culprit(
23                      "maximum allowed size", 
24                      8224
25                  ).culprit(
26                      "record size",
27                      recordData.length
28                  ).mishap();
29              }
30          }
31      },
32  
33      BIFF8X {
34          @Override
35          public void checkRecordSize( final byte[] recordData ) {
36              // BIFF8 allows records to contain 8224 bytes of data excluding
37              // the record header
38              if ( recordData.length > 8224 ) {
39                  throw new BIFFAlert(
40                      "Record size exceeds allowed size"
41                  ).culprit(
42                      "maximum allowed size", 
43                      8224
44                  ).culprit(
45                      "record size",
46                      recordData.length
47                  ).mishap();
48              }
49          }
50      };
51  
52      public static BIFFVersion getVersion( final int identifier, final int version ) {
53          switch ( identifier ) {
54              case 0x0009:
55                  // BIFF2
56                  return BIFF2;
57              case 0x0209:
58                  // BIFF3
59                  return BIFF3;
60              case 0x0409:
61                  // BIFF4S, BIFF4W
62                  return BIFF4S;
63              case 0x0809:
64                  // BIFF5, BIFF7, BIFF8, BIFF8X
65                  switch ( version ) {
66                      case 0x0500:
67                          // BIFF5/BIFF7
68                          return BIFF5;
69                      case 0x0600:
70                          // BIFF8
71                          return BIFF8;
72                      default:
73                          throw new BIFFAlert(
74                              "Unknown BIFF version in BOF record"
75                          ).culprit(
76                              "identifier",
77                              identifier
78                          ).culprit(
79                              "version",
80                              version
81                          ).mishap();
82                  }
83              default:
84                  throw new BIFFAlert(
85                      "Unexpected record in BIFF file"
86                  ).culprit(
87                      "expected identifier",
88                      "0x0009, 0x0209, 0x0409 or 0x0809(i.e. a BOF record)"
89                  ).culprit(
90                      "identifier",
91                      identifier
92                  ).mishap();
93          }
94      }
95  
96      public void checkRecordSize( final byte[] recordData ) {
97          // BIFF2 - BIFF7 allowed records to contain 2080 bytes of data
98          // excluding the record header
99          if ( recordData.length > 2080 ) {
100             throw new BIFFAlert(
101                 "Record size exceeds allowed size"
102             ).culprit(
103                 "maximum allowed size", 
104                 2080
105             ).culprit(
106                 "record size",
107                 recordData.length
108             ).mishap();
109         }
110     }
111 
112 }