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