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/dexdump/DexDump.c b/dexdump/DexDump.c
index 914f49f..9353471 100644
--- a/dexdump/DexDump.c
+++ b/dexdump/DexDump.c
@@ -29,14 +29,17 @@
  * - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
  * - class shows declared fields and methods; does not show inherited fields
  */
+
 #include "libdex/DexFile.h"
+
+#include "libdex/CmdUtils.h"
 #include "libdex/DexCatch.h"
 #include "libdex/DexClass.h"
+#include "libdex/DexDebugInfo.h"
 #include "libdex/DexOpcodes.h"
 #include "libdex/DexProto.h"
 #include "libdex/InstrUtils.h"
 #include "libdex/SysUtil.h"
-#include "libdex/CmdUtils.h"
 
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/dexlist/DexList.c b/dexlist/DexList.c
index 3552d2e..5326692 100644
--- a/dexlist/DexList.c
+++ b/dexlist/DexList.c
@@ -17,11 +17,14 @@
 /*
  * List all methods in all concrete classes in one or more DEX files.
  */
+
 #include "libdex/DexFile.h"
+
+#include "libdex/CmdUtils.h"
 #include "libdex/DexClass.h"
+#include "libdex/DexDebugInfo.h"
 #include "libdex/DexProto.h"
 #include "libdex/SysUtil.h"
-#include "libdex/CmdUtils.h"
 
 #include <stdlib.h>
 #include <stdio.h>
diff --git a/libdex/Android.mk b/libdex/Android.mk
index 281801e..26f84d6 100644
--- a/libdex/Android.mk
+++ b/libdex/Android.mk
@@ -19,12 +19,14 @@
 	DexCatch.c \
 	DexClass.c \
 	DexDataMap.c \
+	DexDebugInfo.c \
 	DexFile.c \
 	DexInlines.c \
 	DexOptData.c \
 	DexOpcodes.c \
 	DexProto.c \
 	DexSwapVerify.c \
+	DexUtf.c \
 	InstrUtils.c \
 	Leb128.c \
 	OptInvocation.c \
diff --git a/libdex/DexDebugInfo.c b/libdex/DexDebugInfo.c
new file mode 100644
index 0000000..43765f7
--- /dev/null
+++ b/libdex/DexDebugInfo.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Handling of method debug info in a .dex file.
+ */
+
+#include "DexDebugInfo.h"
+#include "DexProto.h"
+#include "Leb128.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Decode the arguments in a method signature, which looks something
+ * like "(ID[Ljava/lang/String;)V".
+ *
+ * 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.
+ */
+static char decodeSignature(const char** pSig)
+{
+    const char* sig = *pSig;
+
+    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);
+    }
+}
+
+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);
+    }
+}
diff --git a/libdex/DexDebugInfo.h b/libdex/DexDebugInfo.h
new file mode 100644
index 0000000..f23e365
--- /dev/null
+++ b/libdex/DexDebugInfo.h
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _LIBDEX_DEXDEBUGINFO
+#define _LIBDEX_DEXDEBUGINFO
+
+/*
+ * Handling of method debug info in a .dex file.
+ */
+
+#include "DexFile.h"
+
+/*
+ * Callback for "new position table entry".
+ * Returning non-0 causes the decoder to stop early.
+ */
+typedef int (*DexDebugNewPositionCb)(void *cnxt, u4 address, u4 lineNum);
+
+/*
+ * Callback for "new locals table entry". "signature" is an empty string
+ * if no signature is available for an entry.
+ */
+typedef void (*DexDebugNewLocalCb)(void *cnxt, u2 reg, u4 startAddress,
+        u4 endAddress, const char *name, const char *descriptor,
+        const char *signature);
+
+/*
+ * Decode debug info for method.
+ *
+ * posCb is called in ascending address order.
+ * localCb is called in order of ascending end address.
+ */
+void dexDecodeDebugInfo(
+            const DexFile* pDexFile,
+            const DexCode* pDexCode,
+            const char* classDescriptor,
+            u4 protoIdx,
+            u4 accessFlags,
+            DexDebugNewPositionCb posCb, DexDebugNewLocalCb localCb,
+            void* cnxt);
+
+#endif /* def _LIBDEX_DEXDEBUGINFO */
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;
 }
diff --git a/libdex/DexFile.h b/libdex/DexFile.h
index 798d607..13eac87 100644
--- a/libdex/DexFile.h
+++ b/libdex/DexFile.h
@@ -750,35 +750,6 @@
     }
 }
 
-/*
- * Callback for "new position table entry".
- * Returning non-0 causes the decoder to stop early.
- */
-typedef int (*DexDebugNewPositionCb)(void *cnxt, u4 address, u4 lineNum);
-
-/*
- * Callback for "new locals table entry". "signature" is an empty string
- * if no signature is available for an entry.
- */
-typedef void (*DexDebugNewLocalCb)(void *cnxt, u2 reg, u4 startAddress,
-        u4 endAddress, const char *name, const char *descriptor,
-        const char *signature);
-
-/*
- * Decode debug info for method.
- *
- * posCb is called in ascending address order.
- * localCb is called in order of ascending end address.
- */
-void dexDecodeDebugInfo(
-            const DexFile* pDexFile,
-            const DexCode* pDexCode,
-            const char* classDescriptor,
-            u4 protoIdx,
-            u4 accessFlags,
-            DexDebugNewPositionCb posCb, DexDebugNewLocalCb localCb,
-            void* cnxt);
-
 /* DexClassDef convenience - get class descriptor */
 DEX_INLINE const char* dexGetClassDescriptor(const DexFile* pDexFile,
     const DexClassDef* pClassDef)
@@ -946,117 +917,4 @@
         (pDexFile->baseAddr + dexGetAnnotationOff(pAnnoSet, idx));
 }
 
-
-/*
- * ===========================================================================
- *      Utility Functions
- * ===========================================================================
- */
-
-/*
- * Retrieve the next UTF-16 character from a UTF-8 string.
- *
- * Advances "*pUtf8Ptr" to the start of the next character.
- *
- * WARNING: If a string is corrupted by dropping a '\0' in the middle
- * of a 3-byte sequence, you can end up overrunning the buffer with
- * reads (and possibly with the writes if the length was computed and
- * cached before the damage). For performance reasons, this function
- * assumes that the string being parsed is known to be valid (e.g., by
- * already being verified). Most strings we process here are coming
- * out of dex files or other internal translations, so the only real
- * risk comes from the JNI NewStringUTF call.
- */
-DEX_INLINE u2 dexGetUtf16FromUtf8(const char** pUtf8Ptr)
-{
-    unsigned int one, two, three;
-
-    one = *(*pUtf8Ptr)++;
-    if ((one & 0x80) != 0) {
-        /* two- or three-byte encoding */
-        two = *(*pUtf8Ptr)++;
-        if ((one & 0x20) != 0) {
-            /* three-byte encoding */
-            three = *(*pUtf8Ptr)++;
-            return ((one & 0x0f) << 12) |
-                   ((two & 0x3f) << 6) |
-                   (three & 0x3f);
-        } else {
-            /* two-byte encoding */
-            return ((one & 0x1f) << 6) |
-                   (two & 0x3f);
-        }
-    } else {
-        /* one-byte encoding */
-        return one;
-    }
-}
-
-/* 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 dexIsValidMemberNameUtf8(), a bit vector indicating valid low ascii */
-extern u4 DEX_MEMBER_VALID_LOW_ASCII[4];
-
-/* Helper for dexIsValidMemberUtf8(); do not call directly. */
-bool dexIsValidMemberNameUtf8_0(const char** pUtf8Ptr);
-
-/* Return whether the pointed-at modified-UTF-8 encoded character is
- * valid as part of a member name, updating the pointer to point past
- * the consumed character. This will consume two encoded UTF-16 code
- * points if the character is encoded as a surrogate pair. Also, if
- * this function returns false, then the given pointer may only have
- * been partially advanced. */
-DEX_INLINE bool dexIsValidMemberNameUtf8(const char** pUtf8Ptr) {
-    u1 c = (u1) **pUtf8Ptr;
-    if (c <= 0x7f) {
-        // It's low-ascii, so check the table.
-        u4 wordIdx = c >> 5;
-        u4 bitIdx = c & 0x1f;
-        (*pUtf8Ptr)++;
-        return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
-    }
-
-    /*
-     * It's a multibyte encoded character. Call a non-inline function
-     * for the heavy lifting.
-     */
-    return dexIsValidMemberNameUtf8_0(pUtf8Ptr);
-}
-
-/* Return whether the given string is a valid field or method name. */
-bool dexIsValidMemberName(const char* s);
-
-/* Return whether the given string is a valid type descriptor. */
-bool dexIsValidTypeDescriptor(const char* s);
-
-/* Return whether the given string is a valid internal-form class
- * name, with components separated either by dots or slashes as
- * specified. A class name is like a type descriptor, except that it
- * can't name a primitive type (including void). In terms of syntax,
- * the form is either (a) the name of the class without adornment
- * (that is, not bracketed by "L" and ";"); or (b) identical to the
- * type descriptor syntax for array types. */
-bool dexIsValidClassName(const char* s, bool 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);
-
-/* 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);
-
-/* 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);
-
 #endif /*_LIBDEX_DEXFILE*/
diff --git a/libdex/DexInlines.c b/libdex/DexInlines.c
index f33835f..cbedb62 100644
--- a/libdex/DexInlines.c
+++ b/libdex/DexInlines.c
@@ -25,6 +25,7 @@
 #include "DexCatch.h"
 #include "DexClass.h"
 #include "DexDataMap.h"
+#include "DexUtf.h"
 #include "DexOpcodes.h"
 #include "DexProto.h"
 #include "InstrUtils.h"
diff --git a/libdex/DexSwapVerify.c b/libdex/DexSwapVerify.c
index a467fa7..5fd3f09 100644
--- a/libdex/DexSwapVerify.c
+++ b/libdex/DexSwapVerify.c
@@ -22,6 +22,7 @@
 #include "DexClass.h"
 #include "DexDataMap.h"
 #include "DexProto.h"
+#include "DexUtf.h"
 #include "Leb128.h"
 
 #include <safe_iop.h>
diff --git a/libdex/DexUtf.c b/libdex/DexUtf.c
new file mode 100644
index 0000000..df49d18
--- /dev/null
+++ b/libdex/DexUtf.c
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Validate and manipulate MUTF-8 encoded string data.
+ */
+
+#include "DexUtf.h"
+
+/* 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';
+}
+
diff --git a/libdex/DexUtf.h b/libdex/DexUtf.h
new file mode 100644
index 0000000..a7eb28c
--- /dev/null
+++ b/libdex/DexUtf.h
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * Validate and manipulate MUTF-8 (modified UTF-8) encoded string data.
+ */
+
+#ifndef _LIBDEX_DEXUTF
+#define _LIBDEX_DEXUTF
+
+#include "DexFile.h"
+
+/*
+ * Retrieve the next UTF-16 character from a UTF-8 string.
+ *
+ * Advances "*pUtf8Ptr" to the start of the next character.
+ *
+ * WARNING: If a string is corrupted by dropping a '\0' in the middle
+ * of a 3-byte sequence, you can end up overrunning the buffer with
+ * reads (and possibly with the writes if the length was computed and
+ * cached before the damage). For performance reasons, this function
+ * assumes that the string being parsed is known to be valid (e.g., by
+ * already being verified). Most strings we process here are coming
+ * out of dex files or other internal translations, so the only real
+ * risk comes from the JNI NewStringUTF call.
+ */
+DEX_INLINE u2 dexGetUtf16FromUtf8(const char** pUtf8Ptr)
+{
+    unsigned int one, two, three;
+
+    one = *(*pUtf8Ptr)++;
+    if ((one & 0x80) != 0) {
+        /* two- or three-byte encoding */
+        two = *(*pUtf8Ptr)++;
+        if ((one & 0x20) != 0) {
+            /* three-byte encoding */
+            three = *(*pUtf8Ptr)++;
+            return ((one & 0x0f) << 12) |
+                   ((two & 0x3f) << 6) |
+                   (three & 0x3f);
+        } else {
+            /* two-byte encoding */
+            return ((one & 0x1f) << 6) |
+                   (two & 0x3f);
+        }
+    } else {
+        /* one-byte encoding */
+        return one;
+    }
+}
+
+/* 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 dexIsValidMemberNameUtf8(), a bit vector indicating valid low ascii */
+extern u4 DEX_MEMBER_VALID_LOW_ASCII[4];
+
+/* Helper for dexIsValidMemberUtf8(); do not call directly. */
+bool dexIsValidMemberNameUtf8_0(const char** pUtf8Ptr);
+
+/* Return whether the pointed-at modified-UTF-8 encoded character is
+ * valid as part of a member name, updating the pointer to point past
+ * the consumed character. This will consume two encoded UTF-16 code
+ * points if the character is encoded as a surrogate pair. Also, if
+ * this function returns false, then the given pointer may only have
+ * been partially advanced. */
+DEX_INLINE bool dexIsValidMemberNameUtf8(const char** pUtf8Ptr) {
+    u1 c = (u1) **pUtf8Ptr;
+    if (c <= 0x7f) {
+        // It's low-ascii, so check the table.
+        u4 wordIdx = c >> 5;
+        u4 bitIdx = c & 0x1f;
+        (*pUtf8Ptr)++;
+        return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
+    }
+
+    /*
+     * It's a multibyte encoded character. Call a non-inline function
+     * for the heavy lifting.
+     */
+    return dexIsValidMemberNameUtf8_0(pUtf8Ptr);
+}
+
+/* Return whether the given string is a valid field or method name. */
+bool dexIsValidMemberName(const char* s);
+
+/* Return whether the given string is a valid type descriptor. */
+bool dexIsValidTypeDescriptor(const char* s);
+
+/* Return whether the given string is a valid internal-form class
+ * name, with components separated either by dots or slashes as
+ * specified. A class name is like a type descriptor, except that it
+ * can't name a primitive type (including void). In terms of syntax,
+ * the form is either (a) the name of the class without adornment
+ * (that is, not bracketed by "L" and ";"); or (b) identical to the
+ * type descriptor syntax for array types. */
+bool dexIsValidClassName(const char* s, bool 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);
+
+/* 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);
+
+/* 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);
+
+#endif /* def _LIBDEX_DEXUTF */
diff --git a/vm/Dalvik.h b/vm/Dalvik.h
index 7164ec4..81dee3c 100644
--- a/vm/Dalvik.h
+++ b/vm/Dalvik.h
@@ -28,8 +28,10 @@
 #include "Bits.h"
 #include "BitVector.h"
 #include "libdex/SysUtil.h"
+#include "libdex/DexDebugInfo.h"
 #include "libdex/DexFile.h"
 #include "libdex/DexProto.h"
+#include "libdex/DexUtf.h"
 #include "libdex/ZipArchive.h"
 #include "DvmDex.h"
 #include "RawDexFile.h"