Skip to content

Commit

Permalink
Code and documentation clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sasq64 committed Nov 10, 2015
1 parent 9026129 commit f642625
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 64 deletions.
4 changes: 4 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ Copyright (c) 1998-2011 The OpenSSL Project. All rights reserved.<br/>
Copyright (C) 1995-1998 Eric Young ([email protected])<br/>
See src/openssl/LICENCE<br/>

### miniz (deflate.cpp)

Public domain<br/>
Rich Geldreich ([email protected])</br>

7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ by _Jonas Minnberg_ ([email protected])

* Parallell zip compression using *Info-ZIP* deflate or *Intel* fast deflate
* On-the-fly Jar signing
* Created with the goal of fast APK creation.
* Created with the goal of fast APK creation

* Check LICENCE.md for license
* Check _LICENCE.md_ for license information

## Build (Unix/OSX)

* Make sure you have *yasm* and *openssl* installed.
* `make`
* `WITH_INTEL=0 make` - Build without Intel code
* `WITH_INTEL=0 make` - Build without Intel code.
* `./test.py` - Simple functional test

## Build (Windows)
Expand All @@ -25,6 +25,7 @@ by _Jonas Minnberg_ ([email protected])

fastzip <directory>
fastzip <file.zip> <paths>...
fastzip --help

## Speed Tests

Expand Down
2 changes: 1 addition & 1 deletion src/asn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace asn1
}
else if (der.tag == 0x02)
{
printf("%llx\n", der.value);
printf("%llx\n", (unsigned long long)der.value);
}
else if (der.tag == 03 || der.tag == 4 || der.tag == 6)
{
Expand Down
2 changes: 1 addition & 1 deletion src/asn.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace asn1
{
// Routines for ASN.1 reading and creation
// Routines for ASN.1 reading and creation

class MemBuffer
{
Expand Down
34 changes: 10 additions & 24 deletions src/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,13 @@ vector<uint8_t> recoverKey(const string &password, const vector<uint8_t> &protec
plainKey[i] = (uint8_t)(encrKey[i] ^ xorKey[i]);
}

/*
* Check the integrity of the recovered key by concatenating it with
* the password, digesting the concatenation, and comparing the
* result of the digest operation with the digest provided at the end
* of <code>protectedKey</code>. If the two digest values are
* different, throw an exception.
*/

// Check the integrity of the recovered key by concatenating it with
// the password, digesting the concatenation, and comparing the
// result of the digest operation with the digest provided at the end
// of 'protectedKey'. If the two digest values are
// different, throw an exception.
SHA1_Update(&context, &passwdBytes[0], passwdBytes.size());
// Arrays.fill(passwdBytes, (uint8_t)0x00);
// passwdBytes = null;
SHA1_Update(&context, &plainKey[0], plainKey.size());
SHA1_Final(&digest[0], &context);

Expand Down Expand Up @@ -216,21 +213,17 @@ vector<uint8_t> KeyStore::getKey(const string &pass, const string &name)
auto alias = membuf.readString();
auto timestamp = membuf.read<uint64_t>();

// printf("%d Alias %s\n", i, alias.c_str());

assert(tag == 1);

auto length = membuf.read<uint32_t>();
DER keyData = readDER(membuf.readBuffer(length).buffer());
if (!foundKey && (name == "" || alias == name))
{
// printf("%02x\n", keyData.tag);
for (int i = 0; i < keyData.size(); i++)
{
if (keyData[i].tag == 0x04)
{
privateKey = keyData[i].data;
// printf("Found private key %lx bytes\n", privateKey.size());
foundKey = true;
break;
}
Expand All @@ -239,27 +232,20 @@ vector<uint8_t> KeyStore::getKey(const string &pass, const string &name)

auto ccount = membuf.read<uint32_t>();
auto certName = membuf.readString();
// printf("%d %s\n", ccount, certName.c_str());
for (unsigned j = 0; j < ccount; j++)

for (unsigned j = 0; j < ccount; j++)
{
auto clen = membuf.read<uint32_t>();
auto certBuf = membuf.readBuffer(clen);
if (foundKey)
{
certificate = certBuf.buffer();
DER certData = readDER(certificate);
// dumpDER(certificate);

for (int i = 0; i < certData[0].size(); i++)
{
// printf("%02x\n", certDatag173.tag);
if (certData[0][i].tag == 0x30 && certData[0][i][0].tag == 0x31)
{
// printf("FOUND\n");
// certMetaData = MemBuffer(certData[0][i].data);
// dumpDER(certMetaData);
break;
}
}
}
}
Expand All @@ -273,8 +259,8 @@ vector<uint8_t> KeyStore::getKey(const string &pass, const string &name)
auto plainKey = recoverKey(pass, privateKey);

DER keyData = readDER(plainKey);
// dumpDER(MemBuffer(plainKey));
for (int j = 0; j < keyData.size(); j++)

for (int j = 0; j < keyData.size(); j++)
{
if (keyData[j].tag == 0x04)
{
Expand Down
3 changes: 0 additions & 3 deletions src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ class KeyStore
asn1::MemBuffer membuf;

std::vector<uint8_t> certificate;
// std::vector<uint8_t> privateKey;
// std::unordered_map<std::string, std::vector<uint8_t>> keys;
// MemBuffer certMetaData;
};

#endif // CRYPTO_H
29 changes: 6 additions & 23 deletions src/fastzip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ static int store_compressed(FILE *fp, int inSize, uint8_t *target, uint8_t *sha)
uint8_t *fileData = target;
fread(fileData, 1, inSize, fp);

// printf("%02x %02x\n", fileData[0], fileData[1]);
int total = 0;
if (sha)
{
Expand All @@ -65,8 +64,8 @@ static int store_compressed(FILE *fp, int inSize, uint8_t *target, uint8_t *sha)
mz_stream stream;
memset(&stream, 0, sizeof(stream));
int rc = mz_inflateInit2(&stream, -MZ_DEFAULT_WINDOW_BITS);
// printf("RC %d\n", rc);
stream.next_in = fileData;

stream.next_in = fileData;
stream.avail_in = inSize;

stream.next_out = buf;
Expand All @@ -86,8 +85,8 @@ static int store_compressed(FILE *fp, int inSize, uint8_t *target, uint8_t *sha)
}
total += (bufSize - stream.avail_out);
}
// printf("Total %d bytes unpacked\n", total);
mz_inflate(&stream, MZ_FINISH);

mz_inflate(&stream, MZ_FINISH);

SHA1_Final(sha, &context);
}
Expand Down Expand Up @@ -125,7 +124,7 @@ static PackResult store_uncompressed(FILE *fp, int inSize, uint8_t *out, size_t

#ifdef WITH_INTEL

static PackResult intel_deflate(FILE *fp, int inSize, uint8_t *buffer, size_t *outSize,
static PackResult intel_deflate(FILE *fp, size_t inSize, uint8_t *buffer, size_t *outSize,
uint32_t *checksum, uint8_t *sha, int earlyOut)
{
LZ_Stream2 stream __attribute__((aligned(16)));
Expand All @@ -135,11 +134,8 @@ static PackResult intel_deflate(FILE *fp, int inSize, uint8_t *buffer, size_t *o
if (fread(fileData, 1, inSize, fp) != inSize)
return RC_FAILED;

// uint8_t *fileData = new uint8_t [inSize];
uint8_t *inBuf = fileData;

// fread(fileData, 1, inSize, fp);

if (sha)
{
SHA_CTX context;
Expand Down Expand Up @@ -212,7 +208,6 @@ static PackResult intel_deflate(FILE *fp, int inSize, uint8_t *buffer, size_t *o
int percent = (int)(*outSize * 100 / inSize);
if (earlyOut && (buffer + *outSize < fileData))
{
// printf("Ratio at end: %d%%\n", percent);
if (percent >= earlyOut)
{
*outSize = inSize;
Expand All @@ -225,7 +220,6 @@ static PackResult intel_deflate(FILE *fp, int inSize, uint8_t *buffer, size_t *o
}
}

// delete [] fileData;
return RC_COMPRESSED;
}

Expand Down Expand Up @@ -325,8 +319,6 @@ void Fastzip::packZipData(FILE *fp, int size, PackFormat inFormat, PackFormat ou
outSize = size;
}

// printf("%s %s", target.name.c_str(), base64_encode(target.sha, SHA_LEN).c_str());

target.data = outBuf;
target.dataSize = outSize;
}
Expand Down Expand Up @@ -432,7 +424,6 @@ void Fastzip::exec()
throw fastzip_exception("No paths specified");
if (fileNames.size() >= 65535)
{
// throw fastzip_exception("Can not put more than 64K files in one archive");
warning("More than 64K files, adding 64bit features.");
}
if (zipfile == "")
Expand Down Expand Up @@ -582,12 +573,7 @@ void Fastzip::exec()
if (doSeq)
{
while (fileName.index != currentIndex)
{
// printf("%s:%d != %d\n", fileName.target.c_str(),
// fileName.index,
// currentIndex);
seq_cv.wait(lock);
}
}
if (doSign)
{
Expand All @@ -603,7 +589,7 @@ void Fastzip::exec()
}
if (doSeq)
seq_cv.notify_all();
delete entry.data;
delete [] entry.data;
}
else
{
Expand All @@ -627,8 +613,6 @@ void Fastzip::exec()
for (int i = 0; i < threadCount; i++)
workerThreads[i].join();

// intf("%ld %ld\n", (long)0xff000000, sizeof(long));

if (ftell_x(zipArchive.getFile()) > (int64_t)0xff000000)
throw fastzip_exception("Resulting file too large");

Expand Down Expand Up @@ -753,7 +737,6 @@ void Fastzip::sign(ZipArchive &zipArchive, const string &digestFile)

RSA_sign(NID_sha1, digest, SHA_LEN, &sign[0], &signLen, rsa);

// printf("signLen %d\n", signLen);
sign.resize(signLen);

auto data =
Expand Down
7 changes: 0 additions & 7 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <functional>
#include <sys/stat.h>
#include <unistd.h>
//#include <fcntl.h>
#include <dirent.h>
#include <cstring>
#include <deque>
Expand All @@ -28,9 +27,6 @@
#define fseek_x fseek
#endif

// template <class C, template <typename> class CONTAINER>
// std::vector<CONTAINER<C>> split(const CONTAINER<C> &s, const std::string &delim, unsigned limit);

template<class C>
std::vector<std::basic_string<C> > split(const std::basic_string<C> &s,
const std::string &delim = " ", unsigned limit = 0)
Expand Down Expand Up @@ -176,9 +172,6 @@ template<typename T> class UniQueue : public std::deque<std::reference_wrapper<c
PARENT::pop_front();
}

// void push_front(const T& value) {}
// template <class ... S> void emplace_front(S&&... s) {}

private:
void cleanup()
{
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import os;

try :
print subprocess.check_output('./fastzip -8 infozip.zip infozip'.split())
print subprocess.check_output('./fastzip -8 infozip.zip src/infozip'.split())
print subprocess.check_output('./fastzip -0 obj.zip obj'.split())
print subprocess.check_output('./fastzip -v --sign -Z infozip.zip igzip.zip igzip -0 -Z obj.zip fastzip=fs.stored -9 fastzip=packed/fastzip'.split())
print subprocess.check_output('./fastzip -j -v --sign -Z infozip.zip igzip.zip src/igzip -0 -Z obj.zip fastzip=fs.stored -9 fastzip=packed/fastzip'.split())
print subprocess.check_output('unzip -lv igzip.zip'.split())
output = subprocess.check_output('unzip -t igzip.zip'.split())
if output.find('No errors detected') >= 0 :
Expand Down

0 comments on commit f642625

Please sign in to comment.