pdf: this is not a pdf
- Commit
7a975f086e6b0d67df0c1d9195c0b7679df83e06- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-04 17:43 UTC (4 months ago)
- Parents
7ea68e8
Diff
pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.ktindex f6f43553be..e55c16df67 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/ui/SafePdfViewerScreen.kt@@ -190,7 +190,9 @@ import java.io.ByteArrayOutputStream private sealed interface LoadState { data object Loading : LoadState- data object Error : LoadState+ /** [notPdf] true when the bytes aren't a PDF at all (e.g. an HTML download page), as+ * opposed to a genuine PDF the parser can't handle. */+ data class Error(val notPdf: Boolean = false) : LoadState data class Loaded(val document: SafePdfDocument) : LoadState } @@ -441,7 +443,7 @@ fun SafePdfViewerScreen(uri: Uri, onBack: () -> Unit) { } else { when (SafePdfDocument.passwordState(context, uri)) { 1 -> { needsPassword = true; pwError = password != null; LoadState.Loading }- else -> LoadState.Error+ else -> LoadState.Error(notPdf = !SafePdfDocument.looksLikePdf(context, uri)) } } }@@ -1023,8 +1025,10 @@ fun SafePdfViewerScreen(uri: Uri, onBack: () -> Unit) { ) { when (val state = loadState) { LoadState.Loading -> CircularProgressIndicator(Modifier.align(Alignment.Center))- LoadState.Error -> Text(- text = stringResource(R.string.safe_pdf_error),+ is LoadState.Error -> Text(+ text = stringResource(+ if (state.notPdf) R.string.safe_pdf_not_a_pdf else R.string.safe_pdf_error+ ), color = MaterialTheme.colorScheme.error, modifier = Modifier.align(Alignment.Center).padding(24.dp), )
pdf/src/main/java/com/vayunmathur/pdf/util/SafePdfDocument.kt
diff --git a/pdf/src/main/java/com/vayunmathur/pdf/util/SafePdfDocument.kt b/pdf/src/main/java/com/vayunmathur/pdf/util/SafePdfDocument.ktindex fd4739939e..c1a191131c 100644--- a/pdf/src/main/java/com/vayunmathur/pdf/util/SafePdfDocument.kt+++ b/pdf/src/main/java/com/vayunmathur/pdf/util/SafePdfDocument.kt@@ -329,5 +329,41 @@ class SafePdfDocument private constructor( }.getOrNull() ?: return@withContext 0 PdfNative.pdfPasswordState(bytes) }++ /**+ * True if [uri] begins like a real PDF — a `%PDF-` header within the first 1024+ * bytes, as the spec requires readers to look for. Used to tell "not a PDF at all"+ * (e.g. a download that returned an HTML error/challenge page saved with a .pdf+ * name) apart from a genuine PDF the parser can't handle, so the error message can+ * be honest. Only the head is read, so it's cheap even for huge inputs.+ */+ suspend fun looksLikePdf(context: Context, uri: Uri): Boolean =+ withContext(Dispatchers.IO) {+ val head = runCatching {+ context.contentResolver.openInputStream(uri)?.use { input ->+ val buf = ByteArray(1024)+ var off = 0+ while (off < buf.size) {+ val r = input.read(buf, off, buf.size - off)+ if (r < 0) break+ off += r+ }+ buf.copyOf(off)+ }+ }.getOrNull() ?: return@withContext false+ indexOf(head, "%PDF-".toByteArray(Charsets.US_ASCII)) >= 0+ }++ /** First index of [needle] in [haystack], or -1. */+ private fun indexOf(haystack: ByteArray, needle: ByteArray): Int {+ if (needle.isEmpty() || haystack.size < needle.size) return -1+ outer@ for (i in 0..haystack.size - needle.size) {+ for (j in needle.indices) {+ if (haystack[i + j] != needle[j]) continue@outer+ }+ return i+ }+ return -1+ } } }
pdf/src/main/res/values/strings.xml
diff --git a/pdf/src/main/res/values/strings.xml b/pdf/src/main/res/values/strings.xmlindex d956d99ebd..f4db5a0af7 100644--- a/pdf/src/main/res/values/strings.xml+++ b/pdf/src/main/res/values/strings.xml@@ -8,6 +8,7 @@ <string name="open_pdf">Open PDF</string> <!-- MainActivity: Error dialog text when safe viewer cannot render file --> <string name="safe_pdf_error">This PDF could not be opened by the safe viewer. It may be damaged or use unsupported features.</string>+ <string name="safe_pdf_not_a_pdf">This file isn\'t a PDF</string> <!-- MainActivity: Button to create PDF from camera capture --> <string name="capture_pdf">Capture PDF</string> <!-- MainActivity: Toolbar title for camera capture flow -->