Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / android / lo-bootstrap.c
blobc43d3ba433bfd56050b477fb945adef689dc9584
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <time.h>
18 #include <dlfcn.h>
19 #include <fcntl.h>
20 #include <pthread.h>
21 #include <sys/mman.h>
22 #include <unistd.h>
24 #include <jni.h>
25 #include <zlib.h>
27 #include <android/log.h>
29 #include "uthash.h"
31 #include "osl/detail/android-bootstrap.h"
33 #undef LOGI
35 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "lo-bootstrap", __VA_ARGS__))
36 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "lo-bootstrap", __VA_ARGS__))
38 #define MAX(a,b) ((a) > (b) ? (a) : (b))
40 struct engine {
41 int dummy;
44 /* These are valid / used in all apps. */
45 static const char *data_dir;
46 static const char *cache_dir;
47 static void *apk_file;
48 static int apk_file_size;
49 static JavaVM *the_java_vm;
51 /* Zip data structures */
53 /* compression methods */
54 #define STORE 0
56 struct local_file_header {
57 uint32_t signature;
58 uint16_t min_version;
59 uint16_t general_flag;
60 uint16_t compression;
61 uint16_t lastmod_time;
62 uint16_t lastmod_date;
63 uint32_t crc32;
64 uint32_t compressed_size;
65 uint32_t uncompressed_size;
66 uint16_t filename_size;
67 uint16_t extra_field_size;
68 char data[0];
69 } __attribute__((__packed__));
71 struct cdir_entry {
72 uint32_t signature;
73 uint16_t creator_version;
74 uint16_t min_version;
75 uint16_t general_flag;
76 uint16_t compression;
77 uint16_t lastmod_time;
78 uint16_t lastmod_date;
79 uint32_t crc32;
80 uint32_t compressed_size;
81 uint32_t uncompressed_size;
82 uint16_t filename_size;
83 uint16_t extra_field_size;
84 uint16_t file_comment_size;
85 uint16_t disk_num;
86 uint16_t internal_attr;
87 uint32_t external_attr;
88 uint32_t offset;
89 char data[0];
90 } __attribute__((__packed__));
92 #define CDIR_END_SIG 0x06054b50
94 struct cdir_end {
95 uint32_t signature;
96 uint16_t disk_num;
97 uint16_t cdir_disk;
98 uint16_t disk_entries;
99 uint16_t cdir_entries;
100 uint32_t cdir_size;
101 uint32_t cdir_offset;
102 uint16_t comment_size;
103 char comment[0];
104 } __attribute__((__packed__));
106 /* End of Zip data structures */
108 static struct cdir_entry *cdir_start;
109 static uint16_t cdir_entries;
111 /* Data structure to turn Zip's list in arbitrary order of
112 * hierarchical pathnames (not necessarily including entries for
113 * directories) into an actual hierarchical directory tree, so that we
114 * can iterate over directory entries properly in the dirent style
115 * functions.
118 typedef struct direntry *direntry;
120 struct direntry {
121 UT_hash_handle hh;
122 enum { REGULAR, DIRECTORY } kind;
123 int ino;
124 union {
125 struct cdir_entry *file;
126 direntry subdir;
130 struct lo_apk_dir {
131 direntry cur;
134 static direntry assets = NULL;
136 static uint32_t
137 cdir_entry_size(struct cdir_entry *entry)
139 return sizeof(*entry) +
140 letoh16(entry->filename_size) +
141 letoh16(entry->extra_field_size) +
142 letoh16(entry->file_comment_size);
145 static int
146 setup_cdir(void)
148 struct cdir_end *dirend = (struct cdir_end *)((char *) apk_file + apk_file_size - sizeof(*dirend));
149 uint32_t cdir_offset;
151 while ((void *)dirend > apk_file &&
152 letoh32(dirend->signature) != CDIR_END_SIG)
153 dirend = (struct cdir_end *)((char *)dirend - 1);
154 if (letoh32(dirend->signature) != CDIR_END_SIG) {
155 LOGE("setup_cdir: Could not find end of central directory record");
156 return 0;
159 cdir_offset = letoh32(dirend->cdir_offset);
161 cdir_entries = letoh16(dirend->cdir_entries);
162 cdir_start = (struct cdir_entry *)((char *)apk_file + cdir_offset);
164 return 1;
167 static struct cdir_entry *
168 find_cdir_entry(struct cdir_entry *entry, int count, const char *name)
170 size_t name_size = strlen(name);
171 while (count--) {
172 if (letoh16(entry->filename_size) == name_size &&
173 !memcmp(entry->data, name, name_size))
174 return entry;
175 entry = (struct cdir_entry *)((char *)entry + cdir_entry_size(entry));
177 return NULL;
180 static void
181 handle_one_asset(struct cdir_entry *entry)
183 /* In the .apk there are no initial slashes */
184 const char *p = entry->data + sizeof("assets/")-1;
185 const char *z = entry->data + entry->filename_size;
186 direntry *dir = &assets;
187 static int ino = 1;
189 while (p < z) {
190 const char *q = p;
191 direntry old, new;
193 while (q < z && *q != '/')
194 q++;
195 HASH_FIND(hh, *dir, p, (unsigned)(q - p), old);
196 if (*q == '/') {
197 if (old == NULL) {
198 new = malloc(sizeof(*new));
199 new->ino = ino++;
200 new->kind = DIRECTORY;
201 new->subdir = NULL;
202 HASH_ADD_KEYPTR(hh, *dir, p, (unsigned)(q - p), new);
203 dir = &new->subdir;
204 } else {
205 dir = &old->subdir;
207 p = q + 1;
208 } else {
209 if (old == NULL) {
210 new = malloc(sizeof(*new));
211 new->ino = ino++;
212 new->kind = REGULAR;
213 new->file = entry;
214 HASH_ADD_KEYPTR(hh, *dir, p, (unsigned)(q - p), new);
215 } else {
216 LOGE("duplicate entry in apk: %.*s", entry->filename_size, entry->data);
218 p = q;
220 (void) dir;
224 static int
225 setup_assets_tree(void)
227 int count = cdir_entries;
228 struct cdir_entry *entry = cdir_start;
230 while (count--) {
231 if (letoh16(entry->filename_size) >= sizeof("assets/")-1 &&
232 memcmp(entry->data, "assets/", sizeof("assets/")-1) == 0)
233 handle_one_asset(entry);
234 entry = (struct cdir_entry *)((char *)entry + cdir_entry_size(entry));
236 return 1;
239 /* The lo-native-code shared library is always loaded from Java, so this is
240 * always called by JNI first.
242 __attribute__ ((visibility("default")))
243 jint
244 JNI_OnLoad(JavaVM* vm, void* reserved)
246 (void) reserved;
248 the_java_vm = vm;
250 return JNI_VERSION_1_2;
253 // public static native boolean setup(String dataDir,
254 // String cacheDir,
255 // String apkFile)
257 __attribute__ ((visibility("default")))
258 jboolean
259 Java_org_libreoffice_android_Bootstrap_setup__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2
260 (JNIEnv* env,
261 jobject clazz,
262 jstring dataDir,
263 jstring cacheDir,
264 jstring apkFile)
266 struct stat st;
267 int fd;
268 const char *dataDirPath;
269 const char *cacheDirPath;
270 const char *apkFilePath;
272 (void) clazz;
274 dataDirPath = (*env)->GetStringUTFChars(env, dataDir, NULL);
275 data_dir = strdup(dataDirPath);
276 (*env)->ReleaseStringUTFChars(env, dataDir, dataDirPath);
278 cacheDirPath = (*env)->GetStringUTFChars(env, cacheDir, NULL);
279 cache_dir = strdup(cacheDirPath);
280 (*env)->ReleaseStringUTFChars(env, cacheDir, cacheDirPath);
282 apkFilePath = (*env)->GetStringUTFChars(env, apkFile, NULL);
284 fd = open(apkFilePath, O_RDONLY);
285 if (fd == -1) {
286 LOGE("Could not open %s", apkFilePath);
287 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
288 return JNI_FALSE;
290 if (fstat(fd, &st) == -1) {
291 LOGE("Could not fstat %s", apkFilePath);
292 close(fd);
293 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
294 return JNI_FALSE;
296 apk_file = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
297 close(fd);
299 if (apk_file == MAP_FAILED) {
300 LOGE("Could not mmap %s", apkFilePath);
301 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
302 return JNI_FALSE;
304 apk_file_size = st.st_size;
306 (*env)->ReleaseStringUTFChars(env, apkFile, apkFilePath);
308 if (!setup_cdir())
309 return JNI_FALSE;
311 if (!setup_assets_tree())
312 return JNI_FALSE;
314 return JNI_TRUE;
317 static jboolean
318 get_jni_string_array(JNIEnv *env,
319 const char *function_and_parameter_name,
320 jobject strv,
321 int *argc,
322 const char ***argv)
324 jclass StringArray;
325 int i;
327 StringArray = (*env)->FindClass(env, "[Ljava/lang/String;");
328 if (StringArray == NULL) {
329 LOGE("Could not find String[] class");
330 return JNI_FALSE;
333 if (!(*env)->IsInstanceOf(env, strv, StringArray)) {
334 LOGE("%s is not a String[]?", function_and_parameter_name);
335 return JNI_FALSE;
338 *argc = (*env)->GetArrayLength(env, strv);
339 *argv = malloc(sizeof(char *) * (*argc+1));
341 for (i = 0; i < *argc; i++) {
342 const char *s = (*env)->GetStringUTFChars(env, (*env)->GetObjectArrayElement(env, strv, i), NULL);
343 (*argv)[i] = strdup(s);
344 (*env)->ReleaseStringUTFChars(env, (*env)->GetObjectArrayElement(env, strv, i), s);
346 (*argv)[*argc] = NULL;
348 return JNI_TRUE;
352 // public static native int getpid();
354 __attribute__ ((visibility("default")))
355 jint
356 Java_org_libreoffice_android_Bootstrap_getpid(JNIEnv* env,
357 jobject clazz)
359 (void) env;
360 (void) clazz;
362 return getpid();
366 // public static native void system(String cmdline);
368 __attribute__ ((visibility("default")))
369 void
370 Java_org_libreoffice_android_Bootstrap_system(JNIEnv* env,
371 jobject clazz,
372 jstring cmdline)
374 const char *s;
376 (void) clazz;
378 s = (*env)->GetStringUTFChars(env, cmdline, NULL);
380 LOGI("system(%s)", s);
382 system(s);
384 (*env)->ReleaseStringUTFChars(env, cmdline, s);
387 // public static native void putenv(String string);
389 __attribute__ ((visibility("default")))
390 void
391 Java_org_libreoffice_android_Bootstrap_putenv(JNIEnv* env,
392 jobject clazz,
393 jstring string)
395 const char *s;
397 (void) clazz;
399 s = (*env)->GetStringUTFChars(env, string, NULL);
401 LOGI("putenv(%s)", s);
403 putenv(s);
405 #if 0
407 static int beenhere=0;
408 if (!beenhere) {
409 LOGI("lo-bootstrap: Sleeping for 20 seconds, start ndk-gdb NOW if that is your intention");
410 sleep(20);
411 beenhere = 1;
414 #endif
416 (*env)->ReleaseStringUTFChars(env, string, s);
419 __attribute__ ((visibility("default")))
420 void *
421 lo_apkentry(const char *filename,
422 size_t *size)
424 struct cdir_entry *entry;
425 struct local_file_header *file;
426 void *data;
428 if (*filename == '/')
429 filename++;
431 entry = find_cdir_entry(cdir_start, cdir_entries, filename);
433 if (entry == NULL) {
434 LOGE("lo_apkentry: Could not find %s", filename);
435 return NULL;
437 file = (struct local_file_header *)((char *)apk_file + letoh32(entry->offset));
439 if (letoh16(file->compression) != STORE) {
440 LOGE("lo_apkentry: File %s is compressed", filename);
441 return NULL;
444 data = ((char *)&file->data) + letoh16(file->filename_size) + letoh16(file->extra_field_size);
445 *size = file->uncompressed_size;
447 /* LOGI("lo_apkentry(%s): %p, %d", filename, data, *size); */
449 return data;
452 __attribute__ ((visibility("default")))
453 lo_apk_dir *
454 lo_apk_opendir(const char *dirname)
456 /* In the .apk there are no initial slashes, but the parameter passed to
457 * us does have it.
459 const char *p = dirname + sizeof("/assets/")-1;
460 direntry dir = assets;
462 if (!*p) {
463 lo_apk_dir *result = malloc(sizeof(*result));
464 result->cur = assets;
465 return result;
468 while (1) {
469 const char *q = p;
470 direntry entry;
472 while (*q && *q != '/')
473 q++;
475 HASH_FIND(hh, dir, p, (unsigned)(q - p), entry);
477 if (entry == NULL && *q == '/') {
478 errno = ENOENT;
479 return NULL;
480 } else if (entry == NULL) {
481 /* Empty directories, or directories containing only "hidden"
482 * files (like the .gitignore in sc/qa/unit/qpro/indeterminate)
483 * are not present in the .apk. So we need to pretend that any
484 * directory that doesn't exist as a parent of an entry in the
485 * .apk *does* exist but is empty.
487 lo_apk_dir *result = malloc(sizeof(*result));
488 result->cur = NULL;
489 return result;
492 if (entry->kind != DIRECTORY) {
493 errno = ENOTDIR;
494 return NULL;
497 if (!q[0] || !q[1]) {
498 lo_apk_dir *result = malloc(sizeof(*result));
499 result->cur = entry->subdir;
500 return result;
503 dir = entry->subdir;
504 p = q + 1;
508 __attribute__ ((visibility("default")))
509 struct dirent *
510 lo_apk_readdir(lo_apk_dir *dirp)
512 static struct dirent result;
514 if (dirp->cur == NULL) {
515 /* LOGI("lo_apk_readdir(%p) = NULL", dirp); */
516 return NULL;
519 result.d_ino = dirp->cur->ino;
520 result.d_off = 0;
521 result.d_reclen = 0;
523 if (dirp->cur->kind == DIRECTORY)
524 result.d_type = DT_DIR;
525 else
526 result.d_type = DT_REG;
528 memcpy(result.d_name, dirp->cur->hh.key, dirp->cur->hh.keylen);
529 result.d_name[dirp->cur->hh.keylen] = '\0';
531 dirp->cur = dirp->cur->hh.next;
533 /* LOGI("lo_apk_readdir(%p) = %s:%s", dirp, result.d_type == DT_DIR ? "DIR" : "REG", result.d_name); */
535 return &result;
538 __attribute__ ((visibility("default")))
540 lo_apk_closedir(lo_apk_dir *dirp)
542 free(dirp);
544 /* LOGI("lo_apk_closedir(%p)", dirp); */
546 return 0;
549 static int
550 new_stat(const char *path,
551 struct stat *statp,
552 struct cdir_entry *entry,
553 int mode,
554 int fake_ino)
556 struct tm tm;
558 memset(statp, 0, sizeof(*statp));
559 statp->st_mode = mode | S_IRUSR;
560 statp->st_nlink = 1;
562 statp->st_uid = getuid();
563 statp->st_gid = getgid();
565 if (entry != NULL)
566 statp->st_size = entry->uncompressed_size;
567 else
568 statp->st_size = 0;
569 statp->st_blksize = 512;
570 if (statp->st_size == 0)
571 statp->st_blocks = 0;
572 else
573 statp->st_blocks = (statp->st_size - 1) / statp->st_blksize + 1;
575 statp->st_atime = time(NULL);
577 memset(&tm, 0, sizeof(tm));
578 tm.tm_sec = (letoh16(entry->lastmod_time) & 0x1F) * 2;
579 tm.tm_min = (letoh16(entry->lastmod_time) >> 5) & 0x3F;
580 tm.tm_hour = (letoh16(entry->lastmod_time) >> 11) & 0x1F;
581 tm.tm_mday = letoh16(entry->lastmod_date) & 0x1F;
582 tm.tm_mon = ((letoh16(entry->lastmod_date) >> 5) & 0x0F) - 1;
583 tm.tm_year = ((letoh16(entry->lastmod_date) >> 9) & 0x7F) + 80;
585 statp->st_mtime = mktime(&tm);
586 statp->st_ctime = statp->st_mtime;
588 statp->st_ino = fake_ino;
590 (void) path;
591 /* LOGI("lo_apk_lstat(%s) = { mode=%o, size=%lld, ino=%lld mtime=%.24s }",
592 path, statp->st_mode, statp->st_size, statp->st_ino,
593 ctime((const time_t *) &statp->st_mtime)); */
595 return 0;
598 __attribute__ ((visibility("default")))
600 lo_apk_lstat(const char *path,
601 struct stat *statp)
603 const char *pn = path;
604 int count = cdir_entries;
605 struct cdir_entry *entry = cdir_start;
606 size_t name_size;
608 if (*pn == '/') {
609 pn++;
610 if (!pn[0])
611 return new_stat(path, statp, NULL, S_IFDIR | S_IXUSR, 1);
614 name_size = strlen(pn);
615 while (count--)
617 if (letoh16(entry->filename_size) >= name_size &&
618 !memcmp(entry->data, pn, name_size) &&
619 (letoh16(entry->filename_size) == name_size || entry->data[name_size] == '/'))
620 break;
621 entry = (struct cdir_entry *)((char *)entry + cdir_entry_size(entry));
623 if (count >= 0) {
624 if (letoh16(entry->filename_size) == name_size)
625 return new_stat(path, statp, entry, S_IFREG, cdir_entries - count + 1);
626 else
627 return new_stat(path, statp, entry, S_IFDIR | S_IXUSR, cdir_entries - count + 1);
630 errno = ENOENT;
631 return -1;
634 #define UNPACK_TREE "/assets/unpack"
635 #define UNPACK_TREE_GZ "/assets/gz.unpack"
637 static int
638 mkdir_p(const char *dirname)
640 char *p = malloc(strlen(dirname) + 1);
641 const char *q = dirname + 1;
642 const char *slash;
644 do {
645 slash = strchr(q, '/');
646 if (slash == NULL)
647 slash = q + strlen(q);
648 memcpy(p, dirname, slash-dirname);
649 p[slash-dirname] = '\0';
650 if (mkdir(p, 0700) == -1 && errno != EEXIST) {
651 LOGE("mkdir_p: Could not create %s: %s", p, strerror(errno));
652 free(p);
653 return 0;
655 if (*slash)
656 q = slash + 1;
657 } while (*slash);
659 free(p);
660 return 1;
663 static int
664 extract_gzipped(const char *filename,
665 const char *apkentry,
666 int size,
667 FILE *f)
669 gzFile gzfd;
670 int gzerrno;
671 int nbytes;
672 char buf[5000];
673 int total = 0;
674 char *tmpname;
675 FILE *tmp;
677 tmpname = malloc(strlen(cache_dir) + strlen("/tmp.gz") + 1);
678 strcpy(tmpname, cache_dir);
679 strcat(tmpname, "/tmp.gz");
681 tmp = fopen(tmpname, "w+");
682 unlink(tmpname);
684 if (tmp == NULL) {
685 LOGE("extract_gzipped: could not create %s: %s", tmpname, strerror(errno));
686 free(tmpname);
687 return 0;
690 if (fwrite(apkentry, size, 1, tmp) != 1) {
691 LOGE("extract_gzipped: could not write gzipped entry to %s: %s", tmpname, strerror(errno));
692 fclose(tmp);
693 free(tmpname);
694 return 0;
697 free(tmpname);
698 rewind(tmp);
700 gzfd = gzdopen(fileno(tmp), "rb");
701 if (gzfd == NULL) {
702 LOGE("extract_gzipped: gzdopen failed");
703 fclose(tmp);
704 return 0;
707 while ((nbytes = gzread(gzfd, buf, sizeof(buf))) > 0) {
708 fwrite(buf, nbytes, 1, f);
709 total += nbytes;
711 if (nbytes == -1) {
712 LOGE("extract_gzipped: Could not gzread from %s: %s", filename, gzerror(gzfd, &gzerrno));
713 return total;
715 if (gzclose(gzfd) == -1) {
716 LOGE("extract_gzipped: gzclose failed");
717 return total;
720 return total;
723 static void
724 extract_files(const char *root,
725 const char *prefix,
726 int gzipped)
728 lo_apk_dir *tree = lo_apk_opendir(prefix);
729 struct dirent *dent;
731 if (tree == NULL)
732 return;
734 while ((dent = lo_apk_readdir(tree)) != NULL) {
735 if (strcmp(dent->d_name, ".") == 0 ||
736 strcmp(dent->d_name, "..") == 0)
737 continue;
739 if (dent->d_type == DT_DIR) {
740 char *subdir = malloc(strlen(prefix) + 1 + strlen(dent->d_name) + 1);
741 strcpy(subdir, prefix);
742 strcat(subdir, "/");
743 strcat(subdir, dent->d_name);
744 extract_files(root, subdir, gzipped);
745 free(subdir);
746 } else {
747 char *filename;
748 char *newfilename;
749 const char *apkentry;
750 size_t size;
751 struct stat st;
752 FILE *f;
754 filename = malloc(strlen(prefix) + 1 + strlen(dent->d_name) + 1);
755 strcpy(filename, prefix);
756 strcat(filename, "/");
757 strcat(filename, dent->d_name);
759 apkentry = lo_apkentry(filename, &size);
760 if (apkentry == NULL) {
761 LOGE("extract_files: Could not find %s in .apk", filename);
762 free(filename);
763 continue;
766 newfilename = malloc(strlen(data_dir) + 1 + strlen(prefix) - strlen(root) + strlen(dent->d_name) + 1);
767 strcpy(newfilename, data_dir);
768 strcat(newfilename, "/");
769 strcat(newfilename, prefix + strlen(root) + 1);
771 if (!mkdir_p(newfilename)) {
772 free(filename);
773 free(newfilename);
774 continue;
777 strcat(newfilename, "/");
778 strcat(newfilename, dent->d_name);
780 if (stat(newfilename, &st) == 0 &&
781 (gzipped || st.st_size == size)) {
782 free(filename);
783 free(newfilename);
784 continue;
787 f = fopen(newfilename, "w");
788 if (f == NULL) {
789 LOGE("extract_files: Could not open %s for writing: %s", newfilename, strerror(errno));
790 free(filename);
791 free(newfilename);
792 continue;
795 if (!gzipped) {
796 if (fwrite(apkentry, size, 1, f) != 1) {
797 LOGE("extract_files: Could not write %d bytes to %s: %s", size, newfilename, strerror(errno));
798 } else {
799 LOGI("extract_files: Copied %s to %s: %d bytes", filename, newfilename, size);
801 } else {
802 size = extract_gzipped(filename, apkentry, size, f);
803 LOGI("extract_files: Decompressed %s to %s: %d bytes", filename, newfilename, size);
806 fclose(f);
808 free(filename);
809 free(newfilename);
812 lo_apk_closedir(tree);
815 // static native void extract_files();
817 __attribute__ ((visibility("default")))
818 void
819 Java_org_libreoffice_android_Bootstrap_extract_1files(JNIEnv* env,
820 jobject clazz)
822 (void) env;
823 (void) clazz;
825 extract_files(UNPACK_TREE, UNPACK_TREE, 0);
826 extract_files(UNPACK_TREE_GZ, UNPACK_TREE_GZ, 1);
829 /* Android's JNI works only to libraries loaded through Java's
830 * System.loadLibrary(), it seems. But now with just one big app-specific .so
831 * on Android, that would not be a problem, but for historical reasons, we
832 * have JNI wrappers here, and then call the VCL etc function from them. Oh
833 * well, one could say it's clean to have all the Android-specific JNI
834 * functions here in this file.
837 // public static native void initVCL();
839 extern void InitVCLWrapper(void);
841 __attribute__ ((visibility("default")))
842 void
843 Java_org_libreoffice_android_Bootstrap_initVCL(JNIEnv* env,
844 jobject clazz)
846 (void) env;
847 (void) clazz;
849 InitVCLWrapper();
852 extern void osl_setCommandArgs(int, char **);
854 __attribute__ ((visibility("default")))
855 void
856 Java_org_libreoffice_android_Bootstrap_setCommandArgs(JNIEnv* env,
857 jobject clazz,
858 jobject argv)
860 char **c_argv;
861 int c_argc;
862 Dl_info lo_bootstrap_info;
864 (void) clazz;
866 if (!get_jni_string_array(env, "setCommandArgs :argv", argv, &c_argc, (const char ***) &c_argv))
867 return;
869 if (dladdr(Java_org_libreoffice_android_Bootstrap_setCommandArgs, &lo_bootstrap_info) != 0) {
870 char *new_argv0 = malloc(strlen(lo_bootstrap_info.dli_fname) + strlen(c_argv[0]));
871 char *slash;
872 strcpy(new_argv0, lo_bootstrap_info.dli_fname);
873 slash = strrchr(new_argv0, '/');
874 if (slash != NULL)
875 *slash = '\0';
876 slash = strrchr(new_argv0, '/');
877 if (slash != NULL)
878 strcpy(slash+1, c_argv[0]);
879 else
880 strcpy(new_argv0, c_argv[0]);
881 free(c_argv[0]);
882 c_argv[0] = new_argv0;
885 osl_setCommandArgs(c_argc, c_argv);
888 /* Code for reading lines from the pipe based on the (Apache-licensed) Android
889 * logwrapper.c
892 static int
893 read_from(int fd, const char *tag, char *buffer, int *sz, int *a, int *b, size_t sizeof_buffer)
895 int nread;
897 nread = read(fd, buffer+*b, sizeof_buffer - 1 - *b);
898 *sz = nread;
900 if (nread == -1) {
901 LOGE("redirect_thread: Reading from %d failed: %s", fd, strerror(errno));
902 close(fd);
903 return -1;
906 if (nread == 0) {
907 LOGI("redirect_thread: EOF from fd %d", fd);
908 close(fd);
909 return 0;
912 *sz += *b;
914 for (*b = 0; *b < *sz; (*b)++) {
915 if (buffer[*b] == '\n') {
916 buffer[*b] = '\0';
917 __android_log_print(ANDROID_LOG_INFO, tag, "%s", &buffer[*a]);
918 *a = *b + 1;
922 if (*a == 0 && *b == (int) sizeof_buffer - 1) {
923 // buffer is full, flush
924 buffer[*b] = '\0';
925 __android_log_print(ANDROID_LOG_INFO, tag, "%s", &buffer[*a]);
926 *b = 0;
927 } else if (*a != *b) {
928 // Keep left-overs
929 *b -= *a;
930 memmove(buffer, &buffer[*a], *b);
931 *a = 0;
932 } else {
933 *a = 0;
934 *b = 0;
937 return nread;
940 static int stdout_pipe[2], stderr_pipe[2];
942 static void *
943 redirect_thread(void *arg)
945 char buffer[2][4096];
946 int a[2] = { 0, 0 };
947 int b[2] = { 0, 0 };
948 int sz[2];
950 (void) arg;
952 while (1) {
953 fd_set readfds;
954 int nfds = 0;
956 FD_ZERO(&readfds);
957 if (stdout_pipe[0] != -1) {
958 FD_SET(stdout_pipe[0], &readfds);
959 nfds = MAX(nfds, stdout_pipe[0] + 1);
961 if (stderr_pipe[0] != -1) {
962 FD_SET(stderr_pipe[0], &readfds);
963 nfds = MAX(nfds, stderr_pipe[0] + 1);
965 if (nfds == 0) {
966 LOGI("redirect_thread: Nothing to read any more, thread exiting");
967 return NULL;
970 if (select(nfds, &readfds, NULL, NULL, NULL) == -1) {
971 LOGE("redirect_thread: select failed: %s, thread exiting", strerror(errno));
972 close(stdout_pipe[0]);
973 stdout_pipe[0] = -1;
974 close(stderr_pipe[0]);
975 stderr_pipe[0] = -1;
976 return NULL;
979 if (stdout_pipe[0] != -1 &&
980 FD_ISSET(stdout_pipe[0], &readfds)) {
981 if (read_from(stdout_pipe[0], "stdout", buffer[0], &sz[0], &a[0], &b[0], sizeof(buffer[0])) <= 0) {
982 stdout_pipe[0] = -1;
986 if (stderr_pipe[0] != -1 &&
987 FD_ISSET(stderr_pipe[0], &readfds)) {
988 if (read_from(stderr_pipe[0], "stderr", buffer[1], &sz[1], &a[1], &b[1], sizeof(buffer[1])) <= 0) {
989 stderr_pipe[0] = -1;
995 static int
996 redirect_to_null(void)
998 int null = open("/dev/null", O_WRONLY);
999 if (null == -1) {
1000 LOGE("redirect_stdio: Could not open /dev/null: %s", strerror(errno));
1001 /* If we can't redirect stdout or stderr to /dev/null, just close them
1002 * then instead. Huh?
1004 close(1);
1005 close(2);
1006 return 0;
1008 if (dup2(null, 1) == -1) {
1009 LOGE("redirect_stdio: Could not dup2 %d to 1: %s", null, strerror(errno));
1010 close(null);
1011 close(1);
1012 close(2);
1013 return 0;
1015 if (dup2(null, 2) == -1) {
1016 LOGE("redirect_stdio: Could not dup2 %d to 2: %s", null, strerror(errno));
1017 close(null);
1018 close(1);
1019 close(2);
1020 return 0;
1022 close(null);
1023 return 1;
1026 __attribute__ ((visibility("default")))
1027 jboolean
1028 Java_org_libreoffice_android_Bootstrap_redirect_1stdio(JNIEnv* env,
1029 jobject clazz,
1030 jboolean state)
1032 static jboolean current = JNI_FALSE;
1033 pthread_t thread;
1034 pthread_attr_t attr;
1036 (void) env;
1037 (void) clazz;
1039 if (state == current)
1040 return current;
1042 if (state == JNI_FALSE) {
1043 if (!redirect_to_null())
1044 return current;
1045 } else {
1046 if (pipe(stdout_pipe) == -1) {
1047 LOGE("redirect_stdio: Could not create pipes: %s", strerror(errno));
1048 return current;
1050 if (pipe(stderr_pipe) == -1) {
1051 LOGE("redirect_stdio: Could not create pipes: %s", strerror(errno));
1052 close(stdout_pipe[0]);
1053 close(stdout_pipe[1]);
1054 return current;
1056 LOGI("redirect_stdio: stdout pipe: [%d,%d], stderr pipe: [%d,%d]",
1057 stdout_pipe[0], stdout_pipe[1], stderr_pipe[0], stderr_pipe[1]);
1059 if (dup2(stdout_pipe[1], 1) == -1) {
1060 LOGE("redirect_stdio: Could not dup2 %d to 1: %s", stdout_pipe[1], strerror(errno));
1061 close(stdout_pipe[0]);
1062 close(stdout_pipe[1]);
1063 close(stderr_pipe[0]);
1064 close(stderr_pipe[1]);
1065 return current;
1068 if (dup2(stderr_pipe[1], 2) == -1) {
1069 LOGE("redirect_stdio: Could not dup2 %d to 2: %s", stdout_pipe[1], strerror(errno));
1070 /* stdout has already been redirected to its pipe, so redirect
1071 * it back to /dev/null
1073 redirect_to_null();
1074 close(stdout_pipe[0]);
1075 close(stdout_pipe[1]);
1076 close(stderr_pipe[0]);
1077 close(stderr_pipe[1]);
1078 return current;
1080 close(stdout_pipe[1]);
1081 close(stderr_pipe[1]);
1083 if (pthread_attr_init(&attr) != 0 ||
1084 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0 ||
1085 pthread_create(&thread, &attr, redirect_thread, NULL) != 0) {
1086 LOGE("redirect_stdio: Could not create thread: %s", strerror(errno));
1087 redirect_to_null();
1088 close(stdout_pipe[0]);
1089 close(stderr_pipe[0]);
1090 return current;
1093 current = state;
1094 return current;
1097 __attribute__ ((visibility("default")))
1098 void
1099 Java_org_libreoffice_android_Bootstrap_twiddle_1BGR_1to_1RGBA(JNIEnv* env,
1100 jobject clazz,
1101 jbyteArray source,
1102 jint offset,
1103 jint width,
1104 jint height,
1105 jobject destination)
1107 jbyte *dst = (jbyte*) (*env)->GetDirectBufferAddress(env, destination);
1108 void *a = (*env)->GetPrimitiveArrayCritical(env, source, NULL);
1109 jbyte *src = ((jbyte *) a) + offset;
1111 jbyte *srcp;
1112 jbyte *dstp = dst;
1113 int step = ((((width * 3) - 1) / 4) + 1) * 4;
1115 int i, j;
1117 (void) clazz;
1119 if (height > 0) {
1120 srcp = src + step * (height - 1);
1121 step = -step;
1122 } else {
1123 srcp = src;
1126 LOGI("twiddle: src=%p, srcp=%p, dstp=%p, step=%d", src, srcp, dstp, step);
1128 for (i = 0; i < height; i++) {
1129 for (j = 0; j < width; j++) {
1130 *dstp++ = srcp[j*3+2];
1131 *dstp++ = srcp[j*3+1];
1132 *dstp++ = srcp[j*3+0];
1133 *dstp++ = 0xFF;
1135 srcp += step;
1138 (*env)->ReleasePrimitiveArrayCritical(env, source, a, 0);
1141 __attribute__ ((visibility("default")))
1142 void
1143 Java_org_libreoffice_android_Bootstrap_force_1full_1alpha_1array(JNIEnv* env,
1144 jobject clazz,
1145 jbyteArray array,
1146 jint offset,
1147 jint length)
1149 void *a = (*env)->GetPrimitiveArrayCritical(env, array, NULL);
1150 jbyte *p = ((jbyte *) a) + offset;
1152 int i;
1154 (void) clazz;
1156 for (i = 0; i < length; i += 4) {
1157 p[3] = 0xFF;
1158 p += 4;
1161 (*env)->ReleasePrimitiveArrayCritical(env, array, a, 0);
1164 __attribute__ ((visibility("default")))
1165 void
1166 Java_org_libreoffice_android_Bootstrap_force_1full_1alpha_1bb(JNIEnv* env,
1167 jobject clazz,
1168 jobject buffer,
1169 jint offset,
1170 jint length)
1172 jbyte *p = (*env)->GetDirectBufferAddress(env, buffer) + offset;
1174 int i;
1176 (void) clazz;
1178 for (i = 0; i < length; i += 4) {
1179 p[3] = 0xFF;
1180 p += 4;
1184 __attribute__ ((visibility("default")))
1185 jlong
1186 Java_org_libreoffice_android_Bootstrap_address_1of_1direct_1byte_1buffer(JNIEnv *env,
1187 jobject bbuffer)
1189 return (jlong) (intptr_t) (*env)->GetDirectBufferAddress(env, bbuffer);
1192 __attribute__ ((visibility("default")))
1193 JavaVM *
1194 lo_get_javavm(void)
1196 return the_java_vm;
1199 __attribute__ ((visibility("default")))
1200 const char *
1201 lo_get_app_data_dir(void)
1203 return data_dir;
1206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */