Пример #1
0
void PdfBookReader::processPage(shared_ptr<PdfObject> pageObject, ZLInputStream &stream) {
	pageObject = resolveReference(pageObject, stream);
	if (pageObject.isNull() || pageObject->type() != PdfObject::DICTIONARY) {
		return;
	}
	const PdfDictionaryObject &pageDictionary = (const PdfDictionaryObject&)*pageObject;
	shared_ptr<PdfObject> contents = pageDictionary["Contents"];
	if (contents.isNull()) {
		return;
	}
	switch (contents->type()) {
		default:
			break;
		case PdfObject::REFERENCE:
			processContents(contents, stream);
			break;
		case PdfObject::ARRAY:
		{
			const PdfArrayObject &array = (const PdfArrayObject&)*contents;
			const size_t len = array.size();
			for (size_t i = 0; i < len; ++i) {
				processContents(array[i], stream);
			}
			break;
		}
	}
}
Пример #2
0
bool OdinPatcher::Impl::patchTar()
{
#ifdef __ANDROID__
    static const char *prefix = "/proc/self/fd/";
    fd = -1;
    if (StringUtils::starts_with(info->inputPath(), prefix)) {
        std::string fdStr = info->inputPath().substr(strlen(prefix));
        if (!convertToInt(fdStr.c_str(), &fd)) {
            LOGE("Invalid fd: %s", fdStr.c_str());
            error = ErrorCode::FileOpenError;
            return false;
        }
        LOGD("Input path '%s' is a file descriptor: %d",
             info->inputPath().c_str(), fd);
    }
#endif

    // Get file size for progress information
#ifdef __ANDROID__
    if (fd > 0) {
        off64_t offset = lseek64(fd, 0, SEEK_END);
        if (offset < 0 || lseek64(fd, 0, SEEK_SET) < 0) {
            LOGE("%s: Failed to seek fd: %s", info->inputPath().c_str(),
                 strerror(errno));
            error = ErrorCode::FileSeekError;
            return false;
        }
        maxBytes = offset;
    } else {
#endif

    io::File file;
    if (!file.open(info->inputPath(), io::File::OpenRead)) {
        LOGE("%s: Failed to open: %s", info->inputPath().c_str(),
             file.errorString().c_str());
        error = ErrorCode::FileOpenError;
        return false;
    }
    if (!file.seek(0, io::File::SeekEnd)) {
        LOGE("%s: Failed to seek: %s", info->inputPath().c_str(),
             file.errorString().c_str());
        error = ErrorCode::FileReadError;
        return false;
    }
    if (!file.tell(&maxBytes)) {
        LOGE("%s: Failed to get position: %s", info->inputPath().c_str(),
             file.errorString().c_str());
        error = ErrorCode::FileReadError;
        return false;
    }
    file.close();

#ifdef __ANDROID__
    }
#endif

    updateProgress(bytes, maxBytes);

    if (!openInputArchive()) {
        return false;
    }
    if (!openOutputArchive()) {
        return false;
    }

    if (cancelled) return false;

    if (!processContents()) {
        return false;
    }

    std::vector<CopySpec> toCopy{
        {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/odinupdater",
            "META-INF/com/google/android/update-binary.orig"
        }, {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/odinupdater.sig",
            "META-INF/com/google/android/update-binary.orig.sig"
        }, {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/fuse-sparse",
            "fuse-sparse"
        }, {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/fuse-sparse.sig",
            "fuse-sparse.sig"
        }, {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/mbtool_recovery",
            "META-INF/com/google/android/update-binary"
        }, {
            pc->dataDirectory() + "/binaries/android/"
                    + mb_device_architecture(info->device())
                    + "/mbtool_recovery.sig",
            "META-INF/com/google/android/update-binary.sig"
        }, {
            pc->dataDirectory() + "/scripts/bb-wrapper.sh",
            "multiboot/bb-wrapper.sh"
        }, {
            pc->dataDirectory() + "/scripts/bb-wrapper.sh.sig",
            "multiboot/bb-wrapper.sh.sig"
        }
    };

    zipFile zf = MinizipUtils::ctxGetZipFile(zOutput);

    ErrorCode result;

    for (const CopySpec &spec : toCopy) {
        if (cancelled) return false;

        updateDetails(spec.target);

        result = MinizipUtils::addFile(zf, spec.target, spec.source);
        if (result != ErrorCode::NoError) {
            error = result;
            return false;
        }
    }

    if (cancelled) return false;

    updateDetails("multiboot/info.prop");

    const std::string infoProp =
            MultiBootPatcher::createInfoProp(pc, info->romId());
    result = MinizipUtils::addFile(
            zf, "multiboot/info.prop",
            std::vector<unsigned char>(infoProp.begin(), infoProp.end()));
    if (result != ErrorCode::NoError) {
        error = result;
        return false;
    }

    if (cancelled) return false;

    updateDetails("block_devs.prop");

    std::string blockDevsProp;
    blockDevsProp += "system=";
    blockDevsProp += mb_device_system_block_devs(info->device())[0];
    blockDevsProp += "\n";
    blockDevsProp += "boot=";
    blockDevsProp += mb_device_boot_block_devs(info->device())[0];
    blockDevsProp += "\n";
    result = MinizipUtils::addFile(
            zf, "block_devs.prop", std::vector<unsigned char>(
                    blockDevsProp.begin(), blockDevsProp.end()));
    if (result != ErrorCode::NoError) {
        error = result;
        return false;
    }

    if (cancelled) return false;

    updateDetails("multiboot/device.json");

    char *json = mb_device_to_json(info->device());
    if (!json) {
        error = ErrorCode::MemoryAllocationError;
        return false;
    }

    result = MinizipUtils::addFile(
            zf, "multiboot/device.json",
            std::vector<unsigned char>(json, json + strlen(json)));
    free(json);

    if (result != ErrorCode::NoError) {
        error = result;
        return false;
    }

    if (cancelled) return false;

    return true;
}
Пример #3
0
JNIEXPORT jint JNICALL
Java_csh_cryptonite_Cryptonite_jniDecrypt(JNIEnv* env, jobject thiz, jstring encodedname,
                                          jstring destdir, jboolean force_readable)
{
    int res = checkGRoot();

    if (res != EXIT_SUCCESS) {
        std::ostringstream err;
        err << "EncFS root hasn't been initialized yet";
        LOGE(err.str().c_str());
        return res;
    }

    jniStringManager mencodedname(env, encodedname);

    jniStringManager mdestdir(env, destdir);
    // if the dir doesn't exist, then create it (with user permission)
    if(!checkDir(mdestdir.str()) && !userAllowMkdir(mdestdir.c_str(), 0700)) {
        std::ostringstream err;
        err << "Destination directory " << mdestdir.str() << " isn't valid";
        LOGE(err.str().c_str());
        
	return EXIT_FAILURE;
    }
    std::string plainPath = gRootInfo->root->plainPath(mencodedname.c_str());
    std::string destname = mdestdir.str() + plainPath;

    /* std::ostringstream out;
    out << "Decoding " << mencodedname.str() << " to " << plainPath << " in " << destname;
    LOGI(out.str().c_str()); */

    shared_ptr<FileNode> node = 
	gRootInfo->root->lookupNode( plainPath.c_str(), "encfsctl");

    if(!node) {
        std::ostringstream err;
        err << "unable to open " << plainPath;
        LOGE(err.str().c_str());
        return EXIT_FAILURE;
    } else {
        /*std::ostringstream err;
        err << "found node " << plainPath;
        LOGE(err.str().c_str()); */

        struct stat st;
        
        if(node->getAttr(&st) != 0) {
            std::ostringstream out;
            out << "Not creating " << destname << ", "
                << "couldn't read node attributes: "
                << strerror(errno);
            LOGE(out.str().c_str());
            return EXIT_FAILURE;
        }

        mode_t srcmode = st.st_mode;
        if (force_readable)
            srcmode = S_IRWXU;
        
        int outfd = creat(destname.c_str(), srcmode);

        if (outfd == -1) {
            std::ostringstream out;
            out << "Not creating " << destname << ": "
                << strerror(errno);
            LOGE(out.str().c_str());
            return EXIT_FAILURE;
        }
        WriteOutput output(outfd);
        /* std::ostringstream out;
        out << "Writing to " << destname;
        LOGE(out.str().c_str()); */
        processContents( gRootInfo, plainPath.c_str(), output );
        
        close(outfd);
    }
    return EXIT_SUCCESS;
}
Пример #4
0
JNIEXPORT jbyteArray JNICALL
Java_csh_cryptonite_Cryptonite_jniDecryptToBuffer(JNIEnv* env, jobject thiz,
                                                  jstring encodedname)
{
    int res = checkGRoot();

    if (res != EXIT_SUCCESS) {
        std::ostringstream err;
        err << "EncFS root hasn't been initialized yet";
        LOGE(err.str().c_str());
        return 0;
    }

    jniStringManager mencodedname(env, encodedname);
    std::string plainPath = gRootInfo->root->plainPath(mencodedname.c_str());
    mencodedname.release();

    shared_ptr<FileNode> node = 
	gRootInfo->root->lookupNode( plainPath.c_str(), "encfsctl");

    if(!node) {
        std::ostringstream err;
        err << "unable to open " << plainPath;
        LOGE(err.str().c_str());
        return 0;
    }

    std::vector<unsigned char> buf;

    BufferOutput output(&buf);
    processContents( gRootInfo, plainPath.c_str(), output );
    
    jbyteArray jb = env->NewByteArray(buf.size());

    jclass oomCls;
    oomCls = env->FindClass("java/lang/OutOfMemoryError");

    jthrowable exc = env->ExceptionOccurred();
    if (exc) {
        env->ExceptionDescribe();
        env->ExceptionClear();
        env->ThrowNew(oomCls, "out of memory in jniDecryptToBuffer");

        return 0;
    }
    if (jb == NULL) {
        buf.clear();
        std::ostringstream err;
        err << "out of memory";
        LOGE(err.str().c_str());

        env->ThrowNew(oomCls, "out of memory in jniDecryptToBuffer");
        
        return 0;
    }
    
    env->SetByteArrayRegion(jb, 0, buf.size(), (jbyte *)&buf[0]);
    buf.clear();

    exc = env->ExceptionOccurred();
    if (exc) {
        env->ExceptionDescribe();
        env->ExceptionClear();
        env->ThrowNew(oomCls, "out of memory in jniDecryptToBuffer");

        return 0;
    }

    return jb;
}
Пример #5
0
static int copyContents(const shared_ptr<EncFS_Root> &rootInfo, 
                        const char* encfsName, const char* targetName,
                        bool fake)
{
    shared_ptr<FileNode> node = 
	rootInfo->root->lookupNode( encfsName, "encfsctl" );

    if(!node)
    {
        std::ostringstream err;
        err << "unable to open " << encfsName;
        LOGE(err.str().c_str());
        return EXIT_FAILURE;
    } else
    {
        struct stat st;

        if(node->getAttr(&st) != 0)
            return EXIT_FAILURE;

        if((st.st_mode & S_IFLNK) == S_IFLNK)
        {
            std::string d = rootInfo->root->cipherPath(encfsName);
            char linkContents[PATH_MAX+2];

            if(readlink (d.c_str(), linkContents, PATH_MAX + 1) <= 0)
            {
                std::ostringstream err;
                err << "unable to read link " << encfsName;
                LOGE(err.str().c_str());
                return EXIT_FAILURE;
            }
            symlink(rootInfo->root->plainPath(linkContents).c_str(), 
		    targetName);
        } else
        {
            int outfd = creat(targetName, st.st_mode);

            if (outfd == -1) {
                if (errno == EACCES /* sic! */ || errno == EROFS || errno == ENOSPC) {
                    std::ostringstream out;
                    out << "Not creating " << targetName << ": "
                        << strerror(errno);
                    LOGE(out.str().c_str());
                    return EXIT_FAILURE;
                }
            }

            if (!fake) {
                WriteOutput output(outfd);
                /*std::ostringstream out;
                out << "Writing to " << encfsName;
                LOGE(out.str().c_str());*/
                processContents( rootInfo, encfsName, output );
            }

            close(outfd);
        }
    }
    return EXIT_SUCCESS;
}