vfs: check userland buffers before reading them.
[haiku.git] / src / bin / hid_decode / hid_decode.cpp
blob3edaa1403c7f1a2cbe43621a1df2a5e6259ec5ff
1 /*
2 * Copyright 2010, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "HIDCollection.h"
7 #include "HIDParser.h"
8 #include "HIDReport.h"
10 #include <File.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
17 int
18 main(int argc, char *argv[])
20 if (argc < 2) {
21 printf("usage: %s <hid_descriptor_file>\n", argv[0]);
22 return 1;
25 BFile file(argv[1], B_READ_ONLY);
26 if (!file.IsReadable()) {
27 printf("can't open file \"%s\" for reading\n", argv[1]);
28 return 2;
31 off_t descriptorLength;
32 file.GetSize(&descriptorLength);
34 uint8 *reportDescriptor = (uint8 *)malloc(descriptorLength);
35 if (reportDescriptor == NULL) {
36 printf("failed to allocate buffer of %lld bytes\n", descriptorLength);
37 return 3;
40 ssize_t read = file.Read(reportDescriptor, descriptorLength);
41 if (read != descriptorLength) {
42 printf("failed to read file of %lld bytes: %s\n", descriptorLength,
43 strerror(read));
44 return 4;
47 HIDParser parser(NULL);
48 status_t result = parser.ParseReportDescriptor(reportDescriptor,
49 descriptorLength);
51 free(reportDescriptor);
52 if (result != B_OK) {
53 printf("failed to parse descriptor: %s\n", strerror(result));
54 return 5;
57 parser.PrintToStream();
58 return 0;