Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / lib / adler32.c
blob43b68af62c140a1f3c66cf647a5b527a1f7b7389
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2012 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/dl.h>
21 #include <grub/crypto.h>
23 GRUB_MOD_LICENSE ("GPLv3+");
25 struct adler32_context
27 grub_uint16_t a, b;
28 grub_uint32_t c;
31 static void
32 adler32_init (void *context)
34 struct adler32_context *ctx = context;
36 ctx->a = 1;
37 ctx->b = 0;
40 #define MOD 65521
42 static grub_uint16_t
43 mod_add (grub_uint16_t a, grub_uint16_t b)
45 if ((grub_uint32_t) a + (grub_uint32_t) b >= MOD)
46 return a + b - MOD;
47 return a + b;
50 static void
51 adler32_write (void *context, const void *inbuf, grub_size_t inlen)
53 struct adler32_context *ctx = context;
54 const grub_uint8_t *ptr = inbuf;
56 while (inlen)
58 ctx->a = mod_add (ctx->a, *ptr);
59 ctx->b = mod_add (ctx->a, ctx->b);
60 inlen--;
61 ptr++;
65 static void
66 adler32_final (void *context __attribute__ ((unused)))
70 static grub_uint8_t *
71 adler32_read (void *context)
73 struct adler32_context *ctx = context;
74 if (ctx->a > MOD)
75 ctx->a -= MOD;
76 if (ctx->b > MOD)
77 ctx->b -= MOD;
78 ctx->c = grub_cpu_to_be32 (ctx->a | (ctx->b << 16));
79 return (grub_uint8_t *) &ctx->c;
82 static gcry_md_spec_t spec_adler32 =
84 "ADLER32", 0, 0, 0, 4,
85 adler32_init, adler32_write, adler32_final, adler32_read,
86 sizeof (struct adler32_context),
87 #ifdef GRUB_UTIL
88 .modname = "adler32",
89 #endif
90 .blocksize = 64
94 GRUB_MOD_INIT(adler32)
96 grub_md_register (&spec_adler32);
99 GRUB_MOD_FINI(adler32)
101 grub_md_unregister (&spec_adler32);