A bit of libdex spring cleaning.

Give the UTF-8 parsing/validation code and the debug info decoder
their own files in libdex. Also, moved the random utility
dexRoundUpPower2() so it wasn't in the middle of unrelated stuff
(though maybe it doesn't want to stay in DexFile.c at all).

Change-Id: I115447e49904e2440dd538b1df90616ea95707a9
diff --git a/libdex/DexFile.c b/libdex/DexFile.c
index 6f601b6..6567dea 100644
--- a/libdex/DexFile.c
+++ b/libdex/DexFile.c
@@ -43,289 +43,6 @@
 static const bool kVerifyChecksum = false;
 static const bool kVerifySignature = false;
 
-
-/* Compare two '\0'-terminated modified UTF-8 strings, using Unicode
- * code point values for comparison. This treats different encodings
- * for the same code point as equivalent, except that only a real '\0'
- * byte is considered the string terminator. The return value is as
- * for strcmp(). */
-int dexUtf8Cmp(const char* s1, const char* s2) {
-    for (;;) {
-        if (*s1 == '\0') {
-            if (*s2 == '\0') {
-                return 0;
-            }
-            return -1;
-        } else if (*s2 == '\0') {
-            return 1;
-        }
-
-        int utf1 = dexGetUtf16FromUtf8(&s1);
-        int utf2 = dexGetUtf16FromUtf8(&s2);
-        int diff = utf1 - utf2;
-
-        if (diff != 0) {
-            return diff;
-        }
-    }
-}
-
-/* for dexIsValidMemberNameUtf8(), a bit vector indicating valid low ascii */
-u4 DEX_MEMBER_VALID_LOW_ASCII[4] = {
-    0x00000000, // 00..1f low control characters; nothing valid
-    0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
-    0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
-    0x07fffffe  // 60..7f lowercase etc.; valid: 'a'..'z'
-};
-
-/* Helper for dexIsValidMemberNameUtf8(); do not call directly. */
-bool dexIsValidMemberNameUtf8_0(const char** pUtf8Ptr) {
-    /*
-     * It's a multibyte encoded character. Decode it and analyze. We
-     * accept anything that isn't (a) an improperly encoded low value,
-     * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
-     * control character, or (e) a high space, layout, or special
-     * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
-     * U+fff0..U+ffff). This is all specified in the dex format
-     * document.
-     */
-
-    u2 utf16 = dexGetUtf16FromUtf8(pUtf8Ptr);
-
-    // Perform follow-up tests based on the high 8 bits.
-    switch (utf16 >> 8) {
-        case 0x00: {
-            // It's only valid if it's above the ISO-8859-1 high space (0xa0).
-            return (utf16 > 0x00a0);
-        }
-        case 0xd8:
-        case 0xd9:
-        case 0xda:
-        case 0xdb: {
-            /*
-             * It's a leading surrogate. Check to see that a trailing
-             * surrogate follows.
-             */
-            utf16 = dexGetUtf16FromUtf8(pUtf8Ptr);
-            return (utf16 >= 0xdc00) && (utf16 <= 0xdfff);
-        }
-        case 0xdc:
-        case 0xdd:
-        case 0xde:
-        case 0xdf: {
-            // It's a trailing surrogate, which is not valid at this point.
-            return false;
-        }
-        case 0x20:
-        case 0xff: {
-            // It's in the range that has spaces, controls, and specials.
-            switch (utf16 & 0xfff8) {
-                case 0x2000:
-                case 0x2008:
-                case 0x2028:
-                case 0xfff0:
-                case 0xfff8: {
-                    return false;
-                }
-            }
-            break;
-        }
-    }
-
-    return true;
-}
-
-/* Return whether the given string is a valid field or method name. */
-bool dexIsValidMemberName(const char* s) {
-    bool angleName = false;
-
-    switch (*s) {
-        case '\0': {
-            // The empty string is not a valid name.
-            return false;
-        }
-        case '<': {
-            /*
-             * '<' is allowed only at the start of a name, and if present,
-             * means that the name must end with '>'.
-             */
-            angleName = true;
-            s++;
-            break;
-        }
-    }
-
-    for (;;) {
-        switch (*s) {
-            case '\0': {
-                return !angleName;
-            }
-            case '>': {
-                return angleName && s[1] == '\0';
-            }
-        }
-        if (!dexIsValidMemberNameUtf8(&s)) {
-            return false;
-        }
-    }
-}
-
-/* Helper for validating type descriptors and class names, which is parametric
- * with respect to type vs. class and dot vs. slash. */
-static bool isValidTypeDescriptorOrClassName(const char* s, bool isClassName,
-        bool dotSeparator) {
-    int arrayCount = 0;
-
-    while (*s == '[') {
-        arrayCount++;
-        s++;
-    }
-
-    if (arrayCount > 255) {
-        // Arrays may have no more than 255 dimensions.
-        return false;
-    }
-
-    if (arrayCount != 0) {
-        /*
-         * If we're looking at an array of some sort, then it doesn't
-         * matter if what is being asked for is a class name; the
-         * format looks the same as a type descriptor in that case, so
-         * treat it as such.
-         */
-        isClassName = false;
-    }
-
-    if (!isClassName) {
-        /*
-         * We are looking for a descriptor. Either validate it as a
-         * single-character primitive type, or continue on to check the
-         * embedded class name (bracketed by "L" and ";").
-         */
-        switch (*(s++)) {
-            case 'B':
-            case 'C':
-            case 'D':
-            case 'F':
-            case 'I':
-            case 'J':
-            case 'S':
-            case 'Z': {
-                // These are all single-character descriptors for primitive types.
-                return (*s == '\0');
-            }
-            case 'V': {
-                // Non-array void is valid, but you can't have an array of void.
-                return (arrayCount == 0) && (*s == '\0');
-            }
-            case 'L': {
-                // Class name: Break out and continue below.
-                break;
-            }
-            default: {
-                // Oddball descriptor character.
-                return false;
-            }
-        }
-    }
-
-    /*
-     * We just consumed the 'L' that introduces a class name as part
-     * of a type descriptor, or we are looking for an unadorned class
-     * name.
-     */
-
-    bool sepOrFirst = true; // first character or just encountered a separator.
-    for (;;) {
-        u1 c = (u1) *s;
-        switch (c) {
-            case '\0': {
-                /*
-                 * Premature end for a type descriptor, but valid for
-                 * a class name as long as we haven't encountered an
-                 * empty component (including the degenerate case of
-                 * the empty string "").
-                 */
-                return isClassName && !sepOrFirst;
-            }
-            case ';': {
-                /*
-                 * Invalid character for a class name, but the
-                 * legitimate end of a type descriptor. In the latter
-                 * case, make sure that this is the end of the string
-                 * and that it doesn't end with an empty component
-                 * (including the degenerate case of "L;").
-                 */
-                return !isClassName && !sepOrFirst && (s[1] == '\0');
-            }
-            case '/':
-            case '.': {
-                if (dotSeparator != (c == '.')) {
-                    // The wrong separator character.
-                    return false;
-                }
-                if (sepOrFirst) {
-                    // Separator at start or two separators in a row.
-                    return false;
-                }
-                sepOrFirst = true;
-                s++;
-                break;
-            }
-            default: {
-                if (!dexIsValidMemberNameUtf8(&s)) {
-                    return false;
-                }
-                sepOrFirst = false;
-                break;
-            }
-        }
-    }
-}
-
-/* Return whether the given string is a valid type descriptor. */
-bool dexIsValidTypeDescriptor(const char* s) {
-    return isValidTypeDescriptorOrClassName(s, false, false);
-}
-
-/* (documented in header) */
-bool dexIsValidClassName(const char* s, bool dotSeparator) {
-    return isValidTypeDescriptorOrClassName(s, true, dotSeparator);
-}
-
-/* Return whether the given string is a valid reference descriptor. This
- * is true if dexIsValidTypeDescriptor() returns true and the descriptor
- * is for a class or array and not a primitive type. */
-bool dexIsReferenceDescriptor(const char* s) {
-    if (!dexIsValidTypeDescriptor(s)) {
-        return false;
-    }
-
-    return (s[0] == 'L') || (s[0] == '[');
-}
-
-/* Return whether the given string is a valid class descriptor. This
- * is true if dexIsValidTypeDescriptor() returns true and the descriptor
- * is for a class and not an array or primitive type. */
-bool dexIsClassDescriptor(const char* s) {
-    if (!dexIsValidTypeDescriptor(s)) {
-        return false;
-    }
-
-    return s[0] == 'L';
-}
-
-/* Return whether the given string is a valid field type descriptor. This
- * is true if dexIsValidTypeDescriptor() returns true and the descriptor
- * is for anything but "void". */
-bool dexIsFieldDescriptor(const char* s) {
-    if (!dexIsValidTypeDescriptor(s)) {
-        return false;
-    }
-
-    return s[0] != 'V';
-}
-
 /* Return the UTF-8 encoded string with the specified string_id index,
  * also filling in the UTF-16 size (number of 16-bit code points).*/
 const char* dexStringAndSizeById(const DexFile* pDexFile, u4 idx,
@@ -430,24 +147,6 @@
 }
 
 /*
- * Round up to the next highest power of 2.
- *
- * Found on http://graphics.stanford.edu/~seander/bithacks.html.
- */
-u4 dexRoundUpPower2(u4 val)
-{
-    val--;
-    val |= val >> 1;
-    val |= val >> 2;
-    val |= val >> 4;
-    val |= val >> 8;
-    val |= val >> 16;
-    val++;
-
-    return val;
-}
-
-/*
  * Create the class lookup hash table.
  *
  * Returns newly-allocated storage.
@@ -776,303 +475,20 @@
     return (handlerData - (u1*) pCode) + offset;
 }
 
-
 /*
- * ===========================================================================
- *      Debug info
- * ===========================================================================
- */
-
-/*
- * Decode the arguments in a method signature, which looks something
- * like "(ID[Ljava/lang/String;)V".
+ * Round up to the next highest power of 2.
  *
- * Returns the type signature letter for the next argument, or ')' if
- * there are no more args.  Advances "pSig" to point to the character
- * after the one returned.
+ * Found on http://graphics.stanford.edu/~seander/bithacks.html.
  */
-static char decodeSignature(const char** pSig)
+u4 dexRoundUpPower2(u4 val)
 {
-    const char* sig = *pSig;
+    val--;
+    val |= val >> 1;
+    val |= val >> 2;
+    val |= val >> 4;
+    val |= val >> 8;
+    val |= val >> 16;
+    val++;
 
-    if (*sig == '(')
-        sig++;
-
-    if (*sig == 'L') {
-        /* object ref */
-        while (*++sig != ';')
-            ;
-        *pSig = sig+1;
-        return 'L';
-    }
-    if (*sig == '[') {
-        /* array; advance past array type */
-        while (*++sig == '[')
-            ;
-        if (*sig == 'L') {
-            while (*++sig != ';')
-                ;
-        }
-        *pSig = sig+1;
-        return '[';
-    }
-    if (*sig == '\0')
-        return *sig;        /* don't advance further */
-
-    *pSig = sig+1;
-    return *sig;
-}
-
-/*
- * returns the length of a type string, given the start of the
- * type string. Used for the case where the debug info format
- * references types that are inside a method type signature.
- */
-static int typeLength (const char *type) {
-    // Assumes any leading '(' has already been gobbled
-    const char *end = type;
-    decodeSignature(&end);
-    return end - type;
-}
-
-/*
- * Reads a string index as encoded for the debug info format,
- * returning a string pointer or NULL as appropriate.
- */
-static const char* readStringIdx(const DexFile* pDexFile,
-        const u1** pStream) {
-    u4 stringIdx = readUnsignedLeb128(pStream);
-
-    // Remember, encoded string indicies have 1 added to them.
-    if (stringIdx == 0) {
-        return NULL;
-    } else {
-        return dexStringById(pDexFile, stringIdx - 1);
-    }
-}
-
-/*
- * Reads a type index as encoded for the debug info format, returning
- * a string pointer for its descriptor or NULL as appropriate.
- */
-static const char* readTypeIdx(const DexFile* pDexFile,
-        const u1** pStream) {
-    u4 typeIdx = readUnsignedLeb128(pStream);
-
-    // Remember, encoded type indicies have 1 added to them.
-    if (typeIdx == 0) {
-        return NULL;
-    } else {
-        return dexStringByTypeIdx(pDexFile, typeIdx - 1);
-    }
-}
-
-/* access_flag value indicating that a method is static */
-#define ACC_STATIC              0x0008
-
-typedef struct LocalInfo {
-    const char *name;
-    const char *descriptor;
-    const char *signature;
-    u2 startAddress;
-    bool live;
-} LocalInfo;
-
-static void emitLocalCbIfLive (void *cnxt, int reg, u4 endAddress,
-        LocalInfo *localInReg, DexDebugNewLocalCb localCb)
-{
-    if (localCb != NULL && localInReg[reg].live) {
-        localCb(cnxt, reg, localInReg[reg].startAddress, endAddress,
-                localInReg[reg].name,
-                localInReg[reg].descriptor,
-                localInReg[reg].signature == NULL
-                ? "" : localInReg[reg].signature );
-    }
-}
-
-// TODO optimize localCb == NULL case
-void dexDecodeDebugInfo(
-            const DexFile* pDexFile,
-            const DexCode* pCode,
-            const char* classDescriptor,
-            u4 protoIdx,
-            u4 accessFlags,
-            DexDebugNewPositionCb posCb, DexDebugNewLocalCb localCb,
-            void* cnxt)
-{
-    const u1 *stream = dexGetDebugInfoStream(pDexFile, pCode);
-    u4 line;
-    u4 parametersSize;
-    u4 address = 0;
-    LocalInfo localInReg[pCode->registersSize];
-    u4 insnsSize = pCode->insnsSize;
-    DexProto proto = { pDexFile, protoIdx };
-
-    memset(localInReg, 0, sizeof(LocalInfo) * pCode->registersSize);
-
-    if (stream == NULL) {
-        goto end;
-    }
-
-    line = readUnsignedLeb128(&stream);
-    parametersSize = readUnsignedLeb128(&stream);
-
-    u2 argReg = pCode->registersSize - pCode->insSize;
-
-    if ((accessFlags & ACC_STATIC) == 0) {
-        /*
-         * The code is an instance method, which means that there is
-         * an initial this parameter. Also, the proto list should
-         * contain exactly one fewer argument word than the insSize
-         * indicates.
-         */
-        assert(pCode->insSize == (dexProtoComputeArgsSize(&proto) + 1));
-        localInReg[argReg].name = "this";
-        localInReg[argReg].descriptor = classDescriptor;
-        localInReg[argReg].startAddress = 0;
-        localInReg[argReg].live = true;
-        argReg++;
-    } else {
-        assert(pCode->insSize == dexProtoComputeArgsSize(&proto));
-    }
-
-    DexParameterIterator iterator;
-    dexParameterIteratorInit(&iterator, &proto);
-
-    while (parametersSize-- != 0) {
-        const char* descriptor = dexParameterIteratorNextDescriptor(&iterator);
-        const char *name;
-        int reg;
-
-        if ((argReg >= pCode->registersSize) || (descriptor == NULL)) {
-            goto invalid_stream;
-        }
-
-        name = readStringIdx(pDexFile, &stream);
-        reg = argReg;
-
-        switch (descriptor[0]) {
-            case 'D':
-            case 'J':
-                argReg += 2;
-                break;
-            default:
-                argReg += 1;
-                break;
-        }
-
-        if (name != NULL) {
-            localInReg[reg].name = name;
-            localInReg[reg].descriptor = descriptor;
-            localInReg[reg].signature = NULL;
-            localInReg[reg].startAddress = address;
-            localInReg[reg].live = true;
-        }
-    }
-
-    for (;;)  {
-        u1 opcode = *stream++;
-        u2 reg;
-
-        switch (opcode) {
-            case DBG_END_SEQUENCE:
-                goto end;
-
-            case DBG_ADVANCE_PC:
-                address += readUnsignedLeb128(&stream);
-                break;
-
-            case DBG_ADVANCE_LINE:
-                line += readSignedLeb128(&stream);
-                break;
-
-            case DBG_START_LOCAL:
-            case DBG_START_LOCAL_EXTENDED:
-                reg = readUnsignedLeb128(&stream);
-                if (reg > pCode->registersSize) goto invalid_stream;
-
-                // Emit what was previously there, if anything
-                emitLocalCbIfLive (cnxt, reg, address,
-                    localInReg, localCb);
-
-                localInReg[reg].name = readStringIdx(pDexFile, &stream);
-                localInReg[reg].descriptor = readTypeIdx(pDexFile, &stream);
-                if (opcode == DBG_START_LOCAL_EXTENDED) {
-                    localInReg[reg].signature
-                        = readStringIdx(pDexFile, &stream);
-                } else {
-                    localInReg[reg].signature = NULL;
-                }
-                localInReg[reg].startAddress = address;
-                localInReg[reg].live = true;
-                break;
-
-            case DBG_END_LOCAL:
-                reg = readUnsignedLeb128(&stream);
-                if (reg > pCode->registersSize) goto invalid_stream;
-
-                emitLocalCbIfLive (cnxt, reg, address, localInReg, localCb);
-                localInReg[reg].live = false;
-                break;
-
-            case DBG_RESTART_LOCAL:
-                reg = readUnsignedLeb128(&stream);
-                if (reg > pCode->registersSize) goto invalid_stream;
-
-                if (localInReg[reg].name == NULL
-                        || localInReg[reg].descriptor == NULL) {
-                    goto invalid_stream;
-                }
-
-                /*
-                 * If the register is live, the "restart" is superfluous,
-                 * and we don't want to mess with the existing start address.
-                 */
-                if (!localInReg[reg].live) {
-                    localInReg[reg].startAddress = address;
-                    localInReg[reg].live = true;
-                }
-                break;
-
-            case DBG_SET_PROLOGUE_END:
-            case DBG_SET_EPILOGUE_BEGIN:
-            case DBG_SET_FILE:
-                break;
-
-            default: {
-                int adjopcode = opcode - DBG_FIRST_SPECIAL;
-
-                address += adjopcode / DBG_LINE_RANGE;
-                line += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE);
-
-                if (posCb != NULL) {
-                    int done;
-                    done = posCb(cnxt, address, line);
-
-                    if (done) {
-                        // early exit
-                        goto end;
-                    }
-                }
-                break;
-            }
-        }
-    }
-
-end:
-    {
-        int reg;
-        for (reg = 0; reg < pCode->registersSize; reg++) {
-            emitLocalCbIfLive (cnxt, reg, insnsSize, localInReg, localCb);
-        }
-    }
-    return;
-
-invalid_stream:
-    IF_LOGE() {
-        char* methodDescriptor = dexProtoCopyMethodDescriptor(&proto);
-        LOGE("Invalid debug info stream. class %s; proto %s",
-                classDescriptor, methodDescriptor);
-        free(methodDescriptor);
-    }
+    return val;
 }