Rough implementation of the state server. Now it is possible to request restore of a particular file

See state-server.h for more details on command syntax.

// <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/"cmd"
// <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash>

<file-hash> component is used solely to disambiguate file version and
need not be specified in full (or specified at all, but the component
need to be present). The system will only check that specified file-hash
is a prefix of the real hash of the file

Change-Id: I7a4d15a04eb1a1c59a3412da46174441c61a45c0
diff --git a/src/db-helper.cc b/src/db-helper.cc
index ac63cdf..33b0136 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -54,6 +54,13 @@
                              << errmsg_info_str ("Cannot create function ``hash''"));
     }
 
+  res = sqlite3_create_function (m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0);
+  if (res != SQLITE_OK)
+    {
+      BOOST_THROW_EXCEPTION (Error::Db ()
+                             << errmsg_info_str ("Cannot create function ``is_prefix''"));
+    }
+
   // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
   // for now, just attempt to create everything
   sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
@@ -138,5 +145,31 @@
   EVP_MD_CTX_destroy (*hash_context);
 }
 
+void
+DbHelper::is_prefix_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
+{
+  int len1 = sqlite3_value_bytes (argv[0]);
+  int len2 = sqlite3_value_bytes (argv[1]);
 
+  if (len1 == 0)
+    {
+      sqlite3_result_int (context, 1);
+      return;
+    }
+
+  if (len1 > len2) // first parameter should be at most equal in length to the second one
+    {
+      sqlite3_result_int (context, 0);
+      return;
+    }
+
+  if (memcmp (sqlite3_value_blob (argv[0]), sqlite3_value_blob (argv[1]), len1) == 0)
+    {
+      sqlite3_result_int (context, 1);
+    }
+  else
+    {
+      sqlite3_result_int (context, 0);
+    }
+}