Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/build.xml

# maven
/dependency-reduced-pom.xml
/target

# vim
Expand All @@ -29,4 +30,4 @@
*.iml
*.ipr
*.iws
.idea/
.idea/
49 changes: 45 additions & 4 deletions pom.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
<groupId>net.techcable</groupId>
<artifactId>parent</artifactId>
<version>1.0.5</version>
</parent>

<groupId>net.md-5</groupId>
<artifactId>jbeat</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.3-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jbeat</name>
Expand All @@ -24,6 +24,14 @@
</license>
</licenses>

<dependencies>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.47</version>
</dependency>
</dependencies>

<developers>
<developer>
<id>md_5</id>
Expand All @@ -47,7 +55,15 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>techcable-repo</id>
<url>http://repo.techcable.net/content/groups/public</url>
</repository>
</repositories>

<build>
<finalName>jbeat</finalName>
<plugins>
<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
Expand Down Expand Up @@ -78,6 +94,31 @@
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Main-Class>net.md_5.jbeat.Main</Main-Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
63 changes: 29 additions & 34 deletions src/main/java/net/md_5/jbeat/LinearCreator.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
/**
* Copyright (c) 2012, md_5. All rights reserved.
* The MIT License
* Copyright (c) 2015 Techcable
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* The name of the author may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.md_5.jbeat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import net.md_5.jbeat.util.ByteBuf;

import static net.md_5.jbeat.Shared.*;

/**
Expand All @@ -41,28 +36,28 @@ public final class LinearCreator extends PatchCreator {

private int targetReadLength, targetRelativeOffset, outputOffset;

public LinearCreator(File original, File modified, File output) throws FileNotFoundException {
super(original, modified, output);
public LinearCreator(ByteBuf source, long sourceLength, ByteBuf modified, long modifiedLength, ByteBuf output, String header) {
super(source, sourceLength, modified, modifiedLength, output, header);
}

public LinearCreator(File original, File modified, File output, String header) throws FileNotFoundException {
super(original, modified, output, header);
public LinearCreator(ByteBuf source, long sourceLength, ByteBuf modified, long modifiedLength, ByteBuf output) {
super(source, sourceLength, modified, modifiedLength, output, null);
}

@Override
protected void doPatch() throws IOException {
while (outputOffset < target.limit()) {
while (outputOffset < target.readableBytes()) {
int sourcePos = 0;
for (int n = 0; outputOffset + n < Math.min(sourceLength, targetLength); n++) {
if (source.get(outputOffset + n) != target.get(outputOffset + n)) {
if (source.read(outputOffset + n) != target.read(outputOffset + n)) {
break;
}
sourcePos++;
}

int rleLength = 0;
for (int n = 1; outputOffset + n < target.limit(); n++) {
if (target.get(outputOffset) != target.get(outputOffset + n)) {
for (int n = 1; outputOffset + n < target.readableBytes(); n++) {
if (target.read(outputOffset) != target.read(outputOffset + n)) {
break;
}
rleLength++;
Expand Down Expand Up @@ -100,7 +95,7 @@ private void targetReadFlush() throws IOException {
encode(out, TARGET_READ | ((targetReadLength - 1) << 2));
int offset = outputOffset - targetReadLength;
while (targetReadLength != 0) {
out.write(target.get(offset++));
out.write(target.read(offset++));
targetReadLength--;
}
}
Expand Down
181 changes: 181 additions & 0 deletions src/main/java/net/md_5/jbeat/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* The MIT License
* Copyright (c) 2015 Techcable
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.md_5.jbeat;

import lombok.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel.MapMode;

import net.md_5.jbeat.util.ByteBuf;
import net.md_5.jbeat.util.ByteBufs;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.converters.FileConverter;

public class Main {

public static void main(String[] rawArgs) {
JBeatOptions options = new JBeatOptions();
new JCommander(options, rawArgs);
if (options.getFirstFile() != null && options.getSecondFile() != null) {
createPatch(options.getFirstFile(), options.getSecondFile(), options.getPatchOut(), true);
} else if (options.getInFile() != null && options.getOutFile() != null && options.getPatchIn() != null) {
patch(options.getInFile(), options.getPatchIn(), options.getOutFile());
} else {
System.err.println("You must either specify --first-file and --second-file to generate a patch");
System.err.println("Or --in-file --patch-in and --out-file to patch a file");
System.exit(1);
}
}

public static void createPatch(File first, File second, File patch, boolean linear) {
if (patch.exists()) {
System.err.println("The patch file " + patch.getName() + " exists.");
System.err.println("Please delete it before running this program");
System.exit(1);
}
if (!linear) throw new UnsupportedOperationException("Only linear patch creation is supported");
createFile(patch);
PatchCreator patchCreator = null;
RandomAccessFile firstRandom = null, secondRandom = null, output = null;
try {
firstRandom = new RandomAccessFile(first, "r");
secondRandom = new RandomAccessFile(second, "r");
output = new RandomAccessFile(patch, "rw");
} catch (FileNotFoundException e) {
System.err.println("Unable to open file: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
try {
long firstLength = firstRandom.length();
long secondLength = secondRandom.length();
ByteBuf firstBuffer = ByteBufs.wrap(firstRandom.getChannel().map(MapMode.READ_ONLY, 0, firstLength));
ByteBuf secondBuffer = ByteBufs.wrap(secondRandom.getChannel().map(MapMode.READ_ONLY, 0, secondLength));
ByteBuf outputBuffer = ByteBufs.create();
patchCreator = linear ? new LinearCreator(firstBuffer, firstLength, secondBuffer, secondLength, outputBuffer) : null;
output.write(outputBuffer.array());
output.close();
patchCreator.create();
} catch (Exception e) {
System.err.println("Unable to create patch of " + first.getName() + " and " + second.getName());
e.printStackTrace();
System.exit(1);
} finally {
try {
if (firstRandom != null) firstRandom.close();
if (secondRandom != null) secondRandom.close();
;
} catch (IOException e) {
System.err.println("Unable to close files");
}
}
System.out.println("Successfuly created patch of " + first.getName() + " and " + second.getName() + " in " + patch.getName());
}

public static void patch(File in, File patch, File out) {
if (out.exists()) {
System.err.println("The output file " + out.getName() + " exists.");
System.err.println("Please delete it before running this program");
System.exit(1);
}
createFile(out);

RandomAccessFile inRandom = null, patchRandom = null, outRandom = null;
try {
inRandom = new RandomAccessFile(in, "r");
patchRandom = new RandomAccessFile(patch, "r");
outRandom = new RandomAccessFile(out, "rw");
} catch (FileNotFoundException ex) {
System.err.println("No such file: " + ex.getMessage());
ex.printStackTrace();
System.exit(1);
}
try {
ByteBuf inBytes = ByteBufs.wrap(inRandom.getChannel().map(MapMode.READ_ONLY, 0, inRandom.length()));
ByteBuf patchBytes = ByteBufs.wrap(patchRandom.getChannel().map(MapMode.READ_ONLY, 0, patchRandom.length()));
ByteBuf outBytes = ByteBufs.create((int) inRandom.length());
Patcher patcher = new Patcher(patchBytes, patchRandom.length(), inBytes, outBytes);
patcher.patch();
outRandom.write(outBytes.array());
} catch (Exception e) {
System.err.println("Unable to patch file " + in.getName());
e.printStackTrace();
System.exit(1);
} finally {
try {
if (inRandom != null) inRandom.close();
if (patchRandom != null) patchRandom.close();
if (outRandom != null) outRandom.close();
} catch (IOException e) {
System.err.println("Unable to close: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}

System.out.println("Successfuly Patched: " + in.getName() + " with " + patch.getName());
}

private static void createFile(File f) {
try {
f.createNewFile();
} catch (Exception e) {
System.err.println("Unable to create file:" + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}

@Getter
public static class JBeatOptions {

@Parameter(names = {"-a", "--first-file"}, description = "Original file for generating patch", converter = FileConverter.class)
private File firstFile;

@Parameter(names = {"-b", "--second-file"}, description = "Changed file for generating patch", converter = FileConverter.class)
private File secondFile;

@Parameter(names = {"--patch-out"}, description = "The file to put the generated patch in", converter = FileConverter.class)
private File patchOut = new File("output.bps");

@Parameter(names = {"-i", "--in-file"}, description = "Input file to patch", converter = FileConverter.class)
private File inFile;

@Parameter(names = {"-o", "--out-file"}, description = "Write patched file here", converter = FileConverter.class)
private File outFile;

@Parameter(names = {"-p", "--patch-in"}, description = "Patch to use to patch in file", converter = FileConverter.class)
private File patchIn;

/* JBeat only supports linear patches
@Paramater(names = {"-l", "--linear-patch"}, description = "Create binary patches in a linear fashion without delta compression.")
private boolean linearPatch = true;
*/
}
}
Loading