X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/d082ac49971a1c1a9e84c5b201c30aab675f154f..b537dd42c1da03890c832e94a4f47369ca3d99af:/mac/macstore.c diff --git a/mac/macstore.c b/mac/macstore.c index fa6493a0..75c88763 100644 --- a/mac/macstore.c +++ b/mac/macstore.c @@ -1,4 +1,4 @@ -/* $Id: macstore.c,v 1.1 2002/11/19 02:13:46 ben Exp $ */ +/* $Id: macstore.c,v 1.11 2003/01/18 20:09:21 ben Exp $ */ /* * macstore.c: Macintosh-specific impementation of the interface @@ -11,16 +11,15 @@ #include #include +#include #include #include "putty.h" #include "storage.h" +#include "mac.h" +#include "macresid.h" -#define PUTTY_CREATOR FOUR_CHAR_CODE('pTTY') -#define SESS_TYPE FOUR_CHAR_CODE('Sess') - -OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID); OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit); /* @@ -28,11 +27,12 @@ OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit); * preferences folder. Each (key,value) pair is stored as a resource. */ -OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) { +OSErr get_putty_dir(Boolean makeit, short *pVRefNum, long *pDirID) +{ OSErr error = noErr; short prefVRefNum; - FSSpec puttydir, sessdir; - long prefDirID, puttyDirID, sessDirID; + FSSpec puttydir; + long prefDirID, puttyDirID; error = FindFolder(kOnSystemDisk, kPreferencesFolderType, makeit, &prefVRefNum, &prefDirID); @@ -43,13 +43,28 @@ OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) { error = FSpGetDirID(&puttydir, &puttyDirID, makeit); if (error != noErr) goto out; - error = FSMakeFSSpec(prefVRefNum, puttyDirID, "\pSaved Sessions", + *pVRefNum = prefVRefNum; + *pDirID = puttyDirID; + + out: + return error; +} + +OSErr get_session_dir(Boolean makeit, short *pVRefNum, long *pDirID) { + OSErr error = noErr; + short puttyVRefNum; + FSSpec sessdir; + long puttyDirID, sessDirID; + + error = get_putty_dir(makeit, &puttyVRefNum, &puttyDirID); + if (error != noErr) goto out; + error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pSaved Sessions", &sessdir); if (error != noErr && error != fnfErr) goto out; error = FSpGetDirID(&sessdir, &sessDirID, makeit); if (error != noErr) goto out; - *pVRefNum = prefVRefNum; + *pVRefNum = puttyVRefNum; *pDirID = sessDirID; out: @@ -78,38 +93,71 @@ OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) { return error; } +/* Copy a resource into the current resource file */ +static OSErr copy_resource(ResType restype, short resid) +{ + Handle h; + Str255 resname; + + fprintf(stderr, "getting resource %x, id %d\n", restype, resid); + h = GetResource(restype, resid); + if (h != NULL) { + GetResInfo(h, &resid, &restype, resname); + DetachResource(h); + AddResource(h, restype, resid, resname); + if (ResError() == noErr) + WriteResource(h); + } + fprintf(stderr, "ResError() == %d\n", ResError()); + return ResError(); +} + struct write_settings { int fd; FSSpec tmpfile; FSSpec dstfile; }; -void *open_settings_w(char *sessionname) { - short sessVRefNum, tmpVRefNum; - long sessDirID, tmpDirID; - FSSpec sessfile; +void *open_settings_w(char const *sessionname) { + short sessVRefNum; + long sessDirID; OSErr error; Str255 psessionname; - struct write_settings *ws; + FSSpec dstfile; - ws = safemalloc(sizeof *ws); error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID); - if (error != noErr) goto out; + if (error != noErr) return NULL; c2pstrcpy(psessionname, sessionname); - error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &ws->dstfile); - if (error != noErr && error != fnfErr) goto out; + error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile); if (error == fnfErr) { - FSpCreateResFile(&ws->dstfile, PUTTY_CREATOR, SESS_TYPE, - smSystemScript); - if ((error = ResError()) != noErr) goto out; - } + FSpCreateResFile(&dstfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript); + if ((error = ResError()) != noErr) return NULL; + } else if (error != noErr) return NULL; + + return open_settings_w_fsp(&dstfile); +} + +/* + * NB: Destination file must exist. + */ +void *open_settings_w_fsp(FSSpec *dstfile) +{ + short tmpVRefNum; + long tmpDirID; + struct write_settings *ws; + OSErr error; + Str255 tmpname; + + ws = smalloc(sizeof *ws); + ws->dstfile = *dstfile; /* Create a temporary file to save to first. */ - error = FindFolder(sessVRefNum, kTemporaryFolderType, kCreateFolder, - &tmpVRefNum, &tmpDirID); + error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType, + kCreateFolder, &tmpVRefNum, &tmpDirID); if (error != noErr) goto out; - error = FSMakeFSSpec(tmpVRefNum, tmpDirID, psessionname, &ws->tmpfile); + c2pstrcpy(tmpname, tmpnam(NULL)); + error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile); if (error != noErr && error != fnfErr) goto out; if (error == noErr) { error = FSpDelete(&ws->tmpfile); @@ -121,6 +169,10 @@ void *open_settings_w(char *sessionname) { ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm); if (ws->fd == -1) {error = ResError(); goto out;} + /* Set up standard resources. Doesn't matter if these fail. */ + copy_resource('STR ', -16396); + copy_resource('TMPL', TMPL_Int); + return ws; out: @@ -128,7 +180,7 @@ void *open_settings_w(char *sessionname) { fatalbox("Failed to open session for write (%d)", error); } -void write_setting_s(void *handle, char *key, char *value) { +void write_setting_s(void *handle, char const *key, char const *value) { int fd = *(int *)handle; Handle h; int id; @@ -150,7 +202,7 @@ void write_setting_s(void *handle, char *key, char *value) { fatalbox("Failed to add resource %s (%d)", key, ResError()); } -void write_setting_i(void *handle, char *key, int value) { +void write_setting_i(void *handle, char const *key, int value) { int fd = *(int *)handle; Handle h; int id; @@ -192,21 +244,32 @@ void close_settings_w(void *handle) { safefree(handle); } -void *open_settings_r(char *sessionname) { +void *open_settings_r(char const *sessionname) +{ short sessVRefNum; long sessDirID; FSSpec sessfile; OSErr error; Str255 psessionname; - int fd; - int *handle; error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); c2pstrcpy(psessionname, sessionname); error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); if (error != noErr) goto out; - fd = FSpOpenResFile(&sessfile, fsRdPerm); + return open_settings_r_fsp(&sessfile); + + out: + return NULL; +} + +void *open_settings_r_fsp(FSSpec *sessfile) +{ + OSErr error; + int fd; + int *handle; + + fd = FSpOpenResFile(sessfile, fsRdPerm); if (fd == 0) {error = ResError(); goto out;} handle = safemalloc(sizeof *handle); @@ -217,11 +280,10 @@ void *open_settings_r(char *sessionname) { return NULL; } - -char *read_setting_s(void *handle, char *key, char *buffer, int buflen) { +char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) { int fd; Handle h; - OSErr error; + size_t len; if (handle == NULL) goto out; fd = *(int *)handle; @@ -230,8 +292,11 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen) { h = get1namedresource(FOUR_CHAR_CODE('TEXT'), key); if (h == NULL) goto out; - if (GetHandleSize(h) > buflen) goto out; - p2cstrcpy(buffer, (StringPtr)*h); + len = GetHandleSize(h); + if (len + 1 > buflen) goto out; + memcpy(buffer, *h, len); + buffer[len] = '\0'; + ReleaseResource(h); if (ResError() != noErr) goto out; return buffer; @@ -240,10 +305,9 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen) { return NULL; } -int read_setting_i(void *handle, char *key, int defvalue) { +int read_setting_i(void *handle, char const *key, int defvalue) { int fd; Handle h; - OSErr error; int value; if (handle == NULL) goto out; @@ -272,7 +336,7 @@ void close_settings_r(void *handle) { safefree(handle); } -void del_settings(char *sessionname) { +void del_settings(char const *sessionname) { OSErr error; FSSpec sessfile; short sessVRefNum; @@ -339,6 +403,63 @@ void enum_settings_finish(void *handle) { safefree(handle); } +#define SEED_SIZE 512 + +void read_random_seed(noise_consumer_t consumer) +{ + short puttyVRefNum; + long puttyDirID; + OSErr error; + char buf[SEED_SIZE]; + short refnum; + long count = SEED_SIZE; + + if (get_putty_dir(kDontCreateFolder, &puttyVRefNum, &puttyDirID) != noErr) + return; + if (HOpenDF(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", fsRdPerm, + &refnum) != noErr) + return; + error = FSRead(refnum, &count, buf); + if (error != noErr && error != eofErr) + return; + (*consumer)(buf, count); + FSClose(refnum); +} + +/* + * We don't bother with the usual FSpExchangeFiles dance here because + * it doesn't really matter if the old random seed gets lost. + */ +void write_random_seed(void *data, int len) +{ + short puttyVRefNum; + long puttyDirID; + OSErr error; + FSSpec dstfile; + short refnum; + long count = len; + + if (get_putty_dir(kCreateFolder, &puttyVRefNum, &puttyDirID) != noErr) + return; + + error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed", + &dstfile); + if (error == fnfErr) { + /* Set up standard resources */ + FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman); + refnum = FSpOpenResFile(&dstfile, fsWrPerm); + if (ResError() == noErr) { + copy_resource('STR ', -16397); + CloseResFile(refnum); + } + } else if (error != noErr) return; + + if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return; + FSWrite(refnum, &count, data); + FSClose(refnum); + + return; +} /* * Emacs magic: