/**
 * Reads a line from file
 *
 * @param line buffer to store the read line
 * @param length buffer size, including '\0' character
 * @param file_handle file handle
 * @return read line
 */
static char *configdb_fgets(char *line, int length, javacall_handle file_handle) {
    long read_length = 0;
    long actual_read = 1;

    while ((read_length < length-1) && (actual_read>0)) {
        actual_read = javacall_file_read(file_handle, (unsigned char *)&line[read_length], 1);
        if (0 < actual_read) {
            if (read_length==0 && ((line[0]=='\n') || (line[0]=='\r'))) {
                continue;
            }

            if (line[read_length] == '\n' || line[read_length] == '\r') {
                break;
            }

            read_length += actual_read;
        }
    }

    line[read_length] = 0;

    if (read_length > 0) {
        return line;
    }

    return NULL;
}
Beispiel #2
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 */