static void SaveButtonClick (void) { DmOpenRef openRef; UInt16 recIndex; MemHandle recH; Err err; if (EditorMidi.dbID == 0) { debugPrintf("dbID==0, create a new record in Palmano DB\n"); /* create new record in palmano DB */ err = getPalmanoDatabase(&openRef, dmModeReadWrite | dmModeExclusive); if (err != 0) { ErrAlert(err); return; } /* allocate new record in DB */ recIndex = dmMaxRecordIndex; recH = DmNewRecord(openRef, &recIndex, 5); /* initial size is 5 bytes */ ErrFatalDisplayIf(!recH, "SaveButtonClick(): can't get new record by index!"); } else { /* replace old song in-place */ openRef = DmOpenDatabase (EditorMidi.cardNo, EditorMidi.dbID, dmModeReadWrite | dmModeExclusive); ErrFatalDisplayIf(!openRef, "SaveButtonClick(): " "Can't open old song database for record"); err = DmFindRecordByID(openRef, EditorMidi.uniqueRecID, &recIndex); ErrFatalDisplayIf(err, "Can't find record by ID"); recH = DmGetRecord(openRef, recIndex); ErrFatalDisplayIf(!recH, "SaveButtonClick(): " "Can't get old song record for writing"); } // save midi to recH GetFieldTextToStr(EditorMidi.name, ID_EditorNameField, sndMidiNameLength); debugPrintf("SaveButtonClick(): smfutils_save(recH=%lx, name=%s\n", recH, EditorMidi.name); smfutils_save(recH, EditorMidi.name, ¬elist); debugPrintf("SaveButtonClick(): return from smfutils_save()\n"); DmReleaseRecord (openRef, recIndex, 1); DmCloseDatabase (openRef); FrmGotoForm(ID_MainForm); }
// Write the header void WriteHeader(void) { MemHandle headerMH = NULL; MemPtr headerMP = NULL; headerMH = DmGetRecord(JMPalmPrefs, 0); if (headerMH != NULL) headerMP = MemHandleLock(headerMH); // write new data if (headerMH != NULL) { //hd.record_count = record_count; DmWrite(headerMP, 0, &hd, sizeof(hd)); MemHandleUnlock(headerMH); DmReleaseRecord(JMPalmPrefs, 0, true); } }
// Read the header data UInt16 ReadHeader(void) { UInt16 err; MemHandle headerMH = NULL; MemPtr headerMP = NULL; headerdata* foo; err = false; headerMH = DmGetRecord(JMPalmPrefs, 0); if (headerMH != NULL) headerMP = MemHandleLock(headerMH); foo = (headerdata*)headerMP; // copy the object if (foo != NULL) { memcpy(&hd, foo, sizeof(headerdata)); // Header version incompatible? if (hd.version <= INCOMPATIBILITY_LEVEL) { FrmCustomAlert(ErrorMessageForm, "Installation error: You must"\ " delete old JMPalm versions before installing"\ " this version. Please delete and reinstall.", 0, 0); err = true; } else if (hd.version < CURRENT_SETTINGS_VERSION) { FrmCustomAlert(ErrorMessageForm, "New (incompatible) version of "\ "JMPalm installed, "\ "your settings are lost!", 0, 0); drawSplash(); ResetHeader(); } } if (headerMH != NULL) { MemHandleUnlock(headerMH); DmReleaseRecord(JMPalmPrefs, 0, true); } return err; }
static void MusicFormSave(UInt16 index) { if (index != dmMaxRecordIndex) { MemHandle recordH; GameInfoType *ogameInfoP; if (!MusicTabSave()) return; if (!AudioCDTabSave()) return; VolumeTabSave(); recordH = DmGetRecord(gameDB, index); ogameInfoP = (GameInfoType *)MemHandleLock(recordH); DmWrite(ogameInfoP, 0, gameInfoP, sizeof(GameInfoType)); MemHandleUnlock(recordH); DmReleaseRecord (gameDB, index, 0); } MemPtrFree(gameInfoP); gameInfoP = NULL; TabDeleteTabs(myTabP); FrmReturnToMain(); }
/************************************************************************* * Function: replaceAccountHash * Description: regernate the hash on a single account based on account * index * ***********************************************************************/ void replaceAccountHash(UInt16 index, DmOpenRef db, md_hash * SysPass) { Account ac; MemHandle rH; MemPtr pac = NULL, scratch = NULL, scratch2=NULL; if ((rH = DmGetRecord (db, index))) { pac = MemHandleLock (rH); if ((scratch = MemPtrNew (MemPtrSize (pac)))) { UnpackAccount (&ac, pac, scratch, SysPass, MemHandleSize (rH), true, true); if ((scratch2 = MemPtrNew (MemPtrSize (pac)))) { MemMove(ac.hash, generateAccountHash(&ac), sizeof(md_hash)); PackAccount (scratch2, ac, SysPass, true); writeRecord(scratch2, rH); } } MemPtrFree(scratch); MemPtrFree(scratch2); MemHandleUnlock (rH); DmReleaseRecord (db, index, true); } }
static void ProcessXlitDB ( UInt16 cardNo, LocalID dbID ) { UInt16 numRecords; UInt16 i; DmOpenRef dbRef; dbRef = DmOpenDatabase( cardNo, dbID, dmModeReadOnly ); if ( dbRef == NULL ) return; numRecords = DmNumRecords( dbRef ); for ( i = 0 ; i < numRecords ; i++ ) { MemHandle handle; handle = DmGetRecord( dbRef, i ); if ( handle != NULL ) { XlitEntry* entry; entry = SafeMemPtrNew( sizeof( XlitEntry ) ); entry->dbRef = dbRef; entry->index = i; entry->handle = handle; entry->header = MemHandleLock( handle ); ListAppend( xlitDataList, entry ); numXlits++; } } }
static Boolean ExamDetailsFormSave(void) { MemHandle newExam=NULL; UInt16 index = dmMaxRecordIndex; ListType *course; ControlType *course_tr, *date_tr, *time_tr; Char *room; Char empty[1]={'\0'}; FieldType *fldRoom; course = GetObjectPtr(LIST_exd_course); course_tr = GetObjectPtr(LIST_exd_course_trigger); date_tr = GetObjectPtr(SELECTOR_exd_date); time_tr = GetObjectPtr(SELECTOR_exd_time); fldRoom = GetObjectPtr(FIELD_exd_room); room = FldGetTextPtr(fldRoom); if (room == NULL) room=empty; if (gExamsLastSelRowUID == 0) { // New record newExam = DmNewRecord(DatabaseGetRefN(DB_MAIN), &index, sizeof(ExamDBRecord)); } else { // Load record DmFindRecordByID(DatabaseGetRefN(DB_MAIN), gExamsLastSelRowUID, &index); newExam = DmGetRecord(DatabaseGetRefN(DB_MAIN), index); } if (! newExam) { // Could not create entry FrmAlert(ALERT_nomem); return false; } else { UInt16 attr=0; ExamDBRecord ex, *ep; ep = (ExamDBRecord *)MemHandleLock(newExam); ex.type=TYPE_EXAM; ex.course=gExamDetailsItemIDs[LstGetSelection(course)]; ex.note = (gExamsLastSelRowUID == 0) ? 0 : ep->note; ex.date.year = gExamDetailsDate.year; ex.date.month = gExamDetailsDate.month; ex.date.day = gExamDetailsDate.day; ex.begin.hours = gExamDetailsBegin.hours; ex.begin.minutes = gExamDetailsBegin.minutes; ex.end.hours = gExamDetailsEnd.hours; ex.end.minutes = gExamDetailsEnd.minutes; ex.flags = 0x0000; StrNCopy(ex.room, room, sizeof(ex.room)); DmWrite(ep, 0, &ex, sizeof(ExamDBRecord)); MemHandleUnlock(newExam); DmReleaseRecord(DatabaseGetRef(), index, false); DmRecordInfo(DatabaseGetRef(), index, &attr, NULL, NULL); attr |= DatabaseGetCat(); DmSetRecordInfo(DatabaseGetRef(), index, &attr, NULL); DatabaseSort(); } AlarmReset(DatabaseGetRef()); return true; }
Boolean ExamsFormHandleEvent(EventPtr event) { FormPtr frm=FrmGetActiveForm(); Boolean handled = false; Boolean categoryEdited, reDraw=false; UInt16 category, numRecords; ControlType *ctl; UInt32 *recordList; if (event->eType == ctlSelectEvent) { // button handling switch (event->data.ctlSelect.controlID) { case BUTTON_ex_back: handled=true; FrmGotoForm(FORM_main); break; case BUTTON_ex_add: handled=true; if (CountCourses() > 0) { gExamsLastSelRowUID=0; FrmPopupForm(FORM_exam_details); } else { FrmAlert(ALERT_nocourses); } break; case BUTTON_ex_edit: handled=true; gExamsLastSelRowUID=TblGetRowData(GetObjectPtr(TABLE_exams), gExamsSelRow); FrmPopupForm(FORM_exam_details); break; case BUTTON_ex_note: { UInt16 index=0; handled=true; gExamsLastSelRowUID=TblGetRowData(GetObjectPtr(TABLE_exams), gExamsSelRow); DmFindRecordByID(DatabaseGetRefN(DB_MAIN), gExamsLastSelRowUID, &index); NoteSet(index, FORM_exams); FrmPopupForm(NewNoteView); break; } case BUTTON_ex_del: handled=true; gExamsLastSelRowUID=TblGetRowData(GetObjectPtr(TABLE_exams), gExamsSelRow); ExamDelete(); break; case BUTTON_ex_beam: handled=true; gExamsLastSelRowUID=TblGetRowData(GetObjectPtr(TABLE_exams), gExamsSelRow); ExamBeam(); break; case LIST_ex_cat_trigger: handled=true; category=DatabaseGetCat(); numRecords=DmNumRecordsInCategory(DatabaseGetRef(), DELETE_CATEGORY); recordList=(UInt32 *)MemPtrNew(numRecords * sizeof(UInt32)); CatPreEdit(numRecords, recordList); categoryEdited = CategorySelect(DatabaseGetRef(), frm, LIST_ex_cat_trigger, LIST_ex_cat, false, &category, gCategoryName, 0, STRING_cat_edit); // categoryDefaultEditCategoryString if (categoryEdited || (category != DatabaseGetCat())) { reDraw=true; DatabaseSetCat(category); ctl = GetObjectPtr(LIST_ex_cat_trigger); CategoryGetName(DatabaseGetRef(), category, gCategoryName); CategorySetTriggerLabel(ctl, gCategoryName); } CatPostEdit(numRecords, recordList); if (reDraw) { gExamsOffset=0; gExamsSelRow=0; FrmUpdateForm(FORM_exams, frmRedrawUpdateCode); } if (recordList != NULL) MemPtrFree((MemPtr)recordList); break; default: break; } } else if (event->eType == tblEnterEvent) { UInt16 i; if (event->data.tblEnter.column == EXCOL_DONE) { handled=false; } else if (event->data.tblEnter.column == EXCOL_NOTE) { MemHandle m; Boolean hasNote=false; gExamsSelRow=event->data.tblEnter.row; for (i=0; i < TblGetNumberOfRows(event->data.tblEnter.pTable); ++i) { RectangleType r; TblGetItemBounds(event->data.tblEnter.pTable, i, EXCOL_SELI, &r); TableDrawSelection(event->data.tblEnter.pTable, i, event->data.tblEnter.column, &r); } m = DmQueryRecord(DatabaseGetRefN(DB_MAIN), TblGetRowID(event->data.tblEnter.pTable, event->data.tblEnter.row)); if (m) { ExamDBRecord *ex = (ExamDBRecord *)MemHandleLock(m); if (ex->note) hasNote = true; else hasNote = false; MemHandleUnlock(m); } if (hasNote) { Coord newPointX, newPointY; Boolean isPenDown=false, drawn=false; RectangleType fieldBounds; IndexedColorType curForeColor, curBackColor, curTextColor; Char noteSymb[2] = { GADGET_NOTESYMBOL, 0 }; FontID oldFont; EvtGetPen(&newPointX, &newPointY, &isPenDown); TblGetItemBounds(event->data.tblEnter.pTable, event->data.tblEnter.row, EXCOL_NOTE, &fieldBounds); oldFont = FntSetFont(symbolFont); while (isPenDown){ if (! drawn && RctPtInRectangle(newPointX, newPointY, &fieldBounds)) { curForeColor = WinSetForeColor(UIColorGetTableEntryIndex(UIObjectSelectedForeground)); curBackColor = WinSetBackColor(UIColorGetTableEntryIndex(UIObjectSelectedFill)); curTextColor = WinSetTextColor(UIColorGetTableEntryIndex(UIObjectSelectedForeground)); TNDrawCharsToFitWidth(noteSymb, &fieldBounds); WinSetForeColor(curForeColor); WinSetForeColor(curBackColor); WinSetForeColor(curTextColor); drawn = true; } else if (drawn && ! RctPtInRectangle(newPointX, newPointY, &fieldBounds)) { curForeColor = WinSetForeColor(UIColorGetTableEntryIndex(UIObjectForeground)); curBackColor = WinSetBackColor(UIColorGetTableEntryIndex(UIObjectFill)); curTextColor = WinSetTextColor(UIColorGetTableEntryIndex(UIObjectForeground)); TNDrawCharsToFitWidth(noteSymb, &fieldBounds); WinSetForeColor(curForeColor); WinSetForeColor(curBackColor); WinSetForeColor(curTextColor); drawn = false; } EvtGetPen(&newPointX, &newPointY, &isPenDown); } FntSetFont(oldFont); } else { handled = true; } } else { gExamsSelRow=event->data.tblEnter.row; for (i=0; i < TblGetNumberOfRows(event->data.tblEnter.pTable); ++i) { RectangleType r; TblGetItemBounds(event->data.tblEnter.pTable, i, EXCOL_SELI, &r); TableDrawSelection(event->data.tblEnter.pTable, i, event->data.tblEnter.column, &r); } handled=true; } } else if (event->eType == tblSelectEvent) { if (event->data.tblEnter.column == EXCOL_DONE) { MemHandle mex; ExamDBRecord *ex; Boolean done=(TblGetItemInt(event->data.tblSelect.pTable, event->data.tblSelect.row, event->data.tblSelect.column) == 0) ? false : true; UInt16 flags, index=TblGetRowID(event->data.tblSelect.pTable, event->data.tblSelect.row); mex=DmGetRecord(DatabaseGetRefN(DB_MAIN), index); ex = MemHandleLock(mex); flags = ex->flags; if (done) { flags |= EX_FLAG_DONE; } else { flags &= (EX_FLAG_DONE ^ 0xFFFF); } DmWrite(ex, offsetof(ExamDBRecord, flags), &flags, sizeof(flags)); DmReleaseRecord(DatabaseGetRefN(DB_MAIN), index, false); TblMarkRowInvalid(event->data.tblSelect.pTable, event->data.tblSelect.row); TblRedrawTable(event->data.tblSelect.pTable); MemHandleUnlock(mex); } else if (event->data.tblEnter.column == EXCOL_NOTE) { ControlType *ctl=GetObjectPtr(BUTTON_ex_note); // Don't need code twice, just read ctlSelect Event for BUTTON_ex_note CtlHitControl(ctl); } handled=true; } else if (event->eType == ctlRepeatEvent) { // Repeat buttons pressed if( event->data.ctlRepeat.controlID == REPEAT_ex_up ) gExamsOffset -= 1; else gExamsOffset += 1; ExamsTableInit(); TblMarkTableInvalid(GetObjectPtr(TABLE_exams)); TblRedrawTable(GetObjectPtr(TABLE_exams)); } else if (event->eType == keyDownEvent) { // We have a hard button assigned and it was pressed if (TxtCharIsHardKey(event->data.keyDown.modifiers, event->data.keyDown.chr)) { if (! (event->data.keyDown.modifiers & poweredOnKeyMask)) { FrmGotoForm(FORM_main); handled = true; } } } else if (event->eType == menuOpenEvent) { return HandleMenuOpenEvent(event); } else if (event->eType == menuEvent) { // forwarding of menu events return HandleMenuEvent(event->data.menu.itemID); } else if (event->eType == frmOpenEvent) { // initializes and draws the form ControlType *ctl; gExamsOffset=0; ExamsTableInit(); FrmDrawForm (frm); ctl = GetObjectPtr(LIST_ex_cat_trigger); CategoryGetName (DatabaseGetRef(), DatabaseGetCat(), gCategoryName); CategorySetTriggerLabel (ctl, gCategoryName); handled = true; } else if (event->eType == frmUpdateEvent) { // redraws the form if needed gExamsOffset=0; ExamsTableInit(); FrmDrawForm(frm); handled = false; } else if (event->eType == frmCloseEvent) { // this is done if form is closed // TblEraseTable(GetObjectPtr(TABLE_exams)); // ExamsFormFree(); FrmEraseForm(frm); } return (handled); }
NCSError NCSFileRead(NCS_FILE_HANDLE hFile, void *pBuffer, UINT32 nLength, UINT32* pRead) { #ifdef WIN32 BOOLEAN bError; DWORD nBytesRead; bError = (BOOLEAN)ReadFile( hFile, // handle of file to read pBuffer, // pointer to buffer that receives data nLength, // number of bytes to read &nBytesRead, // pointer to number of bytes read NULL); // pointer to structure for data if(pRead) { *pRead = (UINT32)nBytesRead; } if( bError != 0 && nBytesRead == nLength ) return( NCS_SUCCESS ); else return( NCS_FILE_IO_ERROR ); #elif defined MACINTOSH int iOReturn; long myLength; UINT32 theOffset; BOOLEAN result; result = GetFPos((short)hFile, (long *)&theOffset); myLength = nLength; iOReturn = FSRead((short)hFile, (long *)&nLength,pBuffer); switch (iOReturn) { case noErr: return NCS_SUCCESS; break; case eofErr: return ((myLength == nLength)?NCS_FILE_IO_ERROR:NCS_SUCCESS); break; case rfNumErr: return (NCS_FILE_IO_ERROR); break; default: return NCS_FILE_IO_ERROR; break; } #elif defined PALM Err eErr; INT16 iStartRecord = hFile->iOffset / hFile->nRecordSize + 1; INT16 iEndRecord = (hFile->iOffset + nLength) / hFile->nRecordSize + 1; INT16 iRecord = hFile->iOffset / hFile->nRecordSize + 1; INT64 nRead = 0; INT32 nToRead = nLength; for(iRecord = iStartRecord; iRecord <= iEndRecord; iRecord++) { if(hFile->iRecord != iRecord) { if(hFile->hRecord) { if(hFile->pData) { MemHandleUnlock(hFile->hRecord); hFile->pData = NULL; } DmReleaseRecord(hFile->dbRef, hFile->iRecord, false); hFile->iRecord = 0; hFile->hRecord = NULL; } hFile->iRecord = iRecord; if(hFile->hRecord = DmGetRecord(hFile->dbRef, hFile->iRecord)) { hFile->pData = MemHandleLock(hFile->hRecord); if(hFile->pData == NULL) { DmReleaseRecord(hFile->dbRef, hFile->iRecord, false); } } if(!hFile->hRecord || !hFile->pData) { return(NCS_FILE_IO_ERROR); } } if(hFile->pData) { INT32 nThisRead = MIN(nToRead, hFile->nRecordSize - (hFile->iOffset % hFile->nRecordSize)); memcpy((UINT8*)pBuffer + nRead, (void*)((UINT8*)hFile->pData + hFile->iOffset % hFile->nRecordSize), nThisRead); hFile->iOffset += nThisRead; nToRead -= nThisRead; nRead += nThisRead; } } if(nRead == nLength) { *pRead = nRead; return(NCS_SUCCESS); } /* nRead = FileRead(hFile, pBuffer, (Int32)(nLength & 0x7fffffff) , 1, &eErr); *pRead = (UINT32)nRead; return(NCSPalmGetNCSError(eErr)); */ #elif defined(POSIX) int nThisRead = read(hFile, (void*)pBuffer, (unsigned int)nLength); if(pRead) { *pRead = nThisRead; } if(nThisRead == nLength) { return(NCS_SUCCESS); } else { return(NCS_FILE_IO_ERROR); } #else /* SOLARIS||IRIX */ #error ERROR: EcwReadFile() routine is not defined for this platform #endif /* WIN32 */ }
NCSError NCSFileOpen(const NCSTChar *szFilename, int iFlags, NCS_FILE_HANDLE *phFile) { #ifdef WIN32 DWORD dwMode = GENERIC_READ; DWORD dwCreate = OPEN_EXISTING; if(iFlags & NCS_FILE_READ) dwMode = GENERIC_READ; if(iFlags & NCS_FILE_READ_WRITE) dwMode = GENERIC_READ|GENERIC_WRITE; if(iFlags & NCS_FILE_CREATE) dwCreate = CREATE_ALWAYS; if(iFlags & NCS_FILE_CREATE_UNIQUE) dwCreate = CREATE_NEW; if(iFlags & NCS_FILE_APPEND) dwCreate = OPEN_ALWAYS; *phFile = CreateFile(szFilename, // file name dwMode, // Generic read mode FILE_SHARE_READ, // Let anyone access and share the file NULL, // No security info (so can't be inherited by child process) dwCreate, // File must exist to be opened FILE_FLAG_RANDOM_ACCESS, // Going to be doing lots of random access NULL); // And no template file for attributes if( *phFile == INVALID_HANDLE_VALUE ) { return( NCS_FILE_OPEN_FAILED ); } else { return( NCS_SUCCESS ); } #elif defined MACINTOSH #if __POWERPC__ int i,length, result; Str255 pascalString; FSSpec fileSpec; // We have a C string, we need a PASCAL string. length = strlen(szFilename) + 1; for(i = 1; i < length; ++i) pascalString[i] = szFilename[i - 1]; pascalString[0] = strlen(szFilename); // Create a File Specification Record, then create a File result = FSMakeFSSpec(0,0,pascalString,&fileSpec); // return is meaningless, since the only possible error doesn't effect processing in this case switch(result) { case noErr: // we could dRes pFile here, but we are the only user result =FSpOpenDF(&fileSpec, fsRdPerm, (short *)phFile); if(result) return NCS_FILE_OPEN_FAILED; else return NCS_SUCCESS; break; default: return NCS_SUCCESS; break; } #else /* __POWERPC__ */ int i,length, result; Str255 pascalString; // We have a C string, we need a PASCAL string. length = strlen(szFilename) + 1; for(i = 1; i < length; ++i) pascalString[i] = szFilename[i - 1]; pascalString[0] = strlen(szFilename); result =FSOpen(pascalString, 0, (short *)phFile); if(result) return TRUE; else return FALSE; #endif /* __POWERPC__ */ #elif defined PALM NCS_FILE_HANDLE hFile; Err eErr; UInt32 nMode = 0; if(hFile = (NCS_FILE_HANDLE)NCSMalloc(sizeof(NCS_FILE_HANDLE_STRUCT), TRUE)) { hFile->dbID = DmFindDatabase(0, szFilename); if(hFile->dbID) { Char nameP[dmDBNameLength]; UInt16 attributes; UInt16 version; UInt32 crDate; UInt32 modDate; UInt32 bckUpDate; UInt32 modNum; LocalID appInfoID; LocalID sortInfoID; UInt32 type; UInt32 creator; DmDatabaseInfo(0, hFile->dbID, nameP, &attributes, &version, &crDate, &modDate, &bckUpDate, &modNum, &appInfoID, &sortInfoID, &type, &creator); if(creator == NCS_PALM_CREATOR_ID) { if(hFile->dbRef = DmOpenDatabase(0, hFile->dbID, dmModeReadOnly|dmModeShowSecret)) { UInt32 nRecords; UInt32 nTotalBytes; UInt32 nDataBytes; eErr = DmDatabaseSize(0, hFile->dbID, &nRecords, &nTotalBytes, &nDataBytes); if(eErr == errNone) { MemHandle hRecord; hFile->nRecords = nRecords; hFile->nDBSize = nDataBytes; #ifdef NOTDEF if(hRecord = DmGetRecord(hFile->dbRef, 0)) { MemPtr pData; if(pData = MemHandleLock(hRecord)) { hFile->nRecordSize = ((UINT16*)pData)[0]; MemHandleUnlock(hRecord); } DmReleaseRecord(hFile->dbRef, 0, false); } #endif *phFile = hFile; return(NCS_SUCCESS); } DmCloseDatabase(hFile->dbRef); return(NCSPalmGetNCSError(eErr)); } } } } else { return(NCS_COULDNT_ALLOC_MEMORY); } /* if(iFlags & NCS_FILE_READ) nMode = fileModeReadOnly|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_READ_WRITE) nMode = fileModeUpdate|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_CREATE) nMode = fileModeReadWrite|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_CREATE_UNIQUE) nMode = fileModeReadWrite|fileModeDontOverwrite|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_APPEND) nMode = fileModeAppend|fileModeAnyTypeCreator; *phFile = FileOpen(0, (char*)szFilename, 0, 0, nMode, &eErr); return(NCSPalmGetNCSError(eErr)); */ #elif defined(POSIX) int flags = O_RDONLY; if(iFlags & NCS_FILE_READ) flags = O_RDONLY; if(iFlags & NCS_FILE_READ_WRITE) flags = O_RDWR; if(iFlags & NCS_FILE_CREATE) flags |= O_CREAT; if(iFlags & NCS_FILE_CREATE_UNIQUE) flags |= O_CREAT|O_EXCL; if(iFlags & NCS_FILE_APPEND) flags |= O_APPEND; #if defined SOLARIS || (defined(HPUX) && !defined(__LP64__)) // Enable 64bit! flags |= O_LARGEFILE; #endif #ifdef HPUX *phFile = open64((const char*)CHAR_STRING(szFilename), (int)flags); #ifdef NOTDEF if (*phFile < 0) { fprintf(stderr, "Error opening file : %ld\n", errno); if (errno == EOVERFLOW) { fprintf(stderr, "The named file is a regular file and the size " "of the file cannot be represented correctly in an object of " "size off_t."); } } #endif #else *phFile = open((const char*)CHAR_STRING(szFilename), (int)flags, S_IRUSR|S_IWUSR); #endif if(*phFile != -1) { return(NCS_SUCCESS); } else { return(NCS_FILE_OPEN_FAILED); } #else /* SOLARIS||IRIX */ #error ERROR EcwFileCreate() routine is not defined for this platform #endif /* WIN32 */ }
/*************************************************************************** * Function: cryptSwitch * Description: handles changing the system password based upon the * password change screen. Basically checks that current password is correct, * checks that the new password was entered correctly, then re-encrypts the * databases based upon the new password. * ************************************************************************/ static void cryptSwitch (int v) { // total number of records to re-write UInt16 totalAItems = DmNumRecordsInCategory (AccountDB, dmAllCategories); UInt16 totalSItems = DmNumRecordsInCategory (SystemDB, dmAllCategories); MemPtr pac = NULL, scratch = NULL, scratch2 = NULL; UInt16 i = 0, senc = 0, aenc = 0; MemHandle rH; char s[5], a[5]; StripPrefType prefs; UInt16 prefsSize, prefsVersion; FormType *preF = FrmGetActiveForm (); FormType *f = FrmInitForm (pleaseWait); FrmDrawForm (f); // re-encrypt the password if ((rH = DmGetRecord (PasswordDB, 0))) { if ((scratch = MemPtrNew (getSCSize(sizeof(md_hash))))) { PackPassword (scratch, &NewSysPass); writeRecord (scratch, rH); MemPtrFree (scratch); } DmReleaseRecord (PasswordDB, 0, true); } // loop through the systems and re-encrypt for (i = 0; i < totalSItems; i++) { System_old sys; if ((rH = DmGetRecord (SystemDB, i))) { pac = MemHandleLock (rH); if ((scratch = MemPtrNew (MemPtrSize (pac)))) { // decrypt the system with old password switch (v) { case 0: UnpackSystem_old (&sys, pac, scratch, SysPass, MemHandleSize (rH), true, 1); scratch2 = MemPtrNew (getSystemSize((System *)&sys, true)); break; case 1: UnpackSystem_old (&sys, pac, scratch, SysPass, MemHandleSize (rH), true, 2); scratch2 = MemPtrNew (getSystemSize ((System *)&sys,true) ); break; case 2: UnpackSystem_old (&sys, pac, scratch, SysPass, MemHandleSize (rH), true, 0); scratch2 = MemPtrNew (getSystemSize ((System *)&sys, true )); break; } if (scratch2) { PackSystem(scratch2, *((System *) &sys), &NewSysPass, true); MemHandleUnlock (rH); writeRecord (scratch2, rH); senc++; MemPtrFree (scratch2); } MemPtrFree (scratch); } DmReleaseRecord (SystemDB, i, true); } } // loop through the accounts and re-encrypt for (i = 0; i < totalAItems; i++) { Account_old ac; Account ac_new; if ((rH = DmGetRecord (AccountDB, i))) { pac = MemHandleLock (rH); if ((scratch = MemPtrNew (MemPtrSize (pac)))) { // decrypt the system with old password switch (v) { case 0: UnpackAccount_old(&ac, pac, scratch, SysPass, MemHandleSize (rH), true, true, 1); ChangeAccountFormat(i, &ac, &ac_new); scratch2 = MemPtrNew (getAccountSize(&ac_new, true)); break; case 1: UnpackAccount_old (&ac, pac, scratch, SysPass, MemHandleSize (rH), true, true, 2); ChangeAccountFormat(i, &ac, &ac_new); scratch2 = MemPtrNew (getAccountSize(&ac_new, true)); break; case 2: UnpackAccount_old(&ac, pac, scratch, SysPass, MemHandleSize (rH), true, true, 0); ChangeAccountFormat(i, &ac, &ac_new); scratch2 = MemPtrNew (getAccountSize(&ac_new,true)); break; } if (scratch2) { PackAccount(scratch2, ac_new, &NewSysPass, true); MemHandleUnlock (rH); writeRecord (scratch2, rH); aenc++; MemPtrFree (scratch2); } MemPtrFree (scratch); } DmReleaseRecord (AccountDB, i, true); } } FrmEraseForm (f); FrmDeleteForm (f); FrmSetActiveForm (preF); // close databases. DmCloseDatabase (SystemDB); DmCloseDatabase (AccountDB); DmCloseDatabase (PasswordDB); { UInt16 cardNo; UInt32 type; LocalID dbID; DmSearchStateType search; type = systemDBType; DmGetNextDatabaseByTypeCreator(true, &search, systemDBTypeOld, StripCreator, true, &cardNo, &dbID); DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &type, NULL); type = accountDBType; DmGetNextDatabaseByTypeCreator(true, &search, accountDBTypeOld, StripCreator, true, &cardNo, &dbID); DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &type, NULL); type = passwordDBType; DmGetNextDatabaseByTypeCreator(true, &search, passwordDBTypeOld, StripCreator, true, &cardNo, &dbID); DmSetDatabaseInfo(cardNo, dbID, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &type, NULL); } prefsSize = sizeof (StripPrefType); prefsVersion = PrefGetAppPreferences (StripCreator, StripPrefID, &prefs, &prefsSize, true); if (prefsVersion != StripVersionNumber) { prefs.smart_beaming = false; PrefSetAppPreferences (StripCreator, StripPrefID, StripVersionNumber, &prefs, sizeof (StripPrefType), true); prefsVersion = PrefGetAppPreferences (StripCreator, StripPrefID, &prefs, &prefsSize, true); } StrIToA (s, senc); StrIToA (a, aenc); FrmCustomAlert (infoDialog, s, a, NULL); StopApplication (); SysReset (); }
// Save preferences previously set via ErrSet*() calls to a database. // If something goes wrong, returns an error // Possible errors: // memErrNotEnoughSpace - not enough memory to allocate needed structures // errors from Dm*() calls Err PrefsStoreWriter::ErrSavePreferences() { Err err = errNone; long blobSize; void * prefsBlob = SerializeItems(_items, _itemsCount, &blobSize); if ( NULL == prefsBlob ) return memErrNotEnoughSpace; DmOpenRef db = DmOpenDatabaseByTypeCreator(_dbType, _dbCreator, dmModeReadWrite); if (!db) { err = DmCreateDatabase(0, _dbName, _dbCreator, _dbType, false); if ( err) return err; db = DmOpenDatabaseByTypeCreator(_dbType, _dbCreator, dmModeReadWrite); if (!db) return DmGetLastErr(); } // set backup bit on the database. code adapted from DataStore.cpp // DataStore::open() if (errNone == err) { LocalID localId; UInt16 cardNo; UInt16 attribs; err = DmOpenDatabaseInfo(db, &localId, NULL, NULL, &cardNo, NULL); if (errNone != err) goto Continue; err = DmDatabaseInfo(cardNo, localId, NULL, &attribs, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (errNone != err) goto Continue; if (0 != attribs & dmHdrAttrBackup) goto Continue; attribs |= dmHdrAttrBackup; err = DmSetDatabaseInfo(cardNo, localId, NULL, &attribs, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); Continue: err = errNone; } UInt16 recNo = 0; UInt16 recsCount = DmNumRecords(db); MemHandle recHandle; Boolean fRecordBusy = false; Boolean fRecFound = false; void * recData; long recSize; while (recNo < recsCount) { recHandle = DmGetRecord(db, recNo); fRecordBusy = true; recData = MemHandleLock(recHandle); recSize = MemHandleSize(recHandle); if (IsValidPrefRecord(recData)) { fRecFound = true; break; } MemPtrUnlock(recData); DmReleaseRecord(db, recNo, true); fRecordBusy = false; ++recNo; } if (fRecFound && blobSize>recSize) { /* need to resize the record */ MemPtrUnlock(recData); DmReleaseRecord(db,recNo,true); fRecordBusy = false; recHandle = DmResizeRecord(db, recNo, blobSize); if ( NULL == recHandle ) return DmGetLastErr(); recData = MemHandleLock(recHandle); Assert( MemHandleSize(recHandle) == blobSize ); } if (!fRecFound) { recNo = 0; recHandle = DmNewRecord(db, &recNo, blobSize); if (!recHandle) { err = DmGetLastErr(); goto CloseDbExit; } recData = MemHandleLock(recHandle); fRecordBusy = true; } err = DmWrite(recData, 0, prefsBlob, blobSize); MemPtrUnlock(recData); if (fRecordBusy) DmReleaseRecord(db, recNo, true); CloseDbExit: // if had error before - preserve that error // otherwise return error code from DmCloseDatabase() if (err) DmCloseDatabase(db); else err = DmCloseDatabase(db); new_free( prefsBlob ); return err; }