From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: "tc@google.com" <tc@google.com>
Date: Tue, 6 Jan 2009 22:39:41 +0000
Subject: [PATCH 03/17] Custom shell.c helpers to load Chromium's ICU data.

History uses fts3 with an icu-based segmenter.  These changes allow building a
sqlite3 binary for Linux or Windows which can read those files.

Original review URL: https://codereview.chromium.org/42250
---
 third_party/sqlite/src/Makefile.linux-gcc    |  7 +++++
 third_party/sqlite/src/main.mk               |  2 +-
 third_party/sqlite/src/src/shell.c.in        | 10 ++++++
 third_party/sqlite/src/src/shell_icu_linux.c | 27 +++++++++++++++++
 third_party/sqlite/src/src/shell_icu_win.c   | 32 ++++++++++++++++++++
 5 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 third_party/sqlite/src/src/shell_icu_linux.c
 create mode 100644 third_party/sqlite/src/src/shell_icu_win.c

diff --git a/third_party/sqlite/src/Makefile.linux-gcc b/third_party/sqlite/src/Makefile.linux-gcc
index b838b844a312..3047d172389b 100644
--- a/third_party/sqlite/src/Makefile.linux-gcc
+++ b/third_party/sqlite/src/Makefile.linux-gcc
@@ -60,6 +60,13 @@ TLIBS =
 OPTS = -DNDEBUG=1
 OPTS += -DHAVE_FDATASYNC=1

+# Support for loading Chromium ICU data in sqlite3.
+ifeq ($(shell uname -s),Darwin)
+SHELL_ICU =
+else
+SHELL_ICU = $(TOP)/src/shell_icu_linux.c -licuuc
+endif
+
 #### The suffix to add to executable files.  ".exe" for windows.
 #    Nothing for unix.
 #
diff --git a/third_party/sqlite/src/main.mk b/third_party/sqlite/src/main.mk
index 81774f9b100f..b460a58d9a94 100644
--- a/third_party/sqlite/src/main.mk
+++ b/third_party/sqlite/src/main.mk
@@ -549,7 +549,7 @@ libsqlite3.a:	$(LIBOBJ)

 sqlite3$(EXE):	shell.c libsqlite3.a sqlite3.h
 	$(TCCX) $(READLINE_FLAGS) -o sqlite3$(EXE) $(SHELL_OPT) \
-		shell.c libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)
+		shell.c $(SHELL_ICU) libsqlite3.a $(LIBREADLINE) $(TLIBS) $(THREADLIB)

 sqldiff$(EXE):	$(TOP)/tool/sqldiff.c sqlite3.c sqlite3.h
 	$(TCCX) -o sqldiff$(EXE) -DSQLITE_THREADSAFE=0 \
diff --git a/third_party/sqlite/src/src/shell.c.in b/third_party/sqlite/src/src/shell.c.in
index 0007d984d045..b13551dc61f2 100644
--- a/third_party/sqlite/src/src/shell.c.in
+++ b/third_party/sqlite/src/src/shell.c.in
@@ -8555,6 +8555,16 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
   }
 #endif

+  /* Begin evanm patch. */
+#if !defined(__APPLE__)
+  extern int sqlite_shell_init_icu();
+  if( !sqlite_shell_init_icu() ){
+    fprintf(stderr, "%s: warning: couldn't find icudt38.dll; "
+                    "queries against ICU FTS tables will fail.\n", argv[0]);
+  }
+#endif /* !defined(__APPLE__) */
+  /* End evanm patch. */
+
   /* Do an initial pass through the command-line argument to locate
   ** the name of the database file, the name of the initialization file,
   ** the size of the alternative malloc heap,
diff --git a/third_party/sqlite/src/src/shell_icu_linux.c b/third_party/sqlite/src/src/shell_icu_linux.c
new file mode 100644
index 000000000000..4ad0e42d2293
--- /dev/null
+++ b/third_party/sqlite/src/src/shell_icu_linux.c
@@ -0,0 +1,27 @@
+/* Copyright 2007 Google Inc. All Rights Reserved.
+**/
+
+#include <limits.h>
+#include <unistd.h>
+#include "unicode/putil.h"
+#include "unicode/udata.h"
+
+/*
+** This function attempts to load the ICU data tables from a data file.
+** Returns 0 on failure, nonzero on success.
+** This a hack job of icu_utils.cc:Initialize().  It's Chrome-specific code.
+*/
+int sqlite_shell_init_icu() {
+  char bin_dir[PATH_MAX + 1];
+  int bin_dir_size = readlink("/proc/self/exe", bin_dir, PATH_MAX);
+  if (bin_dir_size < 0 || bin_dir_size > PATH_MAX)
+    return 0;
+  bin_dir[bin_dir_size] = 0;;
+
+  u_setDataDirectory(bin_dir);
+  // Only look for the packaged data file;
+  // the default behavior is to look for individual files.
+  UErrorCode err = U_ZERO_ERROR;
+  udata_setFileAccess(UDATA_ONLY_PACKAGES, &err);
+  return err == U_ZERO_ERROR;
+}
diff --git a/third_party/sqlite/src/src/shell_icu_win.c b/third_party/sqlite/src/src/shell_icu_win.c
new file mode 100644
index 000000000000..67ebbf4fbdb4
--- /dev/null
+++ b/third_party/sqlite/src/src/shell_icu_win.c
@@ -0,0 +1,32 @@
+/* Copyright 2011 Google Inc. All Rights Reserved.
+**/
+
+#include <windows.h>
+#include "unicode/udata.h"
+
+/*
+** This function attempts to load the ICU data tables from a DLL.
+** Returns 0 on failure, nonzero on success.
+** This a hack job of icu_utils.cc:Initialize().  It's Chrome-specific code.
+*/
+
+#define ICU_DATA_SYMBOL "icudt" U_ICU_VERSION_SHORT "_dat"
+int sqlite_shell_init_icu() {
+  HMODULE module;
+  FARPROC addr;
+  UErrorCode err;
+
+  // Chrome dropped U_ICU_VERSION_SHORT from the icu data dll name.
+  module = LoadLibrary(L"icudt.dll");
+  if (!module)
+    return 0;
+
+  addr = GetProcAddress(module, ICU_DATA_SYMBOL);
+  if (!addr)
+    return 0;
+
+  err = U_ZERO_ERROR;
+  udata_setCommonData(addr, &err);
+
+  return 1;
+}
--
2.18.0

