summaryrefslogtreecommitdiffstats
path: root/include/linux
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux')
0 files changed, 0 insertions, 0 deletions
n45'>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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/*
   Copyright (C) 2009 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 <string.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <spice/macros.h>

#define TAB "    "

#define ERROR(str) printf("%s: error: %s\n", prog_name, str); exit(-1);

static char *prog_name = NULL;

static size_t read_input(const char *file_name, uint8_t** out_buf)
{
    int fd = open(file_name, O_RDONLY);
    if (fd == -1) {
        ERROR("open source file failed");
        return 0;
    }
    struct stat file_stat;
    if (fstat(fd, &file_stat) == -1) {
        ERROR("fstat on source file failed");
        return 0;
    }

    uint8_t *buf = malloc(file_stat.st_size);

    if (!buf) {
        close(fd);
        ERROR("alloc mem failed");
        return 0;
    }

    size_t to_read = file_stat.st_size;
    uint8_t *buf_pos = buf;
    while (to_read) {
        int n = read(fd, buf_pos, to_read);
        if (n <= 0) {
            ERROR("read from source file failed");
            close(fd);
            free(buf);
            return 0;
        }
        to_read -= n;
        buf_pos += n;
    }
    close(fd);
    *out_buf = buf;
    return file_stat.st_size;
}

typedef struct __attribute__ ((__packed__)) BitmpaHeader {
    uint32_t header_size;
    uint32_t width;
    uint32_t height;
    uint16_t plans;
    uint16_t bpp;
    uint32_t compression;
    uint32_t image_size;
    uint32_t horizontal_resolution;
    uint32_t vertical_resolution;
    uint32_t num_colors;
    uint32_t important_colors;
} BitmpaHeader;


typedef struct __attribute__ ((__packed__)) ICOHeader {
    uint8_t width;
    uint8_t height;
    uint8_t colors_count;
    uint8_t reserved;
    uint16_t plans;
    uint16_t bpp;
    uint32_t bitmap_size;
    uint32_t bitmap_offset;
} ICOHeader;

typedef struct __attribute__ ((__packed__)) ICOFileHeader {
    uint16_t reserved;
    uint16_t type;
    uint16_t image_count;
    ICOHeader directory[0];
} ICOFileHeader;


typedef struct Icon {
    int width;
    int height;
    uint8_t* pixmap;
    uint8_t* mask;
} Icon;


static Icon *init_icon(uint8_t *buf, size_t buf_size)
{
    ICOFileHeader *file_header;
    int i;

    uint8_t *buf_end = buf + buf_size;

    if (buf_size < sizeof(ICOFileHeader)) {
        ERROR("invalid source file");
        return NULL;
    }

    file_header = (ICOFileHeader *)buf;

    if (file_header->reserved != 0 || file_header->type != 1) {
        ERROR("invalid icon");
        return NULL;
    }

    if (sizeof(ICOFileHeader) + file_header->image_count * sizeof(ICOHeader) > buf_size) {
        ERROR("invalid source file");
    }

    for (i = 0; i < file_header->image_count; i++) {
        int j;

        ICOHeader *ico = &file_header->directory[i];
        printf("%ux%ux%u size %u\n", (unsigned)ico->width, (unsigned)ico->height,
               (unsigned)ico->bpp, ico->bitmap_size);

        if (ico->bitmap_offset + ico->bitmap_size > buf_size) {
            ERROR("invalid source file");
        }
        BitmpaHeader *bitmap = (BitmpaHeader *)(buf + ico->bitmap_offset);
        if (bitmap->header_size != 40) {
            ERROR("invalid bitmap header");
        }

        if (ico->plans == 0) { // 0 and 1 are equivalent
            ico->plans = 1;
        }

        if (bitmap->width != ico->width || bitmap->height != ico->height * 2 ||
            bitmap->plans != ico->plans || bitmap->bpp != ico->bpp) {
            ERROR("invalid bitmap header");
        }

        if (!bitmap->image_size) {
            bitmap->image_size = SPICE_ALIGN(bitmap->bpp * bitmap->width, 32) / 8 * bitmap->height;
        }

        if (bitmap->compression || bitmap->horizontal_resolution || bitmap->vertical_resolution ||
                                                   bitmap->num_colors || bitmap->important_colors) {
            ERROR("invalid bitmap header");
        }

        if (ico->width != 32 || ico->height != 32 || ico->bpp != 32) {
            continue;
        }

        int pixmap_size = bitmap->width * sizeof(uint32_t) * ico->height;
        int mask_size = SPICE_ALIGN(bitmap->width, 8) / 8 * ico->height;
        Icon* icon = malloc(sizeof(*icon) + pixmap_size + mask_size);
        icon->width = ico->width;
        icon->height = ico->height;
        icon->pixmap = (uint8_t *)(icon + 1);
        icon->mask = icon->pixmap + pixmap_size;

        if ((uint8_t *)(bitmap + 1) + pixmap_size + mask_size > buf_end) {
            ERROR("invalid source file");
        }
        memcpy(icon->pixmap, bitmap + 1, pixmap_size);
        memcpy(icon->mask, (uint8_t *)(bitmap + 1) + pixmap_size, mask_size);
        for (j = 0; j < mask_size; j++) {
            icon->mask[j] = ~icon->mask[j];
        }
        return icon;
    }
    printf("%s: missing 32x32x32\n", prog_name);
    return NULL;
}

static inline void put_char(FILE* f, uint8_t val)
{
    fprintf(f, "0x%.2x,", val);
}

static int icon_to_c_struct(const Icon *icon, const char *dest_file, const char *image_name)
{
    int i, j;
    int fd;
    FILE *f;

    if ((fd = open(dest_file, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
        ERROR("open dest file failed");
        return -1;
    }

    if (!(f = fdopen(fd, "w"))) {
        ERROR("fdopen failed");
        close(fd);
        return -1;
    }
    uint32_t pixmap_stride = icon->width * sizeof(uint32_t);
    uint32_t pixmap_size = pixmap_stride * icon->height;
    uint32_t mask_stride = SPICE_ALIGN(icon->width, 8) / 8;
    uint32_t mask_size = mask_stride * icon->height;
    fprintf(f, "static const struct {\n"
               TAB "uint32_t width;\n"
               TAB "uint32_t height;\n"
               TAB "uint8_t pixmap[%u];\n"