static int findNextMatch(PCSLFileIterator* pIterator, const pcsl_string * match, pcsl_string * result) { WIN32_FIND_DATAW findData; pcsl_string root = PCSL_STRING_NULL; pcsl_string foundName = PCSL_STRING_NULL; jsize rootLen = 0; if (result == NULL) { return -1; } * result = PCSL_STRING_NULL; if (!FindNextFileW(pIterator->iteratorHandle, &findData)) { return -1; } rootLen = pIterator->savedRootLength; if (pcsl_string_substring(match, 0, rootLen, &root) != PCSL_STRING_OK) { return -1; } if (pcsl_string_convert_from_utf16(findData.cFileName, wcslen(findData.cFileName), &foundName) != PCSL_STRING_OK) { pcsl_string_free(&root); return -1; } if (pcsl_string_cat(&root, &foundName, result) != PCSL_STRING_OK) { pcsl_string_free(&foundName); pcsl_string_free(&root); return -1; } pcsl_string_free(&foundName); pcsl_string_free(&root); return 0; }
static MidpProperties midpParseMf(jchar* jchar_buffer) { MidpProperties mfsmp = {0, ALL_OK, NULL}; pcsl_string mfkey; pcsl_string mfkey_trimmed; pcsl_string mfvalue; pcsl_string mfvalue_trimmed; pcsl_string mfline; MIDPError err; pcsl_string_status rc; int countLines = 0; int index = 0; int count = 0; if (!jchar_buffer) { mfsmp.status = BAD_PARAMS; return mfsmp; } countLines = count_mf_lines(jchar_buffer); if (countLines <= 0) { REPORT_INFO(LC_AMS, "midpParseMf(): Empty manifest."); mfsmp.status = OUT_OF_MEMORY; return mfsmp; } mfsmp.pStringArr = alloc_pcsl_string_list(countLines * 2); if (mfsmp.pStringArr == NULL) { mfsmp.status = OUT_OF_MEMORY; return mfsmp; } mfsmp.numberOfProperties = countLines; for (count = 0; count < countLines * 2 ; /* count increased at the end of for */ ) { /* memory for the line is allocated here */ /* line continuation striped out */ err = readMfLine(&jchar_buffer, &mfline); if (OUT_OF_MEMORY == err) { midp_free_properties(&mfsmp); mfsmp.status = OUT_OF_MEMORY; return mfsmp; } else if (END_OF_MF == err) { /* we are done */ mfsmp.status = ALL_OK; break; } index = pcsl_string_index_of(&mfline, ':'); if (index <= 0) { mfsmp.status = BAD_MF_KEY; pcsl_string_free(&mfline); continue; } /* memory for mfkey is allocated here */ if (PCSL_STRING_OK != pcsl_string_substring(&mfline, 0, index, &mfkey)) { midp_free_properties(&mfsmp); mfsmp.status = OUT_OF_MEMORY; pcsl_string_free(&mfline); return mfsmp; } rc = pcsl_string_trim(&mfkey, &mfkey_trimmed); pcsl_string_free(&mfkey); if (PCSL_STRING_OK != rc) { mfsmp.status = OUT_OF_MEMORY; midp_free_properties(&mfsmp); pcsl_string_free(&mfline); return mfsmp; } if (pcsl_string_length(&mfkey_trimmed) < 1) { mfsmp.status = BAD_PARAMS; pcsl_string_free(&mfline); pcsl_string_free(&mfkey_trimmed); continue; } err = checkMfKeyChars(&mfkey_trimmed); if (OUT_OF_MEMORY == err) { mfsmp.status = OUT_OF_MEMORY; midp_free_properties(&mfsmp); pcsl_string_free(&mfline); pcsl_string_free(&mfkey_trimmed); return mfsmp; } else if (BAD_MF_KEY == err) { mfsmp.status = BAD_MF_KEY; pcsl_string_free(&mfline); pcsl_string_free(&mfkey_trimmed); continue; } rc = pcsl_string_substring(&mfline, index + 1, pcsl_string_length(&mfline), &mfvalue); /* free the mfline once we have got the key and value */ pcsl_string_free(&mfline); if (PCSL_STRING_OK != rc) { mfsmp.status = OUT_OF_MEMORY; midp_free_properties(&mfsmp); pcsl_string_free(&mfkey_trimmed); return mfsmp; } /* memory for value is allocated here */ rc = pcsl_string_trim(&mfvalue, &mfvalue_trimmed); pcsl_string_free(&mfvalue); if (PCSL_STRING_OK != rc) { mfsmp.status = OUT_OF_MEMORY; midp_free_properties(&mfsmp); pcsl_string_free(&mfkey_trimmed); return mfsmp; } if (pcsl_string_is_null(&mfvalue_trimmed)) { mfsmp.status = NULL_LEN; pcsl_string_free(&mfkey_trimmed); continue; } err = checkMfValueChars(&mfvalue_trimmed); if (OUT_OF_MEMORY == err) { mfsmp.status = OUT_OF_MEMORY; midp_free_properties(&mfsmp); pcsl_string_free(&mfkey_trimmed); pcsl_string_free(&mfvalue_trimmed); return mfsmp; } else if (BAD_MF_VALUE == err) { mfsmp.status = BAD_MF_VALUE; pcsl_string_free(&mfkey_trimmed); pcsl_string_free(&mfvalue_trimmed); continue; } printPcslStringWithMessage("midpParseMf()", &mfkey_trimmed); printPcslStringWithMessage(" = ", &mfvalue_trimmed); /* Store key:value pair. */ mfsmp.pStringArr[count] = mfkey_trimmed; mfsmp.pStringArr[count+1] = mfvalue_trimmed; count += 2; } /* end of for */ mfsmp = verifyMfMustProperties(mfsmp); REPORT_INFO3(LC_AMS, "End of midpParseMf: Status=%d, count=%d, countLines=%d", mfsmp.status, count, countLines); return mfsmp; } /* end of midpParseMf */
static MidpProperties midpParseJad(jchar* jchar_buffer) { MidpProperties jadsmp = {0, ALL_OK, NULL}; pcsl_string jadkey; pcsl_string jadkey_trimmed; pcsl_string jadvalue; pcsl_string jadvalue_trimmed; pcsl_string jadline; MIDPError err; pcsl_string_status rc; int countLines = 0; jint index = 0; int count = 0; if (!jchar_buffer) { jadsmp.status = BAD_PARAMS; return jadsmp; } countLines = count_jad_lines(jchar_buffer); if (countLines <= 0) { REPORT_INFO(LC_AMS, "midpParseJad(): Empty jad file."); jadsmp.status = OUT_OF_MEMORY; return jadsmp; } jadsmp.pStringArr = alloc_pcsl_string_list(countLines * 2); if (jadsmp.pStringArr == NULL) { jadsmp.status = OUT_OF_MEMORY; return jadsmp; } jadsmp.numberOfProperties = countLines; for (count = 0; count < countLines * 2 ; /* count increased at the end of for */ ) { /* memory for the line is allocated here */ err = readJadLine(&jchar_buffer, &jadline); if (OUT_OF_MEMORY == err) { midp_free_properties(&jadsmp); jadsmp.status = OUT_OF_MEMORY; return jadsmp; } else if (END_OF_JAD == err) { /* we are done */ jadsmp.status = ALL_OK; break; } index = pcsl_string_index_of(&jadline, ':'); if (index <= 0) { jadsmp.status = BAD_JAD_KEY; pcsl_string_free(&jadline); continue; } /* memory for key is allocated here */ if (PCSL_STRING_OK != pcsl_string_substring(&jadline, 0, index, &jadkey)) { midp_free_properties(&jadsmp); jadsmp.status = OUT_OF_MEMORY; pcsl_string_free(&jadline); return jadsmp; } rc = pcsl_string_trim(&jadkey, &jadkey_trimmed); pcsl_string_free(&jadkey); if (PCSL_STRING_OK != rc) { jadsmp.status = OUT_OF_MEMORY; midp_free_properties(&jadsmp); pcsl_string_free(&jadline); return jadsmp; } if (pcsl_string_length(&jadkey_trimmed) < 1) { jadsmp.status = BAD_PARAMS; pcsl_string_free(&jadline); pcsl_string_free(&jadkey_trimmed); continue; } err = checkJadKeyChars(&jadkey_trimmed); if (OUT_OF_MEMORY == err) { jadsmp.status = OUT_OF_MEMORY; midp_free_properties(&jadsmp); pcsl_string_free(&jadline); pcsl_string_free(&jadkey_trimmed); return jadsmp; } else if (BAD_JAD_KEY == err) { jadsmp.status = BAD_JAD_KEY; pcsl_string_free(&jadline); pcsl_string_free(&jadkey_trimmed); continue; } rc = pcsl_string_substring(&jadline, index + 1, pcsl_string_length(&jadline), &jadvalue); /* free the jadline once we have got the key and value */ pcsl_string_free(&jadline); if (PCSL_STRING_OK != rc) { jadsmp.status = OUT_OF_MEMORY; midp_free_properties(&jadsmp); pcsl_string_free(&jadkey_trimmed); return jadsmp; } /* memory for value is allocated here */ rc = pcsl_string_trim(&jadvalue, &jadvalue_trimmed); pcsl_string_free(&jadvalue); if (PCSL_STRING_OK != rc) { jadsmp.status = OUT_OF_MEMORY; midp_free_properties(&jadsmp); pcsl_string_free(&jadkey_trimmed); return jadsmp; } if (pcsl_string_is_null(&jadvalue_trimmed)) { jadsmp.status = NULL_LEN; pcsl_string_free(&jadkey_trimmed); continue; } err = checkJadValueChars(&jadvalue_trimmed); if (OUT_OF_MEMORY == err) { jadsmp.status = OUT_OF_MEMORY; midp_free_properties(&jadsmp); pcsl_string_free(&jadkey_trimmed); pcsl_string_free(&jadvalue_trimmed); return jadsmp; } else if (BAD_JAD_VALUE == err) { jadsmp.status = BAD_JAD_VALUE; pcsl_string_free(&jadkey_trimmed); pcsl_string_free(&jadvalue_trimmed); continue; } printPcslStringWithMessage("midpParseJad()", &jadkey_trimmed); printPcslStringWithMessage("midpParseJad()", &jadvalue_trimmed); /* Store key:value pair. */ jadsmp.pStringArr[count] = jadkey_trimmed; jadsmp.pStringArr[count+1] = jadvalue_trimmed; count += 2; } /* end of for */ jadsmp = verifyJadMustProperties(jadsmp); REPORT_INFO3(LC_AMS, "End jad parsing. Status=%d, count=%d, countLines=%d.", jadsmp.status, count, countLines); return jadsmp; } /* end of midpParseJad */
static int findFirstMatch(PCSLFileIterator* pIterator, const pcsl_string * match, pcsl_string * result) { WIN32_FIND_DATAW findData; HANDLE handle; PCSL_DEFINE_ASCII_STRING_LITERAL_START(starSuffix) {'*', '\0'} PCSL_DEFINE_ASCII_STRING_LITERAL_END(starSuffix); pcsl_string root = PCSL_STRING_NULL; pcsl_string foundName = PCSL_STRING_NULL; pcsl_string matchStar = PCSL_STRING_NULL; jsize rootLen = 0; if (result == NULL) { return -1; } * result = PCSL_STRING_NULL; if (pcsl_string_cat(match, &starSuffix, &matchStar) != PCSL_STRING_OK) { return -1; } { const jchar * pwszMatch = pcsl_string_get_utf16_data(&matchStar); if (NULL == pwszMatch) { pcsl_string_free(&matchStar); return -1; } handle = FindFirstFileW(pwszMatch, &findData); pcsl_string_free(&matchStar); } if (INVALID_HANDLE_VALUE == handle) { return -1; } pIterator->iteratorHandle = handle; rootLen = pIterator->savedRootLength; if (pcsl_string_substring(match, 0, rootLen, &root) != PCSL_STRING_OK) { return -1; } if (pcsl_string_convert_from_utf16(findData.cFileName, wcslen(findData.cFileName), &foundName) != PCSL_STRING_OK) { pcsl_string_free(&root); return -1; } if (pcsl_string_cat(&root, &foundName, result) != PCSL_STRING_OK) { pcsl_string_free(&foundName); pcsl_string_free(&root); return -1; } pcsl_string_free(&foundName); pcsl_string_free(&root); return 0; }
/** The getNextEntry function search the next file which is specified DIR */ int pcsl_file_getnextentry(void *handle, const pcsl_string * string, pcsl_string * result) { pcsl_string rootpath = PCSL_STRING_NULL; pcsl_string returnVal = PCSL_STRING_NULL; pcsl_string matchName = PCSL_STRING_NULL; jsize matchLen; jsize len; struct dirent *de; char* pszFilename = NULL; DIR *dir; jsize savedRootLength = 0; jsize savedMatchLength = 0; PCSLStorageDirInfo* pPCSLStorageDirInfo = (PCSLStorageDirInfo *)handle; if (pPCSLStorageDirInfo == NULL) { return -1; } savedRootLength = pPCSLStorageDirInfo->savedRootLength; savedMatchLength = pPCSLStorageDirInfo->savedMatchLength; if (pcsl_string_substring(string, savedRootLength, savedRootLength + savedMatchLength, &matchName) != PCSL_STRING_OK) { return -1; } { const jbyte * pszMatch = NULL; dir = pPCSLStorageDirInfo->savedDirectory; if (dir == NULL) { return -1; } pszMatch = pcsl_string_get_utf8_data(&matchName); if (pszMatch == NULL) { return -1; } matchLen = strlen((char*)pszMatch); /* find the first match file not "." or ".." */ for (de = readdir(dir); de != NULL; de = readdir(dir)) { pszFilename = de->d_name; if (strcmp(pszFilename, ".") == 0 || strcmp(pszFilename, "..") == 0) { continue; } if (strncmp(pszFilename, (char*)pszMatch, matchLen) == 0) { break; } } pcsl_string_release_utf8_data(pszMatch, &matchName); pcsl_string_free(&matchName); if (NULL == de) { /* End of Dir without a match. */ return -1; } len = strlen(pszFilename); if (len >= 0) { if (pcsl_string_convert_from_utf8((jbyte*)pszFilename, len, &returnVal) != PCSL_STRING_OK) { return -1; } if (pcsl_string_substring(string, 0, savedRootLength, &rootpath) != PCSL_STRING_OK) { pcsl_string_free(&returnVal); return -1; } if (pcsl_string_cat(&rootpath, &returnVal, result) != PCSL_STRING_OK) { pcsl_string_free(&returnVal); pcsl_string_free(&rootpath); return -1; } pcsl_string_free(&returnVal); pcsl_string_free(&rootpath); } } return 0; }
int find_midlet_class(SuiteIdType id, int midletNumber, pcsl_string* res) { MidpProperties properties; pcsl_string keySuffix = PCSL_STRING_NULL; pcsl_string key = PCSL_STRING_NULL; const pcsl_string* property = &PCSL_STRING_NULL; pcsl_string temp = PCSL_STRING_NULL; pcsl_string_status stat; int begin; int result = 0; *res = PCSL_STRING_NULL; properties = midp_get_suite_properties(id); if (OUT_OF_MEM_PROPERTY_STATUS(properties)) { return OUT_OF_MEM_LEN; } do { if (CORRUPTED_PROPERTY_STATUS(properties)) { midp_free_properties(&properties); REPORT_ERROR(LC_AMS, "Error : Suite is corrupted"); fprintf(stderr, "Error : Suite is corrupted\n"); result = NULL_LEN; break; } if (READ_ERROR_PROPERTY_STATUS(properties)) { midp_free_properties(&properties); REPORT_ERROR(LC_AMS, "Corrupt properties"); fprintf(stderr, "Corrupt properties\n"); result = NULL_LEN; break; } stat = pcsl_string_convert_from_jint(midletNumber, &keySuffix); if (PCSL_STRING_OK != stat) { if(PCSL_STRING_ENOMEM == stat) { result = OUT_OF_MEM_LEN; } else { result = NULL_LEN; } break; } stat = pcsl_string_cat(&KEY_PREFIX, &keySuffix, &key); pcsl_string_free(&keySuffix); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } property = midp_find_property(&properties, &key); if (pcsl_string_length(property) <= 0) { /* property not found */ result = NULL_LEN; break; } /* The class is the last item in the set. */ begin = pcsl_string_last_index_of(property, (jchar)','); if (begin < 0 || begin >= pcsl_string_length(property)) { result = NULL_LEN; break; } begin++; stat = pcsl_string_substring(property, begin, pcsl_string_length(property), &temp); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } if (pcsl_string_length(&temp) <= 0) { pcsl_string_free(&temp); result = NULL_LEN; break; } stat = pcsl_string_trim(&temp, res); pcsl_string_free(&temp); if (PCSL_STRING_OK != stat) { result = OUT_OF_MEM_LEN; break; } } while (0); midp_free_properties(&properties); return result; }
/** * Returns an array of the names of record stores owned by the * MIDlet suite. * * @param filenameBase filenameBase of the suite * @param ppNames pointer to pointer that will be filled in with names * * @return number of record store names or OUT_OF_MEM_LEN */ int rmsdb_get_record_store_list(pcsl_string* filenameBase, pcsl_string* *const ppNames) { int numberOfStores; pcsl_string root; pcsl_string* pStores; pcsl_string filename; pcsl_string ascii_name = PCSL_STRING_NULL_INITIALIZER; int i; void* handle = NULL; MIDPError status; int f_errc; pcsl_string_status s_errc; /* IMPL_NOTE: how can we get it statically? */ const int dbext_len = pcsl_string_length(&DB_EXTENSION); *ppNames = NULL; /* * IMPL_NOTE: for security reasons the record store is always * located in the internal storage. */ status = buildSuiteFilename(filenameBase, &PCSL_STRING_EMPTY, -1, &root); if (status != MIDP_ERROR_NONE) { return status; } if (pcsl_string_is_null(&root)) { return 0; } numberOfStores = rmsdb_get_number_of_record_stores_int(&root); if (numberOfStores <= 0) { pcsl_string_free(&root); return numberOfStores; } pStores = alloc_pcsl_string_list(numberOfStores); if (pStores == NULL) { pcsl_string_free(&root); return OUT_OF_MEM_LEN; } handle = storage_open_file_iterator(&root); if (!handle) { pcsl_string_free(&root); return OUT_OF_MEM_LEN; } /* the main loop */ for (i=0,f_errc=0,s_errc=0;;) { f_errc = storage_get_next_file_in_iterator(&root, handle, &filename); if (0 != f_errc) { f_errc = 0; break; } if (pcsl_string_ends_with(&filename, &DB_EXTENSION)) { s_errc = pcsl_string_substring(&filename, pcsl_string_length(&root), pcsl_string_length(&filename) - dbext_len, &ascii_name); pcsl_string_free(&filename); if (PCSL_STRING_OK != s_errc ) { break; } s_errc = pcsl_esc_extract_attached(0, &ascii_name, &pStores[i]); pcsl_string_free(&ascii_name); if (PCSL_STRING_OK != s_errc ) { break; } i++; } pcsl_string_free(&filename); /* IMPL_NOTE: do we need this one? isn't it useless? */ if (i == numberOfStores) { break; } } pcsl_string_free(&root); storageCloseFileIterator(handle); if (f_errc || s_errc) { /* The loop stopped because we ran out of memory. */ free_pcsl_string_list(pStores, i); return OUT_OF_MEM_LEN; } *ppNames = pStores; return numberOfStores; }
/** * Returns an array of the names of record stores owned by the * MIDlet suite. * * @param suiteId * @param ppNames pointer to pointer that will be filled in with names * * @return number of record store names or OUT_OF_MEM_LEN */ int rmsdb_get_record_store_list(SuiteIdType suiteId, pcsl_string* *const ppNames) { int numberOfStores; pcsl_string root; pcsl_string* pStores; pcsl_string filename; pcsl_string ascii_name = PCSL_STRING_NULL_INITIALIZER; int i; void* handle = NULL; MIDPError status; int f_errc; pcsl_string_status s_errc; StorageIdType storageId; /* IMPL_NOTE: how can we get it statically? */ const int dbext_len = pcsl_string_length(&DB_EXTENSION); *ppNames = NULL; /* * IMPL Note: here is assumed that the record store is located in the same * storage as the midlet suite. This may not be true. */ status = midp_suite_get_suite_storage(suiteId, &storageId); if (status != ALL_OK) { return OUT_OF_MEM_LEN; } status = midp_suite_get_rms_filename(suiteId, storageId, -1, &PCSL_STRING_EMPTY, &root); if (status != ALL_OK) { return OUT_OF_MEM_LEN; } if (pcsl_string_is_null(&root)) { return 0; } numberOfStores = rmsdb_get_number_of_record_stores_int(&root); if (numberOfStores <= 0) { pcsl_string_free(&root); return numberOfStores; } pStores = alloc_pcsl_string_list(numberOfStores); if (pStores == NULL) { pcsl_string_free(&root); return OUT_OF_MEM_LEN; } handle = storage_open_file_iterator(&root); if (!handle) { pcsl_string_free(&root); return OUT_OF_MEM_LEN; } /* the main loop */ for(i=0,f_errc=0,s_errc=0;;) { f_errc = storage_get_next_file_in_iterator(&root, handle, &filename); if (0 != f_errc) { f_errc = 0; break; } if (pcsl_string_ends_with(&filename, &DB_EXTENSION)) { s_errc = pcsl_string_substring(&filename, pcsl_string_length(&root), pcsl_string_length(&filename) - dbext_len, &ascii_name); pcsl_string_free(&filename); if (PCSL_STRING_OK != s_errc ) { break; } s_errc = escaped_ascii_to_unicode(&ascii_name, &pStores[i]); pcsl_string_free(&ascii_name); if (PCSL_STRING_OK != s_errc ) { break; } i++; } pcsl_string_free(&filename); /* IMPL_NOTE: do we need this one? isn't it useless? */ if (i == numberOfStores) { break; } } pcsl_string_free(&root); storageCloseFileIterator(handle); if (f_errc || s_errc) { /* The loop stopped because we ran out of memory. */ free_pcsl_string_list(pStores, i); return OUT_OF_MEM_LEN; } *ppNames = pStores; return numberOfStores; }