RegistryQuery.java 4.86 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.pdfg.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RegistryQuery {
    public static final String APPDATA = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\AppData";
    private static Logger logger = LoggerFactory.getLogger(RegistryQuery.class);
    private static final String SZ = "REG_SZ";
    private static final String XSZ = "REG_EXPAND_SZ";
    private static final String MSZ = "REG_MULTI_SZ";
    private static final String BINARY = "REG_BINARY";
    private static final String DWORD = "REG_DWORD";
    private static final String QWORD = "REG_QWORD";

    private static void SeparateArguments(String key, StringBuilder FolderPath, StringBuilder ArgumentValue) {
        int divider = key.lastIndexOf("\\");
        FolderPath.append("\"");
        FolderPath.append(key.substring(0, divider));
        FolderPath.append("\"");
        ArgumentValue.append(key.substring(divider + 1));
    }

    public static String getRegistryValue(String key) {
        String value = "";
        try {
            StringBuilder FolderPath = new StringBuilder();
            StringBuilder ArgumentValue = new StringBuilder();
            RegistryQuery.SeparateArguments(key, FolderPath, ArgumentValue);
            String fp = FolderPath.toString();
            String keyVal = ArgumentValue.toString();
            if (fp == null || "".equals(fp) || keyVal == null || "".equals(keyVal)) {
                logger.warn("The Folder path or the Argument value is null, please check the the key passed \n");
                return value;
            }
            String[] find_system_value_command = new String[]{"REG", "QUERY", fp, "/v", keyVal};
            Process prc = Runtime.getRuntime().exec(find_system_value_command);
            InputStream is = prc.getInputStream();
            BufferedReader bfr = new BufferedReader(new InputStreamReader(is, "utf-8"));
            String line = "";
            while ((line = bfr.readLine()) != null && !line.contains(keyVal)) {
            }
            if (line == null) {
                logger.warn("There is no value in registry which satisfies the key value passed \n");
                return value;
            }
            int indexOfValue = (line = line.trim()).indexOf("REG_SZ");
            if (indexOfValue != -1) {
                indexOfValue += "REG_SZ".length();
            } else {
                indexOfValue = line.indexOf("REG_EXPAND_SZ");
                if (indexOfValue != -1) {
                    indexOfValue += "REG_EXPAND_SZ".length();
                } else {
                    indexOfValue = line.indexOf("REG_BINARY");
                    if (indexOfValue != -1) {
                        indexOfValue += "REG_BINARY".length();
                    } else {
                        indexOfValue = line.indexOf("REG_DWORD");
                        if (indexOfValue != -1) {
                            indexOfValue += "REG_DWORD".length();
                        } else {
                            indexOfValue = line.indexOf("REG_QWORD");
                            if (indexOfValue != -1) {
                                indexOfValue += "REG_QWORD".length();
                            } else {
                                indexOfValue = line.indexOf("REG_MULTI_SZ");
                                if (indexOfValue != -1) {
                                    indexOfValue += "REG_MULTI_SZ".length();
                                } else {
                                    logger.warn("Invalid Registry Entry: Please check the registry value you are looking for \n");
                                    return value;
                                }
                            }
                        }
                    }
                }
            }
            if (indexOfValue >= 0) {
                if ((line = line.substring(indexOfValue).trim()).startsWith("%")) {
                    int second_percentage_sign = line.indexOf(37, 1);
                    if (second_percentage_sign != -1) {
                        String env = line.substring(1, second_percentage_sign);
                        value = System.getenv(env) + line.substring(second_percentage_sign + 1);
                    }
                } else {
                    value = line;
                }
            } else {
                logger.warn("Error while retrieving system environment variable: " + key + " NOT FOUND...............");
            }
            prc.waitFor();
        }
        catch (Exception e) {
            logger.warn("Error while retrieving system environment variable: " + key, (Throwable)e);
        }
        return value;
    }
}