termux-packages/packages/glib/glib-gtimezone.c.patch
2017-12-22 00:10:55 +01:00

149 lines
4.3 KiB
Diff

diff -u -r ../glib-2.54.2/glib/gtimezone.c ./glib/gtimezone.c
--- ../glib-2.54.2/glib/gtimezone.c 2017-07-14 01:03:39.000000000 +0200
+++ ./glib/gtimezone.c 2017-12-21 23:47:57.704190589 +0100
@@ -43,6 +43,10 @@
#include <windows.h>
#endif
+#ifdef __ANDROID__
+#include <sys/system_properties.h>
+#endif
+
/**
* SECTION:timezone
* @title: GTimeZone
@@ -392,7 +396,109 @@
gtz->transitions = NULL;
}
-#ifdef G_OS_UNIX
+#ifdef __ANDROID__
+/* Android uses a 'persist.sys.timezone' system property for the
+ * current timezone instead of a /etc/localtime file:
+ * https://android.googlesource.com/platform/ndk/+/android-2.2_r1/docs/system/libc/OVERVIEW.TXT#67
+ *
+ * There are no files under /usr/share/zoneinfo - instead a single
+ * /system/usr/share/zoneinfo/tzdata file is used which contains all
+ * files compiled together with the following tool:
+ * https://android.googlesource.com/platform/system/timezone/+/master/zone_compactor/main/java/ZoneCompactor.java
+ */
+static GBytes *
+zone_info_android (const gchar *identifier)
+{
+ char sys_timezone[PROP_VALUE_MAX];
+ GMappedFile *file;
+ gchar *tzdata;
+ gsize tzdata_length;
+ const gsize index_entry_size = 52;
+ gint32 header_index_offset, header_data_offset;
+ gint32 entry_count, current_index;
+ char* entry_name;
+ gint32 entry_offset, entry_length;
+ GBytes *zoneinfo;
+
+ if (identifier == NULL)
+ {
+ if (__system_property_get ("persist.sys.timezone", sys_timezone) < 1)
+ {
+ g_warning ("__system_property_get(\"persist.sys.timezone\") failed");
+ return NULL;
+ }
+ identifier = sys_timezone;
+ }
+
+ file = g_mapped_file_new ("/system/usr/share/zoneinfo/tzdata", FALSE, NULL);
+ if (file == NULL)
+ {
+ g_warning ("Failed mapping tzdata file");
+ return NULL;
+ }
+
+ tzdata = g_mapped_file_get_contents (file);
+ tzdata_length = g_mapped_file_get_length (file);
+ if (tzdata == NULL || tzdata_length < 24)
+ {
+ g_warning ("Too small tzdata file");
+ goto error;
+ }
+
+ header_index_offset = gint32_from_be (*((gint32_be*) (tzdata + 12)));
+ header_data_offset = gint32_from_be (*((gint32_be*) (tzdata + 16)));
+
+ entry_count = (header_data_offset - header_index_offset) / index_entry_size;
+ if (entry_count < 1)
+ {
+ g_warning("No index entry found");
+ goto error;
+ }
+
+ current_index = 0;
+ while (current_index < entry_count)
+ {
+ entry_name = tzdata + header_index_offset + current_index * index_entry_size;
+ /* The name should be null terminated within the 40 chars. */
+ if (memchr (entry_name, 0, 40) == NULL)
+ {
+ g_warning("Invalid index entry");
+ goto error;
+ }
+
+ if (strcmp (entry_name, identifier) == 0)
+ {
+ entry_offset = gint32_from_be (*(gint32_be*) (entry_name + 40));
+ entry_length = gint32_from_be (*(gint32_be*) (entry_name + 44));
+ if (entry_length == 0)
+ {
+ g_warning ("Invalid tzdata entry with length zero");
+ goto error;
+ }
+ else if (entry_length > 65536 || header_data_offset + entry_offset + entry_length > tzdata_length)
+ {
+ /* Use a reasonable but arbitrary max length of an entry. */
+ g_warning ("Too large tzdata entry length");
+ goto error;
+ }
+
+ zoneinfo = g_bytes_new_with_free_func (tzdata + header_data_offset + entry_offset,
+ entry_length,
+ (GDestroyNotify)g_mapped_file_unref,
+ g_mapped_file_ref (file));
+ g_mapped_file_unref (file);
+ return zoneinfo;
+ }
+ current_index++;
+ }
+
+error:
+ g_mapped_file_unref (file);
+ return NULL;
+}
+
+#elif defined(G_OS_UNIX)
+
static GBytes*
zone_info_unix (const gchar *identifier)
{
@@ -436,6 +542,10 @@
return zoneinfo;
}
+#endif
+
+#ifdef G_OS_UNIX
+
static void
init_zone_from_iana_info (GTimeZone *gtz, GBytes *zoneinfo)
{
@@ -1387,7 +1497,11 @@
if (tz->t_info == NULL)
{
#ifdef G_OS_UNIX
+# ifdef __ANDROID__
+ GBytes *zoneinfo = zone_info_android (identifier);
+# else
GBytes *zoneinfo = zone_info_unix (identifier);
+# endif
if (!zoneinfo)
zone_for_constant_offset (tz, "UTC");
else