blob.c 15.1 KB
Newer Older
1 2 3
/* blob.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6
 * All rights reserved.
 *
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11 12 13
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#include <malloc.h>
#include <string.h>
14
#include <stdio.h>
15 16

#include "apk_blob.h"
17 18 19 20 21 22
#include "apk_hash.h"

struct apk_blob_atom {
	struct hlist_node hash_node;
	apk_blob_t blob;
};
23 24 25 26 27

char *apk_blob_cstr(apk_blob_t blob)
{
	char *cstr;

28
	if (blob.len == 0)
29 30
		return strdup("");

31 32 33 34 35 36 37 38 39 40
	if (blob.ptr[blob.len-1] == 0)
		return strdup(blob.ptr);

	cstr = malloc(blob.len + 1);
	memcpy(cstr, blob.ptr, blob.len);
	cstr[blob.len] = 0;

	return cstr;
}

Timo Teräs's avatar
Timo Teräs committed
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
#if defined(__i386__)
static unsigned long inline memspn(
		const unsigned char *ptr,
		unsigned long len,
		const unsigned char *array)
{
	unsigned long p = len;

	__asm__("cld ; xorl %%eax, %%eax\n"
		"1:\n"
		"lodsb\n"
		"btl %%eax, %2\n"
		"jnc 2f\n"
		"decl %1\n"
		"jnz 1b\n"
		"2:\n"
		: "+&S" (ptr), "+&r" (p)
		: "m" (*array)
		: "cc", "%eax");

	return p;
}

static unsigned long inline memcspn(
		const unsigned char *ptr,
		unsigned long len,
		const unsigned char *array)
{
	unsigned long p = len;

	__asm__("cld ; xorl %%eax, %%eax\n"
		"1:\n"
		"lodsb\n"
		"btl %%eax, %2\n"
		"jc 2f\n"
		"decl %1\n"
		"jnz 1b\n"
		"2:\n"
		: "+&S" (ptr), "+&r" (p)
		: "m" (*array)
		: "cc", "%eax");

	return p;
}

int apk_blob_spn(apk_blob_t blob, const apk_spn_match accept, apk_blob_t *l, apk_blob_t *r)
{
	unsigned int i;

	if (blob.len == 0)
		return 0;
	i = blob.len - memspn((unsigned char*) blob.ptr, blob.len, accept);
	if (i == blob.len)
		return 0;
	if (l != NULL)
		*l = APK_BLOB_PTR_LEN(blob.ptr, i);
	if (r != NULL)
		*r = APK_BLOB_PTR_LEN(blob.ptr+i, blob.len-i);
	return 1;
}

int apk_blob_cspn(apk_blob_t blob, const apk_spn_match reject, apk_blob_t *l, apk_blob_t *r)
{
	unsigned int i;

	if (blob.len == 0)
		return 0;
	i = blob.len - memcspn((unsigned char*) blob.ptr, blob.len, reject);
	if (i == blob.len)
		return 0;
	if (l != NULL)
		*l = APK_BLOB_PTR_LEN(blob.ptr, i);
	if (r != NULL)
		*r = APK_BLOB_PTR_LEN(blob.ptr+i, blob.len-i);
	return 1;
}
#else
static int inline test_bit(const unsigned char *array, unsigned bit)
{
	return array[bit >> 3] & (1 << (bit & 7));
}

int apk_blob_spn(apk_blob_t blob, const apk_spn_match accept, apk_blob_t *l, apk_blob_t *r)
Timo Teräs's avatar
Timo Teräs committed
124 125 126 127
{
	int i;

	for (i = 0; i < blob.len; i++) {
Timo Teräs's avatar
Timo Teräs committed
128
		if (!test_bit(accept, blob.ptr[i])) {
129 130 131 132
			if (l != NULL)
				*l = APK_BLOB_PTR_LEN(blob.ptr, i);
			if (r != NULL)
				*r = APK_BLOB_PTR_LEN(blob.ptr+i, blob.len-i);
Timo Teräs's avatar
Timo Teräs committed
133 134 135 136 137 138
			return 1;
		}
	}
	return 0;
}

Timo Teräs's avatar
Timo Teräs committed
139
int apk_blob_cspn(apk_blob_t blob, const apk_spn_match reject, apk_blob_t *l, apk_blob_t *r)
Timo Teräs's avatar
Timo Teräs committed
140 141 142 143
{
	int i;

	for (i = 0; i < blob.len; i++) {
Timo Teräs's avatar
Timo Teräs committed
144
		if (test_bit(reject, blob.ptr[i])) {
145 146 147 148
			if (l != NULL)
				*l = APK_BLOB_PTR_LEN(blob.ptr, i);
			if (r != NULL)
				*r = APK_BLOB_PTR_LEN(blob.ptr+i, blob.len-i);
Timo Teräs's avatar
Timo Teräs committed
149 150 151 152 153
			return 1;
		}
	}
	return 0;
}
Timo Teräs's avatar
Timo Teräs committed
154
#endif
Timo Teräs's avatar
Timo Teräs committed
155

156 157 158 159 160 161 162 163 164 165 166
int apk_blob_rsplit(apk_blob_t blob, char split, apk_blob_t *l, apk_blob_t *r)
{
	char *sep;

	sep = memrchr(blob.ptr, split, blob.len);
	if (sep == NULL)
		return 0;

	if (l != NULL)
		*l = APK_BLOB_PTR_PTR(blob.ptr, sep - 1);
	if (r != NULL)
167
		*r = APK_BLOB_PTR_PTR(sep + 1, blob.ptr + blob.len - 1);
168 169 170 171

	return 1;
}

172
int apk_blob_split(apk_blob_t blob, apk_blob_t split, apk_blob_t *l, apk_blob_t *r)
173
{
174
	char *pos = blob.ptr, *end = blob.ptr + blob.len - split.len + 1;
175 176 177 178 179

	if (end < pos)
		return 0;

	while (1) {
180
		pos = memchr(pos, split.ptr[0], end - pos);
181 182 183
		if (pos == NULL)
			return 0;

184
		if (split.len > 1 && memcmp(pos, split.ptr, split.len) != 0) {
185 186 187 188 189
			pos++;
			continue;
		}

		*l = APK_BLOB_PTR_PTR(blob.ptr, pos-1);
190
		*r = APK_BLOB_PTR_PTR(pos+split.len, blob.ptr+blob.len-1);
191 192 193 194
		return 1;
	}
}

195 196 197 198 199 200 201 202
apk_blob_t apk_blob_pushed(apk_blob_t buffer, apk_blob_t left)
{
	if (buffer.ptr + buffer.len != left.ptr + left.len)
		return APK_BLOB_NULL;

	return APK_BLOB_PTR_LEN(buffer.ptr, left.ptr - buffer.ptr);
}

203
unsigned long apk_blob_hash_seed(apk_blob_t blob, unsigned long seed)
204
{
205
	unsigned long hash = seed;
206 207 208 209 210 211 212 213
	int i;

        for (i = 0; i < blob.len; i++)
		hash = hash * 33 + blob.ptr[i];

	return hash;
}

214 215 216 217 218
unsigned long apk_blob_hash(apk_blob_t blob)
{
	return apk_blob_hash_seed(blob, 5381);
}

219 220 221 222 223 224 225 226 227
int apk_blob_compare(apk_blob_t a, apk_blob_t b)
{
	if (a.len == b.len)
		return memcmp(a.ptr, b.ptr, a.len);
	if (a.len < b.len)
		return -1;
	return 1;
}

Timo Teräs's avatar
Timo Teräs committed
228 229 230
int apk_blob_for_each_segment(apk_blob_t blob, const char *split,
			      int (*cb)(void *ctx, apk_blob_t blob), void *ctx)
{
231
	apk_blob_t l, r, s = APK_BLOB_STR(split);
Timo Teräs's avatar
Timo Teräs committed
232 233 234
	int rc;

	r = blob;
235
	while (apk_blob_split(r, s, &l, &r)) {
Timo Teräs's avatar
Timo Teräs committed
236 237 238 239 240 241 242 243 244
		rc = cb(ctx, l);
		if (rc != 0)
			return rc;
	}
	if (r.len > 0)
		return cb(ctx, r);
	return 0;
}

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
static unsigned char digitdecode[] = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0xff,
	   0,    1,    2,    3,    4,    5,    6,    7,
	   8,    9, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff,
	0xff,  0xa,  0xb,  0xc,  0xd,  0xe,  0xf, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff,  0xa,  0xb,  0xc,  0xd,  0xe,  0xf, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};

static inline int dx(unsigned char c)
281
{
282
	return digitdecode[c];
283 284
}

285
void apk_blob_push_blob(apk_blob_t *to, apk_blob_t literal)
286
{
287
	if (unlikely(APK_BLOB_IS_NULL(*to)))
288
		return;
289

290
	if (unlikely(to->len < literal.len)) {
291 292
		*to = APK_BLOB_NULL;
		return;
293
	}
294

295 296 297
	memcpy(to->ptr, literal.ptr, literal.len);
	to->ptr += literal.len;
	to->len -= literal.len;
298 299
}

300 301 302
static const char *xd = "0123456789abcdefghijklmnopqrstuvwxyz";

void apk_blob_push_uint(apk_blob_t *to, unsigned int value, int radix)
303
{
304 305
	char buf[64];
	char *ptr = &buf[sizeof(buf)-1];
306

307 308 309 310 311 312 313 314 315
	if (value == 0) {
		apk_blob_push_blob(to, APK_BLOB_STR("0"));
		return;
	}

	while (value != 0) {
		*(ptr--) = xd[value % radix];
		value /= radix;
	}
316

317
	apk_blob_push_blob(to, APK_BLOB_PTR_PTR(ptr+1, &buf[sizeof(buf)-1]));
318 319
}

Timo Teräs's avatar
Timo Teräs committed
320 321 322 323 324 325 326
void apk_blob_push_csum(apk_blob_t *to, struct apk_checksum *csum)
{
	switch (csum->type) {
	case APK_CHECKSUM_MD5:
		apk_blob_push_hexdump(to, APK_BLOB_CSUM(*csum));
		break;
	case APK_CHECKSUM_SHA1:
327 328
		apk_blob_push_blob(to, APK_BLOB_STR("Q1"));
		apk_blob_push_base64(to, APK_BLOB_CSUM(*csum));
Timo Teräs's avatar
Timo Teräs committed
329 330 331 332 333 334 335
		break;
	default:
		*to = APK_BLOB_NULL;
		break;
	}
}

336
void apk_blob_push_hexdump(apk_blob_t *to, apk_blob_t binary)
337
{
338
	char *d;
339 340
	int i;

341
	if (unlikely(APK_BLOB_IS_NULL(*to)))
342
		return;
343

344
	if (unlikely(to->len < binary.len * 2)) {
345 346 347
		*to = APK_BLOB_NULL;
		return;
	}
348

349 350 351 352 353 354
	for (i = 0, d = to->ptr; i < binary.len; i++) {
		*(d++) = xd[(binary.ptr[i] >> 4) & 0xf];
		*(d++) = xd[binary.ptr[i] & 0xf];
	}
	to->ptr = d;
	to->len -= binary.len * 2;
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
static const char b64encode[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static inline void push_b64_chunk(unsigned char *to, const unsigned char *from, int len)
{
	to[0] = b64encode[from[0] >> 2];
	to[1] = b64encode[((from[0] & 0x03) << 4) | ((from[1] & 0xf0) >> 4)];
	to[2] = len < 2 ? '=' : b64encode[((from[1] & 0x0f) << 2) |
	                                  ((from[2] & 0xc0) >> 6)];
	to[3] = len < 3 ? '=' : b64encode[from[2] & 0x3f ];
}

void apk_blob_push_base64(apk_blob_t *to, apk_blob_t binary)
{
	unsigned char *src = (unsigned char *) binary.ptr;
	unsigned char *dst = (unsigned char *) to->ptr;
	int i, needed;

	if (unlikely(APK_BLOB_IS_NULL(*to)))
		return;

	needed = ((binary.len + 2) / 3) * 4;
	if (unlikely(to->len < needed)) {
		*to = APK_BLOB_NULL;
		return;
	}

	for (i = 0; i < binary.len / 3; i++, src += 3, dst += 4)
		push_b64_chunk(dst, src, 4);
	i = binary.len % 3;
	if (i != 0)
		push_b64_chunk(dst, src, i);
	to->ptr += needed;
	to->len -= needed;
}

393
void apk_blob_pull_char(apk_blob_t *b, int expected)
394
{
395
	if (unlikely(APK_BLOB_IS_NULL(*b)))
396
		return;
397
	if (unlikely(b->len < 1 || b->ptr[0] != expected)) {
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
		*b = APK_BLOB_NULL;
		return;
	}
	b->ptr ++;
	b->len --;
}

unsigned int apk_blob_pull_uint(apk_blob_t *b, int radix)
{
	unsigned int val;
	int ch;

	val = 0;
	while (b->len && b->ptr[0] != 0) {
		ch = dx(b->ptr[0]);
413
		if (ch >= radix)
414 415 416
			break;
		val *= radix;
		val += ch;
417

418 419
		b->ptr++;
		b->len--;
420 421
	}

422 423 424
	return val;
}

Timo Teräs's avatar
Timo Teräs committed
425 426
void apk_blob_pull_csum(apk_blob_t *b, struct apk_checksum *csum)
{
427 428
	int encoding;

Timo Teräs's avatar
Timo Teräs committed
429 430 431 432 433 434 435 436
	if (unlikely(APK_BLOB_IS_NULL(*b)))
		return;

	if (unlikely(b->len < 2)) {
		*b = APK_BLOB_NULL;
		return;
	}

437
	if (dx(b->ptr[0]) != 0xff) {
438 439
		/* Assume MD5 for backwards compatibility */
		csum->type = APK_CHECKSUM_MD5;
Timo Teräs's avatar
Timo Teräs committed
440
		apk_blob_pull_hexdump(b, APK_BLOB_CSUM(*csum));
441 442 443 444 445 446 447
		return;
	}

	encoding = b->ptr[0];
	switch (b->ptr[1]) {
	case '1':
		csum->type = APK_CHECKSUM_SHA1;
Timo Teräs's avatar
Timo Teräs committed
448 449
		break;
	default:
450 451 452 453 454 455 456 457
		*b = APK_BLOB_NULL;
		return;
	}
	b->ptr += 2;
	b->len -= 2;

	switch (encoding) {
	case 'X':
Timo Teräs's avatar
Timo Teräs committed
458 459
		apk_blob_pull_hexdump(b, APK_BLOB_CSUM(*csum));
		break;
460 461 462 463 464 465
	case 'Q':
		apk_blob_pull_base64(b, APK_BLOB_CSUM(*csum));
		break;
	default:
		*b = APK_BLOB_NULL;
		break;
Timo Teräs's avatar
Timo Teräs committed
466 467 468
	}
}

469 470 471
void apk_blob_pull_hexdump(apk_blob_t *b, apk_blob_t to)
{
	char *s, *d;
472
	int i, r, r1, r2;
473

474
	if (unlikely(APK_BLOB_IS_NULL(*b)))
475 476
		return;

477
	if (unlikely(to.len > b->len * 2))
478 479
		goto err;

480
	r = 0;
481
	for (i = 0, s = b->ptr, d = to.ptr; i < to.len; i++) {
482 483
		r |= r1 = dx(*(s++));
		r |= r2 = dx(*(s++));
484 485
		*(d++) = (r1 << 4) + r2;
	}
486 487
	if (unlikely(r == 0xff))
		goto err;
488 489 490 491 492
	b->ptr = s;
	b->len -= to.len * 2;
	return;
err:
	*b = APK_BLOB_NULL;
493
}
494

495 496 497 498 499 500 501 502 503
int apk_blob_pull_blob_match(apk_blob_t *b, apk_blob_t match)
{
	if (b->len < match.len) return 0;
	if (memcmp(b->ptr, match.ptr, match.len) != 0) return 0;
	b->ptr += match.len;
	b->len -= match.len;
	return 1;
}

504
static unsigned char b64decode[] = {
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
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
	0x3c, 0x3d, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff,
	0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
	0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
	0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
	0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
	0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
	0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,

	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
538 539
};

540 541
static inline __attribute__((always_inline))
int pull_b64_chunk(unsigned char *to, const unsigned char *from, int len)
542 543
{
	unsigned char tmp[4];
544
	int i, r = 0;
545

546
	for (i = 0; i < 4; i++) {
547
		tmp[i] = b64decode[from[i]];
548
		r |= tmp[i];
549
	}
550 551
	if (unlikely(r == 0xff))
		return -1;
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

	to[0] = (tmp[0] << 2 | tmp[1] >> 4);
	if (len > 1)
		to[1] = (tmp[1] << 4 | tmp[2] >> 2);
	else if (unlikely(from[2] != '='))
		return -1;
	if (len > 2)
		to[2] = (((tmp[2] << 6) & 0xc0) | tmp[3]);
	else if (unlikely(from[3] != '='))
		return -1;
	return 0;
}

void apk_blob_pull_base64(apk_blob_t *b, apk_blob_t to)
{
567
	unsigned char tmp[4];
568 569
	unsigned char *src = (unsigned char *) b->ptr;
	unsigned char *dst = (unsigned char *) to.ptr;
570
	int i, r, needed;
571 572 573 574 575 576 577 578 579 580

	if (unlikely(APK_BLOB_IS_NULL(*b)))
		return;

	needed = ((to.len + 2) / 3) * 4;
	if (unlikely(b->len < needed)) {
		*b = APK_BLOB_NULL;
		return;
	}

581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
	r = 0;
	for (i = 0; i < to.len / 3; i++, src += 4, dst += 3) {
		r |= tmp[0] = b64decode[src[0]];
		r |= tmp[1] = b64decode[src[1]];
		r |= tmp[2] = b64decode[src[2]];
		r |= tmp[3] = b64decode[src[3]];
		dst[0] = (tmp[0] << 2 | tmp[1] >> 4);
		dst[1] = (tmp[1] << 4 | tmp[2] >> 2);
		dst[2] = (((tmp[2] << 6) & 0xc0) | tmp[3]);
	}
	if (unlikely(r == 0xff)) {
		*b = APK_BLOB_NULL;
		return;
	}

596
	i = to.len % 3;
597 598 599 600 601 602 603
	if (i != 0) {
		if (pull_b64_chunk(dst, src, i) != 0) {
			*b = APK_BLOB_NULL;
			return;
		}
	}

604 605 606
	b->ptr += needed;
	b->len -= needed;
}
607 608 609 610 611 612 613 614 615 616 617 618 619 620

static apk_blob_t atom_hash_get_key(apk_hash_item item)
{
	return ((struct apk_blob_atom *) item)->blob;
}

static struct apk_hash atom_hash;
static struct apk_hash_ops atom_ops = {
	.node_offset = offsetof(struct apk_blob_atom, hash_node),
	.get_key = atom_hash_get_key,
	.hash_key = apk_blob_hash,
	.compare = apk_blob_compare,
	.delete_item = (apk_hash_delete_f) free,
};
621 622

apk_blob_t apk_null_blob = {0,0};
623 624 625

void apk_atom_init(void)
{
626
	apk_hash_init(&atom_hash, &atom_ops, 10000);
627 628 629 630 631 632 633 634
}

apk_blob_t *apk_blob_atomize(apk_blob_t blob)
{
	struct apk_blob_atom *atom;
	unsigned long hash = apk_hash_from_key(&atom_hash, blob);

	if (blob.len < 0 || blob.ptr == NULL)
635
		return &apk_null_blob;
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

	atom = (struct apk_blob_atom *) apk_hash_get_hashed(&atom_hash, blob, hash);
	if (atom != NULL)
		return &atom->blob;

	atom = malloc(sizeof(*atom));
	atom->blob = blob;
	apk_hash_insert_hashed(&atom_hash, atom, hash);

	return &atom->blob;
}

apk_blob_t *apk_blob_atomize_dup(apk_blob_t blob)
{
	struct apk_blob_atom *atom;
	unsigned long hash = apk_hash_from_key(&atom_hash, blob);
	char *ptr;

	if (blob.len < 0 || blob.ptr == NULL)
655
		return &apk_null_blob;
656 657 658 659 660 661 662 663 664 665 666 667
	atom = (struct apk_blob_atom *) apk_hash_get_hashed(&atom_hash, blob, hash);
	if (atom != NULL)
		return &atom->blob;

	atom = malloc(sizeof(*atom) + blob.len);
	ptr = (char*) (atom + 1);
	memcpy(ptr, blob.ptr, blob.len);
	atom->blob = APK_BLOB_PTR_LEN(ptr, blob.len);
	apk_hash_insert_hashed(&atom_hash, atom, hash);

	return &atom->blob;
}