Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1a8b6c29 authored by Homin Lee's avatar Homin Lee
Browse files

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: default avatarHomin Lee <suapapa@insignal.co.kr>
parent 3d7f0cb3
Loading
Loading
Loading
Loading
+5 −48
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#define LOG_TAG "MediaScannerJNI"
#include <utils/Log.h>
#include <utils/threads.h>
#include <utils/Unicode.h>
#include <media/mediascanner.h>
#include <media/stagefright/StagefrightMediaScanner.h>

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

// stolen from dalvik/vm/checkJni.cpp
static bool isValidUtf8(const char* bytes) {
    while (*bytes != '\0') {
        unsigned char utf8 = *(bytes++);
        // Switch on the high four bits.
        switch (utf8 >> 4) {
        case 0x00:
        case 0x01:
        case 0x02:
        case 0x03:
        case 0x04:
        case 0x05:
        case 0x06:
        case 0x07:
            // Bit pattern 0xxx. No need for any extra bytes.
            break;
        case 0x08:
        case 0x09:
        case 0x0a:
        case 0x0b:
        case 0x0f:
            /*
             * Bit pattern 10xx or 1111, which are illegal start bytes.
             * Note: 1111 is valid for normal UTF-8, but not the
             * modified UTF-8 used here.
             */
            return false;
        case 0x0e:
            // Bit pattern 1110, so there are two additional bytes.
            utf8 = *(bytes++);
            if ((utf8 & 0xc0) != 0x80) {
                return false;
            }
            // Fall through to take care of the final byte.
        case 0x0c:
        case 0x0d:
            // Bit pattern 110x, so there is one additional byte.
            utf8 = *(bytes++);
            if ((utf8 & 0xc0) != 0x80) {
                return false;
            }
            break;
        }
    }
    return true;
}

class MyMediaScannerClient : public MediaScannerClient
{
public:
@@ -170,8 +124,11 @@ public:
            mEnv->ExceptionClear();
            return NO_MEMORY;
        }

        // Check if the value is valid UTF-8 string and replace
        // any un-printable characters with '?' when it's not.
        char *cleaned = NULL;
        if (!isValidUtf8(value)) {
        if (utf8_length(value) == -1) {
            cleaned = strdup(value);
            char *chp = cleaned;
            char ch;