1#!/bin/bash -eu
2# Copyright 2018 Google Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16################################################################################
17
18cd bzip2
19SRCL=(blocksort.o huffman.o crctable.o randtable.o compress.o decompress.o bzlib.o)
20
21for source in ${SRCL[@]}; do
22    name=$(basename $source .o)
23    $CC $CFLAGS -c ${name}.c
24done
25rm -f libbz2.a
26ar cq libbz2.a ${SRCL[@]} && ranlib libbz2.a
27
28# build fuzzers
29for file in $SRC/*.c;
30do
31    name=$(basename $file .c)
32    $CC $CFLAGS -c -I . $SRC/${name}.c -o $OUT/${name}.o
33    $CXX $CXXFLAGS -o $OUT/${name} $OUT/${name}.o $LIB_FUZZING_ENGINE \
34    libbz2.a
35    rm -f $OUT/${name}.o
36done
37
38# build decompress seed corpus from ".bz2" samples
39# 1) look for all ".bz2" files in ./bzip2 and ./bzip2-test that are <100k
40# 2) remove base file name collisions
41# 3) add to (bzip2_decompress_target_seed_corpus.zip) archive
42find $SRC/bzip2* -type f -name "*.bz2" -size -100k \
43  | awk -F/ '{a[$NF]=$0}END{for(i in a)print a[i]}' \
44  | zip -j0r bzip2_decompress_target_seed_corpus.zip -@
45