libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

commit 6db4d47e934619fade06ee3d454b90f9c6e584e1
parent e01ad6557ad72c1a4196ab7696a03a1c57bb981b
Author: Christian Grothoff <christian@grothoff.org>
Date:   Tue, 19 Jul 2005 10:37:49 +0000

decode buffer of size 0 should return success

Diffstat:
Msrc/main/Makefile.am | 11+++++++++++
Msrc/main/extractor.c | 2+-
Asrc/main/test_binary.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/src/main/Makefile.am b/src/main/Makefile.am @@ -49,3 +49,14 @@ extract_SOURCES = \ getopt.h \ getopt1.c + +check_PROGRAMS = \ + test_binary + +TESTS = $(check_PROGRAMS) + +test_binary_SOURCES = \ + test_binary.c +test_binary_LDADD = \ + $(top_builddir)/src/main/libextractor.la + diff --git a/src/main/extractor.c b/src/main/extractor.c @@ -991,7 +991,7 @@ int EXTRACTOR_binaryDecode(const unsigned char * in, if (inSize == 0) { *out = NULL; *outSize = 0; - return 1; + return 0; } buf = malloc(inSize); /* slightly more than needed ;-) */ diff --git a/src/main/test_binary.c b/src/main/test_binary.c @@ -0,0 +1,66 @@ +/* + This file is part of libextractor. + (C) 2005 Vidyut Samanta and Christian Grothoff + + libextractor is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 2, or (at your + option) any later version. + + libextractor is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libextractor; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + */ + +#include "platform.h" +#include "extractor.h" + +static int test(const char * buf, + size_t size) { + char * enc; + unsigned char * dec; + size_t out; + + enc = EXTRACTOR_binaryEncode(buf, + size); + if (0 != EXTRACTOR_binaryDecode(enc, + &dec, + &out)) { + free(enc); + return 0; + } + free(enc); + if (out != size) { + free(dec); + return 0; + } + if (0 != memcmp(buf, + dec, + size)) { + free(dec); + return 0; + } + free(dec); + return 1; +} + +int main(int argc, + char * argv[]) { + unsigned int i; + char buf[2048]; + + for (i=0;i<2048;i++) { + buf[i] = (char) rand(); + if (! test(buf, i)) { + printf("Failed: %u\n", i); + return -1; + } + } + return 0; +}