📄 epocstore.cpp
字号:
} state->iSettings = settings; // Open settings file CLineReader *reader = CLineReader::NewLC(namePtr, storeStatics->iFs); // Check that the file starts with the correct ID TPtrC8 &line = reader->ReadLineL(); if ( line.Compare(KSettingsId) ) { User::Leave(KErrCorrupt); } // Read each setting line.Set(reader->ReadLineL()); while ( line.Length() > 0 ) { // Ignore comments and empty lines if ( (line.Length() == 0) || (line.Locate(';') == 0) ) { line.Set(reader->ReadLineL()); continue; } // Parse and separate into name and value TInt eqpos = line.Locate('='); if ( (eqpos == KErrNotFound) || (eqpos == 0) || (eqpos > (line.Length() - 1)) ) { User::Leave(KErrCorrupt); } TPtrC8 name(line.Left(eqpos)); TPtrC8 val(line.Right(line.Length() - eqpos - 1)); // Create a new TSetting object with this data and insert to the tree TSetting *set = new (ELeave) TSetting; set->iName = HBufC8::NewL(name.Length()); *set->iName = name; set->iName->Des().Trim(); set->iValue = HBufC8::NewL(val.Length()); *set->iValue = val; set->iValue->Des().Trim(); if ( add234(settings, set) != (void*) set ) { User::Leave(KErrGeneral); } line.Set(reader->ReadLineL()); } CleanupStack::PopAndDestroy(2); // reader, buf} void *open_settings_r(const char *sessionname) { if ( sessionname == NULL ) return NULL; TSettingsReadState *state = snew(TSettingsReadState); if ( !state ) { return NULL; } state->iSettings = NULL; TRAPD(error, open_settings_r_L(state, sessionname)); if ( error != KErrNone ) { if ( state->iSettings != NULL ) { FreeSettingsTree(state->iSettings); state->iSettings = NULL; } sfree(state); return NULL; } return (void*) state;}char *read_setting_s(void *handle, const char *key, char *buffer, int buflen) { if ( handle == NULL ) { return NULL; } TSettingsReadState *state = (TSettingsReadState*) handle; // Find the setting from the settings tree TSetting *s = (TSetting*) find234(state->iSettings, (void*)key, cmp_name_setting); if ( !s ) { return NULL; } // Copy data to the target buffer and return it TInt len = s->iValue->Length(); if ( buflen < (len+1) ) { return NULL; } Mem::Copy(buffer, s->iValue->Ptr(), len); buffer[len] = 0; return buffer;} int read_setting_i(void *handle, const char *key, int defvalue) { if ( handle == NULL ) { return defvalue; } TSettingsReadState *state = (TSettingsReadState*) handle; // Find the setting from the settings tree TSetting *s = (TSetting*) find234(state->iSettings, (void*)key, cmp_name_setting); if ( !s ) { return defvalue; } // Convert to an integer and return it TLex8 lexx(*s->iValue); TInt val; TInt err = lexx.Val(val); if ( err != KErrNone ) { return defvalue; } return val;}int read_setting_fontspec(void *handle, const char *name, FontSpec *result){ FontSpec ret; if (!read_setting_s(handle, name, ret.name, sizeof(ret.name))) return 0; *result = ret; return 1;}void write_setting_fontspec(void *handle, const char *name, FontSpec font){ write_setting_s(handle, name, font.name);}int read_setting_filename(void *handle, const char *name, Filename *result){ return !!read_setting_s(handle, name, result->path, sizeof(result->path));}void write_setting_filename(void *handle, const char *name, Filename result){ write_setting_s(handle, name, result.path);}void close_settings_r(void *handle) { if ( handle == NULL ) { return; } TSettingsReadState *state = (TSettingsReadState*) handle; if ( state->iSettings ) { FreeSettingsTree(state->iSettings); state->iSettings = NULL; } sfree(state);}static void FreeSettingsTree(tree234 *aSettings) { TInt i; TSetting *s; for ( i = 0; (s = (TSetting*) index234(aSettings, i)) != NULL; i++) { delete s->iName; delete s->iValue; delete s; } freetree234(aSettings);}void del_settings(const char * /*sessionname*/){}struct enumsettings { int i;};void *enum_settings_start(void){ return NULL;}char *enum_settings_next(void * /*handle*/, char * /*buffer*/, int /*buflen*/){ return NULL;}void enum_settings_finish(void * /*handle*/){}/* * Returns: * 2, if key is different in db * 1, if key does not exist in db * 0, if key matched OK in db */int VerifyHostKeyL(const char *hostname, int port, const char *keytype, const char *key) { // Construct host key file name and check if it exists. TFileName fileName = storeStatics->iDataPath; fileName.Append(KHostKeysFile); if ( !BaflUtils::FileExists(storeStatics->iFs, fileName) ) { return 1; // not in database } // Open database file CLineReader *reader = CLineReader::NewLC(fileName, storeStatics->iFs); // Construct key ID and key descriptors HBufC8 *keyId = HBufC8::NewLC(strlen(hostname) + strlen(keytype) + 12); _LIT8(KKeyFormat, "%s:%d:%s"); keyId->Des().Format(KKeyFormat, hostname, port, keytype); TPtrC8 keyDes((TUint8*) key, strlen(key)); // Go through the file, looking for this key TInt result = -1; while ( result == -1 ) { TPtrC8 &line = reader->ReadLineL(); if ( line.Length() == 0 ) { result = 1; // not in database } else { // Check if the line starts with the key ID int firstSpace = line.Locate(' '); if ( firstSpace == KErrNotFound ) { continue; } if ( line.Left(firstSpace).Compare(*keyId) == 0 ) { // ID matches. Check if they key matches too TInt keyLen = line.Length() - firstSpace - 1; if ( (keyLen > 0 ) && line.Right(keyLen).Compare(keyDes) == 0 ) { result = 0; // key matched OK } else { result = 2; // key is different in database } } } } CleanupStack::PopAndDestroy(2); // keyid, reader return result;}int verify_host_key(const char *hostname, int port, const char *keytype, const char *key) { TInt res = 0; TRAPD(error, res = VerifyHostKeyL(hostname, port, keytype, key)); if ( error != KErrNone ) { fatalbox("verify_host_key: Error %d", error); } return res;}void StoreHostKeyL(const char *hostname, int port, const char *keytype, const char *key) { _LIT8(KCRLF, "\r\n"); // Check if the file exists. If not, create it. Determine full file name. TBool haveHostFile = EFalse; TFileName fileName = storeStatics->iDataPath; fileName.Append(KHostKeysFile); if ( !BaflUtils::FileExists(storeStatics->iFs, fileName) ) { storeStatics->iFs.MkDirAll(storeStatics->iDataPath); } else { haveHostFile = ETrue; } // Build the new key database entry and key ID HBufC8 *newKey = HBufC8::NewLC(strlen(hostname)+strlen(keytype)+strlen(key)+14); _LIT8(KKeyFormat, "%s:%d:%s %s"); newKey->Des().Format(KKeyFormat, hostname, port, keytype, key); HBufC8 *keyId = HBufC8::NewLC(strlen(hostname) + strlen(keytype) + 12); _LIT8(KIdFormat, "%s:%d:%s"); keyId->Des().Format(KIdFormat, hostname, port, keytype); // We'll go through the key database file, copy all other keys to a // temporary file, and replace the key for this host/port/type with a new // one. If it doesn't exist, we'll append it to the end. RFile tempFile; TFileName tempFileName; tempFileName = storeStatics->iDataPath; tempFileName.Append(KTmpHostKeysFile); User::LeaveIfError(tempFile.Replace(storeStatics->iFs, tempFileName, EFileWrite | EFileShareExclusive)); CleanupClosePushL(tempFile); TBool keyWritten = EFalse; if ( haveHostFile ) { CLineReader *reader = CLineReader::NewLC(fileName, storeStatics->iFs); TBool atEof = EFalse; while ( !atEof ) { TPtrC8 &line = reader->ReadLineL(); if ( line.Length() == 0 ) { // EOF atEof = ETrue; break; } // Check if the line starts with the key ID int firstSpace = line.Locate(' '); if ( (firstSpace != KErrNotFound) && (line.Left(firstSpace).Compare(*keyId) == 0) ) { // Yes, we'll replace this line with the new key User::LeaveIfError(tempFile.Write(*newKey)); User::LeaveIfError(tempFile.Write(KCRLF)); keyWritten = ETrue; } else { // No, just write this line back User::LeaveIfError(tempFile.Write(line)); User::LeaveIfError(tempFile.Write(KCRLF)); } } CleanupStack::PopAndDestroy(); // reader } // If we didn't manage to write the key yet, do it now if ( !keyWritten ) { User::LeaveIfError(tempFile.Write(*newKey)); User::LeaveIfError(tempFile.Write(KCRLF)); } CleanupStack::PopAndDestroy(); // tempFile; // Replace the old key file with the new one User::LeaveIfError(storeStatics->iFs.Replace(tempFileName, fileName)); // Done CleanupStack::PopAndDestroy(2); // keyId, newkey}void store_host_key(const char *hostname, int port, const char *keytype, const char *key) { TRAPD(error, StoreHostKeyL(hostname, port, keytype, key)); if ( error != KErrNone ) { fatalbox("store_host_key: Error %d", error); }}void read_random_seed(noise_consumer_t consumer){ RFile f; TBuf8<200> buf; TFileName fileName = storeStatics->iDataPath; fileName.Append(KRandomFile); if ( !BaflUtils::FileExists(storeStatics->iFs, fileName) ) { return; } User::LeaveIfError(f.Open(storeStatics->iFs, fileName, EFileRead)); while(f.Read(buf)==KErrNone) { if (buf.Length()==0) break; consumer((void *)buf.Ptr(), buf.Length()); } f.Close();}void write_random_seed(void *data, int len){ RFile f; TPtrC8 d((unsigned char *)data,len); TFileName fileName = storeStatics->iDataPath; fileName.Append(KRandomFile); if ( !BaflUtils::FileExists(storeStatics->iFs, fileName) ) { storeStatics->iFs.MkDirAll(storeStatics->iDataPath); } User::LeaveIfError(f.Replace(storeStatics->iFs, fileName, EFileWrite)); User::LeaveIfError(f.Write(d)); User::LeaveIfError(f.Flush()); f.Close();}void cleanup_all(void){}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -