Copy w32api from trunk
[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 <pseh/excpt.h>
29 #include <excpt.h>
30 #include <pseh/framebased.h>
31
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 *);
37
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;
41
42 __declspec(noreturn) void __cdecl _SEHCallHandler(_SEHPortableFrame_t * frame)
43 {
44 frame->SPF_Handling = 1;
45 _SEHUnwind(frame);
46 frame->SPF_Handlers->SH_Handler(frame);
47 }
48
49 int __cdecl _SEHFrameHandler
50 (
51 struct _EXCEPTION_RECORD * ExceptionRecord,
52 void * EstablisherFrame,
53 struct _CONTEXT * ContextRecord,
54 void * DispatcherContext
55 )
56 {
57 _SEHPortableFrame_t * frame;
58
59 _SEHCleanHandlerEnvironment();
60
61 frame = EstablisherFrame;
62
63 /* Unwinding */
64 if(ExceptionRecord->ExceptionFlags & (4 | 2))
65 {
66 if(frame->SPF_Handlers->SH_Finally && !frame->SPF_Handling)
67 frame->SPF_Handlers->SH_Finally(frame);
68 }
69 /* Handling */
70 else
71 {
72 int ret;
73
74 if(ExceptionRecord->ExceptionCode)
75 frame->SPF_Code = ExceptionRecord->ExceptionCode;
76 else
77 frame->SPF_Code = 0xC0000001;
78
79 switch((UINT_PTR)frame->SPF_Handlers->SH_Filter)
80 {
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):
84 {
85 ret = (int)((UINT_PTR)frame->SPF_Handlers->SH_Filter) - 2;
86 break;
87 }
88
89 default:
90 {
91 if(frame->SPF_Handlers->SH_Filter)
92 {
93 EXCEPTION_POINTERS ep;
94
95 ep.ExceptionRecord = ExceptionRecord;
96 ep.ContextRecord = ContextRecord;
97
98 ret = frame->SPF_Handlers->SH_Filter(&ep, frame);
99 }
100 else
101 ret = _SEH_CONTINUE_SEARCH;
102
103 break;
104 }
105 }
106
107 /* _SEH_CONTINUE_EXECUTION */
108 if(ret < 0)
109 return ExceptionContinueExecution;
110 /* _SEH_EXECUTE_HANDLER */
111 else if(ret > 0)
112 _SEHCallHandler(frame);
113 /* _SEH_CONTINUE_SEARCH */
114 else
115 /* fall through */;
116 }
117
118 return ExceptionContinueSearch;
119 }
120
121 void __stdcall _SEHEnter(_SEHPortableFrame_t * frame)
122 {
123 frame->SPF_Registration.SER_Handler = _SEHFrameHandler;
124 frame->SPF_Code = 0;
125 frame->SPF_Handling = 0;
126 _SEHRegisterFrame(&frame->SPF_Registration);
127 }
128
129 void __stdcall _SEHLeave(_SEHPortableFrame_t * frame)
130 {
131 _SEHUnregisterFrame(&frame->SPF_Registration);
132 }
133
134 /* EOF */