11c6fd17fSIngo Weinhold/*
21c6fd17fSIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
31c6fd17fSIngo Weinhold * Distributed under the terms of the MIT License.
41c6fd17fSIngo Weinhold */
51c6fd17fSIngo Weinhold
6c4a5ca93SIngo Weinhold
71c6fd17fSIngo Weinhold#include "TeamMemory.h"
81c6fd17fSIngo Weinhold
9c4a5ca93SIngo Weinhold#include <algorithm>
10c4a5ca93SIngo Weinhold
11c4a5ca93SIngo Weinhold#include <OS.h>
12c4a5ca93SIngo Weinhold#include <String.h>
13c4a5ca93SIngo Weinhold
141c6fd17fSIngo Weinhold
151c6fd17fSIngo WeinholdTeamMemory::~TeamMemory()
161c6fd17fSIngo Weinhold{
171c6fd17fSIngo Weinhold}
18c4a5ca93SIngo Weinhold
19c4a5ca93SIngo Weinhold
20c4a5ca93SIngo Weinholdstatus_t
21c4a5ca93SIngo WeinholdTeamMemory::ReadMemoryString(target_addr_t address, size_t maxLength,
22c4a5ca93SIngo Weinhold	BString& _string)
23c4a5ca93SIngo Weinhold{
24c4a5ca93SIngo Weinhold	char buffer[B_PAGE_SIZE];
25c4a5ca93SIngo Weinhold
26bcb394d6SRene Gollent	_string.Truncate(0);
27c4a5ca93SIngo Weinhold	while (maxLength > 0) {
28c4a5ca93SIngo Weinhold		// read at max maxLength bytes, but don't read across page bounds
29c4a5ca93SIngo Weinhold		size_t toRead = std::min(maxLength,
30c4a5ca93SIngo Weinhold			B_PAGE_SIZE - size_t(address % B_PAGE_SIZE));
31c4a5ca93SIngo Weinhold		ssize_t bytesRead = ReadMemory(address, buffer, toRead);
32c4a5ca93SIngo Weinhold		if (bytesRead < 0)
33bcb394d6SRene Gollent			return _string.Length() == 0 ? bytesRead : B_OK;
34c4a5ca93SIngo Weinhold
35c4a5ca93SIngo Weinhold		if (bytesRead == 0)
36bcb394d6SRene Gollent			return _string.Length() == 0 ? B_BAD_ADDRESS : B_OK;
37c4a5ca93SIngo Weinhold
38c4a5ca93SIngo Weinhold		// append the bytes read
39c4a5ca93SIngo Weinhold		size_t length = strnlen(buffer, bytesRead);
40bcb394d6SRene Gollent		_string.Append(buffer, length);
41c4a5ca93SIngo Weinhold
42c4a5ca93SIngo Weinhold		// stop at end of string
43c4a5ca93SIngo Weinhold		if (length < (size_t)bytesRead)
44c4a5ca93SIngo Weinhold			return B_OK;
45c4a5ca93SIngo Weinhold
46c4a5ca93SIngo Weinhold		address += bytesRead;
47c4a5ca93SIngo Weinhold		maxLength -= bytesRead;
48c4a5ca93SIngo Weinhold	}
49c4a5ca93SIngo Weinhold
50c4a5ca93SIngo Weinhold	return B_OK;
51c4a5ca93SIngo Weinhold}
52c4a5ca93SIngo Weinhold
53