summaryrefslogtreecommitdiffstats
path: root/src/libr-icons.c
blob: 18dc5367bf49b43ba564278d88d680d46aacee5f (plain)
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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*
 *
 *  Copyright (c) 2008-2011 Erich Hoover
 *
 *  libr icons - Add icon resources into ELF binaries
 *
 * This program 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 program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * To provide feedback, report bugs, or otherwise contact me:
 * ehoover at mines dot edu
 *
 */

/* Include compile-time parameters */
#include "config.h"

#include "libr-icons.h"

/* For "one canvas" SVG documents */
#include "onecanvas.h"

/* For string manipulation */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

/* For handling files */
#include <sys/stat.h>

/* For C99 number types */
#include <stdint.h>

#define ICON_SECTION     ".icon"
#define TERM_LEN         1

#define OFFSET_ENTRIES   0
#define OFFSET_GUID      OFFSET_ENTRIES+sizeof(uint32_t)

#if defined(__i386)
	#define ID12FORMAT "%012llx"
#elif defined(__x86_64)
	#define ID12FORMAT "%012lx"
#else
	#define ID12FORMAT "%012lx"
	#warning "string formatting may be incorrect on this architecture."
#endif

#ifndef DOXYGEN_SHOULD_SKIP_THIS

typedef uint32_t ID8;
typedef uint16_t ID4;
typedef struct {uint64_t p:48;} __attribute__((__packed__)) ID12;

typedef struct {
	ID8  g1;
	ID4  g2;
	ID4  g3;
	ID4  g4;
	ID12 g5;
} __attribute__((__packed__)) UUID;

typedef struct {
	char *name;
	size_t offset;
	size_t entry_size;
	libr_icontype_t type;
	unsigned int icon_size;
} iconentry;

typedef struct{
	size_t size;
	char *buffer;
	iconentry entry;
} iconlist;

#endif /* DOXYGEN_SHOULD_SKIP_THIS */

/*
 * Decode a UUID to its binary representation
 *
 * NOTE: The last 12-bit parameter cannot be obtained using (uint64_t *) with
 * some versions of GCC using some optimization levels.  This problem is very
 * frustrating to debug, so I do not recommend playing with it yourself.
 */
UUID guid_decode(char *guid)
{
	UUID id = {0x00000000, 0x0000, 0x0000, 0x0000, {0x000000000000} };
	uint64_t tmp12;
	
	sscanf(guid, "%08x-%04hx-%04hx-%04hx-" ID12FORMAT, &id.g1, &id.g2, &id.g3, &id.g4, &tmp12);
	id.g5.p = tmp12;
	return id;
}

/*
 * Return the size of the file represented by the file stream
 */
off_t fsize(FILE *handle)
{
	struct stat file_stat;
	
	if(fstat(fileno(handle), &file_stat) == ERROR)
		return ERROR;
	return file_stat.st_size;
}

/*
 * Create a new icon handle
 */
libr_icon *new_icon_handle(libr_icontype_t type, unsigned int icon_size, char *buffer, size_t buffer_size)
{
	libr_icon *icon_handle = (libr_icon *) malloc(sizeof(libr_icon));
	
	icon_handle->type = type;
	icon_handle->buffer = buffer;
	icon_handle->icon_size = icon_size;
	icon_handle->buffer_size = buffer_size;
	return icon_handle;
}

/*
 * Obtain an existing icon resource list
 */
int get_iconlist(libr_file *file_handle, iconlist *icons)
{
	if(icons == NULL)
	{
		/* Need to be able to return SOMETHING */
		return false;
	}
	/* Obtain the icon resource list */
	icons->buffer = libr_malloc(file_handle, ICON_SECTION, &(icons->size));
	if(icons->buffer == NULL)
		return false;
	return true;
}

/*
 * Get the next entry in an icon resource list
 */
iconentry *get_nexticon(iconlist *icons, iconentry *last_entry)
{
	size_t i;
	
	/* The icon list is needed both for the data buffer and for a call-specific iconentry instance */ 
	if(icons == NULL)
		return NULL;
	/* If this is the first call (last_entry == NULL) then return the first entry */
	if(last_entry == NULL)
		icons->entry.offset = sizeof(uint32_t)+sizeof(UUID);
	else
		icons->entry.offset += icons->entry.entry_size;
	/* Check to see if we've run out of entries */
	if(icons->entry.offset >= icons->size)
		return NULL;
	i = icons->entry.offset;
	memcpy(&(icons->entry.entry_size), &(icons->buffer[i]), sizeof(uint32_t));
	i += sizeof(uint32_t);
	icons->entry.type = icons->buffer[i];
	i += sizeof(unsigned char);
	switch(icons->entry.type)
	{
		case LIBR_SVG:
			icons->entry.icon_size = 0;
			icons->entry.name = &(icons->buffer[i]);
			break;
		case LIBR_PNG:
			memcpy(&(icons->entry.icon_size), &(icons->buffer[i]), sizeof(uint32_t));
			i += sizeof(uint32_t);
			icons->entry.name = &(icons->buffer[i]);
			break;
		default:
			/* Invalid entry type */
			return NULL;
	}
	return &(icons->entry);
}

/*
 * Free an icon handle
 */
EXPORT_FN int libr_icon_close(libr_icon *icon)
{
	if(icon == NULL)
		return false;
	if(icon->buffer == NULL)
		return false;
	free(icon->buffer);
	free(icon);
	return true;
}

/*
 * Read an icon resource from an ELF file by name
 */
EXPORT_FN libr_icon *libr_icon_geticon_byname(libr_file *handle, char *icon_name)
{
	iconentry *entry = NULL;
	libr_icon *icon = NULL;
	size_t buffer_size = 0;
	unsigned int icon_size;
	libr_icontype_t type;
	char *buffer = NULL;
	int inlist = false;
	iconlist icons;
	
	if(!get_iconlist(handle, &icons))
	{
		/* Failed to obtain a list of ELF icons */
		return NULL;
	}
	/* Look for the icon name in the entry list */
	while((entry = get_nexticon(&icons, entry)) != NULL)
	{
		if(!strcmp(entry->name, icon_name))
		{
			type = entry->type;
			icon_size = entry->icon_size;
			inlist = true;
			break;
		}
	}
	if(!inlist)
	{
		/* Could not find icon name in the list of icons */
		return false;
	}
	/* Get the icon from the ELF binary */
	if(!libr_size(handle, icon_name, &buffer_size))
	{
		/* Failed to obtain ELF icon size */
		return NULL;
	}
	/* Allocate memory for the icon */
	buffer = (char *) malloc(buffer_size);
	if(buffer == NULL)
	{
		/* Failed to allocate memory for icon */
		return NULL;
	}
	/* Get the compressed icon from the ELF file */
	if(!libr_read(handle, icon_name, buffer))
	{
		/* Failed to obtain ELF icon */
		goto geticon_byname_complete;
	}
	icon = new_icon_handle(type, icon_size, buffer, buffer_size);
	
geticon_byname_complete:
	if(icon == NULL)
		free(buffer);
	return icon;
}

/*
 * Read an icon resource from an ELF file by the square icon size
 */
EXPORT_FN libr_icon *libr_icon_geticon_bysize(libr_file *handle, unsigned int iconsize)
{
	unsigned int closest_id = 0, i = 0, j = 0;
	int found_png = false, found_svg = false;
	unsigned long closest_size = 0;
	iconentry *entry = NULL;
	iconlist icons;
	
	if(!get_iconlist(handle, &icons))
	{
		/* Failed to obtain a list of ELF icons */
		return NULL;
	}
	/* Look for the closest size match, ignore SVG in case there are multiple icons */
	while((entry = get_nexticon(&icons, entry)) != NULL)
	{
		if(entry->type == LIBR_SVG)
			found_svg = true;
		if(entry->type == LIBR_PNG)
		{
			if(j == 0)
			{
				closest_size = entry->icon_size;
				found_png = true;
			}
			if(abs(iconsize-entry->icon_size) < closest_size)
			{
				closest_size = entry->icon_size;
				closest_id = i;
			}
			j++;
		}
		i++;
	}
	/* If any PNG files were found then use the file if:
	 *  1) There are no SVG files <OR>
	 *  2) The PNG is an EXACT size match 
	 */
	if(found_png)
	{
		i=0;
		entry = NULL;
		while((entry = get_nexticon(&icons, entry)) != NULL)
		{
			if(i == closest_id)
			{
				if(entry->icon_size == iconsize || !found_svg)
					return libr_icon_geticon_byname(handle, entry->name);
				break;
			}
			i++;
		}
	}
	/* Otherwise use the SVG (provided that there is one) */
	if(found_svg)
	{
		entry = NULL;
		while((entry = get_nexticon(&icons, entry)) != NULL)
		{
			if(entry->type == LIBR_SVG)
			{
				libr_icon *icon = libr_icon_geticon_byname(handle, entry->name);
				if (icon) {
					libr_icon *icon_onecanvas;
					char *buffer;
				
					/* should we report the requested size for SVG? */
					icon->icon_size = iconsize;
				
					/* if the SVG is a "one canvas" document then extract the correctly sized icon */
					if((buffer = onecanvas_geticon_bysize(icon->buffer, iconsize)) != NULL)
					{
						libr_icon_close(icon);
						icon_onecanvas = new_icon_handle(LIBR_SVG, iconsize, buffer, strlen(buffer));
						return icon_onecanvas;
					}
				}
				return icon;
			}
		}
	}
	/* Give up */
	return NULL;
}

/*
 * Obtains the icon UUID for the ELF file
 */
EXPORT_FN int libr_icon_getuuid(libr_file *handle, char *uuid)
{
	UUID id = {0x00000000, 0x0000, 0x0000, 0x0000, {0x000000000000} };
	iconlist icons;
	
	if(!get_iconlist(handle, &icons))
	{
		/* Failed to obtain the list of ELF icons */
		return false;
	}
	/* Now store the GUID to the return string */
	memcpy(&id, &(icons.buffer[OFFSET_GUID]), sizeof(UUID));
	snprintf(uuid, GUIDSTR_LENGTH, "%08x-%04hx-%04hx-%04hx-" ID12FORMAT "\n", id.g1, id.g2, id.g3, id.g4, (uint64_t) id.g5.p);
	free(icons.buffer);
	return true;
}
EXPORT_FN int libr_icon_getguid(libr_file *handle, char *uuid) ALIAS_FN(libr_icon_getuuid);

/*
 * Allocate a buffer containing the data of an icon
 */
EXPORT_FN char *libr_icon_malloc(libr_icon *icon, size_t *size)
{
	char *iconfile = NULL;
	
	if(size == NULL)
	{
		/* No return size passed */
		return NULL;
	}
	if(!libr_icon_size(icon, size))
	{
		/* Failed to obtain embedded icon file size */
		return NULL;
	}
	iconfile = (char *) malloc(*size);
	if(!libr_icon_read(icon, iconfile))
	{
		/* Failed to obtain embedded icon file */
		free(iconfile);
		return NULL;
	}
	return iconfile;
}

/*
 * Create an icon resource to represent a file on the hard disk 
 */
EXPORT_FN libr_icon *libr_icon_newicon_byfile(libr_icontype_t type, unsigned int icon_size, char *icon_file)
{
	libr_icon *icon_handle = NULL;
	size_t len, buffer_size = 0;
	char *buffer = NULL;
	FILE *handle = NULL;
	
	/* Open a handle to the icon file */
	if((handle = fopen(icon_file, "r")) == NULL)
	{
		/* Failed to open icon file */
		return NULL;
	}
	/* Get the size of the icon file */
	if((buffer_size = fsize(handle)) == ERROR)
	{
		/* Failed to obtain the icon's file size */
		return NULL;
	}
	/* Allocate a buffer for the uncompressed icon */
	buffer = (char *) malloc(buffer_size);
	if(buffer == NULL)
	{
		/* Failed to allocate a buffer for the icon data */
		return NULL;
	}
	/* Read the uncompressed image from the disk */
	if((len = fread(buffer, 1, buffer_size, handle)) <= 0)
	{
		/* Failed to read icon from disk */
		goto newicon_complete;
	}
	fclose(handle);
	if(len != buffer_size)
	{
		/* Failed to read the entire icon */
		goto newicon_complete;
	}
	/* Allocate the icon handle */
	icon_handle = new_icon_handle(type, icon_size, buffer, buffer_size);
	
newicon_complete:
	if(icon_handle == NULL)
		free(buffer);
	return icon_handle;
}

/*
 * Copy the icon resource into a buffer
 */
EXPORT_FN int libr_icon_read(libr_icon *icon, char *buffer)
{
	if(icon == NULL)
		return false;
	memcpy(buffer, icon->buffer, icon->buffer_size);
	return true;
}

/*
 * Get the memory size of an icon resource
 */
EXPORT_FN int libr_icon_size(libr_icon *icon, size_t *size)
{
	if(icon == NULL)
		return false;
	*size = icon->buffer_size;
	return true;
}

/*
 * Save the icon resource to a file
 */
EXPORT_FN int libr_icon_save(libr_icon *icon, char *filename)
{
	FILE *file = NULL;
	int ret = false;
	size_t len;
	
	if(icon == NULL)
		return false;
	/* Open the file to store the image */
	if((file = fopen(filename, "w")) == NULL)
	{
		/* Failed to open file to write the icon */
		return false;
	}
	/* Store the uncompressed icon to disk */
	if((len = fwrite(icon->buffer, 1, icon->buffer_size, file)) <= 0)
	{
		/* Failed to write output file */
		goto saveicon_complete;
	}
	if(len != icon->buffer_size)
	{
		/* Did not write the entire file */ 
		goto saveicon_complete;
	}
	ret = true;
	
saveicon_complete:
	/* Close remaining resources */
	fclose(file);
	return ret;
}

/*
 * Sets the icon GUID for the ELF file
 */
EXPORT_FN int libr_icon_setuuid(libr_file *handle, char *guid)
{
	int ret = false;
	iconlist icons;
	UUID id;
	int i;
	
	/* First check the GUID string */
	for(i=0;i<strlen(guid);i++)
	{
		if(!isxdigit(guid[i]))
		{
			if(guid[i] == '-' && (i == 8 || i == 13 || i == 18 || i == 23))
				continue;
			/* not a valid GUID string */
			return false;
		}
	}
	id = guid_decode(guid);
	/* Now check existing resources */
	if(!get_iconlist(handle, &icons))
	{
		/* No icons exist in the file, create a new icon section with the GUID */
		uint32_t entries = 0;
		
		icons.size = sizeof(uint32_t)+sizeof(UUID);
		icons.buffer = (char *) malloc(icons.size);
		memcpy(&(icons.buffer[OFFSET_ENTRIES]), &entries, sizeof(uint32_t));
	}
	/* Set the GUID and write the resource */
	if(!libr_write(handle, ICON_SECTION, icons.buffer, icons.size, LIBR_UNCOMPRESSED, LIBR_OVERWRITE))
	{
		/* failed to write icon resource */
		goto setguid_complete;
	}
	ret = true;
	
setguid_complete:
	free(icons.buffer);
	return ret;
}
EXPORT_FN int libr_icon_setguid(libr_file *handle, char *uuid) ALIAS_FN(libr_icon_setuuid);

/*
 * Add an icon resource to an ELF file
 */
EXPORT_FN int libr_icon_write(libr_file *handle, libr_icon *icon, char *icon_name, libr_overwrite_t overwrite)
{
	size_t entry_size, i;
	iconentry *entry = NULL;
	iconlist icons;
	int ret = false;
	
	/* Check to make sure the user did not make a poor name choice */
	if(!strcmp(icon_name, ICON_SECTION))
	{
		/* ".icon" is a reserved section name */
		return false;
	
	}
	/* Check to make sure the file supports icon resources */
	if(!get_iconlist(handle, &icons))
	{
		/* A GUID must be set first */
		return false;
	}
	/* First add the icon as a new named section */
	if(!libr_write(handle, icon_name, icon->buffer, icon->buffer_size, LIBR_COMPRESSED, overwrite))
	{
		/* Failed to add the icon as a resource */
		goto writeicon_complete;
	}
	/* Look to see if the icon already has an entry */
	while((entry = get_nexticon(&icons, entry)) != NULL)
	{
		if(!strcmp(entry->name, icon_name))
		{
			ret = true;
			goto writeicon_complete;
		}
	}
	/* Now add the icon to the list of icon resources in the ".icon" section */
	switch(icon->type)
	{
		case LIBR_SVG:
			entry_size = sizeof(uint32_t)+sizeof(unsigned char)+strlen(icon_name)+TERM_LEN;
			break;
		case LIBR_PNG:
			entry_size = sizeof(uint32_t)+sizeof(unsigned char)+sizeof(uint32_t)+strlen(icon_name)+TERM_LEN;
			break;
		default:
			/* Unhandled icon type */
			goto writeicon_complete;
	}
	icons.buffer = (char *) realloc(icons.buffer, icons.size+entry_size);
	if(icons.buffer == NULL)
	{
		/* Failed to expand memory size */
		goto writeicon_complete;
	}
	i = icons.size; 
	memcpy(&(icons.buffer[i]), &entry_size, sizeof(uint32_t));
	i+=sizeof(uint32_t);
	icons.buffer[i] = icon->type;
	i+=sizeof(unsigned char);
	if(icon->type == LIBR_PNG)
	{
		memcpy(&(icons.buffer[i]), &icon->icon_size, sizeof(uint32_t));
		i+=sizeof(uint32_t);
	}
	memcpy(&(icons.buffer[i]), icon_name, strlen(icon_name));
	i+=strlen(icon_name);
	icons.buffer[i] = '\0';
	icons.size += entry_size;
	if(i != (icons.size-1))
		printf("Really dangerous, buffer size mismatch!\n");
	/* Write the updated icon table */
	if(!libr_write(handle, ICON_SECTION, icons.buffer, icons.size, LIBR_UNCOMPRESSED, LIBR_OVERWRITE))
	{
		/* failed to write icon resource */
		goto writeicon_complete;
	}
	ret = true;
	
writeicon_complete:
	if(icons.buffer)
		free(icons.buffer);
	return ret;
}