vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / app / DanoMessageTest.cpp
blobe05cd957ab363d741e76129759cd29e8f5c5c542
1 #include <File.h>
2 #include <Message.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
9 namespace BPrivate {
10 status_t unflatten_dano_message(uint32 magic, BDataIO& stream, BMessage& message);
11 size_t dano_message_size(const char* buffer);
15 extern const char* __progname;
17 static const uint32 kMessageFormat = 'FOB2';
18 static const uint32 kMessageFormatSwapped = '2BOF';
21 int
22 main(int argc, char** argv)
24 if (argc < 2) {
25 fprintf(stderr, "usage: %s <flattened dano message>\n", __progname);
26 return -1;
29 for (int32 i = 1; i < argc; i++) {
30 BFile file(argv[i], B_READ_ONLY);
31 if (file.InitCheck() != B_OK) {
32 fprintf(stderr, "Could not open message \"%s\": %s\n", argv[i], strerror(file.InitCheck()));
33 continue;
36 off_t size;
37 if (file.GetSize(&size) != B_OK)
38 continue;
40 uint32 magic;
41 if (file.Read(&magic, sizeof(uint32)) != sizeof(uint32))
42 continue;
44 if (magic != kMessageFormat && magic != kMessageFormatSwapped) {
45 fprintf(stderr, "Not a dano message \"%s\"\n", argv[i]);
46 continue;
49 BMessage message;
50 status_t status = BPrivate::unflatten_dano_message(magic, file, message);
51 if (status == B_OK)
52 message.PrintToStream();
53 else
54 fprintf(stderr, "Could not unflatten message: %s\n", strerror(status));
57 return 0;