[trunk] Start FolderReorgProposal task
[openjpeg.git] / src / lib / openjpip / openjpip.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 #include <stdlib.h>
32 #include "openjpip.h"
33 #include "jpip_parser.h"
34 #include "channel_manager.h"
35 #include "byte_manager.h"
36 #ifdef _WIN32
37 #include <io.h>
38 #else
39 #include <unistd.h>
40 #endif
41
42 #ifdef SERVER
43 #include "auxtrans_manager.h"
44 #endif
45
46 #include <stdio.h>
47 #include "dec_clientmsg_handler.h"
48 #include "jpipstream_manager.h"
49
50 #include <string.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include "jp2k_encoder.h"
55
56 #ifdef SERVER
57
58 server_record_t * init_JPIPserver( int tcp_auxport, int udp_auxport)
59 {
60   server_record_t *record = (server_record_t *)malloc( sizeof(server_record_t));
61   
62   record->sessionlist = gene_sessionlist();
63   record->targetlist  = gene_targetlist();
64   record->auxtrans = init_aux_transport( tcp_auxport, udp_auxport);
65    
66   return record;
67 }
68
69 void terminate_JPIPserver( server_record_t **rec)
70 {
71   delete_sessionlist( &(*rec)->sessionlist);
72   delete_targetlist( &(*rec)->targetlist); 
73   close_aux_transport( (*rec)->auxtrans);
74    
75   free( *rec);
76 }
77
78 QR_t * parse_querystring( const char *query_string)
79 {
80   QR_t *qr;
81
82   qr = (QR_t *)malloc( sizeof(QR_t));
83     
84   qr->query = parse_query( query_string);
85   qr->msgqueue = NULL;
86   qr->channel = NULL;
87
88   return qr;
89 }
90
91 bool process_JPIPrequest( server_record_t *rec, QR_t *qr)
92 {
93   target_param_t *target = NULL;
94   session_param_t *cursession = NULL;
95   channel_param_t *curchannel = NULL;
96
97   if( qr->query->target || qr->query->tid){
98     if( !identify_target( *(qr->query), rec->targetlist, &target))
99       return false;
100   }
101
102   if( qr->query->cid){
103     if( !associate_channel( *(qr->query), rec->sessionlist, &cursession, &curchannel))
104       return false;
105     qr->channel = curchannel;
106   }
107   
108   if( qr->query->cnew != non){
109     if( !open_channel( *(qr->query), rec->sessionlist, rec->auxtrans, target, &cursession, &curchannel))
110       return false;
111     qr->channel = curchannel;
112   }
113   
114   if( qr->query->cclose)
115     if( !close_channel( *(qr->query), rec->sessionlist, &cursession, &curchannel))
116       return false;
117   
118   if( (qr->query->fx > 0 && qr->query->fy > 0) || qr->query->box_type[0][0] != 0 || qr->query->len > 0)
119     if( !gene_JPIPstream( *(qr->query), target, cursession, curchannel, &qr->msgqueue))
120       return false;
121
122   return true;
123 }
124
125 void add_EORmsg( int fd, QR_t *qr);
126
127 void send_responsedata( server_record_t *rec, QR_t *qr)
128 {
129   int fd;
130   const char tmpfname[] = "tmpjpipstream.jpp";
131   Byte_t *jpipstream;
132   Byte8_t len_of_jpipstream;
133
134   if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
135     fprintf( FCGI_stderr, "file open error %s", tmpfname);
136     fprintf( FCGI_stdout, "Status: 503\r\n");
137     fprintf( FCGI_stdout, "Reason: Implementation failed\r\n");
138     return;
139   }
140   
141   recons_stream_from_msgqueue( qr->msgqueue, fd);
142   
143   add_EORmsg( fd, qr); /* needed at least for tcp and udp */
144   
145   len_of_jpipstream = (Byte8_t)get_filesize( fd);
146   jpipstream = fetch_bytes( fd, 0, len_of_jpipstream);
147   
148   close( fd);
149   remove( tmpfname);
150
151   fprintf( FCGI_stdout, "\r\n");
152
153   if( len_of_jpipstream){
154     
155     if( qr->channel)
156       if( qr->channel->aux == tcp || qr->channel->aux == udp){
157         send_responsedata_on_aux( qr->channel->aux==tcp, rec->auxtrans, qr->channel->cid, jpipstream, len_of_jpipstream, 1000); /* 1KB per frame*/
158         return;
159       }
160     
161     if( fwrite( jpipstream, len_of_jpipstream, 1, FCGI_stdout) != 1)
162       fprintf( FCGI_stderr, "Error: failed to write jpipstream\n");
163   }
164
165   free( jpipstream);
166
167   return;
168 }
169
170 void add_EORmsg( int fd, QR_t *qr)
171 {
172   unsigned char EOR[3];
173
174   if( qr->channel){
175     EOR[0] = 0x00;   
176     EOR[1] = is_allsent( *(qr->channel->cachemodel)) ? 0x01 : 0x02;
177     EOR[2] = 0x00;
178     if( write( fd, EOR, 3) != 3)
179       fprintf( FCGI_stderr, "Error: failed to write EOR message\n");
180   }
181 }
182
183 void end_QRprocess( server_record_t *rec, QR_t **qr)
184 {
185   /* TODO: record client preferences if necessary*/
186   (void)rec; /* unused */
187   delete_query( &((*qr)->query));
188   delete_msgqueue( &((*qr)->msgqueue));
189   free( *qr);
190 }
191
192
193 void local_log( bool query, bool messages, bool sessions, bool targets, QR_t *qr, server_record_t *rec)
194 {
195   if( query)
196     print_queryparam( *qr->query);
197
198   if( messages)
199     print_msgqueue( qr->msgqueue);
200
201   if( sessions)
202     print_allsession( rec->sessionlist);
203   
204   if( targets)
205     print_alltarget( rec->targetlist);
206 }
207
208 #endif /*SERVER*/
209
210 #ifndef SERVER
211
212 dec_server_record_t * init_dec_server( int port)
213 {
214   dec_server_record_t *record = (dec_server_record_t *)malloc( sizeof(dec_server_record_t));
215
216   record->cachelist = gene_cachelist();
217   record->jpipstream = NULL;
218   record->jpipstreamlen = 0;
219   record->msgqueue = gene_msgqueue( true, NULL);
220   record->listening_socket = open_listeningsocket( (uint16_t)port);
221
222   return record;
223 }
224
225 void terminate_dec_server( dec_server_record_t **rec)
226 {
227   delete_cachelist( &(*rec)->cachelist);  
228   free( (*rec)->jpipstream);
229   
230   if( (*rec)->msgqueue)
231     delete_msgqueue( &((*rec)->msgqueue));
232
233   if( close_socket( (*rec)->listening_socket) != 0)
234     perror("close");
235   
236   free( *rec);
237 }
238
239 client_t accept_connection( dec_server_record_t *rec)
240 {
241   client_t client;
242   
243   client = accept_socket( rec->listening_socket);
244   if( client == -1)
245     fprintf( stderr, "error: failed to connect to client\n");
246   
247   return client;
248 }
249
250 bool handle_clientreq( client_t client, dec_server_record_t *rec)
251 {
252   bool quit = false;
253   msgtype_t msgtype = identify_clientmsg( client);
254   
255   switch( msgtype){
256   case JPIPSTREAM:
257     handle_JPIPstreamMSG( client, rec->cachelist, &rec->jpipstream, &rec->jpipstreamlen, rec->msgqueue);
258     break;
259       
260   case PNMREQ:
261     handle_PNMreqMSG( client, rec->jpipstream, rec->msgqueue, rec->cachelist);
262     break;
263     
264   case XMLREQ:
265     handle_XMLreqMSG( client, rec->jpipstream, rec->cachelist);
266     break;
267
268   case TIDREQ:
269     handle_TIDreqMSG( client, rec->cachelist);
270     break;
271                                                 
272   case CIDREQ:
273     handle_CIDreqMSG( client, rec->cachelist);
274     break;
275
276   case CIDDST:
277     handle_dstCIDreqMSG( client, rec->cachelist);
278     break;
279     
280   case SIZREQ:
281     handle_SIZreqMSG( client, rec->jpipstream, rec->msgqueue, rec->cachelist);
282     break;
283
284   case JP2SAVE:
285     handle_JP2saveMSG( client, rec->cachelist, rec->msgqueue, rec->jpipstream);
286     break;
287
288   case QUIT:
289     quit = true;
290     save_codestream( rec->jpipstream, rec->jpipstreamlen, "jpt");
291     break;
292   case MSGERROR:
293     break;
294   }
295
296   fprintf( stderr, "\t end of the connection\n\n");
297   if( close_socket(client) != 0){
298     perror("close");
299     return false;
300   }
301
302   if( quit)
303     return false;
304
305   return true;
306 }
307
308
309 jpip_dec_param_t * init_jpipdecoder( bool jp2)
310 {
311   jpip_dec_param_t *dec;
312   
313   dec = (jpip_dec_param_t *)calloc( 1, sizeof(jpip_dec_param_t));
314
315   dec->msgqueue = gene_msgqueue( true, NULL);
316   
317   if( jp2)
318     dec->metadatalist = gene_metadatalist();
319
320   return dec;
321 }
322
323
324 bool fread_jpip( const char fname[], jpip_dec_param_t *dec)
325 {
326   int infd;
327
328   if(( infd = open( fname, O_RDONLY)) == -1){
329     fprintf( stderr, "file %s not exist\n", fname);
330     return false;
331   }
332   
333   if(!(dec->jpiplen = (Byte8_t)get_filesize(infd)))
334     return false;
335   
336   dec->jpipstream = (Byte_t *)malloc( dec->jpiplen);
337
338   if( read( infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen){
339     fprintf( stderr, "file reading error\n");
340     free( dec->jpipstream);
341     return false;
342   }
343
344   close(infd);
345
346   return true;
347 }
348
349 void decode_jpip( jpip_dec_param_t *dec)
350 {
351   parse_JPIPstream( dec->jpipstream, dec->jpiplen, 0, dec->msgqueue);
352
353   if( dec->metadatalist){ /* JP2 encoding*/
354     parse_metamsg( dec->msgqueue, dec->jpipstream, dec->jpiplen, dec->metadatalist);
355     dec->ihdrbox = gene_ihdrbox( dec->metadatalist, dec->jpipstream);
356     
357     dec->jp2kstream = recons_jp2( dec->msgqueue, dec->jpipstream, dec->msgqueue->first->csn, &dec->jp2klen);
358   }
359   else /* J2k encoding  */
360     /* Notice: arguments fw, fh need to be set for LRCP, PCRL, CPRL*/
361     dec->jp2kstream = recons_j2k( dec->msgqueue, dec->jpipstream, dec->msgqueue->first->csn, 0, 0, &dec->jp2klen);  
362 }
363
364 bool fwrite_jp2k( const char fname[], jpip_dec_param_t *dec)
365 {
366   int outfd;
367   
368 #ifdef _WIN32
369   if(( outfd = open( fname, O_WRONLY|O_CREAT, _S_IREAD | _S_IWRITE)) == -1){
370 #else
371  if(( outfd = open( fname, O_WRONLY|O_CREAT, S_IRWXU|S_IRWXG)) == -1){
372 #endif
373    fprintf( stderr, "file %s open error\n", fname);
374    return false;
375  }
376   
377  if( write( outfd, dec->jp2kstream, dec->jp2klen) != (int)dec->jp2klen)
378    fprintf( stderr, "j2k file write error\n");
379
380  close(outfd);
381
382  return true;
383 }
384
385 void output_log( bool messages, bool metadata, bool ihdrbox, jpip_dec_param_t *dec)
386 {
387   if( messages)
388     print_msgqueue( dec->msgqueue);
389
390   if( metadata)
391     print_allmetadata( dec->metadatalist);
392
393   if( ihdrbox){
394     printf("W*H: %d*%d\n", dec->ihdrbox->height, dec->ihdrbox->width);
395     printf("NC: %d, bpc: %d\n", dec->ihdrbox->nc, dec->ihdrbox->bpc);
396   }
397 }
398
399 void destroy_jpipdecoder( jpip_dec_param_t **dec)
400 {
401   free( (*dec)->jpipstream);
402   delete_msgqueue( &(*dec)->msgqueue);
403   if( (*dec)->metadatalist){
404     delete_metadatalist( &(*dec)->metadatalist);
405     free( (*dec)->ihdrbox);
406   }
407
408   free( (*dec)->jp2kstream);
409   free( *dec);
410 }
411
412 index_t * get_index_from_JP2file( int fd)
413 {
414   char *data;
415  
416   /* Check resource is a JP family file.*/
417   if( lseek( fd, 0, SEEK_SET)==-1){
418     fprintf( stderr, "Error: File broken (lseek error)\n");
419     return NULL;
420   }
421   
422   data = (char *)malloc( 12); /* size of header*/
423   if( read( fd, data, 12) != 12){
424     free( data);
425     fprintf( stderr, "Error: File broken (read error)\n");
426     return NULL;
427   }
428     
429   if( *data || *(data + 1) || *(data + 2) ||
430       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
431     free( data);
432     fprintf( stderr, "Error: No JPEG 2000 Signature box in this file\n");
433     return NULL;
434   }
435   free( data);
436   
437   return parse_jp2file( fd);
438 }
439
440 void destroy_index( index_t **idx)
441 {
442   delete_index( idx);
443 }
444
445 void output_index( index_t *index)
446 {
447   print_index( *index);
448 }
449
450 #endif /*SERVER*/