2 Copyright (c) 2004 KJK::Hyperion
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define WIN32_LEAN_AND_MEAN
27 #include <pseh/framebased/internal.h>
28 #include <pseh/excpt.h>
30 #include <pseh/framebased.h>
32 /* Assembly helpers, see i386/framebased.asm */
33 extern void __cdecl
_SEHCleanHandlerEnvironment(void);
34 extern void __cdecl
_SEHRegisterFrame(_SEHRegistration_t
*);
35 extern void __cdecl
_SEHUnregisterFrame(const _SEHRegistration_t
*);
36 extern void __cdecl
_SEHUnwind(_SEHPortableFrame_t
*);
38 /* Borland C++ uses a different decoration (i.e. none) for stdcall functions */
39 extern void __stdcall
RtlUnwind(void *, void *, void *, void *);
40 void const * _SEHRtlUnwind
= RtlUnwind
;
42 __declspec(noreturn
) void __cdecl
_SEHCallHandler(_SEHPortableFrame_t
* frame
)
44 frame
->SPF_Handling
= 1;
46 frame
->SPF_Handlers
->SH_Handler(frame
);
49 int __cdecl _SEHFrameHandler
51 struct _EXCEPTION_RECORD
* ExceptionRecord
,
52 void * EstablisherFrame
,
53 struct _CONTEXT
* ContextRecord
,
54 void * DispatcherContext
57 _SEHPortableFrame_t
* frame
;
59 _SEHCleanHandlerEnvironment();
61 frame
= EstablisherFrame
;
64 if(ExceptionRecord
->ExceptionFlags
& (4 | 2))
66 if(frame
->SPF_Handlers
->SH_Finally
&& !frame
->SPF_Handling
)
67 frame
->SPF_Handlers
->SH_Finally(frame
);
74 if(ExceptionRecord
->ExceptionCode
)
75 frame
->SPF_Code
= ExceptionRecord
->ExceptionCode
;
77 frame
->SPF_Code
= 0xC0000001;
79 switch((UINT_PTR
)frame
->SPF_Handlers
->SH_Filter
)
81 case (UINT_PTR
)_SEH_STATIC_FILTER(_SEH_EXECUTE_HANDLER
):
82 case (UINT_PTR
)_SEH_STATIC_FILTER(_SEH_CONTINUE_SEARCH
):
83 case (UINT_PTR
)_SEH_STATIC_FILTER(_SEH_CONTINUE_EXECUTION
):
85 ret
= (int)((UINT_PTR
)frame
->SPF_Handlers
->SH_Filter
) - 2;
91 if(frame
->SPF_Handlers
->SH_Filter
)
93 EXCEPTION_POINTERS ep
;
95 ep
.ExceptionRecord
= ExceptionRecord
;
96 ep
.ContextRecord
= ContextRecord
;
98 ret
= frame
->SPF_Handlers
->SH_Filter(&ep
, frame
);
101 ret
= _SEH_CONTINUE_SEARCH
;
107 /* _SEH_CONTINUE_EXECUTION */
109 return ExceptionContinueExecution
;
110 /* _SEH_EXECUTE_HANDLER */
112 _SEHCallHandler(frame
);
113 /* _SEH_CONTINUE_SEARCH */
118 return ExceptionContinueSearch
;
121 void __stdcall
_SEHEnter(_SEHPortableFrame_t
* frame
)
123 frame
->SPF_Registration
.SER_Handler
= _SEHFrameHandler
;
125 frame
->SPF_Handling
= 0;
126 _SEHRegisterFrame(&frame
->SPF_Registration
);
129 void __stdcall
_SEHLeave(_SEHPortableFrame_t
* frame
)
131 _SEHUnregisterFrame(&frame
->SPF_Registration
);