rem
stringlengths
0
477k
add
stringlengths
0
313k
context
stringlengths
6
599k
DocumentListItem.create(d, keys.contains(d.getId())));
DocumentListItem.create( d, ArtifactUtil.isKeyHolder(d.getId())));
private void reloadDocuments() { final Object[] elements = ((CompositeFlatContentProvider) contentProvider).getElements(0, null); Document d; for(final Object e : elements) { d = (Document) e; jListModel.addElement( DocumentListItem.create(d, keys.contains(d.getId()))); } }
public abstract IQ handleIQ(final IQ iq, final Session session) throws ParityServerModelException, UnauthorizedException;
public IQ handleIQ(final IQ iq) throws UnauthorizedException { logger.debug(iq); try { final Session session = new Session() { final JID jid = iq.getFrom(); final JabberId jabberId = JabberIdBuilder.parseQualifiedJabberId(jid.toString()); public JabberId getJabberId() { return jabberId; } public JID getJID() { return jid; } }; logger.debug(session); final IQ resultIQ = handleIQ(iq, session); logger.debug(resultIQ); return resultIQ; } catch(final UnauthorizedException ux) { logger.error("handleIQ(IQ)", ux); throw ux; } catch(final Throwable t) { logger.error("handleIQ(IQ)", t); return translate(iq, "An un-expected error has occured.", t); } }
public abstract IQ handleIQ(final IQ iq, final Session session) throws ParityServerModelException, UnauthorizedException;
webPageJLabel.setText(bundle.getString("DisplayInfoAvatar.WebPage"));
webPageJLabel.setText(bundle.getString("DisplayInfoAvatar.ExampleWebPage"));
private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; logoJLabel = new javax.swing.JLabel(); versionJLabel = new javax.swing.JLabel(); buildJLabel = new javax.swing.JLabel(); copyrightJLabel = new javax.swing.JLabel(); fillLeftJLabel = new javax.swing.JLabel(); webPageJLabel = new javax.swing.JLabel(); fillRightJLabel = new javax.swing.JLabel(); okJButton = new javax.swing.JButton(); fillBottomLeftJLabel = new javax.swing.JLabel(); setLayout(new java.awt.GridBagLayout()); logoJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); logoJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/thinkParityLogo.png"))); logoJLabel.setFocusable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(20, 0, 20, 0); add(logoJLabel, gridBagConstraints); versionJLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); versionJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("localization/JPanel_Messages"); // NOI18N versionJLabel.setText(bundle.getString("DisplayInfoAvatar.Version")); // NOI18N versionJLabel.setFocusable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(2, 15, 0, 15); add(versionJLabel, gridBagConstraints); buildJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); buildJLabel.setText(bundle.getString("DisplayInfoAvatar.Build")); // NOI18N buildJLabel.setFocusable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(12, 15, 0, 15); add(buildJLabel, gridBagConstraints); copyrightJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); copyrightJLabel.setText(bundle.getString("DisplayInfoAvatar.Copyright")); // NOI18N copyrightJLabel.setFocusable(false); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(2, 15, 0, 15); add(copyrightJLabel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 5; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.weightx = 1.0; add(fillLeftJLabel, gridBagConstraints); webPageJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); webPageJLabel.setText(bundle.getString("DisplayInfoAvatar.WebPage")); // NOI18N webPageJLabel.setFocusable(false); webPageJLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); webPageJLabel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { webPageJLabelMouseClicked(evt); } public void mouseEntered(java.awt.event.MouseEvent evt) { webPageJLabelMouseEntered(evt); } public void mouseExited(java.awt.event.MouseEvent evt) { webPageJLabelMouseExited(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 5; gridBagConstraints.insets = new java.awt.Insets(2, 0, 0, 0); add(webPageJLabel, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 5; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST; gridBagConstraints.weightx = 1.0; add(fillRightJLabel, gridBagConstraints); okJButton.setText("OK"); okJButton.setPreferredSize(new java.awt.Dimension(65, 23)); okJButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okJButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 6; gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST; gridBagConstraints.insets = new java.awt.Insets(10, 0, 10, 10); add(okJButton, gridBagConstraints); fillBottomLeftJLabel.setPreferredSize(new java.awt.Dimension(65, 23)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 6; gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST; gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 0); add(fillBottomLeftJLabel, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents
webPageJLabel.setText(getString("WebPage", new Object[] {getWebPageString()}));
private void initWebPageJLabel() { // TODO Get www.thinkparity.com from somewhere. Below returns thinkparity.dyndns.org //final String webPage = LinkFactory.getInstance(Application.OPHELIA, Version.getMode()).create().toString(); //webPageJLabel.setText(webPage); }
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http:
String runString = "rundll32 url.dll,FileProtocolHandler " + getWebPage(); Runtime.getRuntime().exec(runString);
private void webPageJLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_webPageJLabelMouseClicked try { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.thinkparity.com/"); } catch (final Throwable t) { throw new BrowserException(LAUNCH_WEB_BROWSER_ERROR, t); } }//GEN-LAST:event_webPageJLabelMouseClicked
webPageJLabel.setText(getString("WebPageHighlighted"));
webPageJLabel.setText(getString("WebPageHighlighted", new Object[] {getWebPageString()}));
private void webPageJLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_webPageJLabelMouseEntered webPageJLabel.setText(getString("WebPageHighlighted")); setCursor(new Cursor(Cursor.HAND_CURSOR)); }//GEN-LAST:event_webPageJLabelMouseEntered
webPageJLabel.setText(getString("WebPage"));
webPageJLabel.setText(getString("WebPage", new Object[] {getWebPageString()}));
private void webPageJLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_webPageJLabelMouseExited webPageJLabel.setText(getString("WebPage")); setCursor(null); }//GEN-LAST:event_webPageJLabelMouseExited
protected Avatar(final String l18nContext, final Color background) { this(l18nContext, ScrollPolicy.NONE, background); }
protected Avatar(final AvatarId id) { this(id.toString()); }
protected Avatar(final String l18nContext, final Color background) { this(l18nContext, ScrollPolicy.NONE, background); }
protected String getString(final String localKey, final Object[] arguments) { return localization.getString(localKey, arguments);
protected String getString(final String localKey) { return localization.getString(localKey);
protected String getString(final String localKey, final Object[] arguments) { return localization.getString(localKey, arguments); }
logger.info("audit(CloseEvent)");
logger.info("[LMODEL] [AUDIT] [AUDIT CLOSE]");
void audit(final CloseEvent closeEvent) { logger.info("audit(CloseEvent)"); logger.debug(closeEvent); auditIO.audit(closeEvent); }
logger.info("delete(Long)");
logger.info("[LMODEL] [AUDIT] [DELETE]");
void delete(final Long artifactId) { logger.info("delete(Long)"); logger.debug(artifactId); auditIO.delete(artifactId); }
logger.info("list(Long)");
logger.info("[LMODEL] [AUDIT] [READ]");
Collection<AuditEvent> read(final Long artifactId) { logger.info("list(Long)"); logger.debug(artifactId); return auditIO.list(artifactId); }
final Long documentId) {
final Long documentId, final Integer eTeamSize) {
private Fixture(final ArtifactModel aModel, final DocumentModel dModel, final Long documentId) { this.aModel = aModel; this.dModel = dModel; this.documentId = documentId; }
this.eTeamSize = eTeamSize;
private Fixture(final ArtifactModel aModel, final DocumentModel dModel, final Long documentId) { this.aModel = aModel; this.dModel = dModel; this.documentId = documentId; }
data = new LinkedList<Fixture>();
protected void setUp() throws Exception { super.setUp(); data = new LinkedList<Fixture>(); final ArtifactModel aModel = getArtifactModel(); final DocumentModel dModel = getDocumentModel(); final SessionModel sModel = getSessionModel(); final ModelTestUser userX = ModelTestUser.getX(); // 2 scenarios // 0: i am the document key holder final File file0 = getInputFiles()[0]; final Document d0 = dModel.create(file0.getName(), file0.getName(), file0); addTeam(d0); modifyDocument(d0); dModel.publish(d0.getId()); data.add(new Fixture(aModel, dModel, d0.getId())); // 1: i am not the document key holder final File file1 = getInputFiles()[1]; final Document d1 = dModel.create(file1.getName(), file1.getName(), file1); addTeam(d1); modifyDocument(d1); dModel.publish(d1.getId()); sModel.sendKeyResponse(d1.getId(), userX.getJabberId(), KeyResponse.ACCEPT); data.add(new Fixture(aModel, dModel, d1.getId())); }
final SessionModel sModel = getSessionModel(); final ModelTestUser userX = ModelTestUser.getX();
protected void setUp() throws Exception { super.setUp(); data = new LinkedList<Fixture>(); final ArtifactModel aModel = getArtifactModel(); final DocumentModel dModel = getDocumentModel(); final SessionModel sModel = getSessionModel(); final ModelTestUser userX = ModelTestUser.getX(); // 2 scenarios // 0: i am the document key holder final File file0 = getInputFiles()[0]; final Document d0 = dModel.create(file0.getName(), file0.getName(), file0); addTeam(d0); modifyDocument(d0); dModel.publish(d0.getId()); data.add(new Fixture(aModel, dModel, d0.getId())); // 1: i am not the document key holder final File file1 = getInputFiles()[1]; final Document d1 = dModel.create(file1.getName(), file1.getName(), file1); addTeam(d1); modifyDocument(d1); dModel.publish(d1.getId()); sModel.sendKeyResponse(d1.getId(), userX.getJabberId(), KeyResponse.ACCEPT); data.add(new Fixture(aModel, dModel, d1.getId())); }
data.add(new Fixture(aModel, dModel, d0.getId())); final File file1 = getInputFiles()[1]; final Document d1 = dModel.create(file1.getName(), file1.getName(), file1); addTeam(d1); modifyDocument(d1); dModel.publish(d1.getId()); sModel.sendKeyResponse(d1.getId(), userX.getJabberId(), KeyResponse.ACCEPT); data.add(new Fixture(aModel, dModel, d1.getId()));
datum = new Fixture(aModel, dModel, d0.getId(), aModel.readTeam(d0.getId()).size());
protected void setUp() throws Exception { super.setUp(); data = new LinkedList<Fixture>(); final ArtifactModel aModel = getArtifactModel(); final DocumentModel dModel = getDocumentModel(); final SessionModel sModel = getSessionModel(); final ModelTestUser userX = ModelTestUser.getX(); // 2 scenarios // 0: i am the document key holder final File file0 = getInputFiles()[0]; final Document d0 = dModel.create(file0.getName(), file0.getName(), file0); addTeam(d0); modifyDocument(d0); dModel.publish(d0.getId()); data.add(new Fixture(aModel, dModel, d0.getId())); // 1: i am not the document key holder final File file1 = getInputFiles()[1]; final Document d1 = dModel.create(file1.getName(), file1.getName(), file1); addTeam(d1); modifyDocument(d1); dModel.publish(d1.getId()); sModel.sendKeyResponse(d1.getId(), userX.getJabberId(), KeyResponse.ACCEPT); data.add(new Fixture(aModel, dModel, d1.getId())); }
data.clear(); data = null;
datum = null;
protected void tearDown() throws Exception { data.clear(); data = null; super.tearDown(); }
Document document; Set<User> team; for(final Fixture datum : data) { try { datum.dModel.close(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); }
try { datum.dModel.close(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); }
public void testClose() { testLogger.info("[LMODEL] [DOCUMENT] [TEST CLOSE]"); Document document; Set<User> team; for(final Fixture datum : data) { try { datum.dModel.close(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } document = null; try { document = datum.dModel.get(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [DOCUMENT IS NULL]", document); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-CLOSED DOCUMENT STATE]", document.getState(), ArtifactState.CLOSED); team = datum.aModel.readTeam(datum.documentId); assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [TEAM IS NULL]", team); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-ZERO TEAM SIZE]", team.size(), 0); } }
document = null; try { document = datum.dModel.get(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [DOCUMENT IS NULL]", document); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-CLOSED DOCUMENT STATE]", document.getState(), ArtifactState.CLOSED);
Document document = null; try { document = datum.dModel.get(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [DOCUMENT IS NULL]", document); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-CLOSED DOCUMENT STATE]", document.getState(), ArtifactState.CLOSED);
public void testClose() { testLogger.info("[LMODEL] [DOCUMENT] [TEST CLOSE]"); Document document; Set<User> team; for(final Fixture datum : data) { try { datum.dModel.close(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } document = null; try { document = datum.dModel.get(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [DOCUMENT IS NULL]", document); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-CLOSED DOCUMENT STATE]", document.getState(), ArtifactState.CLOSED); team = datum.aModel.readTeam(datum.documentId); assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [TEAM IS NULL]", team); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-ZERO TEAM SIZE]", team.size(), 0); } }
team = datum.aModel.readTeam(datum.documentId); assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [TEAM IS NULL]", team); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-ZERO TEAM SIZE]", team.size(), 0); }
final Set<User> team = datum.aModel.readTeam(datum.documentId); assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [TEAM IS NULL]", team); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-ZERO TEAM SIZE]", datum.eTeamSize.intValue(), team.size());
public void testClose() { testLogger.info("[LMODEL] [DOCUMENT] [TEST CLOSE]"); Document document; Set<User> team; for(final Fixture datum : data) { try { datum.dModel.close(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } document = null; try { document = datum.dModel.get(datum.documentId); } catch(final ParityException px) { fail(createFailMessage(px)); } assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [DOCUMENT IS NULL]", document); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-CLOSED DOCUMENT STATE]", document.getState(), ArtifactState.CLOSED); team = datum.aModel.readTeam(datum.documentId); assertNotNull("[LMODEL] [DOCUMENT] [TEST CLOSE] [TEAM IS NULL]", team); assertEquals("[LMODEL] [DOCUMENT] [TEST CLOSE] [NON-ZERO TEAM SIZE]", team.size(), 0); } }
case DOCUMENT_HISTORY_LIST:
case DOCUMENT_HISTORY:
public static Avatar create(final AvatarId id) { switch(id) { case BROWSER_LOGO: return SINGLETON.createBrowserLogo(); case BROWSER_MAIN: return SINGLETON.createBrowserMain(); case BROWSER_TITLE: return SINGLETON.createBrowserTitle(); case DOCUMENT_HISTORY_LIST: return SINGLETON.createDocumentHistoryList(); case SESSION_LOGIN: return SINGLETON.createSessionLogin(); case SESSION_SEND_FORM: return SINGLETON.createSessionSendForm(); case SESSION_SEND_KEY_FORM: return SINGLETON.createSessionSendKeyForm(); case SYSTEM_MESSAGE: return SINGLETON.createSystemMessage(); default: throw Assert.createUnreachable("Unknown avatar: " + id); } }
documentHistoryList = new DocumentHistoryListAvatar(controller);
documentHistoryList = new DocumentHistoryAvatar(controller);
private Avatar createDocumentHistoryList() { if(null == documentHistoryList) { documentHistoryList = new DocumentHistoryListAvatar(controller); documentHistoryList.setContentProvider(ProviderFactory.getHistoryProvider()); } return documentHistoryList; }
case CONFIRM_DIALOGUE: avatar = createConfirmDialogue(); break;
private Avatar doCreate(final AvatarId id) { final Avatar avatar; switch(id) { case BROWSER_INFO: avatar = createBrowserInfo(); break; case BROWSER_MAIN: avatar = createBrowserMain(); break; case BROWSER_TITLE: avatar = createBrowserTitle(); break; case DOCUMENT_HISTORY3: avatar = createDocumentHistory3List(); break; case PLATFORM_LOGIN: avatar = createPlatformLogin(); break; case SESSION_INVITE_PARTNER: avatar = createSessionInvitePartner(); break; case SESSION_MANAGE_CONTACTS: avatar = createSessionManageContacts(); break; case SESSION_SEARCH_PARTNER: avatar = createSessionSearchPartner(); break; case SESSION_SEND_FORM: avatar = createSessionSendForm(); break; case SESSION_SEND_VERSION: avatar = createSendVersion(); break; default: throw Assert.createUnreachable("Unknown avatar: " + id); } register(avatar); return avatar; }
CopyActionEnforcer.applyEnforcer(this);
BrowserInfoAvatar() { super("BrowserInfo"); setLayout(new GridBagLayout()); setOpaque(false); setTransferHandler(new CreateDocumentTxHandler(getController())); initComponents(); }
@Override protected Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId) { return Collections.emptyMap(); }
static final <T extends Artifact, U extends ArtifactVersion> BackupReader<T, U> emptyReader() { return new BackupReader<T, U>() { @Override public List<T> read() { return Collections.emptyList(); } @Override public T read(final UUID uniqueId) { return null; } @Override public List<U> readVersions(final UUID uniqueId) { return Collections.emptyList(); } @Override protected Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId) { return Collections.emptyMap(); } }; }
protected Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId) {
public Map<User, ArtifactReceipt> readPublishedTo( final UUID uniqueId, final Long versionId) {
protected Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId) { return Collections.emptyMap(); }
protected abstract Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId);
public abstract Map<User, ArtifactReceipt> readPublishedTo( final UUID uniqueId, final Long versionId);
protected abstract Map<User, ArtifactReceipt> readPublishedTo(final UUID uniqueId, final Long versionId);
.add(searchPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE) .addContainerGap())
.add(searchPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
private void initComponents() { com.thinkparity.browser.application.browser.display.avatar.MainTitleAvatarSearchPanel searchPanel; searchPanel = new com.thinkparity.browser.application.browser.display.avatar.MainTitleAvatarSearchPanel(); tabPanel = new com.thinkparity.browser.application.browser.display.avatar.MainTitleAvatarTabPanel(); searchPanel.setMainTitleAvatar(this); tabPanel.setMainTitleAvatar(this); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .add(tabPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 235, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) .add(searchPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup() .addContainerGap(35, Short.MAX_VALUE) .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(org.jdesktop.layout.GroupLayout.TRAILING, tabPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(org.jdesktop.layout.GroupLayout.TRAILING, searchPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) ); }// </editor-fold>//GEN-END:initComponents
if(graphical) { System.setProperty("swing.aatext", "true"); try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} }
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
boolean errorsFound = false;
int errorsReported = 0; int warningsReported = 0;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock);
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
if(addInOffset)
if(addInOffset) { if(DEBUG) println("!-----addInOffset mode: inOffset tranformation " + inOffset + "->" + (inOffset+lastInOffset));
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
inOffset += lastInOffset;
inOffset = lastInOffset;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_ADC not supported.");
println(" " + elementNumber + ":" + blockCount + ". ERROR: BT_ADC not supported."); ++errorsReported;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_ZLIB processing...");
println(" " + elementNumber + ":" + blockCount + ". BT_ZLIB processing...");
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_ZLIB FP != outOffset (" +
println(" " + elementNumber + ":" + blockCount + ". WARNING: BT_ZLIB FP != outOffset (" +
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_ZLIB Could not decode...");
println(" " + elementNumber + ":" + blockCount + ". ERROR: BT_ZLIB Could not decode..."); ++errorsReported;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
errorsFound = true;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_BZIP2 not currently supported.");
println(" " + elementNumber + ":" + blockCount + ". ERROR: BT_BZIP2 not currently supported."); ++errorsReported;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")");
println(" " + elementNumber + ":" + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) { println(" " + elementNumber + ":" + blockCount + ". WARNING: BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); ++warningsReported; }
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" +
println(" " + elementNumber + ":" + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) { println(" " + elementNumber + ":" + blockCount + ". WARNING: BT_ZERO FP != outOffset (" +
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_UNKNOWN processing...");
println(" " + elementNumber + ":" + blockCount + ". BT_UNKNOWN processing...");
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...",
println(" " + elementNumber + ":" + blockCount + ". WARNING: Blocktype BT_UNKNOWN had non-zero sizes...",
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_END processing...");
println(" " + elementNumber + ":" + blockCount + ". BT_END processing...");
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". BT_END FP != outOffset (" +
println(" " + elementNumber + ":" + blockCount + ". WARNING: BT_END FP != outOffset (" +
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize);
println(" " + elementNumber + ":" + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + elementNumber + ":" + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); ++warningsReported;
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(" " + blockCount + ". unknown blocktype FP != outOffset (" +
println(" " + elementNumber + ":" + blockCount + ". unknown blocktype FP != outOffset (" +
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
String errors = errorsFound?"There were errors...":"No errors reported.";
String summary = (errorsReported != 0)?errorsReported+" errors reported":"No errors reported"; summary += (warningsReported != 0)?" ("+warningsReported+" warnings emitted).":".";
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
println(errors);
println(summary);
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" +
JOptionPane.showMessageDialog(null, "Extraction complete! " + summary + "\n" +
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
isoRaf.close();
if(isoRaf != null) isoRaf.close();
public static void notmain(String[] args) throws Exception { if(DEBUG) verbose = true; parseArgs(args); if(graphical) { System.setProperty("swing.aatext", "true"); //Antialiased text try { javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) {} } printlnVerbose("Processing: " + dmgFile); RandomAccessFile dmgRaf = new RandomAccessFile(dmgFile, "r"); RandomAccessFile isoRaf = null; boolean testOnly = false; if(isoFile != null) { isoRaf = new RandomAccessFile(isoFile, "rw"); isoRaf.setLength(0); printlnVerbose("Extracting to: " + isoFile); } else { testOnly = true; printlnVerbose("Simulating extraction..."); } dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_1); long plistBegin1 = dmgRaf.readLong(); long plistEnd = dmgRaf.readLong(); dmgRaf.seek(dmgRaf.length()-PLIST_ADDRESS_2); long plistBegin2 = dmgRaf.readLong(); long plistSize = dmgRaf.readLong(); if(DEBUG) { println("Read addresses:", " " + plistBegin1, " " + plistBegin2); } if(plistBegin1 != plistBegin2) { println("Addresses not equal! Assumption broken... =/", plistBegin1 + " != " + plistBegin2); System.exit(0); } if(plistSize != (plistEnd-plistBegin1)) { println("plistSize field does not match plistEnd marker!", "plistSize=" + plistSize + " plistBegin1=" + plistBegin1 + " plistEnd=" + plistEnd + " plistEnd-plistBegin1=" + (plistEnd-plistBegin1)); } printlnVerbose("Jumping to address..."); dmgRaf.seek(plistBegin1); byte[] buffer = new byte[(int)plistSize]; dmgRaf.read(buffer); InputStream is = new ByteArrayInputStream(buffer); NodeBuilder handler = new NodeBuilder(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); try {// System.out.println("validation: " + saxParser.getProperty("validation"));// System.out.println("external-general-entities: " + saxParser.getProperty("external-general-entities"));// System.out.println("external-parameter-entities: " + saxParser.getProperty("external-parameter-entities"));// System.out.println("is-standalone: " + saxParser.getProperty("is-standalone"));// System.out.println("lexical-handler: " + saxParser.getProperty("lexical-handler"));// System.out.println("parameter-entities: " + saxParser.getProperty("parameter-entities"));// System.out.println("namespaces: " + saxParser.getProperty("namespaces"));// System.out.println("namespace-prefixes: " + saxParser.getProperty("namespace-prefixes"));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println(": " + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty(""));// System.out.println("" + saxParser.getProperty("")); //System.out.println("isValidating: " + saxParser.isValidating()); saxParser.parse(is, handler); } catch(SAXException se) { se.printStackTrace(); System.err.println("Could not read the partition list... exiting."); System.exit(1); } XMLNode[] rootNodes = handler.getRoots(); if(rootNodes.length != 1) { println("Could not parse DMG-file!"); System.exit(0); } /* Ok, now we have a tree built from the XML-document. Let's walk to the right place. */ /* cd plist cd dict cdkey resource-fork (type:dict) cdkey blkx (type:array) */ XMLNode current; XMLElement[] children; boolean keyFound; current = rootNodes[0]; //We are at plist... probably (there should be only one root node) current = current.cd("dict"); current = current.cdkey("resource-fork"); current = current.cdkey("blkx"); printlnVerbose("Found " + current.getChildren().length + " partitions:"); byte[] inBuffer = new byte[0x40000]; byte[] outBuffer = new byte[0x40000]; byte[] zeroblock = new byte[4096]; /* I think java always zeroes its arrays on creation... but let's play safe. */ for(int y = 0; y < zeroblock.length; ++y) zeroblock[y] = 0; LinkedList<DMGBlock> blocks = new LinkedList<DMGBlock>(); //long lastOffs = 0; long lastOutOffset = 0; long lastInOffset = 0; long totalSize = 0; boolean errorsFound = false; reportProgress(0); for(XMLElement xe : current.getChildren()) { if(progmon != null && progmon.isCanceled()) System.exit(0); if(xe instanceof XMLNode) { XMLNode xn = (XMLNode)xe; byte[] data = Base64.decode(xn.getKeyValue("Data")); long partitionSize = calculatePartitionSize(data); totalSize += partitionSize; printlnVerbose(" " + xn.getKeyValue("Name")); printlnVerbose(" ID: " + xn.getKeyValue("ID")); printlnVerbose(" Attributes: " + xn.getKeyValue("Attributes")); printlnVerbose(" Partition map data length: " + data.length + " bytes"); printlnVerbose(" Partition size: " + partitionSize + " bytes"); if(verbose) { printlnVerbose(" Dumping blkx..."); FileOutputStream fos = new FileOutputStream(xn.getKeyValue("ID") + ".blkx"); fos.write(data); fos.close(); } if(DEBUG) { File dumpFile = new File("data " + xn.getKeyValue("ID") + ".bin"); println(" Dumping partition map to file: " + dumpFile); FileOutputStream dump = new FileOutputStream(dumpFile); dump.write(data); dump.close(); } int offset = 0xCC; int blockType = 0; /* Offset of the input data for the current block in the input file */ long inOffset = 0; /* Size of the input data for the current block */ long inSize = 0; /* Offset of the output data for the current block in the output file */ long outOffset = 0; /* Size of the output data (possibly larger than inSize because of decompression, zero expansion...) */ long outSize = 0; long lastByteReadInBlock = -1; boolean addInOffset = false; //, lastInOffs = 0; int blockCount = 0; while(blockType != BT_END) { if(progmon != null && progmon.isCanceled()) System.exit(0); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); int bytesSkipped = 0; while(bytesSkipped < offset) bytesSkipped += dis.skipBytes(offset-bytesSkipped); blockType = dis.readInt(); int skipped = dis.readInt(); //Skip 4 bytes forward outOffset = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward outSize = dis.readLong()*0x200;//(dis.readInt() & 0xffffffffL)*0x200; //unsigned int -> long inOffset = dis.readLong();// & 0xffffffffL; //unsigned int -> long //dis.readInt(); //Skip 4 bytes forward inSize = dis.readLong();//dis.readInt() & 0xffffffffL; //unsigned int -> long DMGBlock currentBlock = new DMGBlock(blockType, skipped, outOffset, outSize, inOffset, inSize); blocks.add(currentBlock); if(lastByteReadInBlock == -1) lastByteReadInBlock = inOffset; lastByteReadInBlock += inSize; /* The lines below are a "hack" that I had to do to make dmgx work with certain dmg-files. I don't understand the issue at all, which is why this hack is here, but sometimes inOffset == 0 means that it is 0 relative to the previous partition's last inOffset. And sometimes it doesn't (meaning the actual position 0 in the dmg file). */ if(addInOffset) inOffset += lastInOffset; else if(inOffset == 0) { addInOffset = true; inOffset += lastInOffset; } outOffset += lastOutOffset; if(DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset /*+ " lastInOffs=" + lastInOffs + " lastOffs=" + lastOffs*/); } if(blockType == BT_ADC) { println(" " + blockCount + ". BT_ADC not supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_ZLIB) { if(DEBUG) println(" " + blockCount + ". BT_ZLIB processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZLIB FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); try { DMGBlockHandlers.processZlibBlock(currentBlock, dmgRaf, isoRaf, testOnly, dummyMonitor); } catch(DataFormatException dfe) { println(" " + blockCount + ". BT_ZLIB Could not decode..."); if(!DEBUG) { println("outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize + " lastOutOffset=" + lastOutOffset + " lastInOffset=" + lastInOffset); } dfe.printStackTrace(); if(!testOnly) System.exit(0); else { println(" Testing mode, so continuing..."); //System.exit(0); errorsFound = true; break; } } } else if(blockType == BT_BZIP2) { println(" " + blockCount + ". BT_BZIP2 not currently supported."); if(!testOnly) System.exit(0); } else if(blockType == BT_COPY) { if(DEBUG) println(" " + blockCount + ". BT_COPY processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_COPY FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); dmgRaf.seek(/*lastOffs+*/inOffset); int bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)inSize, inBuffer.length)); long totalBytesRead = bytesRead; while(bytesRead != -1) { reportFilePointerProgress(dmgRaf); if(!testOnly) isoRaf.write(inBuffer, 0, bytesRead); if(totalBytesRead >= inSize) break; bytesRead = dmgRaf.read(inBuffer, 0, Math.min((int)(inSize-totalBytesRead), inBuffer.length)); if(bytesRead > 0) totalBytesRead += bytesRead; } //lastInOffs = inOffset+inSize; } else if(blockType == BT_ZERO) { if(DEBUG) println(" " + blockCount + ". BT_ZERO processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_ZERO FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); reportFilePointerProgress(dmgRaf); long numberOfZeroBlocks = outSize/zeroblock.length; int numberOfRemainingBytes = (int)(outSize%zeroblock.length); for(int j = 0; j < numberOfZeroBlocks; ++j) { if(!testOnly) isoRaf.write(zeroblock); } if(!testOnly) isoRaf.write(zeroblock, 0, numberOfRemainingBytes); //lastInOffs = inOffset+inSize; } else if(blockType == BT_UNKNOWN) { /* I have no idea what this blocktype is... but it's common, and usually doesn't appear more than 2-3 times in a dmg. As long as its input and output sizes are 0, there's no reason to complain... is there? */ if(DEBUG) println(" " + blockCount + ". BT_UNKNOWN processing..."); if(!(inSize == 0 && outSize == 0)) { println(" " + blockCount + ". WARNING! Blocktype BT_UNKNOWN had non-zero sizes...", " inSize=" + inSize + ", outSize=" + outSize); //println(" The author of the program would be pleased if you contacted him about this."); // ...or would I? } } else if(blockType == BT_END) { if(DEBUG) println(" " + blockCount + ". BT_END processing..."); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". BT_END FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); //lastOffs += lastInOffs; lastOutOffset = outOffset; lastInOffset += lastByteReadInBlock; } else { println(" " + blockCount + ". WARNING: previously unseen blocktype " + blockType + " [0x" + Integer.toHexString(blockType) + "]", " " + blockCount + ". outOffset=" + outOffset + " outSize=" + outSize + " inOffset=" + inOffset + " inSize=" + inSize); if(!testOnly && isoRaf.getFilePointer() != outOffset) println(" " + blockCount + ". unknown blocktype FP != outOffset (" + isoRaf.getFilePointer() + " != " + outOffset + ")"); } offset += 0x28; ++blockCount; } } } //printlnVerbose("Progress: 100% Done!"); reportProgress(100); String errors = errorsFound?"There were errors...":"No errors reported."; if(!graphical) { newline(); println(errors); printlnVerbose("Total extracted bytes: " + totalSize + " B"); } else { progmon.close(); JOptionPane.showMessageDialog(null, "Extraction complete! " + errors + "\n" + "Total extracted bytes: " + totalSize + " B", "Information", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } if(!DEBUG) { isoRaf.close(); dmgRaf.close(); } else { isoRaf.close();// System.out.println("blocks.size()=" + blocks.size());// for(DMGBlock b : blocks)// System.out.println(" " + b.toString()); LinkedList<DMGBlock> merged = mergeBlocks(blocks);// System.out.println("merged.size()=" + merged.size());// for(DMGBlock b : merged)// System.out.println(" " + b.toString()); println("Extracting all the parts not containing block data from source file:"); int i = 1; DMGBlock previous = null; for(DMGBlock b : merged) { if(previous == null && b.inOffset > 0) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(0); byte[] data = new byte[(int)(b.inOffset)]; dmgRaf.read(data); curFos.write(data); curFos.close(); } else if(previous != null) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(b.inOffset-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } previous = b; } if(previous.inOffset+previous.inSize != dmgRaf.length()) { String filename = i++ + ".block"; println(" " + new File(filename).getCanonicalPath() + "..."); FileOutputStream curFos = new FileOutputStream(new File(filename)); dmgRaf.seek(previous.inOffset+previous.inSize); byte[] data = new byte[(int)(dmgRaf.length()-(previous.inOffset+previous.inSize))]; dmgRaf.read(data); curFos.write(data); curFos.close(); } dmgRaf.close(); System.out.println("done!"); } }
else if(cur.equals("-gui"))
else if(cur.equals("-gui")) {
public static void parseArgs(String[] args) { boolean parseSuccessful = false; try { /* Take care of the options... */ int i; for(i = 0; i < args.length; ++i) { String cur = args[i]; if(!cur.startsWith("-")) break; else if(cur.equals("-gui")) graphical = true; else if(cur.equals("-v")) verbose = true; else if(cur.equals("-startupcommand")) { startupCommand = args[i+1]; ++i; } } println(APPNAME + " " + BUILDSTRING, "Copyright (c) 2006 Erik Larsson <[email protected]>", " written from the source code to the original dmg2iso program", " Copyright (c) 2004 vu1tur <[email protected]>", " also using the iharder Base64 Encoder/Decoder <http://iharder.sf.net>", "", "This program is distributed under the GNU General Public License version 2 or", "later.", "See <http://www.gnu.org/copyleft/gpl.html> for the details.", ""); if(i == args.length) { dmgFile = getInputFileFromUser(); if(dmgFile == null) System.exit(0); if(getOutputConfirmationFromUser()) { isoFile = getOutputFileFromUser(); if(isoFile == null) System.exit(0); } } else { dmgFile = new File(args[i++]); if(!dmgFile.exists()) { println("File \"" + dmgFile + "\" could not be found!"); System.exit(0); } if(i == args.length-1) isoFile = new File(args[i]); else if(i != args.length) throw new Exception(); } parseSuccessful = true; } catch(Exception e) { println(); println(" usage: " + startupCommand + " [options] <dmgFile> [<isoFile>]"); println(" if an iso-file is not supplied, the program will simulate an extraction"); println(" (useful for detecting errors in dmg-files)"); println(); System.exit(0); } }
" written from the source code to the original dmg2iso program", " Copyright (c) 2004 vu1tur <[email protected]>", " also using the iharder Base64 Encoder/Decoder <http:
" based upon dmg2iso, Copyright (c) 2004 vu1tur <[email protected]>", " also using the iHarder Base64 Encoder/Decoder <http:
public static void parseArgs(String[] args) { boolean parseSuccessful = false; try { /* Take care of the options... */ int i; for(i = 0; i < args.length; ++i) { String cur = args[i]; if(!cur.startsWith("-")) break; else if(cur.equals("-gui")) graphical = true; else if(cur.equals("-v")) verbose = true; else if(cur.equals("-startupcommand")) { startupCommand = args[i+1]; ++i; } } println(APPNAME + " " + BUILDSTRING, "Copyright (c) 2006 Erik Larsson <[email protected]>", " written from the source code to the original dmg2iso program", " Copyright (c) 2004 vu1tur <[email protected]>", " also using the iharder Base64 Encoder/Decoder <http://iharder.sf.net>", "", "This program is distributed under the GNU General Public License version 2 or", "later.", "See <http://www.gnu.org/copyleft/gpl.html> for the details.", ""); if(i == args.length) { dmgFile = getInputFileFromUser(); if(dmgFile == null) System.exit(0); if(getOutputConfirmationFromUser()) { isoFile = getOutputFileFromUser(); if(isoFile == null) System.exit(0); } } else { dmgFile = new File(args[i++]); if(!dmgFile.exists()) { println("File \"" + dmgFile + "\" could not be found!"); System.exit(0); } if(i == args.length-1) isoFile = new File(args[i]); else if(i != args.length) throw new Exception(); } parseSuccessful = true; } catch(Exception e) { println(); println(" usage: " + startupCommand + " [options] <dmgFile> [<isoFile>]"); println(" if an iso-file is not supplied, the program will simulate an extraction"); println(" (useful for detecting errors in dmg-files)"); println(); System.exit(0); } }
"later.", "See <http:
"later. See <http:
public static void parseArgs(String[] args) { boolean parseSuccessful = false; try { /* Take care of the options... */ int i; for(i = 0; i < args.length; ++i) { String cur = args[i]; if(!cur.startsWith("-")) break; else if(cur.equals("-gui")) graphical = true; else if(cur.equals("-v")) verbose = true; else if(cur.equals("-startupcommand")) { startupCommand = args[i+1]; ++i; } } println(APPNAME + " " + BUILDSTRING, "Copyright (c) 2006 Erik Larsson <[email protected]>", " written from the source code to the original dmg2iso program", " Copyright (c) 2004 vu1tur <[email protected]>", " also using the iharder Base64 Encoder/Decoder <http://iharder.sf.net>", "", "This program is distributed under the GNU General Public License version 2 or", "later.", "See <http://www.gnu.org/copyleft/gpl.html> for the details.", ""); if(i == args.length) { dmgFile = getInputFileFromUser(); if(dmgFile == null) System.exit(0); if(getOutputConfirmationFromUser()) { isoFile = getOutputFileFromUser(); if(isoFile == null) System.exit(0); } } else { dmgFile = new File(args[i++]); if(!dmgFile.exists()) { println("File \"" + dmgFile + "\" could not be found!"); System.exit(0); } if(i == args.length-1) isoFile = new File(args[i]); else if(i != args.length) throw new Exception(); } parseSuccessful = true; } catch(Exception e) { println(); println(" usage: " + startupCommand + " [options] <dmgFile> [<isoFile>]"); println(" if an iso-file is not supplied, the program will simulate an extraction"); println(" (useful for detecting errors in dmg-files)"); println(); System.exit(0); } }
throw new RuntimeException("Unexpectedly reached end of file");
throw new RuntimeException("Unexpectedly reached end of file. (bytesRemainingToRead=" + bytesRemainingToRead + ", curBytesRead=" + curBytesRead + ", totalBytesRead=" + totalBytesRead + ", block.inSize=" + block.inSize + ", inBuffer.length=" + inBuffer.length + ")");
public static void processZlibBlock(DMGBlock block, RandomAccessFile dmgRaf, RandomAccessFile isoRaf, boolean testOnly, UserInterface ui) throws IOException, DataFormatException { inflater.reset(); dmgRaf.seek(/*block.lastOffs+*/block.inOffset); /* * medan det finns komprimerat data att lsa: * ls in komprimerat data i inbuffer * medan det finns data kvar att lsa i inbuffer * dekomprimera data frn inbuffer till utbuffer * skriv utbuffer till fil */ long totalBytesRead = 0; while(totalBytesRead < block.inSize) { long bytesRemainingToRead = block.inSize-totalBytesRead; int curBytesRead = dmgRaf.read(inBuffer, 0, (int)Math.min(bytesRemainingToRead, inBuffer.length)); ui.reportProgress((int)(dmgRaf.getFilePointer()*100/dmgRaf.length())); if(curBytesRead < 0) throw new RuntimeException("Unexpectedly reached end of file"); else { totalBytesRead += curBytesRead; inflater.setInput(inBuffer, 0, curBytesRead); long totalBytesInflated = 0; while(!inflater.needsInput() && !inflater.finished()) { long bytesRemainingToInflate = block.outSize-totalBytesInflated; //System.out.println(); //System.out.println("inflater.needsInput()" + inflater.needsInput()); int curBytesInflated = inflater.inflate(outBuffer, 0, (int)Math.min(bytesRemainingToInflate, outBuffer.length)); if(curBytesInflated == 0 && !inflater.needsInput()) { System.out.println("inflater.finished()" + inflater.finished()); System.out.println("inflater.needsDictionary()" + inflater.needsDictionary()); System.out.println("inflater.needsInput()" + inflater.needsInput()); //System.out.println("inflater.()" + inflater.()); throw new RuntimeException("Unexpectedly blocked inflate."); } else { totalBytesInflated += curBytesInflated; if(!testOnly) isoRaf.write(outBuffer, 0, curBytesInflated); } } } } if(!inflater.finished()) throw new RuntimeException("Unclosed ZLIB stream!"); }
final String keyStorePath = "security/client_keystore";
final String keyStorePath = "security/stream_client_keystore";
protected StreamClient(final StreamSession session) { super(); this.logger = new Log4JWrapper(); this.session = session; final Environment environment = session.getEnvironment(); this.socketAddress = new InetSocketAddress( environment.getStreamHost(), environment.getStreamPort()); if (environment.isStreamTLSEnabled()) { logger.logInfo("Stream Client - {0}:{1} - Secure", environment.getStreamHost(), environment.getStreamPort()); final String keyStorePath = "security/client_keystore"; final char[] keyStorePassword = "password".toCharArray(); try { socketFactory = com.thinkparity.codebase.net.SocketFactory.getSecureInstance(keyStorePath, keyStorePassword, keyStorePath, keyStorePassword); } catch (final Exception x) { throw new StreamException(x); } } else { logger.logInfo("Stream Client - {0}:{1}", environment.getStreamHost(), environment.getStreamPort()); socketFactory = com.thinkparity.codebase.net.SocketFactory.getInstance(); } }
public static void copy(final InputStream is, final OutputStream os, final Integer bufferSize) throws IOException { int len; final byte[] b = new byte[bufferSize]; while((len = is.read(b)) > 0) { os.write(b, 0, len); } os.flush();
public static void copy(final InputStream is, final OutputStream os) throws IOException { copy(is, os, 512);
public static void copy(final InputStream is, final OutputStream os, final Integer bufferSize) throws IOException { int len; final byte[] b = new byte[bufferSize]; while((len = is.read(b)) > 0) { os.write(b, 0, len); } os.flush(); }
void addPacketListeners(final XMPPConnection xmppConnection) {}
void addPacketListeners(final XMPPConnection xmppConnection) { xmppConnection.addPacketListener( new PacketListener() { public void processPacket(final Packet packet) { notifyTeamMemberAdded((IQTeamMemberAddedNotification) packet); } }, new PacketTypeFilter(IQTeamMemberAddedNotification.class)); xmppConnection.addPacketListener( new PacketListener() { public void processPacket(final Packet packet) { notifyTeamMemberRemoved((IQTeamMemberRemovedNotification) packet); } }, new PacketTypeFilter(IQTeamMemberRemovedNotification.class)); }
void addPacketListeners(final XMPPConnection xmppConnection) {}
return (XMPPMethodResponse) idCollector.nextResult(); } catch (final ClassCastException ccx) { final String errorId = new ErrorHelper().getErrorId(ccx); logger.logError(errorId, ccx);
final XMPPMethodResponse response = (XMPPMethodResponse) idCollector.nextResult(7 * 1000); return response; } catch (final Throwable t) { final String errorId = new ErrorHelper().getErrorId(t); logger.logError(t, errorId);
public XMPPMethodResponse execute(final XMPPConnection xmppConnection) { // add an executed on parameter setParameter(Xml.All.EXECUTED_ON, DateUtil.getInstance()); // create a collector for the response final PacketCollector idCollector = createPacketCollector(xmppConnection); // TIME This is a local timestamp. logVariable("preSendPacket", DateUtil.getInstance()); xmppConnection.sendPacket(this); // TIME This is a local timestamp. logVariable("postSendPacket", DateUtil.getInstance()); // this sleep has been inserted because when packets are sent within // x milliseconds of each other, they tend to get swallowed by the // smack library try { Thread.sleep(75); } catch(final InterruptedException ix) {} // the timeout is used because the timeout is not expected to be long; // and it helps debug non-implemented responses logVariable("preResponseCollected", DateUtil.getInstance()); try {// return (XMPPMethodResponse) idCollector.nextResult(3 * 1000); return (XMPPMethodResponse) idCollector.nextResult(); } catch (final ClassCastException ccx) { final String errorId = new ErrorHelper().getErrorId(ccx); logger.logError(errorId, ccx); logger.logError("name:{0}", name); logger.logError("xmppConnection:{0}", xmppConnection); throw new XMPPException(errorId); } finally { // re-set the parameters post execution logVariable("postResponseCollected", DateUtil.getInstance()); parameters.clear(); } }
throws IOException { final ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes);
throws DataFormatException, IOException {
public static synchronized byte[] decompress(final byte[] bytes) throws IOException { final ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes); final Inflater inflater = new Inflater(); final InflaterInputStream inflaterInputStream = new InflaterInputStream( bytesIn, inflater, bytes.length); byte[] decompressedContent = new byte[0]; byte[] inflatedBytes = new byte[1024]; byte[] tempoararyBytes; int bytesRead = inflaterInputStream.read(inflatedBytes); int offset = 0; while (-1 != bytesRead) { /* Copy that which is already read to a temp location. */ tempoararyBytes = new byte[decompressedContent.length]; System.arraycopy(decompressedContent, 0, tempoararyBytes, 0, decompressedContent.length); /* Enlarge and copy that in the temp location back. */ decompressedContent = new byte[tempoararyBytes.length + bytesRead]; System.arraycopy(tempoararyBytes, 0, decompressedContent, 0, tempoararyBytes.length); /* Copy that which was just read. */ System.arraycopy(inflatedBytes, 0, decompressedContent, offset, bytesRead); bytesRead = inflaterInputStream.read(inflatedBytes); offset += bytesRead; } return decompressedContent; }
final InflaterInputStream inflaterInputStream = new InflaterInputStream( bytesIn, inflater, bytes.length); byte[] decompressedContent = new byte[0]; byte[] inflatedBytes = new byte[1024]; byte[] tempoararyBytes; int bytesRead = inflaterInputStream.read(inflatedBytes); int offset = 0; while (-1 != bytesRead) { tempoararyBytes = new byte[decompressedContent.length]; System.arraycopy(decompressedContent, 0, tempoararyBytes, 0, decompressedContent.length); decompressedContent = new byte[tempoararyBytes.length + bytesRead]; System.arraycopy(tempoararyBytes, 0, decompressedContent, 0, tempoararyBytes.length); System.arraycopy(inflatedBytes, 0, decompressedContent, offset, bytesRead); bytesRead = inflaterInputStream.read(inflatedBytes); offset += bytesRead; } return decompressedContent;
inflater.setInput(bytes); final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(bytes.length); byte[] byteBuffer = new byte[1024]; while(!inflater.finished()) { int count = inflater.inflate(byteBuffer); byteOutput.write(byteBuffer, 0, count); } byteOutput.close(); return byteOutput.toByteArray();
public static synchronized byte[] decompress(final byte[] bytes) throws IOException { final ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytes); final Inflater inflater = new Inflater(); final InflaterInputStream inflaterInputStream = new InflaterInputStream( bytesIn, inflater, bytes.length); byte[] decompressedContent = new byte[0]; byte[] inflatedBytes = new byte[1024]; byte[] tempoararyBytes; int bytesRead = inflaterInputStream.read(inflatedBytes); int offset = 0; while (-1 != bytesRead) { /* Copy that which is already read to a temp location. */ tempoararyBytes = new byte[decompressedContent.length]; System.arraycopy(decompressedContent, 0, tempoararyBytes, 0, decompressedContent.length); /* Enlarge and copy that in the temp location back. */ decompressedContent = new byte[tempoararyBytes.length + bytesRead]; System.arraycopy(tempoararyBytes, 0, decompressedContent, 0, tempoararyBytes.length); /* Copy that which was just read. */ System.arraycopy(inflatedBytes, 0, decompressedContent, offset, bytesRead); bytesRead = inflaterInputStream.read(inflatedBytes); offset += bytesRead; } return decompressedContent; }
final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); final Deflater deflater = new Deflater(level.getLevel()); final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream( bytesOut, deflater, bytes.length); deflaterOutputStream.write(bytes); deflaterOutputStream.finish(); return bytesOut.toByteArray();
final Deflater deflater = new Deflater(); deflater.setLevel(level.getLevel()); deflater.setInput(bytes); deflater.finish(); final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(bytes.length); byte[] byteBuffer = new byte[BUFFER_SIZE]; while(!deflater.finished()) { int count = deflater.deflate(byteBuffer); byteOutput.write(byteBuffer, 0, count); } byteOutput.close(); return byteOutput.toByteArray();
public static synchronized byte[] compress(final byte[] bytes, final Level level) throws IOException { final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); final Deflater deflater = new Deflater(level.getLevel()); final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream( bytesOut, deflater, bytes.length); deflaterOutputStream.write(bytes); deflaterOutputStream.finish(); return bytesOut.toByteArray(); }
browserMain = new BrowserMainAvatar(controller);
browserMain = new BrowserMainAvatar();
private Avatar createBrowserMain() { if(null == browserMain) { browserMain = new BrowserMainAvatar(controller); browserMain.setContentProvider(ProviderFactory.getMainProvider()); } return browserMain; }
BrowserMainAvatar(final Controller controller) { super("MainListAvatar", ScrollPolicy.VERTICAL, new Color(255, 255, 255, 0)); this.listItemMap = new Hashtable<JVMUniqueId, Component>(20, 0.75F);
BrowserMainAvatar() { super("BrowserMainAvatar", ScrollPolicy.NONE, Color.WHITE);
BrowserMainAvatar(final Controller controller) { super("MainListAvatar", ScrollPolicy.VERTICAL, new Color(255, 255, 255, 0)); this.listItemMap = new Hashtable<JVMUniqueId, Component>(20, 0.75F); setLayout(new GridBagLayout()); }
initBrowserMainComponents();
BrowserMainAvatar(final Controller controller) { super("MainListAvatar", ScrollPolicy.VERTICAL, new Color(255, 255, 255, 0)); this.listItemMap = new Hashtable<JVMUniqueId, Component>(20, 0.75F); setLayout(new GridBagLayout()); }
final UUID uniqueId, final Long versionId, final UUID documentUniqueId) {
final UUID uniqueId, final Long versionId) {
private List<DocumentVersion> readBackupDocumentVersions(final JabberId userId, final UUID uniqueId, final Long versionId, final UUID documentUniqueId) { return getContainerModel().readBackupDocumentVersions(userId, uniqueId, versionId, documentUniqueId); }
uniqueId, versionId, documentUniqueId);
uniqueId, versionId);
private List<DocumentVersion> readBackupDocumentVersions(final JabberId userId, final UUID uniqueId, final Long versionId, final UUID documentUniqueId) { return getContainerModel().readBackupDocumentVersions(userId, uniqueId, versionId, documentUniqueId); }
readLong("versionId"), readUUID("documentUniqueId"));
readLong("versionId"));
public void service() { logApiId(); final List<DocumentVersion> versions = readBackupDocumentVersions( readJabberId("userId"), readUUID("uniqueId"), readLong("versionId"), readUUID("documentUniqueId")); writeDocumentVersions("documentVersions", "documentVersion", versions); }
this.logger = new Log4JWrapper();
this.logger = new Log4JWrapper(getClass());
public AbstractHandler(final String action) { super(action); this.logger = new Log4JWrapper(); }
public List<DocumentVersion> readBackupDocumentVersions(final JabberId userId, final UUID uniqueId, final Long versionId, final UUID documentUniqueId) {
public List<DocumentVersion> readBackupDocumentVersions( final JabberId userId, final UUID uniqueId, final Long versionId) {
public List<DocumentVersion> readBackupDocumentVersions(final JabberId userId, final UUID uniqueId, final Long versionId, final UUID documentUniqueId) { synchronized (getImplLock()) { return getImpl().readBackupDocumentVersions(userId, uniqueId, versionId, documentUniqueId); } }
return getImpl().readBackupDocumentVersions(userId, uniqueId, versionId, documentUniqueId);
return getImpl().readBackupDocumentVersions(userId, uniqueId, versionId);
public List<DocumentVersion> readBackupDocumentVersions(final JabberId userId, final UUID uniqueId, final Long versionId, final UUID documentUniqueId) { synchronized (getImplLock()) { return getImpl().readBackupDocumentVersions(userId, uniqueId, versionId, documentUniqueId); } }
public void close() {
public void close() throws IOException {
public void close() { disconnect(); }
public void open() {
public void open() throws IOException {
public void open() { connect(Type.UPSTREAM); }
public void write(final String streamId, final InputStream stream) { write(new StreamHeader(StreamHeader.Type.STREAM_BEGIN, streamId)); write(stream);
public void write(final String streamId, final InputStream stream, final Long streamSize) { write(new StreamHeader(StreamHeader.Type.STREAM_ID, streamId)); write(new StreamHeader(StreamHeader.Type.STREAM_SIZE, String.valueOf(streamSize))); write(stream, streamSize);
public void write(final String streamId, final InputStream stream) { write(new StreamHeader(StreamHeader.Type.STREAM_BEGIN, streamId)); write(stream); }
public Boolean confirm(final String messageKey, final Object[] messageArguments) { final Data input = new Data(2);
public Boolean confirm(final String messageKey) { final Data input = new Data(1);
public Boolean confirm(final String messageKey, final Object[] messageArguments) { final Data input = new Data(2); input.set(ConfirmDialog.DataKey.MESSAGE_KEY, messageKey); input.set(ConfirmDialog.DataKey.MESSAGE_ARGUMENTS, messageArguments); return confirm(input); }
input.set(ConfirmDialog.DataKey.MESSAGE_ARGUMENTS, messageArguments);
public Boolean confirm(final String messageKey, final Object[] messageArguments) { final Data input = new Data(2); input.set(ConfirmDialog.DataKey.MESSAGE_KEY, messageKey); input.set(ConfirmDialog.DataKey.MESSAGE_ARGUMENTS, messageArguments); return confirm(input); }
public void runAddContainerDocuments(final Long containerId, final File[] files) { final Data data = new Data(2); data.set(AddDocument.DataKey.CONTAINER_ID, containerId); data.set(AddDocument.DataKey.FILES, files); invoke(ActionId.CONTAINER_ADD_DOCUMENT, data);
public void runAddContainerDocuments(final Long containerId) { if(JFileChooser.APPROVE_OPTION == getJFileChooser().showOpenDialog(mainWindow)) { persistence.set( Keys.Persistence.JFILECHOOSER_CURRENT_DIRECTORY, jFileChooser.getCurrentDirectory()); runAddContainerDocuments(containerId, jFileChooser.getSelectedFiles()); }
public void runAddContainerDocuments(final Long containerId, final File[] files) { final Data data = new Data(2); data.set(AddDocument.DataKey.CONTAINER_ID, containerId); data.set(AddDocument.DataKey.FILES, files); invoke(ActionId.CONTAINER_ADD_DOCUMENT, data); }
public void runCreateContainer(final List<File> files) { runCreateContainer(null, files);
public void runCreateContainer() { runCreateContainer(null, null);
public void runCreateContainer(final List<File> files) { runCreateContainer(null, files); }
public void runRenameDocument(final Long documentId, final String documentName) { final Data data = new Data(3);
public void runRenameDocument(final Long documentId) { final Data data = new Data(1);
public void runRenameDocument(final Long documentId, final String documentName) { final Data data = new Data(3); data.set(Rename.DataKey.DOCUMENT_ID, documentId); data.set(Rename.DataKey.DOCUMENT_NAME, documentName); invoke(ActionId.DOCUMENT_RENAME, data); }
data.set(Rename.DataKey.DOCUMENT_NAME, documentName);
public void runRenameDocument(final Long documentId, final String documentName) { final Data data = new Data(3); data.set(Rename.DataKey.DOCUMENT_ID, documentId); data.set(Rename.DataKey.DOCUMENT_NAME, documentName); invoke(ActionId.DOCUMENT_RENAME, data); }
mainWindowLocation.setLocation(pFinal);
public void setMainWindowSizeAndLocation(final Point p, final Dimension d) { final Point pFinal = new Point(p); final Dimension dFinal = new Dimension(d); // Honour the minimum window size. When going below the size limit in // either x or y, limit both the resize and the move in that direction. if (dFinal.getWidth() < Dimensions.BrowserWindow.MIN_SIZE.getWidth()) { if (pFinal.x != getLocation().x) { pFinal.x += (dFinal.getWidth() - Dimensions.BrowserWindow.MIN_SIZE.getWidth()); } dFinal.width = (int)Dimensions.BrowserWindow.MIN_SIZE.getWidth(); } if (dFinal.getHeight() < Dimensions.BrowserWindow.MIN_SIZE.getHeight()) { if (pFinal.y != getLocation().y) { pFinal.y += (dFinal.getHeight() - Dimensions.BrowserWindow.MIN_SIZE.getHeight()); } dFinal.height = (int)Dimensions.BrowserWindow.MIN_SIZE.getHeight(); } final Boolean move = !getLocation().equals(pFinal); final Boolean resize = !getSize().equals(dFinal); if (resize) { if (move) { // Move and resize setBounds(pFinal.x, pFinal.y, (int)dFinal.getWidth(), (int)dFinal.getHeight()); } else { // Resize only setSize(dFinal); } setMinimumSize(dFinal); mainWindowSize.setSize(dFinal); roundCorners(); validate(); } else if (move) { // Move only setLocation(pFinal); } }
this.persistence = PersistenceFactory.getPersistence(getClass());
this.persistence = PersistenceFactory.getPersistence(getClass()); final Boolean maximized = persistence.get("maximized", Boolean.FALSE);
BrowserWindow(final Browser browser) throws HeadlessException { super("BrowserWindow"); this.browser = browser; this.logger = Logger.getLogger(getClass()); this.persistence = PersistenceFactory.getPersistence(getClass()); getRootPane().setBorder(new WindowBorder2()); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { persist(); browser.hibernate(); }}); initMenu(); setIconImage(com.thinkparity.ophelia.browser.Constants.Images.WINDOW_ICON_IMAGE); setTitle(java.util.ResourceBundle.getBundle("localization/JFrame_Messages").getString("BrowserWindow.Title")); setUndecorated(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); final Point location = persistence.get("location", new Point(100, 100)); location.x = (location.x < 0 ? 0 : location.x ); location.y = (location.y < 0 ? 0 : location.y ); setLocation(location.x, location.y); final Dimension size = persistence.get("size", getMainWindowSize()); mainWindowSize.setSize(size); setResizable(true); setMinimumSize(getMainWindowSize()); setSize(getMainWindowSize()); initComponents(); // Set up the semi-transparent JPanel semiTransparentJPanel = new SemiTransparentJPanel(Boolean.FALSE); getLayeredPane().add(semiTransparentJPanel, JLayeredPane.PALETTE_LAYER); // Set up the resizer resizer = new Resizer(browser, this, Boolean.FALSE, Resizer.ResizeEdges.ALL_EDGES); }
initMenu();
initMenu(maximized);
BrowserWindow(final Browser browser) throws HeadlessException { super("BrowserWindow"); this.browser = browser; this.logger = Logger.getLogger(getClass()); this.persistence = PersistenceFactory.getPersistence(getClass()); getRootPane().setBorder(new WindowBorder2()); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { persist(); browser.hibernate(); }}); initMenu(); setIconImage(com.thinkparity.ophelia.browser.Constants.Images.WINDOW_ICON_IMAGE); setTitle(java.util.ResourceBundle.getBundle("localization/JFrame_Messages").getString("BrowserWindow.Title")); setUndecorated(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); final Point location = persistence.get("location", new Point(100, 100)); location.x = (location.x < 0 ? 0 : location.x ); location.y = (location.y < 0 ? 0 : location.y ); setLocation(location.x, location.y); final Dimension size = persistence.get("size", getMainWindowSize()); mainWindowSize.setSize(size); setResizable(true); setMinimumSize(getMainWindowSize()); setSize(getMainWindowSize()); initComponents(); // Set up the semi-transparent JPanel semiTransparentJPanel = new SemiTransparentJPanel(Boolean.FALSE); getLayeredPane().add(semiTransparentJPanel, JLayeredPane.PALETTE_LAYER); // Set up the resizer resizer = new Resizer(browser, this, Boolean.FALSE, Resizer.ResizeEdges.ALL_EDGES); }
final Point location = persistence.get("location", new Point(100, 100));
final Point location = persistence.get("location", getMainWindowLocation());
BrowserWindow(final Browser browser) throws HeadlessException { super("BrowserWindow"); this.browser = browser; this.logger = Logger.getLogger(getClass()); this.persistence = PersistenceFactory.getPersistence(getClass()); getRootPane().setBorder(new WindowBorder2()); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { persist(); browser.hibernate(); }}); initMenu(); setIconImage(com.thinkparity.ophelia.browser.Constants.Images.WINDOW_ICON_IMAGE); setTitle(java.util.ResourceBundle.getBundle("localization/JFrame_Messages").getString("BrowserWindow.Title")); setUndecorated(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); final Point location = persistence.get("location", new Point(100, 100)); location.x = (location.x < 0 ? 0 : location.x ); location.y = (location.y < 0 ? 0 : location.y ); setLocation(location.x, location.y); final Dimension size = persistence.get("size", getMainWindowSize()); mainWindowSize.setSize(size); setResizable(true); setMinimumSize(getMainWindowSize()); setSize(getMainWindowSize()); initComponents(); // Set up the semi-transparent JPanel semiTransparentJPanel = new SemiTransparentJPanel(Boolean.FALSE); getLayeredPane().add(semiTransparentJPanel, JLayeredPane.PALETTE_LAYER); // Set up the resizer resizer = new Resizer(browser, this, Boolean.FALSE, Resizer.ResizeEdges.ALL_EDGES); }
mainWindowSize.setSize(size);
mainWindowSize.setSize(size); if (maximized) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); setMaximizedBounds(env.getMaximumWindowBounds()); setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH); }
BrowserWindow(final Browser browser) throws HeadlessException { super("BrowserWindow"); this.browser = browser; this.logger = Logger.getLogger(getClass()); this.persistence = PersistenceFactory.getPersistence(getClass()); getRootPane().setBorder(new WindowBorder2()); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { persist(); browser.hibernate(); }}); initMenu(); setIconImage(com.thinkparity.ophelia.browser.Constants.Images.WINDOW_ICON_IMAGE); setTitle(java.util.ResourceBundle.getBundle("localization/JFrame_Messages").getString("BrowserWindow.Title")); setUndecorated(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); final Point location = persistence.get("location", new Point(100, 100)); location.x = (location.x < 0 ? 0 : location.x ); location.y = (location.y < 0 ? 0 : location.y ); setLocation(location.x, location.y); final Dimension size = persistence.get("size", getMainWindowSize()); mainWindowSize.setSize(size); setResizable(true); setMinimumSize(getMainWindowSize()); setSize(getMainWindowSize()); initComponents(); // Set up the semi-transparent JPanel semiTransparentJPanel = new SemiTransparentJPanel(Boolean.FALSE); getLayeredPane().add(semiTransparentJPanel, JLayeredPane.PALETTE_LAYER); // Set up the resizer resizer = new Resizer(browser, this, Boolean.FALSE, Resizer.ResizeEdges.ALL_EDGES); }
if (!maximized) { roundCorners(); }
BrowserWindow(final Browser browser) throws HeadlessException { super("BrowserWindow"); this.browser = browser; this.logger = Logger.getLogger(getClass()); this.persistence = PersistenceFactory.getPersistence(getClass()); getRootPane().setBorder(new WindowBorder2()); addWindowListener(new WindowAdapter() { public void windowClosing(final WindowEvent e) { persist(); browser.hibernate(); }}); initMenu(); setIconImage(com.thinkparity.ophelia.browser.Constants.Images.WINDOW_ICON_IMAGE); setTitle(java.util.ResourceBundle.getBundle("localization/JFrame_Messages").getString("BrowserWindow.Title")); setUndecorated(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); final Point location = persistence.get("location", new Point(100, 100)); location.x = (location.x < 0 ? 0 : location.x ); location.y = (location.y < 0 ? 0 : location.y ); setLocation(location.x, location.y); final Dimension size = persistence.get("size", getMainWindowSize()); mainWindowSize.setSize(size); setResizable(true); setMinimumSize(getMainWindowSize()); setSize(getMainWindowSize()); initComponents(); // Set up the semi-transparent JPanel semiTransparentJPanel = new SemiTransparentJPanel(Boolean.FALSE); getLayeredPane().add(semiTransparentJPanel, JLayeredPane.PALETTE_LAYER); // Set up the resizer resizer = new Resizer(browser, this, Boolean.FALSE, Resizer.ResizeEdges.ALL_EDGES); }
final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input);
final Long documentId = (Long) ((Pair) input).getFirst(); final Filter<Artifact> filter = (Filter<Artifact>) ((Pair) input).getSecond();
public MainProvider(final ArtifactModel artifactModel, final DocumentModel documentModel, final SystemMessageModel systemMessageModel) { super(); this.documentProvider = new SingleContentProvider() { public Object getElement(final Object input) { final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input); try { final Document document = documentModel.get(documentId); return toDisplay(document, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.documentsProvider = new FlatContentProvider() { public Object[] getElements(final Object input) { try { // sort by: // +> remote update ? later b4 earlier // +> last update ? later b4 earlier final AbstractArtifactComparator sort = new RemoteUpdatedOnComparator(Boolean.FALSE); sort.add(new UpdatedOnComparator(Boolean.FALSE)); final Filter<Artifact> filter = (Filter<Artifact>) input; final Collection<Document> documents; if(null != filter) { documents = documentModel.list(sort, filter); } else { documents = documentModel.list(sort); } return toDisplay(documents, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.systemMessageProvider = new SingleContentProvider() { public Object getElement(final Object input) { final Long systemMessageId = assertNotNullLong( "The main provider's system message provider " + "requires non-null java.lang.Long input.", input); final SystemMessage systemMessage; try { systemMessage = systemMessageModel.read(systemMessageId); } catch(final ParityException px) { throw new RuntimeException(px); } switch(systemMessage.getType()) { case INFO: case CONTACT_INVITATION: case CONTACT_INVITATION_RESPONSE: return systemMessage; case KEY_REQUEST: case KEY_RESPONSE: return null; default: throw Assert.createUnreachable("Unknown message type: " + systemMessage.getType()); } } }; this.systemMessagesProvider = new FlatContentProvider() { public Object[] getElements(final Object input) { try { final Collection<SystemMessage> messages = systemMessageModel.readForNonArtifacts(); return messages.toArray(new SystemMessage[] {}); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.flatProviders = new FlatContentProvider[] {documentsProvider, systemMessagesProvider}; this.singleProviders = new SingleContentProvider[] {documentProvider, systemMessageProvider}; }
return toDisplay(document, artifactModel);
if(filter.doFilter(document)) { return null; } else { return toDisplay(document, artifactModel); }
public MainProvider(final ArtifactModel artifactModel, final DocumentModel documentModel, final SystemMessageModel systemMessageModel) { super(); this.documentProvider = new SingleContentProvider() { public Object getElement(final Object input) { final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input); try { final Document document = documentModel.get(documentId); return toDisplay(document, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.documentsProvider = new FlatContentProvider() { public Object[] getElements(final Object input) { try { // sort by: // +> remote update ? later b4 earlier // +> last update ? later b4 earlier final AbstractArtifactComparator sort = new RemoteUpdatedOnComparator(Boolean.FALSE); sort.add(new UpdatedOnComparator(Boolean.FALSE)); final Filter<Artifact> filter = (Filter<Artifact>) input; final Collection<Document> documents; if(null != filter) { documents = documentModel.list(sort, filter); } else { documents = documentModel.list(sort); } return toDisplay(documents, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.systemMessageProvider = new SingleContentProvider() { public Object getElement(final Object input) { final Long systemMessageId = assertNotNullLong( "The main provider's system message provider " + "requires non-null java.lang.Long input.", input); final SystemMessage systemMessage; try { systemMessage = systemMessageModel.read(systemMessageId); } catch(final ParityException px) { throw new RuntimeException(px); } switch(systemMessage.getType()) { case INFO: case CONTACT_INVITATION: case CONTACT_INVITATION_RESPONSE: return systemMessage; case KEY_REQUEST: case KEY_RESPONSE: return null; default: throw Assert.createUnreachable("Unknown message type: " + systemMessage.getType()); } } }; this.systemMessagesProvider = new FlatContentProvider() { public Object[] getElements(final Object input) { try { final Collection<SystemMessage> messages = systemMessageModel.readForNonArtifacts(); return messages.toArray(new SystemMessage[] {}); } catch(final ParityException px) { throw new RuntimeException(px); } } }; this.flatProviders = new FlatContentProvider[] {documentsProvider, systemMessagesProvider}; this.singleProviders = new SingleContentProvider[] {documentProvider, systemMessageProvider}; }
final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input);
final Long documentId = (Long) ((Pair) input).getFirst(); final Filter<Artifact> filter = (Filter<Artifact>) ((Pair) input).getSecond();
public Object getElement(final Object input) { final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input); try { final Document document = documentModel.get(documentId); return toDisplay(document, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } }
return toDisplay(document, artifactModel);
if(filter.doFilter(document)) { return null; } else { return toDisplay(document, artifactModel); }
public Object getElement(final Object input) { final Long documentId = assertNotNullLong( "The main provider's document provider requires " + "non-null java.lang.Long input.", input); try { final Document document = documentModel.get(documentId); return toDisplay(document, artifactModel); } catch(final ParityException px) { throw new RuntimeException(px); } }
final ContainerVersion latestVersion = readLatestVersion(containerId); return null != latestVersion;
final List<ContainerVersion> versions = readVersions(containerId); return 0 < versions.size();
private Boolean isDistributed(final Long containerId) { final ContainerVersion latestVersion = readLatestVersion(containerId); return null != latestVersion; }
for (final Document document : draft.getDocuments()) { if (ContainerDraft.ArtifactState.NONE == draft.getState(document)) { if (documentModel.isDraftModified(document.getId())) { draft.putState(document, ContainerDraft.ArtifactState.MODIFIED);
if (null != draft) { for (final Document document : draft.getDocuments()) { if (ContainerDraft.ArtifactState.NONE == draft.getState(document)) { if (documentModel.isDraftModified(document.getId())) { draft.putState(document, ContainerDraft.ArtifactState.MODIFIED); }
ContainerDraft readDraft(final Long containerId) { logger.info(getApiId("[READ DRAFT]")); logger.debug(containerId); final InternalDocumentModel documentModel = getInternalDocumentModel(); final ContainerDraft draft = containerIO.readDraft(containerId); for (final Document document : draft.getDocuments()) { if (ContainerDraft.ArtifactState.NONE == draft.getState(document)) { if (documentModel.isDraftModified(document.getId())) { draft.putState(document, ContainerDraft.ArtifactState.MODIFIED); } } } return draft; }
void create(final Long containerId, final String containerName) throws ParityException {
void create(final Long containerId, final String containerName) {
void create(final Long containerId, final String containerName) throws ParityException { getInternalIndexModel().createContainer(containerId, containerName); }
protected void assertOnline(final StringBuffer api) { assertOnline(api.toString());
protected void assertOnline(final String assertion) { Assert.assertTrue(assertion, isOnline());
protected void assertOnline(final StringBuffer api) { assertOnline(api.toString()); }
void delete(final Long containerId) throws ParityException {
void delete(final Long containerId) {
void delete(final Long containerId) throws ParityException { getInternalIndexModel().deleteArtifact(containerId); }
public Container read(final Long containerId);
public List<Container> read();
public Container read(final Long containerId);
final String containerName) throws ParityException {
final String containerName) {
public void createContainer(final Long containerId, final String containerName) throws ParityException { synchronized(getImplLock()) { getImpl().createContainer(containerId, containerName); } }
void createContainer(final Long containerId, final String containerName) throws ParityException { logger.info(getApiId("[CREATE CONTAINER]")); logger.debug(containerId); logger.debug(containerName);
void createContainer(final Long containerId, final String containerName) { logApiId(); logVariable("containerId", containerId); logVariable("containerName", containerName); try { final Set<User> team = getInternalArtifactModel().readTeam(containerId); final DocumentBuilder documentBuilder = new DocumentBuilder(6); documentBuilder.append(IDX_ARTIFACT_ID.setValue(containerId).toField()) .append(IDX_ARTIFACT_TYPE.setValue(ArtifactType.CONTAINER).toField()) .append(IDX_ARTIFACT_NAME.setValue(containerName).toField()) .append(IDX_ARTIFACT_CONTACTS.setValue(team).toField());
void createContainer(final Long containerId, final String containerName) throws ParityException { logger.info(getApiId("[CREATE CONTAINER]")); logger.debug(containerId); logger.debug(containerName); final Set<User> team = getInternalArtifactModel().readTeam(containerId); final DocumentBuilder documentBuilder = new DocumentBuilder(6); documentBuilder.append(IDX_ARTIFACT_ID.setValue(containerId).toField()) .append(IDX_ARTIFACT_TYPE.setValue(ArtifactType.CONTAINER).toField()) .append(IDX_ARTIFACT_NAME.setValue(containerName).toField()) .append(IDX_ARTIFACT_CONTACTS.setValue(team).toField()); index(documentBuilder.toDocument()); }
final Set<User> team = getInternalArtifactModel().readTeam(containerId); final DocumentBuilder documentBuilder = new DocumentBuilder(6); documentBuilder.append(IDX_ARTIFACT_ID.setValue(containerId).toField()) .append(IDX_ARTIFACT_TYPE.setValue(ArtifactType.CONTAINER).toField()) .append(IDX_ARTIFACT_NAME.setValue(containerName).toField()) .append(IDX_ARTIFACT_CONTACTS.setValue(team).toField()); index(documentBuilder.toDocument());
index(documentBuilder.toDocument()); } catch (final Throwable t) { throw translateError("CREATE CONTAINER", t); }
void createContainer(final Long containerId, final String containerName) throws ParityException { logger.info(getApiId("[CREATE CONTAINER]")); logger.debug(containerId); logger.debug(containerName); final Set<User> team = getInternalArtifactModel().readTeam(containerId); final DocumentBuilder documentBuilder = new DocumentBuilder(6); documentBuilder.append(IDX_ARTIFACT_ID.setValue(containerId).toField()) .append(IDX_ARTIFACT_TYPE.setValue(ArtifactType.CONTAINER).toField()) .append(IDX_ARTIFACT_NAME.setValue(containerName).toField()) .append(IDX_ARTIFACT_CONTACTS.setValue(team).toField()); index(documentBuilder.toDocument()); }