Coverage Report

Created: 2025-06-16 07:00

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