Example #1
0
/**
 * Searches fields in records.
 *
 * @param fld_num internal ID of field to be found
 * @return number of bytes read
 */
static int position_field(int fld_num) {
  int record_size;
  int field_cnt;
  field_type* cur_type;

  if (fld_num < 0 || fld_num >= JSR211_CHR_COUNT) {
    return -1;
  }

  /* skip header */
  if (storageRead(&io_error_message, table_file, (char *)&record_size, 
                                                        sizeof(int)) == -1) {
    return 0;
  }
  for (field_cnt = 0, cur_type = record_struct; field_cnt < fld_num; 
                                                    field_cnt++, cur_type++) {
    int field_size;

    if (*cur_type != field_int) {
      storageRead(&io_error_message, table_file, 
                                            (char*)&field_size, sizeof(int));
    }
    else {
      field_size = sizeof(int);
    }
    storageRelativePosition(&io_error_message, table_file, field_size);
  }
  return record_size;
}
Example #2
0
/**
 * Loads current handler's main fields (ID, suite_ID, class_name, flag).
 * @param handler loaded structure pointer.
 * @return operation result
 */
static void load_current_handler(JSR211_CH* ch) {
    long pos = storageRelativePosition(&io_error_message, table_file, 
                                       sizeof(int)) - sizeof(int);
    read_string(&(ch->id));
    storageRead(&io_error_message, table_file, (char*)&(ch->flag), sizeof(int));
    storageRead(&io_error_message, table_file, (char*)&(ch->suite_id), sizeof(int));
    read_string(&(ch->class_name));
    storagePosition(&io_error_message, table_file, pos);
}
/**
 * Reads named secure resource of the suite with specified suiteId
 * from secure persistent storage.
 *
 * Note that when porting memory for the in/out parameter
 * returnValue MUST be allocated using pcsl_mem_malloc().
 * The caller is responsible for freeing it.
 *
 * @param suiteId           The suite id used to identify the MIDlet suite
 * @param resourceName      The name of secure resource to read from storage
 * @param returnValue       The in/out parameter that will return the
 *                          value part of the requested secure resource
 *                          (NULL is a valid return value)
 * @param valueSize         The length of the secure resource value
 *
 * @return one of the error codes:
 * <pre>
 *     ALL_OK, OUT_OF_MEMORY, NOT_FOUND,
 *     SUITE_CORRUPTED_ERROR, BAD_PARAMS
 * </pre>
 */
MIDPError
midp_suite_read_secure_resource(SuiteIdType suiteId,
                                const pcsl_string* resourceName,
                                jbyte **returnValue,
                                jint *valueSize) {
    pcsl_string filename = PCSL_STRING_NULL;
    char *pszError = NULL;
    MIDPError errorCode;
    int bytesRead;
    int handle;

    *returnValue = NULL;
    *valueSize = 0;

    errorCode = get_secure_resource_file(suiteId, resourceName, &filename);
    if (errorCode != ALL_OK) {
        pcsl_string_free(&filename);
        return errorCode;
    }

    handle = storage_open(&pszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (pszError != NULL) {
        storageFreeError(pszError);
        return SUITE_CORRUPTED_ERROR;
    }

    do {
        bytesRead = storageRead(&pszError, handle,
            (char*)valueSize, sizeof (int));
        if (bytesRead != sizeof (int) || *valueSize == 0)
            break;

        *returnValue = (jbyte*)pcsl_mem_malloc(*valueSize * sizeof (jbyte));
        if (*returnValue == NULL) {
            errorCode = OUT_OF_MEMORY;
            break;
        }

        bytesRead = storageRead(&pszError, handle, (char*)(*returnValue),
            *valueSize * sizeof (jbyte));

        if (pszError != NULL || bytesRead != *valueSize) {
            errorCode = SUITE_CORRUPTED_ERROR;
            pcsl_mem_free(*returnValue);
            *returnValue = NULL;
            break;
        }
    } while (0);

    storageClose(&pszError, handle);
    storageFreeError(pszError);

    return errorCode;
}
Example #4
0
/**
 * Reads string from the file.
 *
 * @param string pointer to result string
 * @return number of bytes read
 */
static int read_string(pcsl_string* string) {
  jint string_len;
  jchar *string_data;

  storageRead(&io_error_message, table_file, (char*)&string_len, sizeof(int));
  string_data = (jchar*)JSR211_MALLOC(string_len);
  storageRead(&io_error_message, table_file, (char*)string_data, string_len);
  pcsl_string_convert_from_utf16(string_data, string_len/sizeof(jchar), string);
  JSR211_FREE(string_data);

  return string_len + sizeof(int);
}
Example #5
0
/**
 * Searches specified string in the array record field
 * Reads strings from a current field of a current
 * record and compares it with a passed value.
 *
 * @param key compare key
 * @param cond internal ID of comparision mode
 * @return 1 if strings are equal (for the given mode)
 */
static int compare_array(const pcsl_string* key, jsr211_boolean case_sensitive, 
                                                        find_condition cond) {
  int array_size, array_len, current_string;

  storageRead(&io_error_message, table_file, (char *)&array_size, sizeof(int));
  storageRead(&io_error_message, table_file, (char *)&array_len, sizeof(int));

  for (current_string = 0; current_string < array_len; current_string++) {
    if (compare_string(key, case_sensitive, cond)) {
      return 1;
    }
  }
  return 0;
}
uint32_t configGetTh1Timeout(void) {
    struct config       config;

    storageRead(Storage, &config);

    return (config.th[1].time);
}
Example #7
0
/**
 * Searches the next content handler in the file by its suite id.
 *
 * @param suite search suite ID
 * @return offset of found record or -1
 */
static long find_next_by_suite(int suite) {
  long current_position;
  int record_size;
  int testSuite;
  int found = 0;

  current_position = storageRelativePosition(&io_error_message, table_file, 0);
  do {
    record_size = position_field(JSR211_CHR_SUITE);
    if (record_size <= 0) {
      current_position = -1;
      break;
    }

    storageRead(&io_error_message, table_file, (char*)&testSuite, sizeof(int));
    found = (testSuite == suite? 1: 0);
    storagePosition(&io_error_message, table_file, current_position);
    if (found) {
      break;
    }
    current_position = storageRelativePosition(&io_error_message, table_file, 
                                    record_size);
  } while (current_position > 0);

  return current_position;
}
uint32_t configGetTh0Vacuum(void) {
    struct config       config;

    storageRead(Storage, &config);

    return (config.th[0].vacuum);
}
Example #9
0
/**
 * Moves file position to next record.
 */
static void goto_next_record(void) {
  int    current_size;
  
  storageRead(&io_error_message, table_file, (char*)&current_size, sizeof(int));
  storageRelativePosition(&io_error_message, table_file, current_size - 
                                                                  sizeof(int));
}
uint32_t configGetTh1RawVacuum(void) {
    struct config       config;

    storageRead(Storage, &config);

    return (config.th[1].rawVacuum);
}
void initAppConfig(void) {
    struct config       config;

    if (storageRead(Storage, &config) != ES_ERROR_NONE) {
        appConfigReset(&config);
        storageWrite(Storage, &config);
    }
}
Example #12
0
/**
 * Read pcsl_string from storage.
 * First read a jint with length, then read the text
 * of that length in the utf-16 encoding.
 *
 * @param ppszError  in the case of error, receives address of a string
 *                   describing the problem; receives NULL in the case of success
 * @param handle     handle of the file to read from
 * @param str        string to receive the text
 */
void
storage_read_utf16_string(char** ppszError, int handle, pcsl_string* str) {
  jint bytesRead = 0;
  jchar *tempStr = NULL;
  jint tempLen = 0;
  pcsl_string_status prc;

  storageRead(ppszError, handle, (char*)&tempLen, sizeof (jint));
  if (*ppszError != NULL) {
    return;
  }

  /* special cases: null and empty strings */
  if (tempLen < 0) {
    if(0 == tempLen) {
        *str = PCSL_STRING_NULL;
    } else if (-1 == tempLen) {
        *str = PCSL_STRING_EMPTY;
    } else {
        *str = PCSL_STRING_NULL;
        *ppszError = (char *)STRING_CORRUPT_ERROR;
    }
    return;
  }

  tempStr = (jchar*)midpMalloc(tempLen * sizeof (jchar));
  if (tempStr == NULL) {
    *ppszError = (char *)OUT_OF_MEM_ERROR;
    return;
  }

  bytesRead = storageRead(ppszError, handle,
        (char*)tempStr, tempLen * sizeof (jchar));
  if (*ppszError != NULL) {
    /* do nothing: error code already there */
  } else if (bytesRead != (signed)(tempLen * sizeof (jchar))) {
    *ppszError = (char *)STRING_CORRUPT_ERROR;
  } else if (PCSL_STRING_OK != (prc
                = pcsl_string_convert_from_utf16(tempStr, tempLen, str))) {
    *ppszError = PCSL_STRING_ENOMEM == prc ? (char *)OUT_OF_MEM_ERROR
                                           : (char *)STRING_CORRUPT_ERROR;
  }
  midpFree(tempStr);
  return;
}
/**
 * Reads the given file into the given buffer.
 * File contents is read as one piece.
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param pFileName file to read
 * @param outBuffer buffer where the file contents should be stored
 * @param outBufferLen length of the outBuffer
 *
 * @return status code (ALL_OK if there was no errors)
 */
MIDPError
read_file(char** ppszError, const pcsl_string* pFileName,
          char** outBuffer, long* outBufferLen) {
    int handle, status = ALL_OK;
    long fileSize, len;
    char* pszTemp;
    char* buffer = NULL;

    *ppszError  = NULL;
    *outBuffer  = NULL;
    *outBufferLen = 0;

    /* open the file */
    handle = storage_open(ppszError, pFileName, OPEN_READ);
    if (*ppszError != NULL) {
        if (!storage_file_exists(pFileName)) {
            return NOT_FOUND;
        }
        return IO_ERROR;
    }

    do {
        /* get the size of file */
        fileSize = storageSizeOf(ppszError, handle);
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }

        if (fileSize > 0) {
            /* allocate a buffer to store the file contents */
            buffer = (char*)pcsl_mem_malloc(fileSize);
            if (buffer == NULL) {
                status = OUT_OF_MEMORY;
                break;
            }

            /* read the whole file */
            len = storageRead(ppszError, handle, buffer, fileSize);
            if (*ppszError != NULL || len != fileSize) {
                pcsl_mem_free(buffer);
                status = IO_ERROR;
            }
        }
    } while (0);

    /* close the file */
    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    if (status == ALL_OK) {
        *outBuffer    = buffer;
        *outBufferLen = fileSize;
    }

    return (MIDPError)status;
}
Example #14
0
static long
readChars(void* state, unsigned char* buffer, long n) {
    long size;
    char* pszError;
 
    size = storageRead(&pszError, (int)state, (char*)buffer, n);
    storageFreeError(pszError);
    return size;
}
/**
 * Opens a file and fills the content of the file in the result_buf. <BR>
 * This function made memory allocation inside.
 *
 * @param filename   Path to the file.
 * @param result_buf Pointer to the buffer that will be filled by content of the file.
 * @return buffer file size in bytes
 */
long readJadFile(const pcsl_string * filename, char** result_buf) {
    int fd = 0;
    char* err = NULL;
    long bufsize = -1;
    int numread = 0;
    char* res = *result_buf;

    if (pcsl_string_length(filename) <= 0) {
        REPORT_INFO(LC_AMS, "readJadFile():No file name.");
        return BAD_PARAMS;
    }

    fd = storage_open(&err, filename, OPEN_READ);
    if(err != NULL) {
        REPORT_INFO1(LC_AMS, "readJadFile():Can't open jad file '%s'",err);
        storageFreeError(err);
        return NO_JAD_FILE;
    }

    bufsize = storageSizeOf(&err, fd);
    if((bufsize <= 0) || (err != NULL)) {
        REPORT_INFO1(LC_AMS,  "readJadFile():Problem getting file size: %s",
		     err );
        storageFreeError(err);
        return IO_ERROR;
    }

    res = (char*)midpMalloc(bufsize+1);
    if (res == NULL || (err != NULL)) {
        REPORT_INFO1(LC_AMS, "readJadFile():Can't allocate memory. %s", err);
        storageFreeError(err);
        return OUT_OF_MEMORY;
    }

    memset(res,0,(bufsize+1));

    REPORT_INFO2(LC_AMS, "fd = %d, bufsize = %ld\n", fd, bufsize);

    numread = storageRead(&err, fd, res, bufsize);
    if((numread <= 0) || (numread != bufsize) || (err)) {
        REPORT_INFO3(LC_AMS, "size = %ld, numread = %d, err = %s.",
               bufsize, numread, err);
        storageClose(&err, fd);
        return IO_ERROR;
    }

    REPORT_INFO2(LC_AMS, "size = %ld, numread = %d", bufsize, numread);

    storageClose(&err, fd);
    if(err != NULL) {
        REPORT_INFO1(LC_AMS, "Can't close jad file %s\n", err);
    }
    *result_buf = res;
    return bufsize;
} /* end of readJadFile */
Example #16
0
/**
 * Reads string array from the file.
 *
 * @param array_size pointer to result array length
 * @param strings pointer to result array
 * @return number of bytes read
 */
static int read_string_array(int* array_size, pcsl_string** strings) {
  int array_len;
  int bytes_read;
  pcsl_string* current_string;

  storageRead(&io_error_message, table_file, (char *)&array_len, sizeof(int));
  storageRead(&io_error_message, table_file, (char *)array_size, sizeof(int));
  bytes_read = sizeof(int) * 2;

  if (*array_size > 0) {
    *strings = alloc_pcsl_string_list(*array_size);
    for (current_string = *strings; bytes_read < array_len; current_string++) {
      bytes_read += read_string(current_string);
    }
  } else {
    *array_size = 0;
  }

  return bytes_read;
}
bool configIsPasswordCharValid(char character, uint8_t position) {
    struct config       config;

    storageRead(Storage, &config);

    if (config.password[position] == character) {

        return (true);
    } else {

        return (false);
    }
}
Example #18
0
/**
 * Copies a file.
 *
 * @param srcName source file
 * @param destName destination file
 *
 * @return 0 for success
 */
static int copyFile(const pcsl_string * srcName, const pcsl_string * destName) {
    char* pszError = NULL;
    char* pszDummy = NULL;
    int src;
    int dest;
    char buffer[1024];
    long bytesRead;

    src = storage_open(&pszError, srcName, OPEN_READ);
    if (pszError == NULL) {
        dest = storage_open(&pszError, destName, OPEN_READ_WRITE_TRUNCATE);
        if (pszError == NULL) {
            bytesRead = storageRead(&pszError, src, buffer, sizeof (buffer));
            while (pszError == NULL && bytesRead > 0) {
                storageWrite(&pszError, dest, buffer, bytesRead);
                if (pszError == NULL) {
                    bytesRead = storageRead(&pszError, src, buffer,
                                            sizeof (buffer));
                }
            }

            storageClose(&pszDummy, dest);
            storageFreeError(pszDummy);
            pszDummy = NULL;
        }

        storageClose(&pszDummy, src);
        storageFreeError(pszDummy);
    }

    if (pszError != NULL) {
        REPORT_ERROR1(LC_AMS, "Error while copying file: %s", pszError);
        storageFreeError(pszError);
        return -1;
    }

    return 0;
}
Example #19
0
/**
 * Deletes content handler information from a registry.
 *
 * @param handler_id content handler ID
 * @return JSR211_OK if content handler unregistered successfully
 */
jsr211_result jsr211_unregister_handler(const pcsl_string* handler_id) {
  long current_position, file_size, buffer_size;
  int record_size;
  char *buffer;

  if (open_table_file(1) != JSR211_OK) {
    return JSR211_FAILED;
  }
  if (find_next_by_field(NULL, JSR211_FIELD_ID, handler_id, JSR211_TRUE,
                                                            find_exact) == -1) {
    close_table_file();
    return JSR211_FAILED;
  }
  
  storageRead(&io_error_message, table_file, (char *)&record_size, sizeof(int));
  current_position = storageRelativePosition(&io_error_message, table_file, 
                                                            -(long)sizeof(int));
  file_size = storageSizeOf(&io_error_message, table_file);
  goto_next_record();
  
  if (!(buffer = JSR211_MALLOC((buffer_size = file_size - current_position - 
                                                            record_size)))) {
    close_table_file();
    return JSR211_FAILED;
  }
  
  storageRead(&io_error_message, table_file, buffer, buffer_size);
  storagePosition(&io_error_message, table_file, current_position);
  storageWrite(&io_error_message, table_file, buffer, buffer_size);
  JSR211_FREE(buffer);
  storageTruncate(&io_error_message, table_file, current_position + 
                                                                   buffer_size);
  
  close_table_file();
  return JSR211_OK;
}
Example #20
0
File: emul.c Project: sfsy1989/j2me
/**
 * Returns class of device and service classes saved for an 
 * emulated device with the same address previous time. 
 * It is considered to be the same device as current one.
 */
static int loadCod() {
    char *error;
    int cod = DEFAULT_COD;
    int handle = openCodFile();
    
    if (-1 != handle) {
        storageRead(&error, handle, (char*)&cod, sizeof(int));
        storageFreeError(error);
    }
    
    storageClose(&error, handle);
    storageFreeError(error);
    
    return cod;
}
bool configSetTh1Timeout(uint32_t timeoutMs) {
    struct config       config;

    if (storageRead(Storage, &config) != ES_ERROR_NONE) {
        goto SPACE_FAILURE;
    }
    config.th[1].time = timeoutMs;

    if (storageWrite(Storage, &config) != ES_ERROR_NONE) {
        goto SPACE_FAILURE;
    }

    return (true);
SPACE_FAILURE:

    return (false);
}
bool configSetTh0RawVacuum(uint32_t rawVacuum) {
    struct config       config;

    if (storageRead(Storage, &config) != ES_ERROR_NONE) {
        goto SPACE_FAILURE;
    }
    config.th[0].rawVacuum = rawVacuum;

    if (storageWrite(Storage, &config) != ES_ERROR_NONE) {
        goto SPACE_FAILURE;
    }

    return (true);
SPACE_FAILURE:

    return (false);
}
Example #23
0
/**
 * Verifies access of caller for current content handler.
 * Reads access restrictions from a current record and
 * compares it with a passed caller_id.
 *
 * @param caller_id caller
 * @param current_position current position of the file
 * @param current_size [out] size of current record
 * @return 1 if access is permitted
 */
static int check_access(const pcsl_string* caller_id, long current_position, 
                                                            int* current_size) {
  int    access_len, access_ok;

  if ((*current_size = position_field(JSR211_CHR_ACCESSES)) <= 0) {
    return -1;
  }
  if (caller_id != NULL && pcsl_string_length(caller_id) > 0) {
    storageRelativePosition(&io_error_message, table_file, sizeof(int));
    storageRead(&io_error_message, table_file, (char*)&access_len, sizeof(int));
    if (!access_len) {
      access_ok = 1;
    }
    else {
      storageRelativePosition(&io_error_message, table_file, -2 * (long)sizeof(int));
      access_ok = compare_array(caller_id, JSR211_TRUE, find_first);
    }
  }
  else {
    access_ok = 1;
  }
  storagePosition(&io_error_message, table_file, current_position);
  return access_ok;
}
/**
 * Gets the settings of a MIDlet suite from persistent storage.
 * <pre>
 * The format of the properties file will be:
 *
 *   push interrupt setting as an jbyte
 *   length of a permissions as an int
 *   array of permissions jbytes
 *   push options as jint
 * </pre>
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param suiteId  ID of the suite
 * @param pEnabled pointer to an enabled setting
 * @param pPushInterrupt pointer to a push interruptSetting
 * @param pPushOptions user options for push interrupts
 * @param ppPermissions pointer a pointer to accept a permissions array
 * @param pNumberOfPermissions pointer to an int
 *
 * @return error code (ALL_OK if successful)
 */
MIDPError
read_settings(char** ppszError, SuiteIdType suiteId, jboolean* pEnabled,
             jbyte* pPushInterrupt, jint* pPushOptions,
             jbyte** ppPermissions, int* pNumberOfPermissions) {
    pcsl_string filename;
    int handle;
    int bytesRead;
    char* pszTemp;
    MIDPError status;

    *ppszError = NULL;
    *ppPermissions = NULL;
    *pNumberOfPermissions = 0;

    status = build_suite_filename(suiteId, &SETTINGS_FILENAME, &filename);
    if (status != ALL_OK) {
        return status;
    }

    handle = storage_open(ppszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    bytesRead = storageRead(ppszError, handle, (char*)pEnabled,
        sizeof (jboolean));
    do {
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)pPushInterrupt,
                                sizeof (jbyte));
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)pNumberOfPermissions,
                                sizeof (int));

        if (bytesRead != sizeof (int) || *pNumberOfPermissions == 0) {
            status = IO_ERROR;
            break;
        }

        *ppPermissions = (jbyte*)pcsl_mem_malloc(
                *pNumberOfPermissions * sizeof (jbyte));
        if (*ppPermissions == NULL) {
            status = OUT_OF_MEMORY;
            break;
        }

        bytesRead = storageRead(ppszError, handle, (char*)(*ppPermissions),
                                *pNumberOfPermissions * sizeof (jbyte));

        if (bytesRead != *pNumberOfPermissions) {
            *pNumberOfPermissions = 0;
            pcsl_mem_free(*ppPermissions);
            status = SUITE_CORRUPTED_ERROR;
            break;
        }

        /* Old versions of the file may not have options. */
        status = ALL_OK;
        *pPushOptions = 0;
        bytesRead = storageRead(ppszError, handle, (char*)pPushOptions,
                                sizeof (jint));
        if (*ppszError != NULL) {
            storageFreeError(*ppszError);
            *ppszError = NULL;
            break;
        }
    } while (0);

    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    return status;
}
/**
 * Retrieves the list of strings in a file.
 * The file has the number of strings at the front, each string
 * is a length and the jchars.
 *
 * @param ppszError pointer to character string pointer to accept an error
 * @param pFilename name of the file of strings
 * @param paList pointer to an array of pcsl_strings, free with
 *               free_pcsl_string_list
 * @param pStringNum number of strings if successful (can be 0)
 *
 * @return error code (ALL_OK if successful)
 */
static MIDPError
get_string_list(char** ppszError, const pcsl_string* pFilename,
                pcsl_string** paList, int* pStringNum) {
    char* pszTemp;
    int i = 0;
    int handle;
    int numberOfStrings = 0;
    pcsl_string* pStrings = NULL;
    MIDPError status = ALL_OK;

    *ppszError = NULL;
    *paList = NULL;
    *pStringNum = 0;

    handle = storage_open(ppszError, pFilename, OPEN_READ);
    if (*ppszError != NULL) {
        return IO_ERROR;
    }

    do {
        storageRead(ppszError, handle, (char*)&numberOfStrings,
            sizeof (numberOfStrings));
        if (*ppszError != NULL) {
            status = IO_ERROR;
            break;
        }
        if (numberOfStrings == 0) {
            break;
        }

        pStrings = alloc_pcsl_string_list(numberOfStrings);
        if (pStrings == NULL) {
            status = OUT_OF_MEMORY;
            break;
        }

        for (i = 0; i < numberOfStrings; i++) {
            pStrings[i] = PCSL_STRING_NULL;
        }

        for (i = 0; i < numberOfStrings; i++) {
            storage_read_utf16_string(ppszError, handle, &pStrings[i]);
            if (*ppszError != NULL) {
                status = IO_ERROR;
                break;
            }

        }

        if (i != numberOfStrings) {
            status = SUITE_CORRUPTED_ERROR;
            break;
        }
    } while (0);

    storageClose(&pszTemp, handle);
    storageFreeError(pszTemp);

    if (status == ALL_OK) {
        *paList = pStrings;
        *pStringNum = numberOfStrings;
    } else if (pStrings != NULL) {
        free_pcsl_string_list(pStrings, i);
    }

    return status;
}
Example #26
0
javacall_result bt_push_startup()
{
    int i;
    char *error;
    pcsl_string full_name = PCSL_STRING_NULL;
    int storage;
    REPORT_INFO(LC_PUSH, "Bluetooth PushRegistry is now starting.");
    javacall_bt_sddb_initialize();
    pcsl_string_cat(storage_get_root(INTERNAL_STORAGE_ID), &BT_PUSH_FILENAME,
            &full_name);
    if (!storage_file_exists(&full_name)) {
        pcsl_string_free(&full_name);
        return JAVACALL_OK;
    }
    storage = storage_open(&error, &full_name, OPEN_READ);
    pcsl_string_free(&full_name);
    if (error != NULL) {
        REPORT_ERROR1(LC_PUSH, "Failed to open `BtPush' file: %s", error);
        storageFreeError(error);
        return JAVACALL_FAIL;
    }
    storageRead(&error, storage, (char *)&g_count, sizeof(g_count));
    for (i = 0; error == NULL && i < g_count; i++) {
        bt_push_t *push = (bt_push_t *)pcsl_mem_malloc(sizeof(bt_push_t));
        if (push == NULL) {
            REPORT_ERROR(LC_PUSH, "Failed to allocate memory.");
            storageClose(&error, storage);
            storageFreeError(error);
            return JAVACALL_FAIL;
        }
        storageRead(&error, storage, (char *)&push->port, sizeof(push->port));
        if (error != NULL) {
            pcsl_mem_free(push);
            break;
        }
        storageRead(&error, storage, (char *)&push->params,
                sizeof(push->params));
        if (error != NULL) {
            pcsl_mem_free(push);
            break;
        }
        storageRead(&error, storage, (char *)&push->record.classes,
                sizeof(push->record.classes));
        if (error != NULL) {
            pcsl_mem_free(push);
            break;
        }
        storageRead(&error, storage, (char *)&push->record.size,
                sizeof(push->record.size));
        if (error != NULL) {
            pcsl_mem_free(push);
            break;
        }
        push->record.data = pcsl_mem_malloc(push->record.size);
        if (push->record.data == NULL) {
            pcsl_mem_free(push);
            REPORT_ERROR(LC_PUSH, "Failed to allocate memory.");
            storageClose(&error, storage);
            storageFreeError(error);
            return JAVACALL_FAIL;
        }
        storageRead(&error, storage, (char *)push->record.data,
                push->record.size);
        if (error != NULL) {
            pcsl_mem_free(push->record.data);
            pcsl_mem_free(push);
            break;
        }
        storageRead(&error, storage, (char *)&push->record.id,
                sizeof(push->record.id));
        if (error != NULL) {
            pcsl_mem_free(push);
            break;
        }
        push->server = BT_INVALID_HANDLE;
        push->client = NULL;
        push->next = g_registry;
        g_registry = push;
    }
    if (error != NULL) {
        REPORT_ERROR1(LC_PUSH, "Error reading `BtPush' file: %s", error);
        storageFreeError(error);
        storageClose(&error, storage);
        storageFreeError(error);
        return JAVACALL_FAIL;
    }
    REPORT_INFO1(LC_PUSH, "%d record(s) read.", g_count);
    storageClose(&error, storage);
    storageFreeError(error);
    if (g_count > 0) {
        /* Attempt to enable Bluetooth radio, if it is not already on. */
        javacall_bool enabled;
        javacall_bt_stack_initialize();
        if (javacall_bt_stack_is_enabled(&enabled) == JAVACALL_OK &&
                enabled == JAVACALL_FALSE) {
            javacall_bt_stack_enable();
        }
    }
    return JAVACALL_OK;
}
/**
 * Loads the properties of a MIDlet suite from persistent storage.
 *
 * @param suiteId ID of the suite
 * @param pJadProps [out] pointer to a structure containing an array of strings,
 * in a pair pattern of key and value; NULL may be passed if it is not required
 * to read JAD properties
 * @param pJarProps [out] pointer to a structure containing an array of strings,
 * in a pair pattern of key and value; NULL may be passed if it is not required
 * to read JAR properties
 *
 * @return error code (ALL_OK for success)
 */
MIDPError
load_install_properties(SuiteIdType suiteId, MidpProperties* pJadProps,
                        MidpProperties* pJarProps) {
    pcsl_string filename;
    char* pszError = NULL;
    int handle, i, n;
    int numberOfProps;
    MIDPError status;

    status = get_property_file(suiteId, KNI_TRUE, &filename);
    if (status != ALL_OK) {
        return status;
    }

    handle = storage_open(&pszError, &filename, OPEN_READ);
    pcsl_string_free(&filename);
    if (pszError != NULL) {
        storageFreeError(pszError);
        return IO_ERROR;
    }

    status = ALL_OK;

    /* Read JAD, then JAR properties. */
    for (n = 0; n < 2; n++) {
        MidpProperties *pProps = n ? pJarProps : pJadProps;
        if (!pProps) {
            continue;
        }

        storageRead(&pszError, handle, (char*)&numberOfProps,
            sizeof (numberOfProps));
        if (pszError != NULL) {
            break;
        }

        pProps->pStringArr = alloc_pcsl_string_list(numberOfProps << 1);

        for (i = 0; i < numberOfProps << 1; i++) {
            storage_read_utf16_string(&pszError, handle,
                                      &pProps->pStringArr[i]);
            if (pszError != NULL) {
                break;
            }
        }

        if (pszError != NULL) {
            break;
        }

        pProps->numberOfProperties = numberOfProps;
    }

    if (pszError != NULL) {
        status = IO_ERROR;
        storageFreeError(pszError);
    }

    storageClose(&pszError, handle);
    storageFreeError(pszError);

    if (status != ALL_OK) {
        if (pJadProps) {
            free_pcsl_string_list(pJadProps->pStringArr,
                                  pJadProps->numberOfProperties << 1);
        }
        if (pJarProps) {
            free_pcsl_string_list(pJarProps->pStringArr,
                                  pJarProps->numberOfProperties << 1);
        }
    }

    return status;
}
KNIEXPORT KNI_RETURNTYPE_VOID
KNIDECL(com_sun_midp_chameleon_skins_resources_LoadedSkinData_beginReadingSkinFile) {
    const unsigned char* skin_description = lfj_get_skin_description();

    if (skin_description != NULL) {
        gsSkinFileDataStart = gsSkinFileDataPos =
            (unsigned char*)skin_description;
        gsSkinFileDataEnd = gsSkinFileDataStart +
            lfj_get_skin_description_size();
    } else {
        char* errorStr = NULL;
        int fileHandle = -1;
        int fileSize;
        int bytesRead;
        jfieldID fid;

        KNI_StartHandles(2);
        KNI_DeclareHandle(classHandle);

        KNI_GetClassPointer(classHandle);

        fid = KNI_GetStaticFieldID(classHandle, "STRING_ENCODING_USASCII", "B");
        STRING_ENCODING_USASCII = (unsigned char)
            KNI_GetStaticByteField(classHandle, fid);

        fid = KNI_GetStaticFieldID(classHandle, "STRING_ENCODING_UTF8", "B");
        STRING_ENCODING_UTF8 = (unsigned char)
            KNI_GetStaticByteField(classHandle, fid);

        GET_PARAMETER_AS_PCSL_STRING(1, fileName);

        do {
            /*
             * Open skin file
             */
            fileHandle = storage_open(&errorStr, &fileName, OPEN_READ);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                break;
            }

            /*
             * Obtain file size
             */
            fileSize = storageSizeOf(&errorStr, fileHandle);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                break;
            }

            /*
             * Read whole file into heap memory
             */
            gsSkinFileDataStart = (unsigned char*)midpMalloc(fileSize);
            if (gsSkinFileDataStart == NULL) {
                KNI_ThrowNew(midpOutOfMemoryError, NULL);
                break;
            }

            bytesRead = storageRead(&errorStr, fileHandle,
                    (char*)gsSkinFileDataStart, fileSize);
            if (errorStr != NULL) {
                KNI_ThrowNew(midpIOException, errorStr);
                storageFreeError(errorStr);
                midpFree(gsSkinFileDataStart);
                gsSkinFileDataStart = NULL;
                break;
            }
            if (bytesRead != fileSize) {
                KNI_ThrowNew(midpIOException, "Failed to read whole file");
                midpFree(gsSkinFileDataStart);
                gsSkinFileDataStart = NULL;
                break;
            }

            gsSkinFileDataPos = gsSkinFileDataStart;
            gsSkinFileDataEnd = gsSkinFileDataStart + fileSize;

        } while (0);

        RELEASE_PCSL_STRING_PARAMETER;

        /*
         * Close skin file
         */
        if (fileHandle != -1) {
            storageClose(&errorStr, fileHandle);
        }

        KNI_EndHandles();
    }

    KNI_ReturnVoid();
}
/**
 * Reads in a property file and makes the key/value pairs available
 * to MIDP as a property set.
 *
 * @param fd An open file descriptor of the property file to read.
 * @param props A property set to hold the key/value pairs read from
 *              the property file.
 * @return <tt>0</tt> for success, otherwise <tt>-1</tt>
 */
static int
parseConfig(int fd, Property** props) {
    char *buffer;
    int bufferSize;
    int i;
    int j;
    int key_index;
    int value_index;
    int len;
    char *errStr = NULL;
    int startPos;
    int endPos;

    bufferSize = storageSizeOf(&errStr, fd);
    buffer = (char *)midpMalloc(bufferSize);
    if (buffer == NULL) {
        REPORT_WARN(LC_CORE, 
            "midpMalloc failed to allocate buffer for config file."); 
        return -1;
    }

    /* Read the config file in one pass */
    len = storageRead(&errStr, fd, buffer, bufferSize);
    if ((errStr != NULL) || (len != bufferSize)) {
        REPORT_WARN1(LC_CORE, 
             "Warning: can not read config file: %s", errStr);
    
        storageFreeError(errStr);
        midpFree(buffer);
        return 0;
    }

    startPos = 0;
    for (i = startPos; i < bufferSize; i++) {
        if (buffer[i] == '\n') {
            
            buffer[i] = 0;

            /* Skip comment lines which begin  with '#'*/
            if (buffer[startPos] != '#') {
                
                /* Parse the line */
                key_index = startPos;
                for (j = key_index; buffer[j]; j++){

                    Property *prop;

                    if (buffer[j] == ':') {
                        buffer[j] = 0;
                        value_index = ++j;

                        prop = (Property *) midpMalloc(sizeof(Property));
                        if (NULL != prop) {
                            char *key, *value;
                            key = midpStrdup(buffer + key_index);
                            value = midpStrdup(buffer + value_index);
                            
                            /* trim leading and trailing white spaces */
                            trim_WhiteSpace(key);
                            trim_WhiteSpace(value);

                            prop->next = *props;
                            prop->key = key;
                            prop->value = value;
        
                            if ((prop->key == NULL) || (prop->value == NULL)) {
                                midpFree((void*)prop->key);
                                midpFree((void*)prop->value);
                                midpFree(prop);

                                /*
                                 * since we are freeing memory, we're not
                                 * exactly out of memory at this point
                                 */
                                break;
                            }

                            *props = prop;
                        }

                        break;
                    }
                }
            }
            endPos = i;
            startPos = endPos + 1;
        }
    }
    midpFree(buffer);
    return 0;
}