summaryrefslogtreecommitdiffstats
path: root/target/linux/s3c24xx/patches-2.6.30/056-pcf50633.patch
Commit message (Expand)AuthorAgeFilesLines
* Implement voltage listing for pc50633.Lars-Peter Clausen2009-07-161-0/+106
* update to 2.6.30-rc7Imre Kaloz2009-05-251-49/+31
* bump to 2.6.30-rc6Lars-Peter Clausen2009-05-181-0/+375
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
/*
 * Copyright (C) 2015 Dominik Schürmann <dominik@dominikschuermann.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.operations;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;

import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.codec.DecodeMonitor;
import org.apache.james.mime4j.dom.field.ContentDispositionField;
import org.apache.james.mime4j.field.DefaultFieldParser;
import org.apache.james.mime4j.parser.AbstractContentHandler;
import org.apache.james.mime4j.parser.MimeStreamParser;
import org.apache.james.mime4j.stream.BodyDescriptor;
import org.apache.james.mime4j.stream.Field;
import org.apache.james.mime4j.stream.MimeConfig;
import org.openintents.openpgp.OpenPgpMetadata;
import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult;
import org.sufficientlysecure.keychain.operations.results.InputDataResult;
import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType;
import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyInputParcel;
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyOperation;
import org.sufficientlysecure.keychain.pgp.Progressable;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.provider.TemporaryStorageProvider;
import org.sufficientlysecure.keychain.service.InputDataParcel;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;


/** This operation deals with input data, trying to determine its type as it goes. */
public class InputDataOperation extends BaseOperation<InputDataParcel> {

    final private byte[] buf = new byte[256];

    public InputDataOperation(Context context, ProviderHelper providerHelper, Progressable progressable) {
        super(context, providerHelper, progressable);
    }

    @NonNull
    @Override
    public InputDataResult execute(InputDataParcel input,
                                     CryptoInputParcel cryptoInput) {

        final OperationLog log = new OperationLog();

        log.add(LogType.MSG_DATA, 0);

        Uri currentUri;

        DecryptVerifyResult decryptResult = null;

        PgpDecryptVerifyInputParcel decryptInput = input.getDecryptInput();
        if (decryptInput != null) {

            log.add(LogType.MSG_DATA_DECRYPT, 1);

            PgpDecryptVerifyOperation op =
                    new PgpDecryptVerifyOperation(mContext, mProviderHelper, mProgressable);

            decryptInput.setInputUri(input.getInputUri());

            currentUri = TemporaryStorageProvider.createFile(mContext);
            decryptInput.setOutputUri(currentUri);

            decryptResult = op.execute(decryptInput, cryptoInput);
            if (decryptResult.isPending()) {
                return new InputDataResult(log, decryptResult);
            }
            log.addByMerge(decryptResult, 2);

        } else {
            currentUri = input.getInputUri();
        }

        // If we aren't supposed to attempt mime decode, we are done here
        if (!input.getMimeDecode()) {

            if (decryptInput == null) {
                throw new AssertionError("no decryption or mime decoding, this is probably a bug");
            }

            log.add(LogType.MSG_DATA_SKIP_MIME, 1);

            ArrayList<Uri> uris = new ArrayList<>();
            uris.add(currentUri);
            ArrayList<OpenPgpMetadata> metadatas = new ArrayList<>();
            metadatas.add(decryptResult.getDecryptionMetadata());

            log.add(LogType.MSG_DATA_OK, 1);
            return new InputDataResult(InputDataResult.RESULT_OK, log, decryptResult, uris, metadatas);

        }

        log.add(LogType.MSG_DATA_MIME, 1);

        InputStream in;
        try {
            in = mContext.getContentResolver().openInputStream(currentUri);
        } catch (FileNotFoundException e) {
            log.add(LogType.MSG_DATA_ERROR_IO, 2);
            return new InputDataResult(InputDataResult.RESULT_ERROR, log);
        }
        MimeStreamParser parser = new MimeStreamParser((MimeConfig) null);

        final ArrayList<Uri> outputUris = new ArrayList<>();
        final ArrayList<OpenPgpMetadata> metadatas = new ArrayList<>();

        parser.setContentDecoding(true);
        parser.setRecurse();
        parser.setContentHandler(new AbstractContentHandler() {
            String mFilename;

            @Override
            public void startHeader() throws MimeException {
                mFilename = null;
            }

            @Override
            public void field(Field field) throws MimeException {
                field = DefaultFieldParser.getParser().parse(field, DecodeMonitor.SILENT);
                if (field instanceof ContentDispositionField) {
                    mFilename = ((ContentDispositionField) field).getFilename();
                }
            }

            @Override
            public void body(BodyDescriptor bd, InputStream is) throws MimeException, IOException {

                log.add(LogType.MSG_DATA_MIME_PART, 2);

                log.add(LogType.MSG_DATA_MIME_TYPE, 3, bd.getMimeType());
                if (mFilename != null) {
                    log.add(LogType.MSG_DATA_MIME_FILENAME, 3, mFilename);
                }
                log.add(LogType.MSG_DATA_MIME_LENGTH, 3, bd.getContentLength());

                Uri uri = TemporaryStorageProvider.createFile(mContext, mFilename, bd.getMimeType());
                OutputStream out = mContext.getContentResolver().openOutputStream(uri, "w");

                if (out == null) {
                    throw new IOException("Error getting file for writing!");
                }

                int len;
                while ((len = is.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                OpenPgpMetadata metadata = new OpenPgpMetadata(mFilename, bd.getMimeType(), 0L, bd.getContentLength());

                out.close();
                outputUris.add(uri);
                metadatas.add(metadata);

            }
        });

        try {

            parser.parse(in);
            log.add(LogType.MSG_DATA_MIME_OK, 2);

            log.add(LogType.MSG_DATA_OK, 1);
            return new InputDataResult(InputDataResult.RESULT_OK, log, decryptResult, outputUris, metadatas);

        } catch (IOException e) {
            e.printStackTrace();
            log.add(LogType.MSG_DATA_MIME_ERROR, 2);
            return new InputDataResult(InputDataResult.RESULT_ERROR, log);
        } catch (MimeException e) {
            e.printStackTrace();
            log.add(LogType.MSG_DATA_MIME_ERROR, 2);
            return new InputDataResult(InputDataResult.RESULT_ERROR, log);
        }

    }

}