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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
/* Copyright (C) 2023 ebandal
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
/* 본 제품은 한글과컴퓨터의 ᄒᆞᆫ글 문서 파일(.hwp) 공개 문서를 참고하여 개발하였습니다.
* 개방형 워드프로세서 마크업 언어(OWPML) 문서 구조 KS X 6101:2018 문서를 참고하였습니다.
* 작성자 : 반희수 ebandal@gmail.com
* 작성일 : 2022.10
*/
package HwpDoc.OLEdoc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import HwpDoc.ErrCode;
import HwpDoc.Exception.CompoundDetectException;
public class CompoundFile {
private static final Logger log = Logger.getLogger(CompoundFile.class.getName());
private RandomAccessFile raf;
private int minorVersion;
private int majorVersion;
private int sectorSize = 512;
private int shortSectorSize;
private int num_Directory; // Support only in version 4
private int num_SAT;
private int first_SecID_Directory;
private int miniStreamCutoffSize;
private int first_SecID_SSAT;
private int num_SSAT;
private int first_SecID_MSAT;
private int num_MSAT;
private ArrayList<Sector> sectorList;
private ArrayList<Integer> SAT_list; // Master SAT
private ArrayList<Integer> SSAT_SecID_list;
private ArrayList<Integer> Directory_SecID_list;
private ArrayList<Integer> SStream_SecID_list;
private ArrayList<Integer> SStream_list;
private ArrayList<DirectoryEntry> DirectoryEntry_list;
final static byte[] COMPOUND_SIGANTURE = { (byte)0xD0, (byte)0xCF, (byte)0x11, (byte)0xE0, (byte)0xA1, (byte)0xB1, (byte)0x1A, (byte)0xE1 };
public CompoundFile(String filename) throws FileNotFoundException {
this(new File(filename));
}
public CompoundFile(File file) throws FileNotFoundException {
raf = new RandomAccessFile(file, "r");
sectorList = new ArrayList<Sector>();
SAT_list = new ArrayList<Integer>();
SSAT_SecID_list = new ArrayList<Integer>();
Directory_SecID_list = new ArrayList<Integer>();
SStream_SecID_list = new ArrayList<Integer>();
SStream_list = new ArrayList<Integer>();
DirectoryEntry_list = new ArrayList<DirectoryEntry>();
}
private void addSiblings(List<Integer> indexList, int currentIndex) {
if (currentIndex==-1)
return;
if (!indexList.contains(currentIndex)) {
indexList.add(currentIndex);
}
int leftSibling = DirectoryEntry_list.size()>currentIndex?DirectoryEntry_list.get(currentIndex).leftSiblingID:-1;
int rightSibling = DirectoryEntry_list.size()>currentIndex?DirectoryEntry_list.get(currentIndex).rightSiblingID:-1;
if (rightSibling!=-1) {
int elderIndex = indexList.indexOf(currentIndex);
indexList.add(elderIndex+1, rightSibling);
if (rightSibling < DirectoryEntry_list.size()) {
addSiblings(indexList, rightSibling);
}
}
if (leftSibling!=-1) {
int elderIndex = indexList.indexOf(currentIndex);
indexList.add(elderIndex, leftSibling);
if (leftSibling < DirectoryEntry_list.size()) {
addSiblings(indexList, leftSibling);
}
}
}
public DirectoryEntry getEntry(String entryName) {
Optional<DirectoryEntry> op = DirectoryEntry_list.stream()
.filter(e -> e.directoryEntryName.trim().equals(entryName))
.findFirst();
return op.isPresent()?op.get():null;
}
public List<DirectoryEntry> getChildEntries(DirectoryEntry baseEntry) {
List<Integer> entryIdx = new LinkedList<Integer>();
int index = 0;
if (baseEntry == null) {
index = DirectoryEntry_list.get(0).childID;
} else {
index = baseEntry.childID;
}
addSiblings(entryIdx, index);
List<DirectoryEntry> entries = entryIdx.stream()
.filter(i -> (i<DirectoryEntry_list.size()))
.map(i -> DirectoryEntry_list.get(i))
.collect(Collectors.toList());
return entries;
}
public List<DirectoryEntry> getChildEntries(String baseEntryName) {
Optional<DirectoryEntry> op = DirectoryEntry_list.stream()
.filter(e -> e.directoryEntryName.trim().equals(baseEntryName))
.findFirst();
if (op.isPresent()) {
return getChildEntries(op.get());
} else {
return new ArrayList<DirectoryEntry>();
}
}
public byte[] getComponent(String entryName) throws CompoundDetectException {
DirectoryEntry entry = getEntry(entryName);
if (entry!=null) {
return read(entry);
} else {
throw new CompoundDetectException();
}
}
public byte[] read(DirectoryEntry entry) {
byte[] buf = new byte[(int)entry.streamSize];
int buff_offset = 0;
int len = 0;
List<Integer> streamContainerSectors = null;
if (entry.streamSize<miniStreamCutoffSize) { // short Stream
byte[] b = new byte[64];
int remainSize = (int)entry.streamSize;
streamContainerSectors = DirectoryEntry_list.get(0).secNums;
for (int secNum: entry.secNums) {
// readStream
int stream_Index = secNum/(sectorSize/64);
int stream_offset = secNum % (sectorSize/64);
int satID = streamContainerSectors.get(stream_Index);
try {
raf.seek((satID+1)*sectorSize + stream_offset*64);
int readLen = raf.read(b, 0, remainSize>=64?64:remainSize);
if (readLen<0) continue;
remainSize -= readLen;
// writeToBuffer
System.arraycopy(b, 0, buf, buff_offset, readLen);
buff_offset += readLen;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} else { // normal Stream
byte[] b = new byte[sectorSize];
int remainSize = (int)entry.streamSize;
streamContainerSectors = entry.secNums;
for (int secNum: entry.secNums) {
if (secNum==0xFFFFFFFE) continue;
// readStream
try {
raf.seek((secNum+1)*sectorSize);
int readLen = raf.read(b, 0, remainSize>=sectorSize?sectorSize:remainSize);
remainSize -= readLen;
// writeToBuf
System.arraycopy(b, 0, buf, buff_offset, readLen);
buff_offset += readLen;
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return buf;
}
public void open() throws CompoundDetectException, IOException {
byte[] buf = new byte[sectorSize]; // from Signature to Number of DIFAT sectors
if (raf.read(buf, 0, sectorSize) != sectorSize) {
throw new CompoundDetectException(ErrCode.FILE_READ_ERROR);
}
parse_Header(buf);
if (majorVersion == 0x0004) {
raf.seek(4096);
sectorSize = 4096;
buf = new byte[sectorSize];
}
// collect MSAT SecID
int secID = first_SecID_MSAT;
if (secID != 0xFFFFFFFE && secID != 0xFFFFFFFF) { // [20211103] 0xFFFFFFFF 조건 추가. (국방CBD방법론v1(1권) 읽지 못하는 이슈 수정)
read_MSAT_sector(secID); // MSAT sector에서 SSAT SecID들을 구한다.
}
if (log.isLoggable(Level.FINEST)) {
log.finest("[______SAT Sector]={"+ SAT_list.stream().map(i -> i.toString()).collect(Collectors.joining(",")) + "}");
}
// Directory
secID = first_SecID_Directory;
Directory_SecID_list.add(secID);
while(secID != 0xFFFFFFFE) {
int satIndex = secID/(sectorSize/4);
// collect Directory SecID
int satID = SAT_list.get(secID/(sectorSize/4));
List<Integer> secID_list = get_SecIDs_from_SAT(satID, satIndex, secID);
secID_list.stream().filter(id -> !Directory_SecID_list.contains(id)).forEach(id -> Directory_SecID_list.add(id));
secID = secID_list.get(secID_list.size()-1);
}
// collect Directory Entries
for (int secID_Directory: Directory_SecID_list) {
if (secID_Directory != 0xFFFFFFFE) {
read_Directory_sector(secID_Directory);
}
}
if (log.isLoggable(Level.FINEST)) {
log.finest("[Directory Sector]={" + Directory_SecID_list.stream().map(i -> i.toString()).collect(Collectors.joining(",")) + "}");
}
// collect SSAT SecID
int lastSecID = first_SecID_SSAT;
SSAT_SecID_list.add(lastSecID);
while(lastSecID != 0xFFFFFFFE) {
int satIndex = lastSecID/(sectorSize/4);
int satID = SAT_list.get(satIndex);
List<Integer> secID_list = get_SecIDs_from_SAT(satID, satIndex, lastSecID); // SAT를 읽어서 SSAT SecID 들을 구한다.
secID_list.stream().filter(id -> !SSAT_SecID_list.contains(id)).forEach(id -> SSAT_SecID_list.add(id));
lastSecID = secID_list.get(secID_list.size()-1);
}
if (log.isLoggable(Level.FINEST)) {
log.finest("[Short SAT Sector]={" +SSAT_SecID_list.stream().map(i->i.toString()).collect(Collectors.joining(",")) + "}");
}
int SecID_SStream = 0;
// collect SecID of Stream for each Directory entries
for (DirectoryEntry entry: DirectoryEntry_list) {
if (entry.objectType == 0x05) { // Root Storage
// Short Stream container Stream SecID
lastSecID = entry.startingSectorID;
entry.secNums = new ArrayList<Integer>();
entry.secNums.add(lastSecID);
while(lastSecID != 0xFFFFFFFE) {
int containerIndex = lastSecID/(sectorSize/4);
int satID = SAT_list.get(containerIndex);
List<Integer> secID_list = get_SecIDs_from_SAT(satID, containerIndex, lastSecID); // SAT를 읽어서 stream container SecID 들을 구한다.
secID_list.stream().filter(id -> !entry.secNums.contains(id)).forEach(id -> entry.secNums.add(id));
lastSecID = secID_list.get(secID_list.size()-1);
}
} else if (entry.objectType == 0x02) { // Stream
entry.secNums = new ArrayList<Integer>();
entry.secNums.add(entry.startingSectorID);
if (entry.streamSize<miniStreamCutoffSize) {
// ShortStream
int lastSSecID = entry.startingSectorID;
while(lastSSecID != 0xFFFFFFFE) {
int ssatIndex = lastSSecID/(sectorSize/4);
int ssatID = SSAT_SecID_list.get(ssatIndex);
List<Integer> secID_list = get_SecIDs_from_SAT(ssatID, ssatIndex, lastSSecID);
secID_list.stream().filter(id -> !entry.secNums.contains(id)).forEach(id -> entry.secNums.add(id));
lastSSecID = entry.secNums.get(entry.secNums.size()-1);
}
} else {
// Stream
int last_SecID = entry.startingSectorID;
while(last_SecID != 0xFFFFFFFE) {
int satIndex = last_SecID/(sectorSize/4);
int satID = SAT_list.get(satIndex);
List<Integer> secID_list = get_SecIDs_from_SAT(satID, satIndex, last_SecID);
secID_list.stream().filter(id -> !entry.secNums.contains(id)).forEach(id -> entry.secNums.add(id));
last_SecID = entry.secNums.get(entry.secNums.size()-1);
}
}
} else {
continue;
}
}
if (log.isLoggable(Level.FINEST)) {
log.finest("_I __________Name_______ ___Type LS RS Chd Sec Size__ Chain__________");
for (int idx=0; idx<DirectoryEntry_list.size();idx++) {
DirectoryEntry e = DirectoryEntry_list.get(idx);
log.finest(String.format("%2d %21s %7s %2d %2d %3d %3d %6d %s{%s}",
idx,
e.directoryEntryName.trim(),
(e.objectType==0x5?"root":e.objectType==0x2?"stream":e.objectType==0x1?"storage":"none"),
e.leftSiblingID,
e.rightSiblingID,
e.childID,
e.startingSectorID,
e.streamSize,
e.streamSize<miniStreamCutoffSize?"s":"N",
e.secNums==null?"null":e.secNums.stream().map(i -> i.toString()).collect(Collectors.joining(",")))
);
}
}
log.fine("open() closing");
}
private List<Integer> get_SecIDs_from_SAT(int secID, int satIndex, int secID_SSAT) throws IOException {
byte[] buf = new byte[sectorSize];
raf.seek((secID+1) * sectorSize);
raf.read(buf, 0, sectorSize);
int currSecID = secID_SSAT;
int iBuf = currSecID%(sectorSize/4) * 4;
List<Integer> secIdList = new ArrayList<Integer>();
while((sectorSize/4)*(satIndex) <= currSecID && currSecID < (sectorSize/4)*(satIndex+1)) {
currSecID = buf[iBuf+3]<<24&0xFF000000 | buf[iBuf+2]<<16&0xFF0000 | buf[iBuf+1]<<8&0xFF00 | buf[iBuf]&0xFF;
if (currSecID == 0xFFFFFFFE) {
secIdList.add(currSecID);
break;
}
secIdList.add(currSecID);
iBuf = currSecID%(sectorSize/4) * 4;
}
return secIdList;
}
public void parseSectors(int secID, byte[] buf) throws CompoundDetectException, IOException {
int sectorType = buf[3]<<24&0xFF000000 | buf[2]<<16&0xFF0000 | buf[1]<<8&0xFF00 | buf[0]&0xFF; // for DIFAT, FAT, MiniFAT,
if (SAT_list.contains(secID)) {
parse_SAT_sector(secID, buf);
}
if (secID < first_SecID_Directory) {
if (secID == first_SecID_SSAT) {
parse_SSAT_sector(buf);
} else {
}
}
}
private void parse_SAT_sector(int secID, byte[] buf) {
Map<String, List<Integer>> multiMap = new HashMap<String, List<Integer>>();
for(int i=secID*(sectorSize/4), iBuf=0; iBuf < sectorSize-4; i++,iBuf+=4) {
int nextSecID = buf[iBuf+3]<<24&0xFF000000 | buf[iBuf+2]<<16&0xFF0000 | buf[iBuf+1]<<8&0xFF00 | buf[iBuf]&0xFF;
Sector sectorInChain = new Sector();
sectorInChain.sectorNum = i;
switch(nextSecID) {
case 0xFFFFFFFC: // DIFAT
sectorInChain.type = SectorType.MSAT;
break;
case 0xFFFFFFFD: // FAT
sectorInChain.type = SectorType.SAT;
break;
case 0xFFFFFFFA: // maximum regular sector number
case 0xFFFFFFFE: // ENDOFCHAIN
sectorInChain.type = SectorType.ENDOFCHAIN;
// Directory
// FAT
// MiniFAT
// DIFAT
// Stream (User-Defined Data)
// Range Lock
break;
case 0xFFFFFFFF: // unallocated
sectorInChain.type = SectorType.FREE;
break;
case 0xFFFFFFFB: // Reserved for future use.
sectorInChain.type = SectorType.CONTINUE;
break;
default:
sectorInChain.type = SectorType.CONTINUE;
sectorInChain.nextNum = nextSecID;
}
if (nextSecID < 0xFFFFFFFF)
sectorList.add(sectorInChain);
}
}
private SectorType lookupSectorType(byte[] buf) {
for(int i=0, index=0; index < sectorSize-4; i++,index+=4) {
int sector = buf[index+3]<<24&0xFF000000 | buf[index+2]<<16&0xFF0000 | buf[index+1]<<8&0xFF00 | buf[index]&0xFF;
if (sector == 0xFFFFFFFC)
return SectorType.MSAT;
else if (sector == 0xFFFFFFFD)
return SectorType.SAT;
}
return SectorType.CONTINUE;
}
private void read_Directory_sector(int secID) throws IOException {
byte[] buf = new byte[sectorSize];
raf.seek((secID+1) * sectorSize);
if (raf.read(buf, 0, sectorSize) == sectorSize) {
parse_Directory_sector(buf);
}
}
private void parse_Directory_sector(byte[] buf) throws UnsupportedEncodingException {
for(int i=0, index=0; index <= sectorSize-128; i++,index+=128) {
int entryNameLen = buf[index+65]<<8&0xFF00 | buf[index+64]&0xFF;
String directoryEntryName = new String(buf, index, entryNameLen, StandardCharsets.UTF_16LE);
int objectType = buf[index+66]&0xFF;
int colorFlag = buf[index+67]&0xFF;
int leftSiblingID = buf[index+71]<<24&0xFF000000 | buf[index+70]<<16&0xFF0000 | buf[index+69]<<8&0xFF00 | buf[index+68]&0xFF;
int rightSiblingID = buf[index+75]<<24&0xFF000000 | buf[index+74]<<16&0xFF0000 | buf[index+73]<<8&0xFF00 | buf[index+72]&0xFF;
int childID = buf[index+79]<<24&0xFF000000 | buf[index+78]<<16&0xFF0000 | buf[index+77]<<8&0xFF00 | buf[index+76]&0xFF;
long clsID1 = buf[index+87]<<24&0xFF000000 | buf[index+86]<<16&0xFF0000 | buf[index+85]<<8&0xFF00 | buf[index+84]&0xFF;
clsID1 = clsID1<<32 | buf[index+83]<<24&0xFF000000 | buf[index+82]<<16&0xFF0000 | buf[index+81]<<8&0xFF00 | buf[index+80]&0xFF;
long clsID2 = buf[index+95]<<24&0xFF000000 | buf[index+94]<<16&0xFF0000 | buf[index+93]<<8&0xFF00 | buf[index+92]&0xFF;
clsID2 = clsID2<<32 | buf[index+91]<<24&0xFF000000 | buf[index+90]<<16&0xFF0000 | buf[index+89]<<8&0xFF00 | buf[index+88]&0xFF;
int stateBit = buf[index+99]<<24&0xFF000000 | buf[index+98]<<16&0xFF0000 | buf[index+97]<<8&0xFF00 | buf[index+96]&0xFF;
long creationTime = buf[index+107]<<24&0xFF000000 | buf[index+106]<<16&0xFF0000 | buf[index+105]<<8&0xFF00 | buf[index+104]&0xFF;
creationTime = creationTime<<32 | buf[index+103]<<24&0xFF000000 | buf[index+102]<<16&0xFF0000 | buf[index+101]<<8&0xFF00 | buf[index+100]&0xFF;
long modifiedTime = buf[index+115]<<24&0xFF000000 | buf[index+114]<<16&0xFF0000 | buf[index+113]<<8&0xFF00 | buf[index+112]&0xFF;
modifiedTime = modifiedTime<<32 | buf[index+111]<<24&0xFF000000 | buf[index+110]<<16&0xFF0000 | buf[index+109]<<8&0xFF00 | buf[index+108]&0xFF;
int startingSectorID = buf[index+119]<<24&0xFF000000 | buf[index+118]<<16&0xFF0000 | buf[index+117]<<8&0xFF00 | buf[index+116]&0xFF;
long streamSize = buf[index+127]<<24&0xFF000000 | buf[index+126]<<16&0xFF0000 | buf[index+125]<<8&0xFF00 | buf[index+124]&0xFF;
streamSize = streamSize<<32 | buf[index+123]<<24&0xFF000000 | buf[index+122]<<16&0xFF0000 | buf[index+121]<<8&0xFF00 | buf[index+120]&0xFF;
DirectoryEntry de = new DirectoryEntry(directoryEntryName, objectType, colorFlag, leftSiblingID, rightSiblingID, childID, clsID1, clsID2,
stateBit, creationTime, modifiedTime, startingSectorID, streamSize);
DirectoryEntry_list.add(de);
}
}
private void read_SSAT_sector(int secID) throws IOException {
byte[] buf = new byte[sectorSize];
raf.seek((secID+1) * sectorSize);
if (raf.read(buf, 0, sectorSize) == sectorSize) {
parse_SSAT_sector(buf);
}
}
private void parse_SSAT_sector(byte[] buf) {
int offset = 0;
while(offset<sectorSize-4) {
int nextSectorID = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
if (nextSectorID != 0xFFFFFFFF)
SStream_list.add(nextSectorID);
}
}
private void read_MSAT_sector(int secID) throws IOException {
byte[] buf = new byte[sectorSize];
raf.seek((secID+1) * sectorSize);
if (raf.read(buf, 0, sectorSize) == sectorSize) {
parse_MSAT_sector(buf);
}
}
private void parse_MSAT_sector(byte[] buf) throws IOException {
int offset = 0;
while(offset<sectorSize-4) {
int sector = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
if (sector != 0xFFFFFFFF)
SAT_list.add(sector);
}
// 다음번 MasterSector ID 읽기
int nextSecID_MSAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
if (nextSecID_MSAT != 0xFFFFFFFE && nextSecID_MSAT != 0xFFFFFFFF) { // [20211103] 0xFFFFFFFF 조건 추가. (국방CBD방법론v1(1권) 읽지 못하는 이슈 수정)
read_MSAT_sector(nextSecID_MSAT);
}
}
public void parse_Header(byte[] buf) throws CompoundDetectException, IOException {
int offset = 0;
byte[] bufSig = new byte[8];
System.arraycopy(buf, 0, bufSig, 0, 8);
if (Arrays.equals(bufSig, COMPOUND_SIGANTURE) == false) {
throw new CompoundDetectException(ErrCode.SIGANTURE_NOT_MATCH);
}
offset += 8; // Header signature
offset += 16; // Header CLSID
minorVersion = buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 2;
if (minorVersion != 0x003E) {
// throw new CompoundDetectException(ErrCode.INVALID_MINORVERSION); // #68 이슈(0x003B 케이스) 수정
}
majorVersion = buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 2;
if (majorVersion != 0x0003 && majorVersion != 0x0004) {
throw new CompoundDetectException(ErrCode.INVALID_MAJORVERSION);
}
int byteOrder = buf[offset+1]<<8&0xFF00 | buf[offset]&0x00FF;
offset += 2;
if (byteOrder != 0xFFFE) {
throw new CompoundDetectException(ErrCode.INVALID_BYTEORDER);
}
int sectorShift = buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 2;
sectorSize = (int)Math.pow(2.0, (double)sectorShift);
if (majorVersion == 0x0003 && sectorShift != 0x0009) {
throw new CompoundDetectException(ErrCode.INVALID_SECTORSHIFT);
} else if (majorVersion == 0x0004 && sectorShift != 0x000C) {
throw new CompoundDetectException(ErrCode.INVALID_SECTORSHIFT);
}
sectorShift = buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 2;
shortSectorSize = (int)Math.pow(2.0, (double)sectorShift);
if (sectorShift != 0x0006) {
throw new CompoundDetectException(ErrCode.INVALID_MINISECTORSHIFT);
}
offset += 6; // reserved
num_Directory = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
if (majorVersion == 0x0003 && num_Directory != 0x0000) {
throw new CompoundDetectException(ErrCode.INVALID_NUM_DIRECTORYSECTOR);
}
num_SAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
first_SecID_Directory = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
offset += 4; // Transaction Signature Number
miniStreamCutoffSize = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
if (miniStreamCutoffSize != 0x00001000) {
throw new CompoundDetectException(ErrCode.INVALID_MINI_STREAM_CUTOFF);
}
first_SecID_SSAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
num_SSAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
first_SecID_MSAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
num_MSAT = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
while(offset<512) {
int sector = buf[offset+3]<<24&0xFF000000 | buf[offset+2]<<16&0xFF0000 | buf[offset+1]<<8&0xFF00 | buf[offset]&0xFF;
offset += 4;
if (sector != 0xFFFFFFFF) {
SAT_list.add(sector);
}
}
}
public byte[] read(int len) throws IOException {
byte[] buf = new byte[len];
raf.read(buf, 0, len);
return buf;
}
public void close() throws IOException {
raf.close();
}
}
|