diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index c7fb9ca7..89a7b634 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -17,6 +17,7 @@ set( LIBEXIV2_SRC
exif.cpp
futils.cpp
gifimage.cpp
+ helper_functions.cpp
http.cpp
image.cpp
ini.cpp
diff --git a/src/helper_functions.cpp b/src/helper_functions.cpp
new file mode 100644
index 00000000..ba8bd6d4
--- /dev/null
+++ b/src/helper_functions.cpp
@@ -0,0 +1,41 @@
+// ********************************************************* -*- C++ -*-
+/*
+ * Copyright (C) 2004-2018 Exiv2 authors
+ *
+ * This program is part of the Exiv2 distribution.
+ *
+ * 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, 5th Floor, Boston, MA 02110-1301 USA.
+ */
+/*!
+ @file helper_functions.cpp
+ @brief A collection of helper functions
+ @author Dan Čermák (D4N)
+ dan.cermak@cgc-instruments.com
+ @date 25-May-18, D4N: created
+ */
+
+#include "helper_functions.hpp"
+
+#include
+
+
+std::string string_from_unterminated(const char* data, size_t data_length)
+{
+ const char* termination = std::find(data, data + data_length, 0);
+ // if find returned the end iterator => no \0 found
+ const size_t string_length = termination == data + data_length ? data_length : termination - data;
+
+ return std::string(data, string_length);
+}
diff --git a/src/helper_functions.hpp b/src/helper_functions.hpp
new file mode 100644
index 00000000..d138490b
--- /dev/null
+++ b/src/helper_functions.hpp
@@ -0,0 +1,43 @@
+// ********************************************************* -*- C++ -*-
+/*
+ * Copyright (C) 2004-2018 Exiv2 authors
+ *
+ * This program is part of the Exiv2 distribution.
+ *
+ * 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, 5th Floor, Boston, MA 02110-1301 USA.
+ */
+/*!
+ @file helper_functions.hpp
+ @brief A collection of helper functions
+ @author Dan Čermák (D4N)
+ dan.cermak@cgc-instruments.com
+ @date 25-May-18, D4N: created
+ */
+#ifndef HELPER_FUNCTIONS_HPP
+#define HELPER_FUNCTIONS_HPP
+
+#include
+
+/*!
+ @brief Convert a (potentially not null terminated) array into a
+ std::string.
+
+ Convert a C style string that may or may not be null terminated safely
+ into a std::string. The string's termination is either set at the first \0
+ or after data_length characters.
+ */
+std::string string_from_unterminated(const char* data, size_t data_length);
+
+#endif // HELPER_FUNCTIONS_HPP
diff --git a/unitTests/CMakeLists.txt b/unitTests/CMakeLists.txt
index a8922de4..1778a5aa 100644
--- a/unitTests/CMakeLists.txt
+++ b/unitTests/CMakeLists.txt
@@ -8,6 +8,7 @@ add_executable(unit_tests mainTestRunner.cpp
test_XmpKey.cpp
test_DateValue.cpp
test_cr2header_int.cpp
+ test_helper_functions.cpp
)
#TODO Use GTest::GTest once we upgrade the minimum CMake version required
diff --git a/unitTests/test_helper_functions.cpp b/unitTests/test_helper_functions.cpp
new file mode 100644
index 00000000..c47f753e
--- /dev/null
+++ b/unitTests/test_helper_functions.cpp
@@ -0,0 +1,21 @@
+#include "helper_functions.hpp"
+
+#include "gtestwrapper.h"
+
+TEST(string_from_unterminated, terminatedArray)
+{
+ const char data[5] = {'a', 'b', 'c', 0, 'd'};
+ const std::string res = string_from_unterminated(data, 5);
+
+ ASSERT_EQ(res.size(), 3);
+ ASSERT_STREQ(res.c_str(), "abc");
+}
+
+TEST(string_from_unterminated, unterminatedArray)
+{
+ const char data[4] = {'a', 'b', 'c', 'd'};
+ const std::string res = string_from_unterminated(data, 4);
+
+ ASSERT_EQ(res.size(), 4);
+ ASSERT_STREQ(res.c_str(), "abcd");
+}