- Added runtime and compiletime version information

diff --git a/include/polarssl/config.h b/include/polarssl/config.h
index 64ec89e..ca87828 100644
--- a/include/polarssl/config.h
+++ b/include/polarssl/config.h
@@ -77,6 +77,11 @@
 #define POLARSSL_SELF_TEST
 
 /*
+ * Enable run-time version information functions
+ */
+#define POLARSSL_VERSION_C
+
+/*
  * Enable the prime-number generation code.
  */
 #define POLARSSL_GENPRIME
diff --git a/include/polarssl/version.h b/include/polarssl/version.h
new file mode 100644
index 0000000..88cb609
--- /dev/null
+++ b/include/polarssl/version.h
@@ -0,0 +1,74 @@
+/**
+ * \file version.h
+ *
+ *  Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
+ *  All rights reserved.
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * This set of compile-time defines and run-time variables can be used to
+ * determine the version number of the PolarSSL library used.
+ */
+#ifndef POLARSSL_VERSION_H
+#define POLARSSL_VERSION_H
+
+#include "polarssl/config.h"
+
+/**
+ * The version number x.y.z is split into three parts.
+ * Major, Minor, Patchlevel
+ */
+#define POLARSSL_VERSION_MAJOR  0
+#define POLARSSL_VERSION_MINOR  13
+#define POLARSSL_VERSION_PATCH  1
+
+/**
+ * The single version number has the following structure:
+ *    MMNNPP00
+ *    Major version | Minor version | Patch version
+ */
+#define POLARSSL_VERSION_NUMBER         0x000D0100
+#define POLARSSL_VERSION_STRING         "0.13.1"
+#define POLARSSL_VERSION_STRING_FULL    "PolarSSL 0.13.1"
+
+#if defined(POLARSSL_VERSION_C)
+
+/**
+ * Get the version number.
+ *
+ * @return          The constructed version number in the format
+ *                  MMNNPP00 (Major, Minor, Patch).
+ */
+unsigned int version_get_number();
+
+/**
+ * Get the version string ("x.y.z").
+ *
+ * @param string    The string that will receive the value.
+ *                  (Should be at least 9 bytes in size)
+ */
+void version_get_string( char *string );
+
+/**
+ * Get the full version string ("PolarSSL x.y.z").
+ *
+ * @param string    The string that will receive the value.
+ *                  (Should be at least 18 bytes in size)
+ */
+void version_get_string_full( char *string );
+
+#endif /* POLARSSL_VERSION_C */
+
+#endif /* version.h */
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index c545d56..9e84ca6 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -22,6 +22,7 @@
      ssl_srv.c 
      ssl_tls.c
      timing.c
+     version.c
      x509parse.c
      xtea.c
 )
diff --git a/library/Makefile b/library/Makefile
index 205c6a2..e27f2bf 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -25,7 +25,7 @@
 	sha1.o		sha2.o		sha4.o		\
 	ssl_cli.o	ssl_srv.o	ssl_tls.o	\
 	timing.o	x509parse.o	xtea.o		\
-	camellia.o
+	camellia.o  version.o
 
 .SILENT:
 
diff --git a/library/version.c b/library/version.c
new file mode 100644
index 0000000..a43e488
--- /dev/null
+++ b/library/version.c
@@ -0,0 +1,46 @@
+/*
+ *  Version information
+ *
+ *  Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
+ *  All rights reserved.
+ *
+ *  This program 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 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_VERSION_C)
+
+#include "polarssl/version.h"
+#include <string.h>
+
+const char version[] = POLARSSL_VERSION_STRING;
+
+unsigned int version_get_number()
+{
+    return POLARSSL_VERSION_NUMBER;
+}
+
+void version_get_string( char *string )
+{
+    memcpy( string, POLARSSL_VERSION_STRING, sizeof( POLARSSL_VERSION_STRING ) );
+}
+
+void version_get_string_full( char *string )
+{
+    memcpy( string, POLARSSL_VERSION_STRING_FULL, sizeof( POLARSSL_VERSION_STRING_FULL ) );
+}
+
+#endif /* POLARSSL_VERSION_C */
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5f73112..7c1fa72 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -26,3 +26,4 @@
 add_test_suite(dhm)
 add_test_suite(x509parse)
 add_test_suite(debug)
+add_test_suite(version)
diff --git a/tests/Makefile b/tests/Makefile
index ee6507a..c90d8b3 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -14,7 +14,8 @@
 		test_suite_hmac_shax	test_suite_mdx		\
 		test_suite_mpi			test_suite_rsa		\
 		test_suite_shax			test_suite_x509parse\
-		test_suite_xtea			test_suite_debug
+		test_suite_xtea			test_suite_debug	\
+		test_suite_version
 
 .SILENT:
 
@@ -80,6 +81,10 @@
 	echo   "  CC    	$@.c"
 	$(CC) $(CFLAGS) $(OFLAGS) $@.c	$(LDFLAGS) -o $@
 
+test_suite_version: test_suite_version.c ../library/libpolarssl.a
+	echo   "  CC    	$@.c"
+	$(CC) $(CFLAGS) $(OFLAGS) $@.c	$(LDFLAGS) -o $@
+
 clean:
 	rm -f $(APPS) *.c
 
diff --git a/tests/suites/test_suite_version.data b/tests/suites/test_suite_version.data
new file mode 100644
index 0000000..6d75b0f
--- /dev/null
+++ b/tests/suites/test_suite_version.data
@@ -0,0 +1,5 @@
+Check compiletime library version
+check_compiletime_version:"0.13.1"
+
+Check runtime library version
+check_runtime_version:"0.13.1"
diff --git a/tests/suites/test_suite_version.function b/tests/suites/test_suite_version.function
new file mode 100644
index 0000000..958857e
--- /dev/null
+++ b/tests/suites/test_suite_version.function
@@ -0,0 +1,60 @@
+BEGIN_HEADER
+#include <polarssl/version.h>
+END_HEADER
+
+BEGIN_CASE
+check_compiletime_version:version_str
+{
+    char build_str[100];
+    char build_str_full[100];
+    unsigned int build_int;
+
+    memset( build_str, 0, 100 );
+    memset( build_str_full, 0, 100 );
+
+    snprintf (build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR,
+        POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
+
+    snprintf( build_str_full, 100, "PolarSSL %d.%d.%d", POLARSSL_VERSION_MAJOR,
+        POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
+
+    build_int = POLARSSL_VERSION_MAJOR << 24 |
+            POLARSSL_VERSION_MINOR << 16 |
+            POLARSSL_VERSION_PATCH << 8;
+
+    TEST_ASSERT( build_int == POLARSSL_VERSION_NUMBER );
+    TEST_ASSERT( strcmp( build_str, POLARSSL_VERSION_STRING ) == 0 );
+    TEST_ASSERT( strcmp( build_str_full, POLARSSL_VERSION_STRING_FULL ) == 0 );
+    TEST_ASSERT( strcmp( {version_str}, POLARSSL_VERSION_STRING ) == 0 );
+}
+END_CASE
+
+BEGIN_CASE
+check_runtime_version:version_str
+{
+    char build_str[100];
+    char get_str[100];
+    char build_str_full[100];
+    char get_str_full[100];
+    unsigned int get_int;
+
+    memset( build_str, 0, 100 );
+    memset( get_str, 0, 100 );
+    memset( build_str_full, 0, 100 );
+    memset( get_str_full, 0, 100 );
+
+    get_int = version_get_number();
+    version_get_string( get_str );
+    version_get_string_full( get_str_full );
+
+    snprintf( build_str, 100, "%d.%d.%d",
+        (get_int >> 24) & 0xFF,
+        (get_int >> 16) & 0xFF,
+        (get_int >> 8) & 0xFF );
+    snprintf( build_str_full, 100, "PolarSSL %s", {version_str} );
+
+    TEST_ASSERT( strcmp( build_str, {version_str} ) == 0 );
+    TEST_ASSERT( strcmp( build_str_full, get_str_full ) == 0 );
+    TEST_ASSERT( strcmp( {version_str}, get_str ) == 0 );
+}
+END_CASE