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.
33 #include "jpip_parser.h"
34 #include "channel_manager.h"
35 #include "byte_manager.h"
43 #include "auxtrans_manager.h"
47 #include "dec_clientmsg_handler.h"
48 #include "jpipstream_manager.h"
51 #include <sys/types.h>
54 #include "jp2k_encoder.h"
58 server_record_t * init_JPIPserver(int tcp_auxport, int udp_auxport)
60 server_record_t *record = (server_record_t *)opj_malloc(sizeof(
63 record->sessionlist = gene_sessionlist();
64 record->targetlist = gene_targetlist();
65 record->auxtrans = init_aux_transport(tcp_auxport, udp_auxport);
70 void terminate_JPIPserver(server_record_t **rec)
72 delete_sessionlist(&(*rec)->sessionlist);
73 delete_targetlist(&(*rec)->targetlist);
74 close_aux_transport((*rec)->auxtrans);
79 QR_t * parse_querystring(const char *query_string)
83 qr = (QR_t *)opj_malloc(sizeof(QR_t));
85 qr->query = parse_query(query_string);
92 OPJ_BOOL process_JPIPrequest(server_record_t *rec, QR_t *qr)
94 target_param_t *target = NULL;
95 session_param_t *cursession = NULL;
96 channel_param_t *curchannel = NULL;
98 if (qr->query->target || qr->query->tid) {
99 if (!identify_target(*(qr->query), rec->targetlist, &target)) {
104 if (qr->query->cid) {
105 if (!associate_channel(*(qr->query), rec->sessionlist, &cursession,
109 qr->channel = curchannel;
112 if (qr->query->cnew != non) {
113 if (!open_channel(*(qr->query), rec->sessionlist, rec->auxtrans, target,
114 &cursession, &curchannel)) {
117 qr->channel = curchannel;
120 if (qr->query->cclose)
121 if (!close_channel(*(qr->query), rec->sessionlist, &cursession, &curchannel)) {
125 if ((qr->query->fx > 0 && qr->query->fy > 0) ||
126 qr->query->box_type[0][0] != 0 || qr->query->len > 0)
127 if (!gene_JPIPstream(*(qr->query), target, cursession, curchannel,
135 void add_EORmsg(int fd, QR_t *qr);
137 void send_responsedata(server_record_t *rec, QR_t *qr)
140 const char tmpfname[] = "tmpjpipstream.jpp";
142 Byte8_t len_of_jpipstream;
144 if ((fd = open(tmpfname, O_RDWR | O_CREAT | O_EXCL, S_IRWXU)) == -1) {
145 fprintf(FCGI_stderr, "file open error %s", tmpfname);
146 fprintf(FCGI_stdout, "Status: 503\r\n");
147 fprintf(FCGI_stdout, "Reason: Implementation failed\r\n");
151 recons_stream_from_msgqueue(qr->msgqueue, fd);
153 add_EORmsg(fd, qr); /* needed at least for tcp and udp */
155 len_of_jpipstream = (Byte8_t)get_filesize(fd);
156 jpipstream = fetch_bytes(fd, 0, len_of_jpipstream);
161 fprintf(FCGI_stdout, "\r\n");
163 if (len_of_jpipstream) {
166 if (qr->channel->aux == tcp || qr->channel->aux == udp) {
167 send_responsedata_on_aux(qr->channel->aux == tcp, rec->auxtrans,
168 qr->channel->cid, jpipstream, len_of_jpipstream, 1000); /* 1KB per frame*/
172 if (fwrite(jpipstream, len_of_jpipstream, 1, FCGI_stdout) != 1) {
173 fprintf(FCGI_stderr, "Error: failed to write jpipstream\n");
177 opj_free(jpipstream);
182 void add_EORmsg(int fd, QR_t *qr)
184 unsigned char EOR[3];
188 EOR[1] = is_allsent(*(qr->channel->cachemodel)) ? 0x01 : 0x02;
190 if (write(fd, EOR, 3) != 3) {
191 fprintf(FCGI_stderr, "Error: failed to write EOR message\n");
196 void end_QRprocess(server_record_t *rec, QR_t **qr)
198 /* TODO: record client preferences if necessary*/
199 (void)rec; /* unused */
200 delete_query(&((*qr)->query));
201 delete_msgqueue(&((*qr)->msgqueue));
206 void local_log(OPJ_BOOL query, OPJ_BOOL messages, OPJ_BOOL sessions,
207 OPJ_BOOL targets, QR_t *qr, server_record_t *rec)
210 print_queryparam(*qr->query);
214 print_msgqueue(qr->msgqueue);
218 print_allsession(rec->sessionlist);
222 print_alltarget(rec->targetlist);
230 dec_server_record_t * OPJ_CALLCONV init_dec_server(int port)
232 dec_server_record_t *record = (dec_server_record_t *)opj_malloc(sizeof(
233 dec_server_record_t));
235 record->cachelist = gene_cachelist();
236 record->jpipstream = NULL;
237 record->jpipstreamlen = 0;
238 record->msgqueue = gene_msgqueue(OPJ_TRUE, NULL);
239 record->listening_socket = open_listeningsocket((uint16_t)port);
244 void OPJ_CALLCONV terminate_dec_server(dec_server_record_t **rec)
246 delete_cachelist(&(*rec)->cachelist);
247 opj_free((*rec)->jpipstream);
249 if ((*rec)->msgqueue) {
250 delete_msgqueue(&((*rec)->msgqueue));
253 if (close_socket((*rec)->listening_socket) != 0) {
260 client_t OPJ_CALLCONV accept_connection(dec_server_record_t *rec)
264 client = accept_socket(rec->listening_socket);
266 fprintf(stderr, "error: failed to connect to client\n");
272 OPJ_BOOL OPJ_CALLCONV handle_clientreq(client_t client,
273 dec_server_record_t *rec)
275 OPJ_BOOL quit = OPJ_FALSE;
276 msgtype_t msgtype = identify_clientmsg(client);
280 handle_JPIPstreamMSG(client, rec->cachelist, &rec->jpipstream,
281 &rec->jpipstreamlen, rec->msgqueue);
285 handle_PNMreqMSG(client, rec->jpipstream, rec->msgqueue, rec->cachelist);
289 handle_XMLreqMSG(client, rec->jpipstream, rec->cachelist);
293 handle_TIDreqMSG(client, rec->cachelist);
297 handle_CIDreqMSG(client, rec->cachelist);
301 handle_dstCIDreqMSG(client, rec->cachelist);
305 handle_SIZreqMSG(client, rec->jpipstream, rec->msgqueue, rec->cachelist);
309 handle_JP2saveMSG(client, rec->cachelist, rec->msgqueue, rec->jpipstream);
314 save_codestream(rec->jpipstream, rec->jpipstreamlen, "jpt");
320 fprintf(stderr, "\t end of the connection\n\n");
321 if (close_socket(client) != 0) {
334 jpip_dec_param_t * OPJ_CALLCONV init_jpipdecoder(OPJ_BOOL jp2)
336 jpip_dec_param_t *dec;
338 dec = (jpip_dec_param_t *)opj_calloc(1, sizeof(jpip_dec_param_t));
340 dec->msgqueue = gene_msgqueue(OPJ_TRUE, NULL);
343 dec->metadatalist = gene_metadatalist();
350 OPJ_BOOL OPJ_CALLCONV fread_jpip(const char fname[], jpip_dec_param_t *dec)
354 if ((infd = open(fname, O_RDONLY)) == -1) {
355 fprintf(stderr, "file %s not exist\n", fname);
359 if (!(dec->jpiplen = (Byte8_t)get_filesize(infd))) {
364 dec->jpipstream = (Byte_t *)opj_malloc(dec->jpiplen);
366 if (read(infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen) {
367 fprintf(stderr, "file reading error\n");
368 opj_free(dec->jpipstream);
378 void OPJ_CALLCONV decode_jpip(jpip_dec_param_t *dec)
380 parse_JPIPstream(dec->jpipstream, dec->jpiplen, 0, dec->msgqueue);
382 if (dec->metadatalist) { /* JP2 encoding*/
383 parse_metamsg(dec->msgqueue, dec->jpipstream, dec->jpiplen, dec->metadatalist);
384 dec->ihdrbox = gene_ihdrbox(dec->metadatalist, dec->jpipstream);
386 dec->jp2kstream = recons_jp2(dec->msgqueue, dec->jpipstream,
387 dec->msgqueue->first->csn, &dec->jp2klen);
388 } else /* J2k encoding */
389 /* Notice: arguments fw, fh need to be set for LRCP, PCRL, CPRL*/
391 dec->jp2kstream = recons_j2k(dec->msgqueue, dec->jpipstream,
392 dec->msgqueue->first->csn, 0, 0, &dec->jp2klen);
396 OPJ_BOOL OPJ_CALLCONV fwrite_jp2k(const char fname[], jpip_dec_param_t *dec)
401 if ((outfd = open(fname, O_WRONLY | O_CREAT, _S_IREAD | _S_IWRITE)) == -1) {
403 if ((outfd = open(fname, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG)) == -1) {
405 fprintf(stderr, "file %s open error\n", fname);
409 if (write(outfd, dec->jp2kstream, dec->jp2klen) != (int)dec->jp2klen) {
410 fprintf(stderr, "j2k file write error\n");
418 void OPJ_CALLCONV output_log(OPJ_BOOL messages, OPJ_BOOL metadata,
419 OPJ_BOOL ihdrbox, jpip_dec_param_t *dec)
422 print_msgqueue(dec->msgqueue);
426 print_allmetadata(dec->metadatalist);
430 printf("W*H: %d*%d\n", dec->ihdrbox->height, dec->ihdrbox->width);
431 printf("NC: %d, bpc: %d\n", dec->ihdrbox->nc, dec->ihdrbox->bpc);
435 void OPJ_CALLCONV destroy_jpipdecoder(jpip_dec_param_t **dec)
437 opj_free((*dec)->jpipstream);
438 delete_msgqueue(&(*dec)->msgqueue);
439 if ((*dec)->metadatalist) {
440 delete_metadatalist(&(*dec)->metadatalist);
441 opj_free((*dec)->ihdrbox);
444 opj_free((*dec)->jp2kstream);
448 index_t * OPJ_CALLCONV get_index_from_JP2file(int fd)
452 /* Check resource is a JP family file.*/
453 if (lseek(fd, 0, SEEK_SET) == -1) {
454 fprintf(stderr, "Error: File broken (lseek error)\n");
458 data = (char *)opj_malloc(12); /* size of header*/
459 if (read(fd, data, 12) != 12) {
461 fprintf(stderr, "Error: File broken (read error)\n");
465 if (*data || *(data + 1) || *(data + 2) ||
466 *(data + 3) != 12 || strncmp(data + 4, "jP \r\n\x87\n", 8)) {
468 fprintf(stderr, "Error: No JPEG 2000 Signature box in this file\n");
473 return parse_jp2file(fd);
476 void OPJ_CALLCONV destroy_index(index_t **idx)
481 void OPJ_CALLCONV output_index(index_t *index)