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

Commit 890b44e5 authored by Jean Chalard's avatar Jean Chalard
Browse files

Correctly read the header of APK-embedded dicts

Bug: 13164518
Change-Id: I8768ad887af8b89ad9f29637f606c3c68629c7ca
parent b08a9e02
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -229,7 +229,7 @@ final public class BinaryDictionaryGetter {
    private static boolean hackCanUseDictionaryFile(final Locale locale, final File f) {
        try {
            // Read the version of the file
            final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f);
            final DictDecoder dictDecoder = FormatSpec.getDictDecoder(f, 0, f.length());
            final DictionaryHeader header = dictDecoder.readHeader();

            final String version = header.mDictionaryOptions.mAttributes.get(VERSION_KEY);
+1 −1
Original line number Diff line number Diff line
@@ -357,7 +357,7 @@ public final class BinaryDictDecoderUtils {
     * @return true if it's a binary dictionary, false otherwise
     */
    public static boolean isBinaryDictionary(final File file) {
        final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file);
        final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, 0, file.length());
        if (dictDecoder == null) {
            return false;
        }
+2 −3
Original line number Diff line number Diff line
@@ -237,7 +237,7 @@ public final class BinaryDictIOUtils {
            final File file, final long offset, final long length)
            throws FileNotFoundException, IOException, UnsupportedFormatException {
        final byte[] buffer = new byte[HEADER_READING_BUFFER_SIZE];
        final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file,
        final DictDecoder dictDecoder = FormatSpec.getDictDecoder(file, offset, length,
                new DictDecoder.DictionaryBufferFactory() {
                    @Override
                    public DictBuffer getDictionaryBuffer(File file)
@@ -251,8 +251,7 @@ public final class BinaryDictIOUtils {
                            inStream.close();
                        }
                    }
                }
        );
                });
        if (dictDecoder == null) {
            return null;
        }
+11 −7
Original line number Diff line number Diff line
@@ -326,30 +326,34 @@ public final class FormatSpec {
     * Returns new dictionary decoder.
     *
     * @param dictFile the dictionary file.
     * @param offset the offset in the file.
     * @param length the length of the file, in bytes.
     * @param bufferType The type of buffer, as one of USE_* in DictDecoder.
     * @return new dictionary decoder if the dictionary file exists, otherwise null.
     */
    public static DictDecoder getDictDecoder(final File dictFile, final int bufferType) {
    public static DictDecoder getDictDecoder(final File dictFile, final long offset,
            final long length, final int bufferType) {
        if (dictFile.isDirectory()) {
            return new Ver4DictDecoder(dictFile, bufferType);
        } else if (dictFile.isFile()) {
            return new Ver2DictDecoder(dictFile, bufferType);
            return new Ver2DictDecoder(dictFile, offset, length, bufferType);
        }
        return null;
    }

    public static DictDecoder getDictDecoder(final File dictFile,
            final DictionaryBufferFactory factory) {
    public static DictDecoder getDictDecoder(final File dictFile, final long offset,
            final long length, final DictionaryBufferFactory factory) {
        if (dictFile.isDirectory()) {
            return new Ver4DictDecoder(dictFile, factory);
        } else if (dictFile.isFile()) {
            return new Ver2DictDecoder(dictFile, factory);
            return new Ver2DictDecoder(dictFile, offset, length, factory);
        }
        return null;
    }

    public static DictDecoder getDictDecoder(final File dictFile) {
        return getDictDecoder(dictFile, DictDecoder.USE_READONLY_BYTEBUFFER);
    public static DictDecoder getDictDecoder(final File dictFile, final long offset,
            final long length) {
        return getDictDecoder(dictFile, offset, length, DictDecoder.USE_READONLY_BYTEBUFFER);
    }

    private FormatSpec() {
+13 −5
Original line number Diff line number Diff line
@@ -116,13 +116,18 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
    }

    protected final File mDictionaryBinaryFile;
    protected final long mOffset;
    protected final long mLength;
    // TODO: Remove mBufferFactory and mDictBuffer from this class members because they are now
    // used only for testing.
    private final DictionaryBufferFactory mBufferFactory;
    protected DictBuffer mDictBuffer;

    /* package */ Ver2DictDecoder(final File file, final int factoryFlag) {
    /* package */ Ver2DictDecoder(final File file, final long offset, final long length,
            final int factoryFlag) {
        mDictionaryBinaryFile = file;
        mOffset = offset;
        mLength = length;
        mDictBuffer = null;
        if ((factoryFlag & MASK_DICTBUFFER) == USE_READONLY_BYTEBUFFER) {
            mBufferFactory = new DictionaryBufferFromReadOnlyByteBufferFactory();
@@ -135,8 +140,11 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
        }
    }

    /* package */ Ver2DictDecoder(final File file, final DictionaryBufferFactory factory) {
    /* package */ Ver2DictDecoder(final File file, final long offset, final long length,
            final DictionaryBufferFactory factory) {
        mDictionaryBinaryFile = file;
        mOffset = offset;
        mLength = length;
        mBufferFactory = factory;
    }

@@ -164,9 +172,9 @@ public class Ver2DictDecoder extends AbstractDictDecoder {
    public DictionaryHeader readHeader() throws IOException, UnsupportedFormatException {
        // dictType is not being used in dicttool. Passing an empty string.
        final BinaryDictionary binaryDictionary = new BinaryDictionary(
                mDictionaryBinaryFile.getAbsolutePath(), 0 /* offset */,
                mDictionaryBinaryFile.length() /* length */, true /* useFullEditDistance */,
                null /* locale */, "" /* dictType */, false /* isUpdatable */);
                mDictionaryBinaryFile.getAbsolutePath(), mOffset, mLength,
                true /* useFullEditDistance */, null /* locale */, "" /* dictType */,
                false /* isUpdatable */);
        final DictionaryHeader header = binaryDictionary.getHeader();
        binaryDictionary.close();
        if (header == null) {
Loading