File: utils.c

package info (click to toggle)
spice 0.16.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,460 kB
  • sloc: ansic: 43,305; cpp: 30,185; sh: 5,342; python: 3,040; makefile: 844
file content (128 lines) | stat: -rw-r--r-- 3,823 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
   Copyright (C) 2009-2015 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>

#include <glib.h>
#include <spice/enums.h>
#include <openssl/err.h>
#include <common/macros.h>

#include "utils.h"

int rgb32_data_has_alpha(int width, int height, size_t stride,
                         const uint8_t *data, int *all_set_out)
{
    const uint8_t *line, *end;
    uint8_t alpha;
    int has_alpha;

    has_alpha = FALSE;
    while (height-- > 0) {
        line = data;
        end = line + sizeof(uint32_t) * width;
        data += stride;
        while (line != end) {
            alpha = line[3];
            if (alpha != 0) {
                has_alpha = TRUE;
                if (alpha != 0xffU) {
                    *all_set_out = FALSE;
                    return TRUE;
                }
            }
            line += sizeof(uint32_t);
        }
    }

    *all_set_out = has_alpha;
    return has_alpha;
}

/* These names are used to parse command line options, don't change them */
static const char *const channel_names[] = {
    [ SPICE_CHANNEL_MAIN     ] = "main",
    [ SPICE_CHANNEL_DISPLAY  ] = "display",
    [ SPICE_CHANNEL_INPUTS   ] = "inputs",
    [ SPICE_CHANNEL_CURSOR   ] = "cursor",
    [ SPICE_CHANNEL_PLAYBACK ] = "playback",
    [ SPICE_CHANNEL_RECORD   ] = "record",
    [ SPICE_CHANNEL_TUNNEL   ] = "tunnel",
    [ SPICE_CHANNEL_SMARTCARD] = "smartcard",
    [ SPICE_CHANNEL_USBREDIR ] = "usbredir",
    [ SPICE_CHANNEL_PORT     ] = "port",
    [ SPICE_CHANNEL_WEBDAV   ] = "webdav",
};

/* Make sure the last channel in the protocol has a name.
 * We don't want to do this check in all cases as this would make code
 * fail to compile if there are additional channels in the protocol so
 * do this check only if ENABLE_EXTRA_CHECKS is enabled */
#if ENABLE_EXTRA_CHECKS
verify(G_N_ELEMENTS(channel_names) == SPICE_END_CHANNEL);
#endif

/**
 * red_channel_type_to_str:
 *
 * This function returns a human-readable name from a SPICE_CHANNEL_* type.
 * It must be called with a valid channel type.
 *
 * Returns: NULL if the channel type is invalid, the channel name otherwise.
 */
const char *red_channel_type_to_str(int type)
{
    g_return_val_if_fail(type >= 0, NULL);
    g_return_val_if_fail(type < G_N_ELEMENTS(channel_names), NULL);
    g_return_val_if_fail(channel_names[type] != NULL, NULL);

    return channel_names[type];
}

/**
 * red_channel_name_to_type:
 *
 * Converts a channel name to a SPICE_CHANNEL_* type. These names are currently
 * used in our public API (see spice_server_set_channel_security()).
 *
 * Returns: -1 if @name was not a known channel name, the channel
 * type otherwise.
 *
 */
int red_channel_name_to_type(const char *name)
{
    int i;

    for (i = 0; i < G_N_ELEMENTS(channel_names); i++) {
        if (g_strcmp0(channel_names[i], name) == 0) {
            return i;
        }
    }
    return -1;
}

void red_dump_openssl_errors(void)
{
    int ssl_error;

    ssl_error = ERR_get_error();
    while (ssl_error != 0) {
        char error_str[256];
        ERR_error_string_n(ssl_error, error_str, sizeof(error_str));
        g_warning("%s", error_str);
        ssl_error = ERR_get_error();
    }
}