Revert "Remove zlib"

This reverts commit d874a1fd14bdf3df8ee232f539ac613adaae648c.

Conflicts:
* CMakeLists.txt:
  * ENABLE_ZLIB_SUPPORT: there has been a change immediately after
    where it was removed. Just re-add what was removed.
* tests/CMakeLists.txt:
  * ENABLE_ZLIB_SUPPORT: there has been a change immediately after
    where it was removed. Just re-add what was removed.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6d5332d..4d40eea 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,6 +8,8 @@
 # Set the project root directory.
 set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
 
+option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
+
 option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
 
 option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
@@ -179,6 +181,14 @@
 include_directories(include/)
 include_directories(library/)
 
+if(ENABLE_ZLIB_SUPPORT)
+    find_package(ZLIB)
+
+    if(ZLIB_FOUND)
+        include_directories(${ZLIB_INCLUDE_DIR})
+    endif(ZLIB_FOUND)
+endif(ENABLE_ZLIB_SUPPORT)
+
 add_subdirectory(include)
 
 add_subdirectory(3rdparty)
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 8db0828..a602a6b 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -148,6 +148,10 @@
     set(libs ${libs} network)
 endif(HAIKU)
 
+if(ENABLE_ZLIB_SUPPORT)
+    set(libs ${libs} ${ZLIB_LIBRARIES})
+endif(ENABLE_ZLIB_SUPPORT)
+
 if(LINK_WITH_PTHREAD)
     set(libs ${libs} pthread)
 endif()
diff --git a/programs/Makefile b/programs/Makefile
index c5ac767..65a31b1 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -44,6 +44,11 @@
 SHARED_SUFFIX=
 endif
 
+# Zlib shared library extensions:
+ifdef ZLIB
+LOCAL_LDFLAGS += -lz
+endif
+
 APPS = \
 	aes/aescrypt2$(EXEXT) \
 	aes/crypt_and_hash$(EXEXT) \
diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt
index 59f8d54..0d2b946 100644
--- a/programs/test/CMakeLists.txt
+++ b/programs/test/CMakeLists.txt
@@ -2,6 +2,10 @@
     mbedtls
 )
 
+if(ENABLE_ZLIB_SUPPORT)
+    set(libs ${libs} ${ZLIB_LIBRARIES})
+endif(ENABLE_ZLIB_SUPPORT)
+
 add_executable(selftest selftest.c)
 target_link_libraries(selftest ${libs})
 
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a1194e5..ecc33ee 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -9,6 +9,10 @@
     set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
 endif()
 
+if(ENABLE_ZLIB_SUPPORT)
+    set(libs ${libs} ${ZLIB_LIBRARIES})
+endif(ENABLE_ZLIB_SUPPORT)
+
 find_package(PythonInterp)
 if(NOT PYTHONINTERP_FOUND)
     message(FATAL_ERROR "Cannot build test suites without Python 2 or 3")
diff --git a/tests/Makefile b/tests/Makefile
index 3203b88..cca7c1c 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -55,6 +55,11 @@
 PYTHON ?= python2
 endif
 
+# Zlib shared library extensions:
+ifdef ZLIB
+LOCAL_LDFLAGS += -lz
+endif
+
 # A test application is built for each suites/test_suite_*.data file.
 # Application name is same as .data file's base name and can be
 # constructed by stripping path 'suites/' and extension .data.