vfs: check userland buffers before reading them.
[haiku.git] / src / servers / app / ServerBitmap.cpp
blob0123bbfb216a63446c677e8a9751834caf4afddb
1 /*
2 * Copyright 2001-2010, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * DarkWyrm <bpmagic@columbus.rr.com>
7 * Axel Dörfler, axeld@pinc-software.de
8 */
11 #include "ServerBitmap.h"
13 #include <new>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "BitmapManager.h"
19 #include "ClientMemoryAllocator.h"
20 #include "ColorConversion.h"
21 #include "HWInterface.h"
22 #include "InterfacePrivate.h"
23 #include "Overlay.h"
24 #include "ServerApp.h"
27 using std::nothrow;
28 using namespace BPrivate;
31 /*! A word about memory housekeeping and why it's implemented this way:
33 The reason why this looks so complicated is to optimize the most common
34 path (bitmap creation from the application), and don't cause any further
35 memory allocations for maintaining memory in that case.
36 If a bitmap was allocated this way, both, the fAllocator and
37 fAllocationCookie members are used.
39 For overlays, the allocator only allocates a small piece of client memory
40 for use with the overlay_client_data structure - the actual buffer will be
41 placed in the graphics frame buffer and is allocated by the graphics driver.
43 If the memory was allocated on the app_server heap, neither fAllocator, nor
44 fAllocationCookie are used, and the buffer is just freed in that case when
45 the bitmap is destructed. This method is mainly used for cursors.
49 /*! \brief Constructor called by the BitmapManager (only).
50 \param rect Size of the bitmap.
51 \param space Color space of the bitmap
52 \param flags Various bitmap flags to tweak the bitmap as defined in Bitmap.h
53 \param bytesperline Number of bytes in each row. -1 implies the default
54 value. Any value less than the the default will less than the default
55 will be overridden, but any value greater than the default will result
56 in the number of bytes specified.
57 \param screen Screen assigned to the bitmap.
59 ServerBitmap::ServerBitmap(BRect rect, color_space space, uint32 flags,
60 int32 bytesPerRow, screen_id screen)
62 fMemory(NULL),
63 fOverlay(NULL),
64 fBuffer(NULL),
65 // WARNING: '1' is added to the width and height.
66 // Same is done in FBBitmap subclass, so if you
67 // modify here make sure to do the same under
68 // FBBitmap::SetSize(...)
69 fWidth(rect.IntegerWidth() + 1),
70 fHeight(rect.IntegerHeight() + 1),
71 fBytesPerRow(0),
72 fSpace(space),
73 fFlags(flags),
74 fOwner(NULL)
75 // fToken is initialized (if used) by the BitmapManager
77 int32 minBytesPerRow = get_bytes_per_row(space, fWidth);
79 fBytesPerRow = max_c(bytesPerRow, minBytesPerRow);
83 //! Copy constructor does not copy the buffer.
84 ServerBitmap::ServerBitmap(const ServerBitmap* bitmap)
86 fMemory(NULL),
87 fOverlay(NULL),
88 fBuffer(NULL),
89 fOwner(NULL)
91 if (bitmap) {
92 fWidth = bitmap->fWidth;
93 fHeight = bitmap->fHeight;
94 fBytesPerRow = bitmap->fBytesPerRow;
95 fSpace = bitmap->fSpace;
96 fFlags = bitmap->fFlags;
97 } else {
98 fWidth = 0;
99 fHeight = 0;
100 fBytesPerRow = 0;
101 fSpace = B_NO_COLOR_SPACE;
102 fFlags = 0;
107 ServerBitmap::~ServerBitmap()
109 if (fMemory != NULL) {
110 if (fMemory != &fClientMemory)
111 delete fMemory;
112 } else
113 delete[] fBuffer;
115 delete fOverlay;
116 // deleting the overlay will also free the overlay buffer
120 /*! \brief Internal function used by subclasses
122 Subclasses should call this so the buffer can automagically
123 be allocated on the heap.
125 void
126 ServerBitmap::AllocateBuffer()
128 uint32 length = BitsLength();
129 if (length > 0) {
130 delete[] fBuffer;
131 fBuffer = new(std::nothrow) uint8[length];
136 status_t
137 ServerBitmap::ImportBits(const void *bits, int32 bitsLength, int32 bytesPerRow,
138 color_space colorSpace)
140 if (!bits || bitsLength < 0 || bytesPerRow <= 0)
141 return B_BAD_VALUE;
143 return BPrivate::ConvertBits(bits, fBuffer, bitsLength, BitsLength(),
144 bytesPerRow, fBytesPerRow, colorSpace, fSpace, fWidth, fHeight);
148 status_t
149 ServerBitmap::ImportBits(const void *bits, int32 bitsLength, int32 bytesPerRow,
150 color_space colorSpace, BPoint from, BPoint to, int32 width, int32 height)
152 if (!bits || bitsLength < 0 || bytesPerRow <= 0 || width < 0 || height < 0)
153 return B_BAD_VALUE;
155 return BPrivate::ConvertBits(bits, fBuffer, bitsLength, BitsLength(),
156 bytesPerRow, fBytesPerRow, colorSpace, fSpace, from, to, width,
157 height);
161 area_id
162 ServerBitmap::Area() const
164 if (fMemory != NULL)
165 return fMemory->Area();
167 return B_ERROR;
171 uint32
172 ServerBitmap::AreaOffset() const
174 if (fMemory != NULL)
175 return fMemory->AreaOffset();
177 return 0;
181 void
182 ServerBitmap::SetOverlay(::Overlay* overlay)
184 fOverlay = overlay;
188 ::Overlay*
189 ServerBitmap::Overlay() const
191 return fOverlay;
195 void
196 ServerBitmap::SetOwner(ServerApp* owner)
198 fOwner = owner;
202 ServerApp*
203 ServerBitmap::Owner() const
205 return fOwner;
209 void
210 ServerBitmap::PrintToStream()
212 printf("Bitmap@%p: (%" B_PRId32 ":%" B_PRId32 "), space %" B_PRId32 ", "
213 "bpr %" B_PRId32 ", buffer %p\n", this, fWidth, fHeight, (int32)fSpace,
214 fBytesPerRow, fBuffer);
218 // #pragma mark -
221 UtilityBitmap::UtilityBitmap(BRect rect, color_space space, uint32 flags,
222 int32 bytesPerRow, screen_id screen)
224 ServerBitmap(rect, space, flags, bytesPerRow, screen)
226 AllocateBuffer();
230 UtilityBitmap::UtilityBitmap(const ServerBitmap* bitmap)
232 ServerBitmap(bitmap)
234 AllocateBuffer();
236 if (bitmap->Bits())
237 memcpy(Bits(), bitmap->Bits(), bitmap->BitsLength());
241 UtilityBitmap::UtilityBitmap(const uint8* alreadyPaddedData, uint32 width,
242 uint32 height, color_space format)
244 ServerBitmap(BRect(0, 0, width - 1, height - 1), format, 0)
246 AllocateBuffer();
247 if (Bits())
248 memcpy(Bits(), alreadyPaddedData, BitsLength());
252 UtilityBitmap::~UtilityBitmap()