4 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2014, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #include "imgsock_manager.h"
36 #define strncasecmp _strnicmp
39 msgtype_t identify_clientmsg(SOCKET connected_socket)
41 OPJ_SIZE_T receive_size;
43 static const char *magicid[] = { "JPIP-stream", "PNM request", "XML request",
44 "TID request", "CID request", "CID destroy", "SIZ request", "JP2 save",
49 receive_size = receive_line(connected_socket, buf);
51 if (receive_size == 0) {
52 fprintf(stderr, "Error to receive the header of client message\n");
56 for (i = 0; i < NUM_OF_MSGTYPES; i++) {
57 if (strncasecmp(magicid[i], buf, strlen(magicid[i])) == 0) {
58 fprintf(stderr, "%s\n", magicid[i]);
63 fprintf(stderr, "Cannot identify client message type %s\n", buf);
67 Byte_t * receive_JPIPstream(SOCKET connected_socket, char **target, char **tid,
68 char **cid, OPJ_SIZE_T *streamlen)
71 const char versionstring[] = "version 1.2";
73 OPJ_SIZE_T linelen, datalen;
76 *target = *cid = *tid = NULL;
78 if ((linelen = receive_line(connected_socket, buf)) == 0) {
81 if (strncmp(versionstring, buf, strlen(versionstring)) != 0) {
82 fprintf(stderr, "Wrong format\n");
86 if ((linelen = receive_line(connected_socket, buf)) == 0) {
90 if (strstr(buf, "jp2")) {
91 /* register cid option*/
92 *target = strdup(buf);
94 if ((linelen = receive_line(connected_socket, buf)) == 0) {
97 if (strcmp(buf, "0") != 0) {
101 if ((linelen = receive_line(connected_socket, buf)) == 0) {
104 if (strcmp(buf, "0") != 0) {
108 if ((linelen = receive_line(connected_socket, buf)) == 0) {
113 idatalen = atoi(buf);
115 fprintf(stderr, "Receive Data: %d Bytes\n", idatalen);
118 datalen = (OPJ_SIZE_T)idatalen;
119 fprintf(stdout, "Receive Data: %lu Bytes\n", datalen);
121 jpipstream = receive_stream(connected_socket, datalen);
124 if (jpipstream[datalen - 3] == 0x00 && (jpipstream[datalen - 2] == 0x01 ||
125 jpipstream[datalen - 2] == 0x02)) {
126 *streamlen = datalen - 3;
128 *streamlen = datalen;
134 void send_XMLstream(SOCKET connected_socket, Byte_t *xmlstream,
142 header[3] = (Byte_t)((length >> 8) & 0xff);
143 header[4] = (Byte_t)(length & 0xff);
145 send_stream(connected_socket, header, 5);
146 send_stream(connected_socket, xmlstream, length);
149 void send_IDstream(SOCKET connected_socket, const char *id, OPJ_SIZE_T idlen,
152 void send_CIDstream(SOCKET connected_socket, const char *cid, OPJ_SIZE_T cidlen)
154 send_IDstream(connected_socket, cid, cidlen, "CID");
157 void send_TIDstream(SOCKET connected_socket, const char *tid, OPJ_SIZE_T tidlen)
159 send_IDstream(connected_socket, tid, tidlen, "TID");
162 void send_IDstream(SOCKET connected_socket, const char *id, OPJ_SIZE_T idlen,
167 header[0] = label[0];
168 header[1] = label[1];
169 header[2] = label[2];
170 header[3] = (char)(idlen & 0xff);
172 send_stream(connected_socket, header, 4);
173 send_stream(connected_socket, id, idlen);
176 void send_PNMstream(SOCKET connected_socket, Byte_t *pnmstream,
177 unsigned int width, unsigned int height, unsigned int numofcomp, Byte_t maxval)
179 OPJ_SIZE_T pnmlen = 0;
182 pnmlen = width * height * numofcomp;
185 header[1] = numofcomp == 3 ? 6 : 5;
186 header[2] = (width >> 8) & 0xff;
187 header[3] = width & 0xff;
188 header[4] = (height >> 8) & 0xff;
189 header[5] = height & 0xff;
192 send_stream(connected_socket, header, 7);
193 send_stream(connected_socket, pnmstream, pnmlen);
196 void send_SIZstream(SOCKET connected_socket, unsigned int width,
204 response[3] = (width >> 16) & 0xff;
205 response[4] = (width >> 8) & 0xff;
206 response[5] = width & 0xff;
207 response[6] = (height >> 16) & 0xff;
208 response[7] = (height >> 8) & 0xff;
209 response[8] = height & 0xff;
211 send_stream(connected_socket, response, 9);
214 void response_signal(SOCKET connected_socket, OPJ_BOOL succeed)
224 send_stream(connected_socket, &code, 1);