/**
 * Loads properties from file stored on the file system
 *
 * @param unicodeFileName file name
 * @param fileNameLen length of the file name
 * @return handle to the properties storage
 */
javacall_handle configdb_load_from_fs(javacall_utf16* unicodeFileName, int fileNameLen) {
    string_db*   d;
    char    line[MAX_LINE_LENGTH+1];
    char    sec[MAX_STR_LENGTH+1];
    char    key[MAX_STR_LENGTH+1];
    char    val[MAX_STR_LENGTH+1];
    char*   where;
    int lineno;
    char sep = '=';
    javacall_handle file_handle;
    javacall_result res;


    res = javacall_file_open(unicodeFileName,
                             fileNameLen,
                             JAVACALL_FILE_O_RDWR,
                             &file_handle);
    if (res != JAVACALL_OK) {
        javacall_print("Error: Unable to open the dynamic properties file.  "
                        "Check that jwc_properties.ini exists in your "
                        "application directory.\n");
        return NULL;
    }

    sec[0] = 0;

    /*
     * Initialize a new string_db entry
     */
    d = javacall_string_db_new(0);
    if (NULL == d) {
        return NULL;
    }
    lineno = 0;
    while (configdb_fgets(line, MAX_LINE_LENGTH, file_handle) != NULL) {
        lineno++;
        where = javautil_string_skip_leading_blanks(line); /* Skip leading spaces */
        if (*where==';' || *where=='#' || *where == 0){
            continue; /* Comment lines */
        }
        else {
            if (JAVACALL_OK == read_section_name(where, sec) == 1) {
                /* Valid section name */
                configdb_add_entry(d, sec, NULL, NULL);
            }
            else if (JAVACALL_OK == parse_line(where, key, val, sep)) {
                javautil_string_strip(key);
                javautil_string_strip(val);
                remove_escape_characters(val);
                configdb_add_entry(d, sec, key, val);
            }
        }
    }
    javacall_file_close(file_handle);
    return(javacall_handle)d ;
}
/**
 * Dumps the content of the parameter database to an open file pointer
 * The output format is as a standard INI file
 * 
 * @param config_handle    database object created by calling javacall_configdb_load
 * @param unicodeFileName  output file name
 * @param fileNameLen      file name length
 */
void javacall_configdb_dump_ini(javacall_handle config_handle,
                                javacall_utf16* unicodeFileName,
                                int fileNameLen) {
    int     i, j;
    char    keym[MAX_STR_LENGTH+1];
    int     nsec;
    char*   secname;
    int     seclen;
    string_db* d = (string_db *)config_handle;
    javacall_handle file_handle;
    char    l[MAX_STR_LENGTH];
    javacall_result res;

    if (d == NULL || unicodeFileName == NULL || fileNameLen <= 0) {
        return;
    }


    res = javacall_file_open(unicodeFileName,
                             fileNameLen,
                             JAVACALL_FILE_O_RDWR | JAVACALL_FILE_O_CREAT,
                             &file_handle);
    if (res != JAVACALL_OK) {
        javacall_print("javacall_configdb_dump_ini(): ERROR - Can't open the dump file!\n");
        return;
    }

    nsec = javacall_configdb_get_num_of_sections(d);
    if (nsec < 1) {
        /* No section in file: dump all keys as they are */
        for (i = 0; i < d->size; i++) {
            if (d->key[i] == NULL) {
                continue;
            }

            if (d->val[i] != NULL) {
                javautil_sprintf(l, "[%s]=[%s]\n", d->key[i], d->val[i]);
            } else {
                javautil_sprintf(l, "[%s]=UNDEF\n", d->key[i]);
            }

            javacall_file_write(file_handle, (unsigned char*)l, strlen(l));
        }
        javacall_file_close(file_handle);
        return ;
    }
    for (i = 0; i < nsec; i++) {
        secname = javacall_configdb_get_section_name(d, i) ;
        seclen  = (int)strlen(secname);
        javautil_sprintf(l, "\n[%s]\n", secname);
        javacall_file_write(file_handle, (unsigned char*)l, strlen(l));
        javautil_sprintf(keym, "%s:", secname);
        for (j = 0; j < d->size; j++) {
            if (d->key[j] == NULL) {
                continue;
            }
            if (!strncmp(d->key[j], keym, seclen+1)) {
                javautil_sprintf(l,
                        "%-30s = %s\n",
                        d->key[j]+seclen+1,
                        d->val[j] ? d->val[j] : "");
                javacall_file_write(file_handle, (unsigned char*)l, strlen(l));
            }
        }
    }
    javautil_sprintf(l, "\n");
    javacall_file_write(file_handle, (unsigned char*)l, strlen(l));

    javacall_file_close(file_handle);
}
Example #3
0
javacall_result javacall_security_keystore_start(javacall_handle* jc_h) {

    javacall_utf16 rootPath[JAVACALL_MAX_FILE_NAME_LENGTH];

    unsigned short unicode_main_ks[] =
    {'\\','_','m','a','i','n','.','k','s'};

    int unicode_main_ks_name_length = sizeof(unicode_main_ks)/sizeof(unsigned short);

    javacall_result    result;
    javacall_handle    handle;

    int    rootPathLen = JAVACALL_MAX_FILE_NAME_LENGTH;
    int    res;
    int    i;


    memset(rootPath, 0, JAVACALL_MAX_FILE_NAME_LENGTH);

    result = javacall_dir_get_root_path(rootPath, &rootPathLen);
    if(result == JAVACALL_FAIL) {
        return JAVACALL_FAIL;
    }
    if(rootPathLen < (JAVACALL_MAX_FILE_NAME_LENGTH - unicode_main_ks_name_length)) {
        for(i = 0; i < unicode_main_ks_name_length; i++) {
            rootPath[rootPathLen+i] = unicode_main_ks[i];
        }
        rootPathLen+=i;
    } else {
        printf("javacall_security.c : File name %d, is to long.\n",
               unicode_to_char(rootPath));
        return JAVACALL_FAIL;
    }

    javacall_set_local_variables();

    currentPosition = NULL;

    printf("Opening %s.\n", unicode_to_char(rootPath));
    result = javacall_file_open(rootPath, rootPathLen,
                                JAVACALL_FILE_O_RDONLY,
                                &handle);
    if(result == JAVACALL_FAIL) {
        printf("Can't open %s.\n", unicode_to_char(rootPath));
        return JAVACALL_FAIL;
    }

    main_ks_size = (int)javacall_file_sizeofopenfile(handle);
    if(-1 == main_ks_size) {
        javacall_file_close(handle);
        printf("Can't get javacall_file_sizeofopenfile() %s\n",
               unicode_to_char(rootPath));
        return JAVACALL_FAIL;
    }

    _main_ks_content = (unsigned char *) malloc(main_ks_size);
    if(_main_ks_content == NULL) {
        javacall_file_close(handle);
        return JAVACALL_FAIL;
    }

    res = javacall_file_read(handle, _main_ks_content, main_ks_size);
    if(res <= 0 ) {
        printf("Can't read %s\n", unicode_to_char(rootPath));
        free(_main_ks_content);
        javacall_file_close(handle);
        return JAVACALL_FAIL;
    }

    javacall_file_close(handle);
    currentPosition  = _main_ks_content;

    if(*currentPosition !=  CURRENT_VERSION) {
        printf("Can't read this key storage. Current version isn't correct\n");
        free(_main_ks_content);
        currentPosition = NULL;
        return JAVACALL_FAIL;
    }

    currentPosition++;

    *jc_h = (javacall_handle)(&currentPosition);

    return JAVACALL_OK;

} /* end of javacall_security_keystore_start */