vfs: check userland buffers before reading them.
[haiku.git] / src / tools / restest / ResourceItem.cpp
blobf686ee8e082c5d485f7a74a189f680b228207a08
1 // ResourceItem.cpp
3 #include "ResourceItem.h"
5 #include <stdio.h>
6 #include <string.h>
8 #include <DataIO.h>
10 // constructor
11 ResourceItem::ResourceItem()
12 : fOffset(0),
13 fSize(0),
14 fType(0),
15 fID(0),
16 fName(),
17 fData(NULL)
21 // destructor
22 ResourceItem::~ResourceItem()
24 UnsetData();
27 // SetLocation
28 void
29 ResourceItem::SetLocation(roff_t offset, roff_t size)
31 SetOffset(offset);
32 SetSize(size);
35 // SetIdentity
36 void
37 ResourceItem::SetIdentity(type_code type, int32 id, const char* name)
39 fType = type;
40 fID = id;
41 fName = name;
44 // SetOffset
45 void
46 ResourceItem::SetOffset(roff_t offset)
48 fOffset = offset;
51 // GetOffset
52 ResourceItem::roff_t
53 ResourceItem::GetOffset() const
55 return fOffset;
58 // SetSize
59 void
60 ResourceItem::SetSize(roff_t size)
62 if (size != fSize) {
63 UnsetData();
64 fSize = size;
68 // GetSize
69 ResourceItem::roff_t
70 ResourceItem::GetSize() const
72 return fSize;
75 // SetType
76 void
77 ResourceItem::SetType(type_code type)
79 fType = type;
82 // GetType
83 type_code
84 ResourceItem::GetType() const
86 return fType;
89 // SetID
90 void
91 ResourceItem::SetID(int32 id)
93 fID = id;
96 // GetID
97 int32
98 ResourceItem::GetID() const
100 return fID;
103 // SetName
104 void
105 ResourceItem::SetName(const char* name)
107 fName = name;
110 // GetName
111 const char*
112 ResourceItem::GetName() const
114 return fName.String();
117 // SetData
118 void
119 ResourceItem::SetData(const void* data, roff_t size)
121 if (size < 0)
122 size = fSize;
123 // set the new data
124 if (data) {
125 AllocData(size);
126 if (fSize > 0)
127 memcpy(fData, data, fSize);
128 } else
129 UnsetData();
132 // UnsetData
133 void
134 ResourceItem::UnsetData()
136 if (fData) {
137 delete[] (char*)fData;
138 fData = NULL;
142 // AllocData
143 void*
144 ResourceItem::AllocData(roff_t size)
146 if (size >= 0)
147 SetSize(size);
148 if (!fData && fSize > 0)
149 fData = new char*[fSize];
150 return fData;
153 // GetData
154 void*
155 ResourceItem::GetData() const
157 return fData;
160 // LoadData
161 status_t
162 ResourceItem::LoadData(BPositionIO& file, roff_t position, roff_t size)
164 status_t error = B_OK;
165 AllocData(size);
166 if (position >= 0)
167 fOffset = position;
168 if (fData && fSize) {
169 ssize_t read = file.ReadAt(fOffset, fData, fSize);
170 if (read < 0)
171 error = read;
172 else if (read < fSize)
173 error = B_ERROR;
175 return error;
178 // WriteData
179 status_t
180 ResourceItem::WriteData(BPositionIO& file) const
182 status_t error = (fData ? B_OK : B_BAD_VALUE);
183 if (error == B_OK && fSize > 0) {
184 ssize_t written = file.WriteAt(fOffset, fData, fSize);
185 if (written < 0)
186 error = written;
187 else if (written < fSize)
188 error = B_FILE_ERROR;
190 return error;
193 // PrintToStream
194 void
195 ResourceItem::PrintToStream()
197 char typeName[4] = { fType >> 24, (fType >> 16) & 0xff,
198 (fType >> 8) & 0xff, fType & 0xff };
199 printf("resource: offset: 0x%08lx (%10ld)\n", fOffset, fOffset);
200 printf(" size : 0x%08lx (%10ld)\n", fSize, fSize);
201 printf(" type : '%.4s' (0x%8lx)\n", typeName, fType);
202 printf(" id : %ld\n", fID);
203 printf(" name : `%s'\n", fName.String());