/* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2012 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot 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 VasEBoot. If not, see . */ #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); struct adler32_context { VasEBoot_uint16_t a, b; VasEBoot_uint32_t c; }; static void adler32_init (void *context) { struct adler32_context *ctx = context; ctx->a = 1; ctx->b = 0; } #define MOD 65521 static VasEBoot_uint16_t mod_add (VasEBoot_uint16_t a, VasEBoot_uint16_t b) { if ((VasEBoot_uint32_t) a + (VasEBoot_uint32_t) b >= MOD) return a + b - MOD; return a + b; } static void adler32_write (void *context, const void *inbuf, VasEBoot_size_t inlen) { struct adler32_context *ctx = context; const VasEBoot_uint8_t *ptr = inbuf; while (inlen) { ctx->a = mod_add (ctx->a, *ptr); ctx->b = mod_add (ctx->a, ctx->b); inlen--; ptr++; } } static void adler32_final (void *context __attribute__ ((unused))) { } static VasEBoot_uint8_t * adler32_read (void *context) { struct adler32_context *ctx = context; if (ctx->a > MOD) ctx->a -= MOD; if (ctx->b > MOD) ctx->b -= MOD; ctx->c = VasEBoot_cpu_to_be32 (ctx->a | (ctx->b << 16)); return (VasEBoot_uint8_t *) &ctx->c; } static gcry_md_spec_t spec_adler32 = { "ADLER32", 0, 0, 0, 4, adler32_init, adler32_write, adler32_final, adler32_read, sizeof (struct adler32_context), #ifdef VasEBoot_UTIL .modname = "adler32", #endif .blocksize = 64 }; VasEBoot_MOD_INIT(adler32) { VasEBoot_md_register (&spec_adler32); } VasEBoot_MOD_FINI(adler32) { VasEBoot_md_unregister (&spec_adler32); }