Added license text (MIT license) to the SEH library, added the header files
[reactos.git] / reactos / lib / pseh / framebased.c
1 /*
2 Copyright (c) 2004 KJK::Hyperion
3
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:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
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
20 SOFTWARE.
21 */
22
23 #define STRICT
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26
27 #include <pseh/framebased/internal.h>
28 #include <excpt.h>
29
30 /* Assembly helpers, see i386/framebased.asm */
31 extern void __cdecl _SEHCleanHandlerEnvironment(void);
32 extern void __cdecl _SEHRegisterFrame(_SEHRegistration_t *);
33 extern void __cdecl _SEHUnregisterFrame(const _SEHRegistration_t *);
34 extern void __cdecl _SEHUnwind(_SEHPortableFrame_t *);
35
36 __declspec(noreturn) void __cdecl _SEHCallHandler(_SEHPortableFrame_t * frame)
37 {
38 frame->SPF_Handling = 1;
39 _SEHUnwind(frame);
40 frame->SPF_Handlers->SH_Handler(frame);
41 }
42
43 int __cdecl _SEHFrameHandler
44 (
45 struct _EXCEPTION_RECORD * ExceptionRecord,
46 void * EstablisherFrame,
47 struct _CONTEXT * ContextRecord,
48 void * DispatcherContext
49 )
50 {
51 _SEHPortableFrame_t * frame;
52
53 _SEHCleanHandlerEnvironment();
54
55 frame = EstablisherFrame;
56
57 /* Unwinding */
58 if(ExceptionRecord->ExceptionFlags & (4 | 2))
59 {
60 if(frame->SPF_Handlers->SH_Finally && !frame->SPF_Handling)
61 frame->SPF_Handlers->SH_Finally(frame);
62 }
63 /* Handling */
64 else
65 {
66 if(ExceptionRecord->ExceptionCode)
67 frame->SPF_Code = ExceptionRecord->ExceptionCode;
68 else
69 frame->SPF_Code = 0xC0000001;
70
71 if(frame->SPF_Handlers->SH_Filter)
72 {
73 int ret;
74
75 switch((UINT_PTR)frame->SPF_Handlers->SH_Filter)
76 {
77 case EXCEPTION_EXECUTE_HANDLER + 1:
78 case EXCEPTION_CONTINUE_SEARCH + 1:
79 case EXCEPTION_CONTINUE_EXECUTION + 1:
80 {
81 ret = (int)((UINT_PTR)frame->SPF_Handlers->SH_Filter) - 1;
82 break;
83 }
84
85 default:
86 {
87 EXCEPTION_POINTERS ep;
88
89 ep.ExceptionRecord = ExceptionRecord;
90 ep.ContextRecord = ContextRecord;
91
92 ret = frame->SPF_Handlers->SH_Filter(&ep, frame);
93 break;
94 }
95 }
96
97 /* EXCEPTION_CONTINUE_EXECUTION */
98 if(ret < 0)
99 return ExceptionContinueExecution;
100 /* EXCEPTION_EXECUTE_HANDLER */
101 else if(ret > 0)
102 _SEHCallHandler(frame);
103 /* EXCEPTION_CONTINUE_SEARCH */
104 else
105 /* fall through */;
106 }
107 }
108
109 return ExceptionContinueSearch;
110 }
111
112 void __stdcall _SEHEnter(_SEHPortableFrame_t * frame)
113 {
114 frame->SPF_Registration.SER_Handler = _SEHFrameHandler;
115 frame->SPF_Code = 0;
116 frame->SPF_Handling = 0;
117 _SEHRegisterFrame(&frame->SPF_Registration);
118 }
119
120 void __stdcall _SEHLeave(_SEHPortableFrame_t * frame)
121 {
122 _SEHUnregisterFrame(&frame->SPF_Registration);
123 }
124
125 /* EOF */