fix :cirrus_vga fix OOB read case qemu Segmentation fault
[qemu.git] / hw / display / cirrus_vga_rop.h
blobb7447f8733fbd750424ba1e9d0cb43f649873a58
1 /*
2 * QEMU Cirrus CLGD 54xx VGA Emulator.
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 static inline void glue(rop_8_,ROP_NAME)(uint8_t *dst, uint8_t src)
27 *dst = ROP_FN(*dst, src);
30 static inline void glue(rop_16_,ROP_NAME)(uint16_t *dst, uint16_t src)
32 *dst = ROP_FN(*dst, src);
35 static inline void glue(rop_32_,ROP_NAME)(uint32_t *dst, uint32_t src)
37 *dst = ROP_FN(*dst, src);
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
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)
51 int x,y;
52 dstpitch -= bltwidth;
53 srcpitch -= bltwidth;
55 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
56 return;
59 for (y = 0; y < bltheight; y++) {
60 for (x = 0; x < bltwidth; x++) {
61 ROP_OP(dst, *src);
62 dst++;
63 src++;
65 dst += dstpitch;
66 src += srcpitch;
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)
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--;
85 dst += dstpitch;
86 src += srcpitch;
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)
96 int x,y;
97 uint8_t p;
98 dstpitch -= bltwidth;
99 srcpitch -= bltwidth;
101 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
102 return;
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++;
113 dst += dstpitch;
114 src += srcpitch;
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)
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--;
136 dst += dstpitch;
137 src += srcpitch;
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)
147 int x,y;
148 uint8_t p1, p2;
149 dstpitch -= bltwidth;
150 srcpitch -= bltwidth;
152 if (bltheight > 1 && (dstpitch < 0 || srcpitch < 0)) {
153 return;
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;
166 dst+=2;
167 src+=2;
169 dst += dstpitch;
170 src += srcpitch;
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)
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;
194 dst-=2;
195 src-=2;
197 dst += dstpitch;
198 src += srcpitch;
202 #define DEPTH 8
203 #include "cirrus_vga_rop2.h"
205 #define DEPTH 16
206 #include "cirrus_vga_rop2.h"
208 #define DEPTH 24
209 #include "cirrus_vga_rop2.h"
211 #define DEPTH 32
212 #include "cirrus_vga_rop2.h"
214 #undef ROP_NAME
215 #undef ROP_OP
216 #undef ROP_OP_16
217 #undef ROP_OP_32