archive.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. /* Archive interface */
  23. JNIEXPORT void JNICALL
  24. FUN(Archive_finalize)(JNIEnv *env, jobject self)
  25. {
  26. fz_context *ctx = get_context(env);
  27. fz_archive *arch = from_Archive_safe(env, self);
  28. if (!ctx || !arch) return;
  29. (*env)->SetLongField(env, self, fid_Archive_pointer, 0);
  30. fz_drop_archive(ctx, arch);
  31. }
  32. JNIEXPORT jlong JNICALL
  33. FUN(Archive_newNativeArchive)(JNIEnv *env, jobject self, jstring jpath)
  34. {
  35. fz_context *ctx = get_context(env);
  36. fz_archive *arch = NULL;
  37. const char *path = NULL;
  38. if (!ctx) return 0;
  39. if (!jpath) jni_throw_arg(env, "path must not be null");
  40. path = (*env)->GetStringUTFChars(env, jpath, NULL);
  41. fz_try(ctx)
  42. {
  43. if (fz_is_directory(ctx, path))
  44. arch = fz_open_directory(ctx, path);
  45. else
  46. arch = fz_open_archive(ctx, path);
  47. }
  48. fz_always(ctx)
  49. (*env)->ReleaseStringUTFChars(env, jpath, path);
  50. fz_catch(ctx)
  51. jni_rethrow(env, ctx);
  52. return jlong_cast(arch);
  53. }
  54. JNIEXPORT jstring JNICALL
  55. FUN(Archive_getFormat)(JNIEnv *env, jobject self)
  56. {
  57. fz_context *ctx = get_context(env);
  58. fz_archive *arch = from_Archive(env, self);
  59. const char *format = NULL;
  60. if (!ctx || !arch) return NULL;
  61. fz_try(ctx)
  62. format = fz_archive_format(ctx, arch);
  63. fz_catch(ctx)
  64. jni_rethrow(env, ctx);
  65. return (*env)->NewStringUTF(env, format);
  66. }
  67. JNIEXPORT jint JNICALL
  68. FUN(Archive_countEntries)(JNIEnv *env, jobject self)
  69. {
  70. fz_context *ctx = get_context(env);
  71. fz_archive *arch = from_Archive(env, self);
  72. int count = -1;
  73. if (!ctx || !arch) return -1;
  74. fz_try(ctx)
  75. count = fz_count_archive_entries(ctx, arch);
  76. fz_catch(ctx)
  77. jni_rethrow(env, ctx);
  78. return count;
  79. }
  80. JNIEXPORT jstring JNICALL
  81. FUN(Archive_listEntry)(JNIEnv *env, jobject self, jint index)
  82. {
  83. fz_context *ctx = get_context(env);
  84. fz_archive *arch = from_Archive(env, self);
  85. const char *name = NULL;
  86. if (!ctx || !arch) return NULL;
  87. fz_try(ctx)
  88. name = fz_list_archive_entry(ctx, arch, index);
  89. fz_catch(ctx)
  90. jni_rethrow(env, ctx);
  91. return (*env)->NewStringUTF(env, name);
  92. }
  93. JNIEXPORT jboolean JNICALL
  94. FUN(Archive_hasEntry)(JNIEnv *env, jobject self, jstring jname)
  95. {
  96. fz_context *ctx = get_context(env);
  97. fz_archive *arch = from_Archive(env, self);
  98. const char *name = NULL;
  99. int has = 0;
  100. if (!ctx || !arch) return JNI_FALSE;
  101. if (!jname) jni_throw_arg(env, "name must not be null");
  102. name = (*env)->GetStringUTFChars(env, jname, NULL);
  103. fz_try(ctx)
  104. has = fz_has_archive_entry(ctx, arch, name);
  105. fz_always(ctx)
  106. (*env)->ReleaseStringUTFChars(env, jname, name);
  107. fz_catch(ctx)
  108. jni_rethrow(env, ctx);
  109. return has;
  110. }
  111. JNIEXPORT jobject JNICALL
  112. FUN(Archive_readEntry)(JNIEnv *env, jobject self, jstring jname)
  113. {
  114. fz_context *ctx = get_context(env);
  115. fz_archive *arch = from_Archive(env, self);
  116. const char *name = NULL;
  117. fz_buffer *buffer = NULL;
  118. if (!ctx || !arch) return NULL;
  119. if (!jname) jni_throw_arg(env, "name must not be null");
  120. name = (*env)->GetStringUTFChars(env, jname, NULL);
  121. fz_try(ctx)
  122. buffer = fz_read_archive_entry(ctx, arch, name);
  123. fz_always(ctx)
  124. (*env)->ReleaseStringUTFChars(env, jname, name);
  125. fz_catch(ctx)
  126. jni_rethrow(env, ctx);
  127. return to_Buffer_safe_own(ctx, env, buffer);
  128. }