-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint.c
49 lines (44 loc) · 1.02 KB
/
print.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "print.h"
#include "huffman.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/types.h>
#include <string.h>
#include <fcntl.h>
#include <wait.h>
#include <time.h>
#include <limits.h>
static int
is_huffman(unsigned char *buf)
{
unsigned char *ptbuf;
int i = 0;
ptbuf = buf;
while (((ptbuf[0] << 8) | ptbuf[1]) != 0xffda) {
if (i++ > 2048)
return 0;
if (((ptbuf[0] << 8) | ptbuf[1]) == 0xffc4)
return 1;
ptbuf++;
}
return 0;
}
extern int
print_picture(int fd, unsigned char *buf, int size)
{
unsigned char *ptdeb, *ptcur = buf;
int sizein;
if (!is_huffman(buf)) {
ptdeb = ptcur = buf;
while (((ptcur[0] << 8) | ptcur[1]) != 0xffc0)
ptcur++;
sizein = ptcur - ptdeb;
if( write(fd, buf, sizein) <= 0) return -1;
if( write(fd, dht_data, DHT_SIZE) <= 0) return -1;
if( write(fd, ptcur, size - sizein) <= 0) return -1;
} else {
if( write(fd, ptcur, size) <= 0) return -1;
}
return 0;
}