summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-03-11 23:30:29 -0500
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-03-11 23:30:29 -0500
commit4ddf5700fb13cca9ef8fec0bd44d6ef769b37216 (patch)
treeb6cd534e9757f91c67d23829f80d472da4ec68b6
parentf0e702ce3f0f86258544f2d6213810d32eb1f4b9 (diff)
fixity fix
-rw-r--r--freenect-server--kinect.c3
-rw-r--r--mpjpeg.c43
2 files changed, 30 insertions, 16 deletions
diff --git a/freenect-server--kinect.c b/freenect-server--kinect.c
index 9a7aafd..2e817dc 100644
--- a/freenect-server--kinect.c
+++ b/freenect-server--kinect.c
@@ -34,7 +34,6 @@ void dump_ffmpeg_pad16(FILE *stream, uint32_t timestamp UNUSED, void *data_anon,
void handle_accel(freenect_device *dev UNUSED, freenect_raw_tilt_state* data)
{
- printf("handle accel\n");
double x, y, z;
freenect_get_mks_accel(data, &x, &y, &z);
//fprintf(accel_stream, "x=%f\ty=%f\tz=%f\n", x, y, z);
@@ -42,7 +41,6 @@ void handle_accel(freenect_device *dev UNUSED, freenect_raw_tilt_state* data)
void handle_depth(freenect_device *dev UNUSED, void *depth, uint32_t timestamp)
{
- printf("handle depth\n");
dump_ffmpeg_pad16(depth_stream, timestamp, depth,
freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM,
FREENECT_DEPTH_11BIT).bytes);
@@ -50,7 +48,6 @@ void handle_depth(freenect_device *dev UNUSED, void *depth, uint32_t timestamp)
void handle_video(freenect_device *dev, void *rgb, uint32_t timestamp)
{
- printf("handle video\n");
dump_ffmpeg_24(video_stream, timestamp, rgb,
freenect_get_current_video_mode(dev).bytes);
}
diff --git a/mpjpeg.c b/mpjpeg.c
index dd4b96e..b64c5f0 100644
--- a/mpjpeg.c
+++ b/mpjpeg.c
@@ -4,12 +4,11 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <error.h>
#include "mpjpeg.h"
#include "util.h"
-#define STARTS_WITH(str, prefix) strncmp(str, prefix, sizeof(prefix)-1)
-
static
char *boundary_line(const char *old) {
size_t len = strlen(old);
@@ -23,6 +22,21 @@ char *boundary_line(const char *old) {
return new;
}
+static
+ssize_t safe_atoi(char *str) {
+ while (*str == ' ')
+ str++;
+ size_t len = 0;
+ while ('0' <= str[len] && str[len] <= '9')
+ len++;
+ size_t i = len;
+ while (str[i] == ' ')
+ i++;
+ if (len < 1 || strcmp(&str[i], "\r\n") != 0)
+ return -1;
+ return atoi(str);
+}
+
void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) {
FILE *stream = fdopen(fd, "r");
boundary = boundary_line(boundary);
@@ -32,30 +46,33 @@ void mpjpeg_reader(struct mpjpeg_stream *s, int fd, const char *boundary) {
ssize_t line_len = 0;
while (1) {
- printf("getting frame\n");
s->back->len = -1;
/* read the frame header (MIME headers) */
- line_len = getline(&line_buf, &line_cap, stream);
- if (strcmp(line_buf, boundary) != 0)
+ do {
+ line_len = getline(&line_buf, &line_cap, stream);
+ } while (line_len >= 0 && strcmp(line_buf, "\r\n") == 0);
+ if (strcmp(line_buf, boundary) != 0) {
+ error(0, 0, "line does not match boundary: \"%s\"", line_buf);
return;
- ssize_t framelen = -1;
+ }
while (strcmp(line_buf, "\r\n") != 0 && line_len >= 0) {
line_len = getline(&line_buf, &line_cap, stream);
- if (STARTS_WITH(line_buf, "Content-Type:") == 0) {
- printf("line: <%s>\n", line_buf);
- s->back->len = atoi(&line_buf[strlen("Content-Type:")]);
+ if (strncasecmp(line_buf, "Content-length:", strlen("Content-length:")) == 0) {
+ s->back->len = safe_atoi(&line_buf[strlen("Content-length:")]);
}
}
- if (s->back->len < 0)
+ if (s->back->len < 0) {
+ error(0, 0, "did not get frame length");
return;
+ }
/* read the frame contents (JPEG) */
if (s->back->cap < (size_t)s->back->len)
s->back->data = xrealloc(s->back->data, s->back->cap = s->back->len);
- if (fread(line_buf, framelen, 1, stream) != 1)
+ if (fread(s->back->data, s->back->len, 1, stream) != 1) {
+ error(0, ferror(stream), "fread(%zd)", s->back->len);
return;
-
- printf("got frame on fd %d\n", fd);
+ }
/* swap the frames */
pthread_mutex_lock(&s->frontlock);