vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / media-add-ons / usb_webcam / addons / uvc / UVCDeframer.cpp
blobde84f3840cbaf6cbf4c877e264386bc47a0042f0
1 /*
2 * Copyright 2011, Gabriel Hartmann, gabriel.hartmann@gmail.com.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "UVCDeframer.h"
9 #include "CamDebug.h"
10 #include "CamDevice.h"
12 #include <Autolock.h>
15 #define MAX_TAG_LEN CAMDEFRAMER_MAX_TAG_LEN
16 #define MAXFRAMEBUF CAMDEFRAMER_MAX_QUEUED_FRAMES
19 UVCDeframer::UVCDeframer(CamDevice* device)
20 : CamDeframer(device),
21 fFrameCount(0),
22 fID(0)
27 UVCDeframer::~UVCDeframer()
32 ssize_t
33 UVCDeframer::Write(const void* buffer, size_t size)
35 const uint8* buf = (const uint8*)buffer;
36 int payloadSize = size - buf[0]; // total length - header length
38 // This packet is just a header
39 if (size == buf[0])
40 return 0;
42 // Allocate frame
43 if (!fCurrentFrame) {
44 BAutolock l(fLocker);
45 if (fFrames.CountItems() < MAXFRAMEBUF)
46 fCurrentFrame = AllocFrame();
47 else {
48 printf("Dropped %ld bytes. Too many queued frames.)\n", size);
49 return size;
53 // Write payload to buffer
54 fInputBuffer.Write(&buf[buf[0]], payloadSize);
56 // If end of frame add frame to list of frames
57 if ((buf[1] & 2) || (buf[1] & 1) != fID) {
58 fID = buf[1] & 1;
59 fFrameCount++;
60 buf = (uint8*)fInputBuffer.Buffer();
61 fCurrentFrame->Write(buf, fInputBuffer.BufferLength());
62 fFrames.AddItem(fCurrentFrame);
63 release_sem(fFrameSem);
64 fCurrentFrame = NULL;
67 return size;
71 void
72 UVCDeframer::_PrintBuffer(const void* buffer, size_t size)
74 uint8* b = (uint8*)buffer;
75 for (size_t i = 0; i < size; i++)
76 printf("0x%x\t", b[i]);
77 printf("\n");