vfs: check userland buffers before reading them.
[haiku.git] / src / bin / listres.cpp
blobc4419ca57b2b3e6d6d0b100befa5ed8dec9d959b
1 /*
2 * Copyright 2006, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT license.
4 */
7 #include <File.h>
8 #include <Mime.h>
9 #include <Resources.h>
10 #include <TypeConstants.h>
12 #include <stdio.h>
13 #include <string.h>
16 static const char *
17 get_type(type_code type)
19 static char buffer[32];
21 switch (type) {
22 case B_MIME_STRING_TYPE:
23 return "MIME String";
24 case B_RAW_TYPE:
25 return "Raw Data";
27 case B_STRING_TYPE:
28 return "Text";
29 case B_INT64_TYPE:
30 return "Int-64";
31 case B_UINT64_TYPE:
32 return "Uint-64";
33 case B_INT32_TYPE:
34 return "Int-32";
35 case B_UINT32_TYPE:
36 return "Uint-32";
37 case B_INT16_TYPE:
38 return "Int-16";
39 case B_UINT16_TYPE:
40 return "Uint-16";
41 case B_INT8_TYPE:
42 return "Int-8";
43 case B_UINT8_TYPE:
44 return "Uint-8";
45 case B_BOOL_TYPE:
46 return "Boolean";
47 case B_FLOAT_TYPE:
48 return "Float";
49 case B_DOUBLE_TYPE:
50 return "Double";
52 case B_MINI_ICON_TYPE:
53 return "Mini Icon";
54 case B_LARGE_ICON_TYPE:
55 return "Icon";
57 default:
59 int32 missed = 0, shift = 24;
60 uint8 value[4];
61 for (int32 i = 0; i < 4; i++, shift -= 8) {
62 value[i] = uint8(type >> shift);
63 if (value[i] < ' ' || value[i] > 127) {
64 value[i] = '.';
65 missed++;
69 if (missed < 2)
70 sprintf(buffer, "'%c%c%c%c'", value[0], value[1], value[2], value[3]);
71 else
72 sprintf(buffer, "0x%08lx", type);
73 return buffer;
79 int
80 main(int argc, char *argv[])
82 const char *program = strrchr(argv[0], '/');
83 if (program == NULL)
84 program = argv[0];
85 else
86 program++;
88 if (argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) {
89 printf("usage: %s <filename> [<filename> ...]\n", program);
90 return 1;
93 off_t total = 0;
95 for (int i = 1; i < argc; ++i) {
96 BFile file(argv[i], B_READ_ONLY);
98 status_t status = file.InitCheck();
99 if (status < B_OK) {
100 fprintf(stderr, "%s: opening file failed for \"%s\": %s\n",
101 program, argv[i], strerror(status));
102 return 1;
105 BResources resources;
106 status = resources.SetTo(&file);
107 if (status != B_OK) {
108 fprintf(stderr, "%s: opening resources failed for \"%s\": %s\n",
109 program, argv[i], strerror(status));
110 return 1;
113 printf("File: %s\n", argv[i]);
114 printf(" Type ID Size Name\n");
115 printf("----------- ----- -------- -------------------------------\n");
117 int32 index = 0;
118 const char* name;
119 type_code type;
120 size_t size;
121 int32 id;
122 while (resources.GetResourceInfo(index++, &type, &id, &name, &size)) {
123 printf("%11s %6ld %9ld \"%s\"\n",
124 get_type(type), id, size, name);
126 total += size;
130 printf("\n%Ld bytes total in resources.\n", total);
131 return 0;