From patchwork Sun May 23 10:59:22 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 53316 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 36601B7BEE for ; Sun, 23 May 2010 21:21:37 +1000 (EST) Received: from localhost ([127.0.0.1]:37572 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OG9FO-0004a1-6K for incoming@patchwork.ozlabs.org; Sun, 23 May 2010 07:21:34 -0400 Received: from [140.186.70.92] (port=48622 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OG8uK-0004Fp-MK for qemu-devel@nongnu.org; Sun, 23 May 2010 07:00:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OG8uC-0004QF-27 for qemu-devel@nongnu.org; Sun, 23 May 2010 06:59:48 -0400 Received: from fmmailgate01.web.de ([217.72.192.221]:34397) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OG8uB-0004Pp-2y for qemu-devel@nongnu.org; Sun, 23 May 2010 06:59:39 -0400 Received: from smtp01.web.de ( [172.20.0.243]) by fmmailgate01.web.de (Postfix) with ESMTP id 5D6FD15B16D2B; Sun, 23 May 2010 12:59:38 +0200 (CEST) Received: from [88.65.39.229] (helo=localhost.localdomain) by smtp01.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #4) id 1OG8u8-0003cZ-01; Sun, 23 May 2010 12:59:36 +0200 From: Jan Kiszka To: qemu-devel@nongnu.org, Anthony Liguori Date: Sun, 23 May 2010 12:59:22 +0200 Message-Id: <26a042b41e726185a36fc8e30735fe81d4918ace.1274612367.git.jan.kiszka@web.de> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: References: In-Reply-To: References: X-Sender: jan.kiszka@web.de X-Provags-ID: V01U2FsdGVkX1/fjiH9c4F0yOM32q9vH3v8giVX/T7TUPe43S9p eoQev6jURRyLSlxCNHuc5Q1+f3v2wS58GfZGMLhpJps6dR5P+P pPQnyq8TM= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4-2.6 Cc: Juan Quintela , Jan Kiszka , Markus Armbruster , Luiz Capitulino , Blue Swirl , Avi Kivity Subject: [Qemu-devel] [PATCH v3 09/17] Add base64 encoder/decoder X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Jan Kiszka Will be used by QBuffer. Signed-off-by: Jan Kiszka --- Makefile.objs | 2 +- base64.c | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ base64.h | 19 ++++++ 3 files changed, 222 insertions(+), 1 deletions(-) create mode 100644 base64.c create mode 100644 base64.h diff --git a/Makefile.objs b/Makefile.objs index 1585101..81481c8 100644 --- a/Makefile.objs +++ b/Makefile.objs @@ -2,7 +2,7 @@ # QObject qobject-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o -qobject-obj-y += qerror.o +qobject-obj-y += qerror.o base64.o ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img diff --git a/base64.c b/base64.c new file mode 100644 index 0000000..750d0fb --- /dev/null +++ b/base64.c @@ -0,0 +1,202 @@ +/* + * Base64 encoder/decoder conforming to RFC 4648 + * (based on Mozilla's nsprpub/lib/libc/src/base64.c) + * + * Copyright (C) 2010 Siemens AG + * + * Authors: + * Jan Kiszka + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#include +#include "base64.h" + +static const char base[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +static void encode3to4(const uint8_t *src, char *dest) +{ + uint32_t b32 = 0; + int i, j = 18; + + for (i = 0; i < 3; i++) { + b32 <<= 8; + b32 |= src[i]; + } + for (i = 0; i < 4; i++) { + dest[i] = base[(b32 >> j) & 0x3F]; + j -= 6; + } +} + +static void encode2to4(const uint8_t *src, char *dest) +{ + dest[0] = base[(src[0] >> 2) & 0x3F]; + dest[1] = base[((src[0] & 0x03) << 4) | ((src[1] >> 4) & 0x0F)]; + dest[2] = base[(src[1] & 0x0F) << 2]; + dest[3] = '='; +} + +static void encode1to4(const uint8_t *src, char *dest) +{ + dest[0] = base[(src[0] >> 2) & 0x3F]; + dest[1] = base[(src[0] & 0x03) << 4]; + dest[2] = '='; + dest[3] = '='; +} + +/* + * Encode data in 'src' of length 'srclen' to a base64 string, saving the + * null-terminated result in 'dest'. The size of the destition buffer must be + * at least ((srclen + 2) / 3) * 4 + 1. + */ +void base64_encode(const uint8_t *src, size_t srclen, char *dest) +{ + while (srclen >= 3) { + encode3to4(src, dest); + src += 3; + dest += 4; + srclen -= 3; + } + switch (srclen) { + case 2: + encode2to4(src, dest); + dest += 4; + break; + case 1: + encode1to4(src, dest); + dest += 4; + break; + case 0: + break; + } + dest[0] = 0; +} + +static int32_t codetovalue(char c) +{ + if (c >= 'A' && c <= 'Z') { + return c - 'A'; + } else if (c >= 'a' && c <= 'z') { + return c - 'a' + 26; + } else if (c >= '0' && c <= '9') { + return c - '0' + 52; + } else if (c == '+') { + return 62; + } else if ( c == '/') { + return 63; + } else { + return -1; + } +} + +static int decode4to3 (const char *src, uint8_t *dest) +{ + uint32_t b32 = 0; + int32_t bits; + int i; + + for (i = 0; i < 4; i++) { + bits = codetovalue(src[i]); + if (bits < 0) { + return bits; + } + b32 <<= 6; + b32 |= bits; + } + dest[0] = (b32 >> 16) & 0xFF; + dest[1] = (b32 >> 8) & 0xFF; + dest[2] = b32 & 0xFF; + + return 0; +} + +static int decode3to2(const char *src, uint8_t *dest) +{ + uint32_t b32 = 0; + int32_t bits; + + bits = codetovalue(src[0]); + if (bits < 0) { + return bits; + } + b32 = (uint32_t)bits; + b32 <<= 6; + + bits = codetovalue(src[1]); + if (bits < 0) { + return bits; + } + b32 |= (uint32_t)bits; + b32 <<= 4; + + bits = codetovalue(src[2]); + if (bits < 0) { + return bits; + } + b32 |= ((uint32_t)bits) >> 2; + + dest[0] = (b32 >> 8) & 0xFF; + dest[1] = b32 & 0xFF; + + return 0; +} + +static int decode2to1(const char *src, uint8_t *dest) +{ + uint32_t b32; + int32_t bits; + + bits = codetovalue(src[0]); + if (bits < 0) { + return bits; + } + b32 = (uint32_t)bits << 2; + + bits = codetovalue(src[1]); + if (bits < 0) { + return bits; + } + b32 |= ((uint32_t)bits) >> 4; + + dest[0] = b32; + + return 0; +} + +/* + * Convert string 'src' of length 'srclen' from base64 to binary form, + * saving the result in 'dest'. The size of the destination buffer must be at + * least srclen * 3 / 4. + * + * Returns 0 on success, -1 on conversion error. + */ +int base64_decode(const char *src, size_t srclen, uint8_t *dest) +{ + int ret; + + while (srclen >= 4) { + ret = decode4to3(src, dest); + if (ret < 0) { + return ret; + } + src += 4; + dest += 3; + srclen -= 4; + } + + switch (srclen) { + case 3: + return decode3to2(src, dest); + case 2: + return decode2to1(src, dest); + case 1: + return -1; + default: /* 0 */ + return 0; + } +} diff --git a/base64.h b/base64.h new file mode 100644 index 0000000..07e72a8 --- /dev/null +++ b/base64.h @@ -0,0 +1,19 @@ +/* + * Base64 encoder/decoder conforming to RFC 4648 + * (based on Mozilla's nsprpub/lib/libc/src/base64.c) + * + * Copyright (C) 2010 Siemens AG + * + * Authors: + * Jan Kiszka + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. + * See the COPYING.LIB file in the top-level directory. + * + */ + +#include +#include + +void base64_encode(const uint8_t *src, size_t srclen, char *dest); +int base64_decode(const char *src, size_t srclen, uint8_t *dest);