Skip to content

Commit 1a8b6c2

Browse files
author
Homin Lee
committed
use utf8_length() instead of local function, isValidUtf8()
utf8_length() from libutils returns -1 when source not contains valid sequence for UTF-8. Fixed to use it and removed the local function isValidUtf8(). Change-Id: If2834ce1d1ae07fd8526ce8bc5df3fd3f44e85c8 Signed-off-by: Homin Lee <suapapa@insignal.co.kr>
1 parent 3d7f0cb commit 1a8b6c2

File tree

1 file changed

+5
-48
lines changed

1 file changed

+5
-48
lines changed

media/jni/android_media_MediaScanner.cpp

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define LOG_TAG "MediaScannerJNI"
2020
#include <utils/Log.h>
2121
#include <utils/threads.h>
22+
#include <utils/Unicode.h>
2223
#include <media/mediascanner.h>
2324
#include <media/stagefright/StagefrightMediaScanner.h>
2425

@@ -56,53 +57,6 @@ static status_t checkAndClearExceptionFromCallback(JNIEnv* env, const char* meth
5657
return OK;
5758
}
5859

59-
// stolen from dalvik/vm/checkJni.cpp
60-
static bool isValidUtf8(const char* bytes) {
61-
while (*bytes != '\0') {
62-
unsigned char utf8 = *(bytes++);
63-
// Switch on the high four bits.
64-
switch (utf8 >> 4) {
65-
case 0x00:
66-
case 0x01:
67-
case 0x02:
68-
case 0x03:
69-
case 0x04:
70-
case 0x05:
71-
case 0x06:
72-
case 0x07:
73-
// Bit pattern 0xxx. No need for any extra bytes.
74-
break;
75-
case 0x08:
76-
case 0x09:
77-
case 0x0a:
78-
case 0x0b:
79-
case 0x0f:
80-
/*
81-
* Bit pattern 10xx or 1111, which are illegal start bytes.
82-
* Note: 1111 is valid for normal UTF-8, but not the
83-
* modified UTF-8 used here.
84-
*/
85-
return false;
86-
case 0x0e:
87-
// Bit pattern 1110, so there are two additional bytes.
88-
utf8 = *(bytes++);
89-
if ((utf8 & 0xc0) != 0x80) {
90-
return false;
91-
}
92-
// Fall through to take care of the final byte.
93-
case 0x0c:
94-
case 0x0d:
95-
// Bit pattern 110x, so there is one additional byte.
96-
utf8 = *(bytes++);
97-
if ((utf8 & 0xc0) != 0x80) {
98-
return false;
99-
}
100-
break;
101-
}
102-
}
103-
return true;
104-
}
105-
10660
class MyMediaScannerClient : public MediaScannerClient
10761
{
10862
public:
@@ -170,8 +124,11 @@ class MyMediaScannerClient : public MediaScannerClient
170124
mEnv->ExceptionClear();
171125
return NO_MEMORY;
172126
}
127+
128+
// Check if the value is valid UTF-8 string and replace
129+
// any un-printable characters with '?' when it's not.
173130
char *cleaned = NULL;
174-
if (!isValidUtf8(value)) {
131+
if (utf8_length(value) == -1) {
175132
cleaned = strdup(value);
176133
char *chp = cleaned;
177134
char ch;

0 commit comments

Comments
 (0)