Search in sources :

Example 1 with InvalidProtocolBufferNanoException

use of com.google.protobuf.nano.InvalidProtocolBufferNanoException in project grpc-java by grpc.

the class NanoUtilsTest method parseInvalid.

@Test
public void parseInvalid() throws Exception {
    InputStream is = new ByteArrayInputStream(new byte[] { -127 });
    try {
        marshaller.parse(is);
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertTrue(ex.getCause() instanceof InvalidProtocolBufferNanoException);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException) Test(org.junit.Test)

Example 2 with InvalidProtocolBufferNanoException

use of com.google.protobuf.nano.InvalidProtocolBufferNanoException in project Launcher3 by chislon.

the class LauncherBackupHelper method readCheckedBytes.

/**
 * Unwrap a proto message from a CheckedMessage, verifying the checksum.
 */
private byte[] readCheckedBytes(byte[] buffer, int offset, int dataSize) throws InvalidProtocolBufferNanoException {
    CheckedMessage wrapper = new CheckedMessage();
    MessageNano.mergeFrom(wrapper, buffer, offset, dataSize);
    CRC32 checksum = new CRC32();
    checksum.update(wrapper.payload);
    if (wrapper.checksum != checksum.getValue()) {
        throw new InvalidProtocolBufferNanoException("checksum does not match");
    }
    return wrapper.payload;
}
Also used : CRC32(java.util.zip.CRC32) CheckedMessage(com.android.launcher3.backup.BackupProtos.CheckedMessage) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException)

Example 3 with InvalidProtocolBufferNanoException

use of com.google.protobuf.nano.InvalidProtocolBufferNanoException in project Launcher3 by chislon.

the class LauncherBackupHelper method readJournal.

/**
 * Read the old journal from the input file.
 *
 * In the event of any error, just pretend we didn't have a journal,
 * in that case, do a full backup.
 *
 * @param oldState the read-0only file descriptor pointing to the old journal
 * @return a Journal protocol bugffer
 */
private Journal readJournal(ParcelFileDescriptor oldState) {
    Journal journal = new Journal();
    if (oldState == null) {
        return journal;
    }
    FileInputStream inStream = new FileInputStream(oldState.getFileDescriptor());
    try {
        int remaining = inStream.available();
        if (DEBUG)
            Log.d(TAG, "available " + remaining);
        if (remaining < MAX_JOURNAL_SIZE) {
            byte[] buffer = new byte[remaining];
            int bytesRead = 0;
            while (remaining > 0) {
                try {
                    int result = inStream.read(buffer, bytesRead, remaining);
                    if (result > 0) {
                        if (DEBUG)
                            Log.d(TAG, "read some bytes: " + result);
                        remaining -= result;
                        bytesRead += result;
                    } else {
                        // stop reading ands see what there is to parse
                        Log.w(TAG, "read error: " + result);
                        remaining = 0;
                    }
                } catch (IOException e) {
                    Log.w(TAG, "failed to read the journal", e);
                    buffer = null;
                    remaining = 0;
                }
            }
            if (DEBUG)
                Log.d(TAG, "journal bytes read: " + bytesRead);
            if (buffer != null) {
                try {
                    MessageNano.mergeFrom(journal, readCheckedBytes(buffer, 0, bytesRead));
                } catch (InvalidProtocolBufferNanoException e) {
                    Log.d(TAG, "failed to read the journal", e);
                    journal.clear();
                }
            }
        }
    } catch (IOException e) {
        Log.d(TAG, "failed to close the journal", e);
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            Log.d(TAG, "failed to close the journal", e);
        }
    }
    return journal;
}
Also used : Journal(com.android.launcher3.backup.BackupProtos.Journal) IOException(java.io.IOException) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException) FileInputStream(java.io.FileInputStream)

Example 4 with InvalidProtocolBufferNanoException

use of com.google.protobuf.nano.InvalidProtocolBufferNanoException in project Launcher3 by chislon.

the class LauncherBackupHelper method restoreIcon.

/**
 * Read an icon from the stream.
 *
 * <P>Keys arrive in any order, so shortcuts that use this icon may already exist.
 *
 * @param key identifier for the row
 * @param buffer the serialized proto from the stream, may be larger than dataSize
 * @param dataSize the size of the proto from the stream
 * @param keys keys to mark as clean in the notes for next backup
 */
private void restoreIcon(Key key, byte[] buffer, int dataSize, ArrayList<Key> keys) {
    Log.v(TAG, "unpacking icon " + key.id);
    if (DEBUG)
        Log.d(TAG, "read (" + buffer.length + "): " + Base64.encodeToString(buffer, 0, dataSize, Base64.NO_WRAP));
    try {
        Resource res = unpackIcon(buffer, 0, dataSize);
        if (DEBUG)
            Log.d(TAG, "unpacked " + res.dpi);
        if (DEBUG)
            Log.d(TAG, "read " + Base64.encodeToString(res.data, 0, res.data.length, Base64.NO_WRAP));
        Bitmap icon = BitmapFactory.decodeByteArray(res.data, 0, res.data.length);
        if (icon == null) {
            Log.w(TAG, "failed to unpack icon for " + key.name);
        }
    } catch (InvalidProtocolBufferNanoException e) {
        Log.w(TAG, "failed to decode proto", e);
    }
}
Also used : Bitmap(android.graphics.Bitmap) Resource(com.android.launcher3.backup.BackupProtos.Resource) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException)

Example 5 with InvalidProtocolBufferNanoException

use of com.google.protobuf.nano.InvalidProtocolBufferNanoException in project Launcher3 by chislon.

the class DecoderRing method main.

public static void main(String[] args) throws Exception {
    File source = null;
    Class type = Key.class;
    int skip = 0;
    for (int i = 0; i < args.length; i++) {
        if ("-k".equals(args[i])) {
            type = Key.class;
        } else if ("-f".equals(args[i])) {
            type = Favorite.class;
        } else if ("-j".equals(args[i])) {
            type = Journal.class;
        } else if ("-i".equals(args[i])) {
            type = Resource.class;
        } else if ("-s".equals(args[i])) {
            type = Screen.class;
        } else if ("-w".equals(args[i])) {
            type = Widget.class;
        } else if ("-S".equals(args[i])) {
            if ((i + 1) < args.length) {
                skip = Integer.valueOf(args[++i]);
            } else {
                usage(args);
            }
        } else if (args[i] != null && !args[i].startsWith("-")) {
            source = new File(args[i]);
        } else {
            System.err.println("Unsupported flag: " + args[i]);
            usage(args);
        }
    }
    // read in the bytes
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    BufferedInputStream input = null;
    if (source == null) {
        input = new BufferedInputStream(System.in);
    } else {
        try {
            input = new BufferedInputStream(new FileInputStream(source));
        } catch (FileNotFoundException e) {
            System.err.println("failed to open file: " + source + ", " + e);
            System.exit(1);
        }
    }
    byte[] buffer = new byte[1024];
    try {
        while (input.available() > 0) {
            int n = input.read(buffer);
            int offset = 0;
            if (skip > 0) {
                offset = Math.min(skip, n);
                n -= offset;
                skip -= offset;
            }
            if (n > 0) {
                byteStream.write(buffer, offset, n);
            }
        }
    } catch (IOException e) {
        System.err.println("failed to read input: " + e);
        System.exit(1);
    }
    System.err.println("read this many bytes: " + byteStream.size());
    MessageNano proto = null;
    if (type == Key.class) {
        Key key = new Key();
        try {
            key = Key.parseFrom(byteStream.toByteArray());
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse proto: " + e);
            System.exit(1);
        }
        // keys are self-checked
        if (key.checksum != checkKey(key)) {
            System.err.println("key ckecksum failed");
            System.exit(1);
        }
        proto = key;
    } else {
        // other types are wrapped in a checksum message
        CheckedMessage wrapper = new CheckedMessage();
        try {
            MessageNano.mergeFrom(wrapper, byteStream.toByteArray());
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse wrapper: " + e);
            System.exit(1);
        }
        CRC32 checksum = new CRC32();
        checksum.update(wrapper.payload);
        if (wrapper.checksum != checksum.getValue()) {
            System.err.println("wrapper ckecksum failed");
            System.exit(1);
        }
        // decode the actual message
        proto = (MessageNano) type.newInstance();
        try {
            MessageNano.mergeFrom(proto, wrapper.payload);
        } catch (InvalidProtocolBufferNanoException e) {
            System.err.println("failed to parse proto: " + e);
            System.exit(1);
        }
    }
    // Generic string output
    System.out.println(proto.toString());
    // save off the icon bits in a file for inspection
    if (proto instanceof Resource) {
        Resource icon = (Resource) proto;
        final String path = "icon.webp";
        FileOutputStream iconFile = new FileOutputStream(path);
        iconFile.write(icon.data);
        iconFile.close();
        System.err.println("wrote " + path);
    }
    // save off the widget icon and preview bits in files for inspection
    if (proto instanceof Widget) {
        Widget widget = (Widget) proto;
        if (widget.icon != null) {
            final String path = "widget_icon.webp";
            FileOutputStream iconFile = new FileOutputStream(path);
            iconFile.write(widget.icon.data);
            iconFile.close();
            System.err.println("wrote " + path);
        }
        if (widget.preview != null) {
            final String path = "widget_preview.webp";
            FileOutputStream iconFile = new FileOutputStream(path);
            iconFile.write(widget.preview.data);
            iconFile.close();
            System.err.println("wrote " + path);
        }
    }
    // success
    System.exit(0);
}
Also used : Favorite(com.android.launcher3.backup.BackupProtos.Favorite) CRC32(java.util.zip.CRC32) MessageNano(com.google.protobuf.nano.MessageNano) Resource(com.android.launcher3.backup.BackupProtos.Resource) Widget(com.android.launcher3.backup.BackupProtos.Widget) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InvalidProtocolBufferNanoException(com.google.protobuf.nano.InvalidProtocolBufferNanoException) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) CheckedMessage(com.android.launcher3.backup.BackupProtos.CheckedMessage) File(java.io.File) Key(com.android.launcher3.backup.BackupProtos.Key)

Aggregations

InvalidProtocolBufferNanoException (com.google.protobuf.nano.InvalidProtocolBufferNanoException)5 CheckedMessage (com.android.launcher3.backup.BackupProtos.CheckedMessage)2 Resource (com.android.launcher3.backup.BackupProtos.Resource)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 CRC32 (java.util.zip.CRC32)2 Bitmap (android.graphics.Bitmap)1 Favorite (com.android.launcher3.backup.BackupProtos.Favorite)1 Journal (com.android.launcher3.backup.BackupProtos.Journal)1 Key (com.android.launcher3.backup.BackupProtos.Key)1 Widget (com.android.launcher3.backup.BackupProtos.Widget)1 MessageNano (com.google.protobuf.nano.MessageNano)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1