1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France
4  * Copyright (c) 2012, CS Systemes d'Information, France
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "opj_includes.h"
30 
31 /* ==========================================================
32      Utility functions
33    ==========================================================*/
34 
35 #ifdef OPJ_CODE_NOT_USED
36 #ifndef _WIN32
37 static char*
i2a(unsigned i,char * a,unsigned r)38 i2a(unsigned i, char *a, unsigned r) {
39 	if (i/r > 0) a = i2a(i/r,a,r);
40 	*a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%r];
41 	return a+1;
42 }
43 
44 /**
45  Transforms integer i into an ascii string and stores the result in a;
46  string is encoded in the base indicated by r.
47  @param i Number to be converted
48  @param a String result
49  @param r Base of value; must be in the range 2 - 36
50  @return Returns a
51 */
52 static char *
_itoa(int i,char * a,int r)53 _itoa(int i, char *a, int r) {
54 	r = ((r < 2) || (r > 36)) ? 10 : r;
55 	if(i < 0) {
56 		*a = '-';
57 		*i2a(-i, a+1, r) = 0;
58 	}
59 	else *i2a(i, a, r) = 0;
60 	return a;
61 }
62 
63 #endif /* !_WIN32 */
64 #endif
65 
66 /* ----------------------------------------------------------------------- */
67 /**
68  * Default callback function.
69  * Do nothing.
70  */
opj_default_callback(const char * msg,void * client_data)71 static void opj_default_callback (const char *msg, void *client_data)
72 {
73     OPJ_ARG_NOT_USED(msg);
74     OPJ_ARG_NOT_USED(client_data);
75 }
76 
77 /* ----------------------------------------------------------------------- */
78 
79 
80 /* ----------------------------------------------------------------------- */
opj_event_msg(opj_event_mgr_t * p_event_mgr,OPJ_INT32 event_type,const char * fmt,...)81 OPJ_BOOL opj_event_msg(opj_event_mgr_t* p_event_mgr, OPJ_INT32 event_type, const char *fmt, ...) {
82 #define OPJ_MSG_SIZE 512 /* 512 bytes should be more than enough for a short message */
83 	opj_msg_callback msg_handler = 00;
84 	void * l_data = 00;
85 
86 	if(p_event_mgr != 00) {
87 		switch(event_type) {
88 			case EVT_ERROR:
89 				msg_handler = p_event_mgr->error_handler;
90 				l_data = p_event_mgr->m_error_data;
91 				break;
92 			case EVT_WARNING:
93 				msg_handler = p_event_mgr->warning_handler;
94 				l_data = p_event_mgr->m_warning_data;
95 				break;
96 			case EVT_INFO:
97 				msg_handler = p_event_mgr->info_handler;
98 				l_data = p_event_mgr->m_info_data;
99 				break;
100 			default:
101 				break;
102 		}
103 		if(msg_handler == 00) {
104 			return OPJ_FALSE;
105 		}
106 	} else {
107 		return OPJ_FALSE;
108 	}
109 
110 	if ((fmt != 00) && (p_event_mgr != 00)) {
111 		va_list arg;
112 		size_t str_length/*, i, j*/; /* UniPG */
113 		char message[OPJ_MSG_SIZE];
114 		memset(message, 0, OPJ_MSG_SIZE);
115 		/* initialize the optional parameter list */
116 		va_start(arg, fmt);
117 		/* check the length of the format string */
118 		str_length = (strlen(fmt) > OPJ_MSG_SIZE) ? OPJ_MSG_SIZE : strlen(fmt);
119         (void)str_length;
120 		/* parse the format string and put the result in 'message' */
121 		vsprintf(message, fmt, arg); /* UniPG */
122 		/* deinitialize the optional parameter list */
123 		va_end(arg);
124 
125 		/* output the message to the user program */
126 		msg_handler(message, l_data);
127 	}
128 
129 	return OPJ_TRUE;
130 }
131 
opj_set_default_event_handler(opj_event_mgr_t * p_manager)132 void opj_set_default_event_handler(opj_event_mgr_t * p_manager)
133 {
134 	p_manager->m_error_data = 00;
135 	p_manager->m_warning_data = 00;
136 	p_manager->m_info_data = 00;
137 	p_manager->error_handler = opj_default_callback;
138 	p_manager->info_handler = opj_default_callback;
139 	p_manager->warning_handler = opj_default_callback;
140 }
141 
142