#include"wsh_shell_session.h"#if WSH_SHELL_SESSIONstaticWshShell_U32_tWshShellSession_CalcHash(constWshShellSession_t*pcSession){returnWshShellMisc_CalcJenkinsHash((constWshShell_U8_t*)&pcSession->RebootsLeft,sizeof(*pcSession)-sizeof(pcSession->Hash));}WshShell_Bool_tWshShellSession_IsValid(constWshShellSession_t*pcSession){if(!pcSession)returnfalse;return(pcSession->Hash==WshShellSession_CalcHash(pcSession))&&(pcSession->RebootsLeft>0);}WshShellSession_tWshShellSession_Read(WshShellSessionIO_t*pSessIO){WSH_SHELL_ASSERT(pSessIO&&pSessIO->Read);if(!pSessIO||!pSessIO->Read)return(WshShellSession_t){0};returnpSessIO->Read();}voidWshShellSession_Store(WshShellSessionIO_t*pSessIO,WshShellSession_tsession){WSH_SHELL_ASSERT(pSessIO&&pSessIO->Write);if(!pSessIO||!pSessIO->Write)return;session.Hash=WshShellSession_CalcHash(&session);pSessIO->Write(session);}voidWshShellSession_Clear(WshShellSessionIO_t*pSessIO){WshShellSession_Store(pSessIO,(WshShellSession_t){0});}voidWshShellSession_Init(WshShellSessionIO_t*pSessIO,WshShellSession_ReadHandler_treadFn,WshShellSession_WriteHandler_twriteFn){WSH_SHELL_ASSERT(pSessIO&&readFn&&writeFn);if(!pSessIO||!readFn||!writeFn)return;pSessIO->Read=readFn;pSessIO->Write=writeFn;/* Discard uninitialised/corrupt backing store so it is never restored. */WshShellSession_tstored=pSessIO->Read();if(!WshShellSession_IsValid(&stored))WshShellSession_Clear(pSessIO);}#else /* WSH_SHELL_SESSION */voidWshShellSession_Init(WshShellSessionIO_t*pSessIO,WshShellSession_ReadHandler_treadFn,WshShellSession_WriteHandler_twriteFn){(void)(pSessIO);(void)(readFn);(void)(writeFn);}WshShellSession_tWshShellSession_Read(WshShellSessionIO_t*pSessIO){(void)(pSessIO);return(WshShellSession_t){0};}voidWshShellSession_Store(WshShellSessionIO_t*pSessIO,WshShellSession_tsession){(void)(pSessIO);(void)(session);}WshShell_Bool_tWshShellSession_IsValid(constWshShellSession_t*pcSession){(void)(pcSession);returnfalse;}voidWshShellSession_Clear(WshShellSessionIO_t*pSessIO){(void)(pSessIO);}#endif /* WSH_SHELL_SESSION */