fix :cirrus_vga fix OOB read case qemu Segmentation fault
[qemu.git] / hw / display / cirrus_vga_rop.h
1 /*
2 * QEMU Cirrus CLGD 54xx VGA Emulator.
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 static inline void glue(rop_8_,ROP_NAME)(uint8_t *dst, uint8_t src)
26 {
27 *dst = ROP_FN(*dst, src);
28 }
29
30 static inline void glue(rop_16_,ROP_NAME)(uint16_t *dst, uint16_t src)
31 {
32 *dst = ROP_FN(*dst, src);
33 }
34
35 static inline void glue(rop_32_,ROP_NAME)(uint32_t *dst, uint32_t src)
36 {
37 *dst = ROP_FN(*dst, src);
38 }
39
40 #define ROP_OP(d, s) glue(rop_8_,ROP_NAME)(d, s)
41 #define ROP_OP_16(d, s) glue(rop_16_,ROP_NAME)(d, s)
42 #define ROP_OP_32(d, s) glue(rop_32_,ROP_NAME)(d, s)
43 #undef ROP_FN
44
45 static void
46 glue(cirrus_bitblt_rop_fwd_, ROP_NAME)(CirrusVGAState *s,
47 uint8_t *dst,const uint8_t *src,
48 int dstpitch,int srcpitch,
49 int bltwidth,int bltheight)
50 {
51 int x,y;
52 dstpitch -= bltwidth;
53 srcpitch -= bltwidth;
54
55 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
56 return;
57 }
58
59 for (y = 0; y < bltheight; y++) {
60 for (x = 0; x < bltwidth; x++) {
61 ROP_OP(dst, *src);
62 dst++;
63 src++;
64 }
65 dst += dstpitch;
66 src += srcpitch;
67 }
68 }
69
70 static void
71 glue(cirrus_bitblt_rop_bkwd_, ROP_NAME)(CirrusVGAState *s,
72 uint8_t *dst,const uint8_t *src,
73 int dstpitch,int srcpitch,
74 int bltwidth,int bltheight)
75 {
76 int x,y;
77 dstpitch += bltwidth;
78 srcpitch += bltwidth;
79 for (y = 0; y < bltheight; y++) {
80 for (x = 0; x < bltwidth; x++) {
81 ROP_OP(dst, *src);
82 dst--;
83 src--;
84 }
85 dst += dstpitch;
86 src += srcpitch;
87 }
88 }
89
90 static void
91 glue(glue(cirrus_bitblt_rop_fwd_transp_, ROP_NAME),_8)(CirrusVGAState *s,
92 uint8_t *dst,const uint8_t *src,
93 int dstpitch,int srcpitch,
94 int bltwidth,int bltheight)
95 {
96 int x,y;
97 uint8_t p;
98 dstpitch -= bltwidth;
99 srcpitch -= bltwidth;
100
101 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
102 return;
103 }
104
105 for (y = 0; y < bltheight; y++) {
106 for (x = 0; x < bltwidth; x++) {
107 p = *dst;
108 ROP_OP(&p, *src);
109 if (p != s->vga.gr[0x34]) *dst = p;
110 dst++;
111 src++;
112 }
113 dst += dstpitch;
114 src += srcpitch;
115 }
116 }
117
118 static void
119 glue(glue(cirrus_bitblt_rop_bkwd_transp_, ROP_NAME),_8)(CirrusVGAState *s,
120 uint8_t *dst,const uint8_t *src,
121 int dstpitch,int srcpitch,
122 int bltwidth,int bltheight)
123 {
124 int x,y;
125 uint8_t p;
126 dstpitch += bltwidth;
127 srcpitch += bltwidth;
128 for (y = 0; y < bltheight; y++) {
129 for (x = 0; x < bltwidth; x++) {
130 p = *dst;
131 ROP_OP(&p, *src);
132 if (p != s->vga.gr[0x34]) *dst = p;
133 dst--;
134 src--;
135 }
136 dst += dstpitch;
137 src += srcpitch;
138 }
139 }
140
141 static void
142 glue(glue(cirrus_bitblt_rop_fwd_transp_, ROP_NAME),_16)(CirrusVGAState *s,
143 uint8_t *dst,const uint8_t *src,
144 int dstpitch,int srcpitch,
145 int bltwidth,int bltheight)
146 {
147 int x,y;
148 uint8_t p1, p2;
149 dstpitch -= bltwidth;
150 srcpitch -= bltwidth;
151
152 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
153 return;
154 }
155
156 for (y = 0; y < bltheight; y++) {
157 for (x = 0; x < bltwidth; x+=2) {
158 p1 = *dst;
159 p2 = *(dst+1);
160 ROP_OP(&p1, *src);
161 ROP_OP(&p2, *(src + 1));
162 if ((p1 != s->vga.gr[0x34]) || (p2 != s->vga.gr[0x35])) {
163 *dst = p1;
164 *(dst+1) = p2;
165 }
166 dst+=2;
167 src+=2;
168 }
169 dst += dstpitch;
170 src += srcpitch;
171 }
172 }
173
174 static void
175 glue(glue(cirrus_bitblt_rop_bkwd_transp_, ROP_NAME),_16)(CirrusVGAState *s,
176 uint8_t *dst,const uint8_t *src,
177 int dstpitch,int srcpitch,
178 int bltwidth,int bltheight)
179 {
180 int x,y;
181 uint8_t p1, p2;
182 dstpitch += bltwidth;
183 srcpitch += bltwidth;
184 for (y = 0; y < bltheight; y++) {
185 for (x = 0; x < bltwidth; x+=2) {
186 p1 = *(dst-1);
187 p2 = *dst;
188 ROP_OP(&p1, *(src - 1));
189 ROP_OP(&p2, *src);
190 if ((p1 != s->vga.gr[0x34]) || (p2 != s->vga.gr[0x35])) {
191 *(dst-1) = p1;
192 *dst = p2;
193 }
194 dst-=2;
195 src-=2;
196 }
197 dst += dstpitch;
198 src += srcpitch;
199 }
200 }
201
202 #define DEPTH 8
203 #include "cirrus_vga_rop2.h"
204
205 #define DEPTH 16
206 #include "cirrus_vga_rop2.h"
207
208 #define DEPTH 24
209 #include "cirrus_vga_rop2.h"
210
211 #define DEPTH 32
212 #include "cirrus_vga_rop2.h"
213
214 #undef ROP_NAME
215 #undef ROP_OP
216 #undef ROP_OP_16
217 #undef ROP_OP_32