diff --git a/lib/cryptopp/License.txt b/lib/cryptopp/License.txt new file mode 100644 index 0000000..e2a30d9 --- /dev/null +++ b/lib/cryptopp/License.txt @@ -0,0 +1,83 @@ +Compilation Copyright (c) 1995-2019 by Wei Dai. All rights reserved. +This copyright applies only to this software distribution package +as a compilation, and does not imply a copyright on any particular +file in the package. + +All individual files in this compilation are placed in the public domain by +Wei Dai and other contributors. + +I would like to thank the following authors for placing their works into +the public domain: + +Joan Daemen - 3way.cpp +Leonard Janke - cast.cpp, seal.cpp +Steve Reid - cast.cpp +Phil Karn - des.cpp +Andrew M. Kuchling - md2.cpp, md4.cpp +Colin Plumb - md5.cpp +Seal Woods - rc6.cpp +Chris Morgan - rijndael.cpp +Paulo Baretto - rijndael.cpp, skipjack.cpp, square.cpp +Richard De Moliner - safer.cpp +Matthew Skala - twofish.cpp +Kevin Springle - camellia.cpp, shacal2.cpp, ttmac.cpp, whrlpool.cpp, ripemd.cpp +Ronny Van Keer - sha3.cpp +Aumasson, Neves, Wilcox-O'Hearn and Winnerlein - blake2.cpp, blake2b_simd.cpp, blake2s_simd.cpp +Aaram Yun - aria.cpp, aria_simd.cpp +Han Lulu, Markku-Juhani O. Saarinen - sm4.cpp sm4_simd.cpp +Daniel J. Bernstein, Jack Lloyd - chacha.cpp, chacha_simd.cpp, chacha_avx.cpp +Andrew Moon - ed25519, x25519, donna_32.cpp, donna_64.cpp, donna_sse.cpp + +The Crypto++ Library uses portions of Andy Polyakov's CRYPTOGAMS for Poly1305 +scalar multiplication, aes_armv4.S, sha1_armv4.S and sha256_armv4.S. CRYPTOGAMS +is dual licensed with a permissive BSD-style license. The CRYPTOGAMS license is +reproduced below. + +The Crypto++ Library uses portions of Jack Lloyd's Botan for ChaCha SSE2 and +AVX. Botan placed the code in public domain for Crypto++ to use. + +The Crypto++ Library (as a compilation) is currently licensed under the Boost +Software License 1.0 (http://www.boost.org/users/license.html). + +Boost Software License - Version 1.0 - August 17th, 2003 + +Permission is hereby granted, free of charge, to any person or organization +obtaining a copy of the software and accompanying documentation covered by +this license (the "Software") to use, reproduce, display, distribute, +execute, and transmit the Software, and to prepare derivative works of the +Software, and to permit third-parties to whom the Software is furnished to +do so, all subject to the following: + +The copyright notices in the Software and this entire statement, including +the above license grant, this restriction and the following disclaimer, +must be included in all copies of the Software, in whole or in part, and +all derivative works of the Software, unless such copies or derivative +works are solely in the form of machine-executable object code generated by +a source language processor. + +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, TITLE AND NON-INFRINGEMENT. IN NO EVENT +SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +CRYPTOGAMS License + +Copyright (c) 2006-2017, CRYPTOGAMS by +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +* Redistributions of source code must retain copyright notices, + this list of conditions and the following disclaimer. +* 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. +* Neither the name of the CRYPTOGAMS nor the names of its copyright + holder and contributors may be used to endorse or promote products + derived from this software without specific prior written permission. diff --git a/lib/cryptopp/readme.md b/lib/cryptopp/readme.md new file mode 100644 index 0000000..860fe15 --- /dev/null +++ b/lib/cryptopp/readme.md @@ -0,0 +1 @@ +Part of https://github.com/weidai11/cryptopp will be copied here, but I cannot find "the proper part" so far. diff --git a/lib/cryptopp/zlibutil_cryptopp.cpp b/lib/cryptopp/zlibutil_cryptopp.cpp new file mode 100644 index 0000000..eead4af --- /dev/null +++ b/lib/cryptopp/zlibutil_cryptopp.cpp @@ -0,0 +1,47 @@ +#include + +#include "../zlib/zlib.h" +#include +#include + +extern "C" int cryptopp_deflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen, + int level +){ + CryptoPP::Deflator zipper(NULL,level,CryptoPP::Deflator::DEFAULT_LOG2_WINDOW_SIZE,false); + //zipper.Put(source,sourceLen); + zipper.Put2(source,sourceLen,1,true); + //zipper.MessageEnd(); // Put2 already sends MessageEnd, so this line will cause double-call (?) + size_t avail = zipper.MaxRetrievable(); + if(!avail)return Z_NEED_DICT; // ??? + if(avail>*destLen)return Z_BUF_ERROR; + *destLen = avail; + zipper.Get(dest,avail); + return 0; +} + +extern "C" int cryptopp_inflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen +){ + CryptoPP::Inflator zipper; + try{ + //zipper.Put(source,sourceLen); + zipper.Put2(source,sourceLen,1,true); + //zipper.MessageEnd(); + }catch(CryptoPP::Inflator::BadBlockErr){ + return Z_DATA_ERROR; + }catch(CryptoPP::Inflator::BadDistanceErr){ + return Z_DATA_ERROR; + } + size_t avail = zipper.MaxRetrievable(); + if(avail>*destLen)return Z_BUF_ERROR; + *destLen = avail; + zipper.Get(dest,avail); + return 0; +} diff --git a/lib/xorshift.c b/lib/xorshift.c new file mode 100644 index 0000000..e06a579 --- /dev/null +++ b/lib/xorshift.c @@ -0,0 +1,33 @@ +#include +#include "xorshift.h" + +//I hope (sizeof(val)*CHAR_BIT-(rot)) will be precalculated in compilation. +//#define lrotr(val,rot) (( (val)<<(sizeof(val)*CHAR_BIT-(rot)) )|( (val)>>(rot) )) +#define lrotl(val,rot) (( (val)<<(rot) )|( (val)>>(sizeof(val)*CHAR_BIT-(rot)) )) + +static unsigned int x = 123456789; +static unsigned int y = 362436069; +static unsigned int z = 521288629; +static unsigned int w = 88675123; + +unsigned int xor_rand(){ + unsigned int t; + t=x^(x<<11); + x=y;y=z;z=w; + return w=(w^(w>>19)) ^ (t^(t>>8)); +} + +// http://d.hatena.ne.jp/gintenlabo/20100930/1285859540 +void xor_srand(unsigned int seed){ +#if 1 + x^=seed; + y^=lrotl(seed,17); + z^=lrotl(seed,31); + w^=lrotl(seed,18); +#else + x^=lrotl(seed,14); + y^=lrotl(seed,31); + z^=lrotl(seed,13); + w^=seed; +#endif +} diff --git a/lib/xorshift.h b/lib/xorshift.h new file mode 100644 index 0000000..3503e25 --- /dev/null +++ b/lib/xorshift.h @@ -0,0 +1,15 @@ +#ifndef _XORSHIFT_H_ +#define _XORSHIFT_H_ + +#ifdef __cplusplus +extern "C"{ +#endif + +unsigned int xor_rand(); +void xor_srand(unsigned int seed); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/xutil.c b/lib/xutil.c new file mode 100644 index 0000000..05911ce --- /dev/null +++ b/lib/xutil.c @@ -0,0 +1,101 @@ +#include "../compat.h" +#include +#include +#include +#include "xutil.h" + +unsigned long long int read64(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[0]|(x[1]<<8)|(x[2]<<16)|((unsigned int)x[3]<<24)|( (unsigned long long int)(x[4]|(x[5]<<8)|(x[6]<<16)|((unsigned int)x[7]<<24)) <<32); +} + +#if 0 +//write32 is now defined in lzmasdk.c. +unsigned int read32(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[0]|(x[1]<<8)|(x[2]<<16)|((unsigned int)x[3]<<24); +} +#endif + +unsigned int read24(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[0]|(x[1]<<8)|(x[2]<<16); +} + +unsigned short read16(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[0]|(x[1]<<8); +} + +void write64(void *p, const unsigned long long int n){ + unsigned char *x=(unsigned char*)p; + x[0]=n&0xff,x[1]=(n>>8)&0xff,x[2]=(n>>16)&0xff,x[3]=(n>>24)&0xff, + x[4]=(n>>32)&0xff,x[5]=(n>>40)&0xff,x[6]=(n>>48)&0xff,x[7]=(n>>56)&0xff; +} + +#if 0 +//write32 is now defined in lzmasdk.c. +void write32(void *p, const unsigned int n){ + unsigned char *x=(unsigned char*)p; + x[0]=n&0xff,x[1]=(n>>8)&0xff,x[2]=(n>>16)&0xff,x[3]=(n>>24)&0xff; +} +#endif + +void write24(void *p, const unsigned int n){ + unsigned char *x=(unsigned char*)p; + x[0]=n&0xff,x[1]=(n>>8)&0xff,x[2]=(n>>16)&0xff; +} + +void write16(void *p, const unsigned short n){ + unsigned char *x=(unsigned char*)p; + x[0]=n&0xff,x[1]=(n>>8)&0xff; +} + +unsigned long long int read64be(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[7]|(x[6]<<8)|(x[5]<<16)|((unsigned int)x[4]<<24)|( (unsigned long long int)(x[3]|(x[2]<<8)|(x[1]<<16)|((unsigned int)x[0]<<24)) <<32); +} + +unsigned int read32be(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[3]|(x[2]<<8)|(x[1]<<16)|((unsigned int)x[0]<<24); +} + +unsigned int read24be(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[2]|(x[1]<<8)|(x[0]<<16); +} + +unsigned short read16be(const void *p){ + const unsigned char *x=(const unsigned char*)p; + return x[1]|(x[0]<<8); +} + +void write64be(void *p, const unsigned long long int n){ + unsigned char *x=(unsigned char*)p; + x[7]=n&0xff,x[6]=(n>>8)&0xff,x[5]=(n>>16)&0xff,x[4]=(n>>24)&0xff, + x[3]=(n>>32)&0xff,x[2]=(n>>40)&0xff,x[1]=(n>>48)&0xff,x[0]=(n>>56)&0xff; +} + +void write32be(void *p, const unsigned int n){ + unsigned char *x=(unsigned char*)p; + x[3]=n&0xff,x[2]=(n>>8)&0xff,x[1]=(n>>16)&0xff,x[0]=(n>>24)&0xff; +} + +void write24be(void *p, const unsigned int n){ + unsigned char *x=(unsigned char*)p; + x[2]=n&0xff,x[1]=(n>>8)&0xff,x[0]=(n>>16)&0xff; +} + +void write16be(void *p, const unsigned short n){ + unsigned char *x=(unsigned char*)p; + x[1]=n&0xff,x[0]=(n>>8)&0xff; +} + +char* myfgets(char *buf,int n,FILE *fp){ //accepts LF/CRLF + char *ret=fgets(buf,n,fp); + if(!ret)return NULL; + if(strlen(buf)&&buf[strlen(buf)-1]=='\n')buf[strlen(buf)-1]=0; + if(strlen(buf)&&buf[strlen(buf)-1]=='\r')buf[strlen(buf)-1]=0; + return ret; +} diff --git a/lib/xutil.h b/lib/xutil.h new file mode 100644 index 0000000..b48b9d9 --- /dev/null +++ b/lib/xutil.h @@ -0,0 +1,68 @@ +#ifndef _XUTIL_H_ +#define _XUTIL_H_ + +#ifdef __cplusplus +extern "C"{ +#endif + +#include +#include + +unsigned long long read64(const void *p); +unsigned int read32(const void *p); +unsigned int read24(const void *p); +unsigned short read16(const void *p); + +void write64(void *p, const unsigned long long n); +void write32(void *p, const unsigned int n); +void write24(void *p, const unsigned int n); +void write16(void *p, const unsigned short n); + +unsigned long long read64be(const void *p); +unsigned int read32be(const void *p); +unsigned int read24be(const void *p); +unsigned short read16be(const void *p); + +void write64be(void *p, const unsigned long long n); +void write32be(void *p, const unsigned int n); +void write24be(void *p, const unsigned int n); +void write16be(void *p, const unsigned short n); + +//accepts LF/CRLF +char* myfgets(char *buf,int n,FILE *fp); + +void msleep(int msec); +int strchrindex(const char *s, const int c, const int idx); +unsigned int txt2bin(const char *src, unsigned char *dst, unsigned int len); + +size_t _FAT_directory_mbstoucs2(unsigned short* dst, const unsigned char* src, size_t len); +size_t mbstoucs2(unsigned short* dst, const unsigned char* src); +size_t _FAT_directory_ucs2tombs(unsigned char* dst, const unsigned short* src, size_t len); +size_t ucs2tombs(unsigned char* dst, const unsigned short* src); + +void NullMemory(void* buf, unsigned int n); +int memcmp_fast(const void *x,const void *y,unsigned int n); + +void *_memmem(const void *s1, size_t siz1, const void *s2, size_t siz2); +void *_memstr(const void *s1, const char *s2, size_t siz1); + +int makedir(const char *_dir); +bool wildmatch(const char *pattern, const char *compare); + +enum{ + wildmode_string, + wildmode_samedir, + wildmode_recursive, +}; + +bool matchwildcard(const char *wildcard, const char *string); +bool matchwildcard2(const char *wildcard, const char *string, const int iMode); + +bool fixpath(const char *src, char *dest); +const char* mybasename(const char *src); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/zlibutil.c b/lib/zlibutil.c index f6ee591..cbc5918 100644 --- a/lib/zlibutil.c +++ b/lib/zlibutil.c @@ -10,6 +10,43 @@ #include "lzma.h" //#include "zlib-ng/zlib-ng.h" +// gzip flag byte +#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ +#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */ +#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ +#define ORIG_NAME 0x08 /* bit 3 set: original file name present */ +#define COMMENT 0x10 /* bit 4 set: file comment present */ +#define RESERVED 0xE0 /* bits 5..7: reserved */ + +int read_gz_header_generic(unsigned char *data, int size, int *extra_off, int *extra_len){ + int method, flags, n, len; + if(size < 2) return 0; + if(data[0] != 0x1f || data[1] != 0x8b) return 0; + if(size < 4) return 0; + method = data[2]; + flags = data[3]; + if(method != Z_DEFLATED || (flags & RESERVED)) return 0; + n = 4 + 6; // Skip 6 bytes + *extra_off = n + 2; + *extra_len = 0; + if(flags & EXTRA_FIELD){ + if(size < n + 2) return 0; + len = data[n]|(data[n+1]<<8); + n += 2; + *extra_off = n; + *extra_len = len; + if(size < n + len) return 0; + n += len; + } + if(flags & ORIG_NAME) while(n < size && data[n++]); + if(flags & COMMENT) while(n < size && data[n++]); + if(flags & HEAD_CRC){ + if(n + 2 > size) return 0; + n += 2; + } + return n; +} + static inline void write32be(void *p, const unsigned int n){ unsigned char *x=(unsigned char*)p; x[3]=n&0xff,x[2]=(n>>8)&0xff,x[1]=(n>>16)&0xff,x[0]=(n>>24)&0xff; diff --git a/lib/zlibutil.h b/lib/zlibutil.h index 3e796e5..8b7e0cc 100644 --- a/lib/zlibutil.h +++ b/lib/zlibutil.h @@ -8,6 +8,8 @@ extern "C"{ #include #include "zlib/zlib.h" +int read_gz_header_generic(unsigned char *data, int size, int *extra_off, int *extra_len); + enum{ DEFLATE_ZLIB = 0, DEFLATE_7ZIP, @@ -17,6 +19,7 @@ enum{ DEFLATE_LIBDEFLATE, DEFLATE_ZLIBNG, DEFLATE_IGZIP, + DEFLATE_CRYPTOPP, }; typedef struct{ @@ -157,6 +160,21 @@ int igzip_inflate( size_t sourceLen ); +int cryptopp_deflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen, + int level +); + +int cryptopp_inflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen +); + #ifdef __cplusplus } #endif diff --git a/lib/zlibutil_cryptopp.c b/lib/zlibutil_cryptopp.c new file mode 100644 index 0000000..5ba2472 --- /dev/null +++ b/lib/zlibutil_cryptopp.c @@ -0,0 +1,21 @@ + #include + +#if !CRYPTOPP +int cryptopp_deflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen, + int level +){ + return -1; +} +int cryptopp_inflate( + unsigned char *dest, + size_t *destLen, + const unsigned char *source, + size_t sourceLen +){ + return -1; +} +#endif diff --git a/lib/zlibutil_igzip.c b/lib/zlibutil_igzip.c index 8a3fc0c..6158281 100644 --- a/lib/zlibutil_igzip.c +++ b/lib/zlibutil_igzip.c @@ -1,7 +1,7 @@ #include "isa-l/include/igzip_lib.h" #include // malloc/free -int level_size_buf[10] = { +int igzip_level_size_buf[10] = { #ifdef ISAL_DEF_LVL0_DEFAULT ISAL_DEF_LVL0_DEFAULT, #else @@ -74,7 +74,7 @@ int igzip_deflate( z.flush = FULL_FLUSH; z.gzip_flag = 0; z.level = level-1; - z.level_buf_size = level_size_buf[z.level]; + z.level_buf_size = igzip_level_size_buf[z.level]; z.level_buf = malloc(z.level_buf_size); z.end_of_stream = 1; diff --git a/lib/zlibutil_zlibng.c b/lib/zlibutil_zlibng.c index 228f875..3cf5396 100644 --- a/lib/zlibutil_zlibng.c +++ b/lib/zlibutil_zlibng.c @@ -8,7 +8,7 @@ int zlibng_deflate( size_t sourceLen, int level ){ -#ifdef FEOS +#if defined(NOZLIBNG) return -1; #else zng_stream z; @@ -45,7 +45,7 @@ int zlibng_inflate( const unsigned char *source, size_t sourceLen ){ -#ifdef FEOS +#if defined(NOZLIBNG) return -1; #else zng_stream z;