Select Git revision
-
Per Hedbor authored
Rev: .how_to_build_from_scratch:1.7 Rev: src/backend.c:1.62 Rev: src/block_alloc.h:1.27 Rev: src/builtin_functions.c:1.319 Rev: src/cpp.c:1.79 Rev: src/docode.c:1.89 Rev: src/dynamic_load.c:1.48 Rev: src/encode.c:1.79 Rev: src/error.c:1.67 Rev: src/fd_control.c:1.35 Rev: src/interpret.c:1.178 Rev: src/interpret_functions.h:1.35 Rev: src/interpreter.h:1.53 Rev: src/las.c:1.229 Rev: src/lexer.h:1.26 Rev: src/modules/Gz/gz_test.c:1.5 Rev: src/modules/HTTPLoop/accept_and_parse.c:1.22 Rev: src/modules/Image/colors.c:1.42 Rev: src/modules/Image/colortable.c:1.102 Rev: src/modules/Image/colortable.h:1.24 Rev: src/modules/Image/encodings/_xpm.c:1.16 Rev: src/modules/Image/encodings/pcx.c:1.15 Rev: src/modules/Image/encodings/png.c:1.41 Rev: src/modules/Image/encodings/psd.c:1.27 Rev: src/modules/Image/encodings/tga.c:1.26 Rev: src/modules/Image/encodings/xbm.c:1.13 Rev: src/modules/Image/encodings/xcf.c:1.35 Rev: src/modules/Image/image.c:1.184 Rev: src/modules/Image/image.h:1.46 Rev: src/modules/Image/layers.c:1.59 Rev: src/modules/Image/match.h:1.12 Rev: src/modules/Image/operator.c:1.35 Rev: src/modules/Image/poly.c:1.12 Rev: src/modules/Java/jvm.c:1.31 Rev: src/modules/Math/math_matrix.c:1.20 Rev: src/modules/Msql/msqlmod.c:1.16 Rev: src/modules/Mysql/mysql.c:1.39 Rev: src/modules/Mysql/precompiled_mysql.h:1.11 Rev: src/modules/Mysql/result.c:1.18 Rev: src/modules/Odbc/odbc.c:1.24 Rev: src/modules/Oracle/oracle.c:1.52 Rev: src/modules/Perl/perlmod.c:1.23 Rev: src/modules/Pipe/pipe.c:1.45 Rev: src/modules/Postgres/postgres.c:1.21 Rev: src/modules/Yp/yp.c:1.21 Rev: src/modules/_Charset/charsetmod.c:1.26 Rev: src/modules/_Crypto/crypto.c:1.41 Rev: src/modules/_Crypto/des.c:1.19 Rev: src/modules/_Image_GIF/image_gif.c:1.3 Rev: src/modules/_Image_JPEG/image_jpeg.c:1.37 Rev: src/modules/_Image_TIFF/image_tiff.c:1.23 Rev: src/modules/_Image_TTF/image_ttf.c:1.38 Rev: src/modules/_math/math.c:1.35 Rev: src/modules/files/efuns.c:1.89 Rev: src/modules/files/file.c:1.205 Rev: src/modules/files/sendfile.c:1.53 Rev: src/modules/files/termios.c:1.11 Rev: src/modules/files/udp.c:1.19 Rev: src/modules/spider/xml.c:1.30 Rev: src/modules/sybase/sybase.c:1.6 Rev: src/modules/sybase/sybase.h:1.3 Rev: src/modules/system/nt.c:1.29 Rev: src/modules/system/system.c:1.95 Rev: src/object.c:1.155 Rev: src/opcodes.c:1.96 Rev: src/peep.c:1.45 Rev: src/pike_error.h:1.4 Rev: src/pike_types.c:1.145 Rev: src/port.c:1.35 Rev: src/post_modules/GL/auto.c.in:1.17 Rev: src/post_modules/GTK/build_pgtk.pike:1.38 Rev: src/post_modules/GTK/dependencies:1.6 Rev: src/post_modules/GTK/pgtk.h:1.19 Rev: src/post_modules/GTK/source/gdkbitmap.pre:1.8 Rev: src/post_modules/GTK/source/gdkcolor.pre:1.8 Rev: src/post_modules/GTK/source/gdkdragcontext.pre:1.8 Rev: src/post_modules/GTK/source/gdkevent.pre:1.5 Rev: src/post_modules/GTK/source/gdkfont.pre:1.7 Rev: src/post_modules/GTK/source/gdkimage.pre:1.9 Rev: src/post_modules/GTK/source/gdkpixmap.pre:1.10 Rev: src/post_modules/GTK/source/gdkrectangle.pre:1.7 Rev: src/post_modules/GTK/source/gdkregion.pre:1.8 Rev: src/post_modules/GTK/source/gdkwindow.pre:1.14 Rev: src/post_modules/GTK/source/gladexml.pre:1.10 Rev: src/post_modules/GTK/source/global.pre:1.17 Rev: src/post_modules/GTK/source/gnomeapp.pre:1.9 Rev: src/post_modules/GTK/source/gnomedialog.pre:1.6 Rev: src/post_modules/GTK/source/gnomemessagebox.pre:1.5 Rev: src/post_modules/GTK/source/gtkadjustment.pre:1.7 Rev: src/post_modules/GTK/source/gtkcalendar.pre:1.5 Rev: src/post_modules/GTK/source/gtkclist.pre:1.9 Rev: src/post_modules/GTK/source/gtkcombo.pre:1.5 Rev: src/post_modules/GTK/source/gtkctree.pre:1.7 Rev: src/post_modules/GTK/source/gtkdatabox.pre:1.3 Rev: src/post_modules/GTK/source/gtkobject.pre:1.6 Rev: src/post_modules/GTK/source/gtkpixmap.pre:1.7 Rev: src/post_modules/GTK/source/gtksocket.pre:1.8 Rev: src/post_modules/GTK/source/gtkwidget.pre:1.12 Rev: src/post_modules/GTK/source/support.c:1.20 Rev: src/preprocessor.h:1.36 Rev: src/program.c:1.286 Rev: src/signal_handler.c:1.186 Rev: src/smartlink.c:1.12 Rev: src/stralloc.c:1.112 Rev: src/svalue.h:1.72 Rev: src/threads.c:1.149 Rev: src/threads.h:1.108
Per Hedbor authoredRev: .how_to_build_from_scratch:1.7 Rev: src/backend.c:1.62 Rev: src/block_alloc.h:1.27 Rev: src/builtin_functions.c:1.319 Rev: src/cpp.c:1.79 Rev: src/docode.c:1.89 Rev: src/dynamic_load.c:1.48 Rev: src/encode.c:1.79 Rev: src/error.c:1.67 Rev: src/fd_control.c:1.35 Rev: src/interpret.c:1.178 Rev: src/interpret_functions.h:1.35 Rev: src/interpreter.h:1.53 Rev: src/las.c:1.229 Rev: src/lexer.h:1.26 Rev: src/modules/Gz/gz_test.c:1.5 Rev: src/modules/HTTPLoop/accept_and_parse.c:1.22 Rev: src/modules/Image/colors.c:1.42 Rev: src/modules/Image/colortable.c:1.102 Rev: src/modules/Image/colortable.h:1.24 Rev: src/modules/Image/encodings/_xpm.c:1.16 Rev: src/modules/Image/encodings/pcx.c:1.15 Rev: src/modules/Image/encodings/png.c:1.41 Rev: src/modules/Image/encodings/psd.c:1.27 Rev: src/modules/Image/encodings/tga.c:1.26 Rev: src/modules/Image/encodings/xbm.c:1.13 Rev: src/modules/Image/encodings/xcf.c:1.35 Rev: src/modules/Image/image.c:1.184 Rev: src/modules/Image/image.h:1.46 Rev: src/modules/Image/layers.c:1.59 Rev: src/modules/Image/match.h:1.12 Rev: src/modules/Image/operator.c:1.35 Rev: src/modules/Image/poly.c:1.12 Rev: src/modules/Java/jvm.c:1.31 Rev: src/modules/Math/math_matrix.c:1.20 Rev: src/modules/Msql/msqlmod.c:1.16 Rev: src/modules/Mysql/mysql.c:1.39 Rev: src/modules/Mysql/precompiled_mysql.h:1.11 Rev: src/modules/Mysql/result.c:1.18 Rev: src/modules/Odbc/odbc.c:1.24 Rev: src/modules/Oracle/oracle.c:1.52 Rev: src/modules/Perl/perlmod.c:1.23 Rev: src/modules/Pipe/pipe.c:1.45 Rev: src/modules/Postgres/postgres.c:1.21 Rev: src/modules/Yp/yp.c:1.21 Rev: src/modules/_Charset/charsetmod.c:1.26 Rev: src/modules/_Crypto/crypto.c:1.41 Rev: src/modules/_Crypto/des.c:1.19 Rev: src/modules/_Image_GIF/image_gif.c:1.3 Rev: src/modules/_Image_JPEG/image_jpeg.c:1.37 Rev: src/modules/_Image_TIFF/image_tiff.c:1.23 Rev: src/modules/_Image_TTF/image_ttf.c:1.38 Rev: src/modules/_math/math.c:1.35 Rev: src/modules/files/efuns.c:1.89 Rev: src/modules/files/file.c:1.205 Rev: src/modules/files/sendfile.c:1.53 Rev: src/modules/files/termios.c:1.11 Rev: src/modules/files/udp.c:1.19 Rev: src/modules/spider/xml.c:1.30 Rev: src/modules/sybase/sybase.c:1.6 Rev: src/modules/sybase/sybase.h:1.3 Rev: src/modules/system/nt.c:1.29 Rev: src/modules/system/system.c:1.95 Rev: src/object.c:1.155 Rev: src/opcodes.c:1.96 Rev: src/peep.c:1.45 Rev: src/pike_error.h:1.4 Rev: src/pike_types.c:1.145 Rev: src/port.c:1.35 Rev: src/post_modules/GL/auto.c.in:1.17 Rev: src/post_modules/GTK/build_pgtk.pike:1.38 Rev: src/post_modules/GTK/dependencies:1.6 Rev: src/post_modules/GTK/pgtk.h:1.19 Rev: src/post_modules/GTK/source/gdkbitmap.pre:1.8 Rev: src/post_modules/GTK/source/gdkcolor.pre:1.8 Rev: src/post_modules/GTK/source/gdkdragcontext.pre:1.8 Rev: src/post_modules/GTK/source/gdkevent.pre:1.5 Rev: src/post_modules/GTK/source/gdkfont.pre:1.7 Rev: src/post_modules/GTK/source/gdkimage.pre:1.9 Rev: src/post_modules/GTK/source/gdkpixmap.pre:1.10 Rev: src/post_modules/GTK/source/gdkrectangle.pre:1.7 Rev: src/post_modules/GTK/source/gdkregion.pre:1.8 Rev: src/post_modules/GTK/source/gdkwindow.pre:1.14 Rev: src/post_modules/GTK/source/gladexml.pre:1.10 Rev: src/post_modules/GTK/source/global.pre:1.17 Rev: src/post_modules/GTK/source/gnomeapp.pre:1.9 Rev: src/post_modules/GTK/source/gnomedialog.pre:1.6 Rev: src/post_modules/GTK/source/gnomemessagebox.pre:1.5 Rev: src/post_modules/GTK/source/gtkadjustment.pre:1.7 Rev: src/post_modules/GTK/source/gtkcalendar.pre:1.5 Rev: src/post_modules/GTK/source/gtkclist.pre:1.9 Rev: src/post_modules/GTK/source/gtkcombo.pre:1.5 Rev: src/post_modules/GTK/source/gtkctree.pre:1.7 Rev: src/post_modules/GTK/source/gtkdatabox.pre:1.3 Rev: src/post_modules/GTK/source/gtkobject.pre:1.6 Rev: src/post_modules/GTK/source/gtkpixmap.pre:1.7 Rev: src/post_modules/GTK/source/gtksocket.pre:1.8 Rev: src/post_modules/GTK/source/gtkwidget.pre:1.12 Rev: src/post_modules/GTK/source/support.c:1.20 Rev: src/preprocessor.h:1.36 Rev: src/program.c:1.286 Rev: src/signal_handler.c:1.186 Rev: src/smartlink.c:1.12 Rev: src/stralloc.c:1.112 Rev: src/svalue.h:1.72 Rev: src/threads.c:1.149 Rev: src/threads.h:1.108
base64-encode.c 4.63 KiB
/* base64-encode.c
*
*/
/* nettle, low-level cryptographics library
*
* Copyright (C) 2002 Niels Mller
*
* The nettle 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.
*
* The nettle 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 the nettle library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*/
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <stdlib.h>
#include "base64.h"
static const uint8_t encode_table[64] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
#define ENCODE(x) (encode_table[0x3F & (x)])
void
base64_encode_raw(uint8_t *dst, unsigned length, const uint8_t *src)
{
const uint8_t *in = src + length;
uint8_t *out = dst + BASE64_ENCODE_RAW_LENGTH(length);
unsigned left_over = length % 3;
if (left_over)
{
in -= left_over;
*--out = '=';
switch(left_over)
{
case 1:
*--out = '=';
*--out = ENCODE(in[0] << 4);
break;
case 2:
*--out = ENCODE( in[1] << 2);
*--out = ENCODE((in[0] << 4) | (in[1] >> 4));
break;
default:
abort();
}
*--out = ENCODE(in[0] >> 2);
}
while (in > src)
{
in -= 3;
*--out = ENCODE( in[2]);
*--out = ENCODE((in[1] << 2) | (in[2] >> 6));
*--out = ENCODE((in[0] << 4) | (in[1] >> 4));
*--out = ENCODE( in[0] >> 2);
}
assert(in == src);
assert(out == dst);
}
#if 0
unsigned
base64_encode(uint8_t *dst,
unsigned src_length,
const uint8_t *src)
{
unsigned dst_length = BASE64_ENCODE_RAW_LENGTH(src_length);
unsigned n = src_length / 3;
unsigned left_over = src_length % 3;
unsigned done = 0;
if (left_over)
{
const uint8_t *in = src + n * 3;
uint8_t *out = dst + dst_length;
switch(left_over)
{
case 1:
*--out = '=';
*--out = ENCODE(in[0] << 4);
break;
case 2:
*--out = ENCODE( in[1] << 2);
*--out = ENCODE((in[0] << 4) | (in[1] >> 4));
break;
default:
abort();
}
*--out = ENCODE(in[0] >> 2);
done = 4;
}
base64_encode_raw(n, dst, src);
done += n * 4;
assert(done == dst_length);
return done;
}
#endif
void
base64_encode_group(uint8_t *dst, uint32_t group)
{
*dst++ = ENCODE(group >> 18);
*dst++ = ENCODE(group >> 12);
*dst++ = ENCODE(group >> 6);
*dst++ = ENCODE(group);
}
void
base64_encode_init(struct base64_encode_ctx *ctx)
{
ctx->word = ctx->bits = 0;
}
/* Encodes a single byte. */
unsigned
base64_encode_single(struct base64_encode_ctx *ctx,
uint8_t *dst,
uint8_t src)
{
unsigned done = 0;
unsigned word = ctx->word << 8 | src;
unsigned bits = ctx->bits + 8;
while (bits >= 6)
{
bits -= 6;
dst[done++] = ENCODE(word >> bits);
}
ctx->bits = bits;
ctx->word = word;
assert(done <= 2);
return done;
}
/* Returns the number of output characters. DST should point to an
* area of size at least BASE64_ENCODE_LENGTH(length). */
unsigned
base64_encode_update(struct base64_encode_ctx *ctx,
uint8_t *dst,
unsigned length,
const uint8_t *src)
{
unsigned done = 0;
unsigned left = length;
unsigned left_over;
unsigned bulk;
while (ctx->bits && left)
{
left--;
done += base64_encode_single(ctx, dst + done, *src++);
}
left_over = left % 3;
bulk = left - left_over;
if (bulk)
{
assert(!(bulk % 3));
base64_encode_raw(dst + done, bulk, src);
done += BASE64_ENCODE_RAW_LENGTH(bulk);
src += bulk;
left = left_over;
}
while (left)
{
left--;
done += base64_encode_single(ctx, dst + done, *src++);
}
assert(done <= BASE64_ENCODE_LENGTH(length));
return done;
}
/* DST should point to an area of size at least
* BASE64_ENCODE_FINAL_SIZE */
unsigned
base64_encode_final(struct base64_encode_ctx *ctx,
uint8_t *dst)
{
unsigned done = 0;
unsigned bits = ctx->bits;
if (bits)
{
dst[done++] = ENCODE(ctx->word << (6 - ctx->bits));
for (; bits < 6; bits += 2)
dst[done++] = '=';
ctx->bits = 0;
}
assert(done <= BASE64_ENCODE_FINAL_LENGTH);
return done;
}