Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazi/i386/boot2/boot.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
30 */
31
32/*
33 * INTEL CORPORATION PROPRIETARY INFORMATION
34 *
35 * This software is supplied under the terms of a license agreement or
36 * nondisclosure agreement with Intel Corporation and may not be copied
37 * nor disclosed except in accordance with the terms of that agreement.
38 *
39 * Copyright 1988, 1989 by Intel Corporation
40 */
41
42/*
43 * Copyright 1993 NeXT Computer, Inc.
44 * All rights reserved.
45 */
46
47/*
48 * Completely reworked by Sam Streeper (sam_s@NeXT.com)
49 * Reworked again by Curtis Galloway (galloway@NeXT.com)
50 */
51
52
53#include "boot.h"
54#include "bootstruct.h"
55#include "fake_efi.h"
56#include "sl.h"
57#include "libsa.h"
58#include "ramdisk.h"
59#include "gui.h"
60#include "platform.h"
61#include "modules.h"
62
63long gBootMode; /* defaults to 0 == kBootModeNormal */
64bool gOverrideKernel;
65static char gBootKernelCacheFile[512];
66static char gCacheNameAdler[64 + 256];
67//char *gPlatformName = gCacheNameAdler; disabled
68char gRootDevice[512];
69char gMKextName[512];
70bool gEnableCDROMRescan;
71bool gScanSingleDrive;
72
73int bvCount = 0;
74//intmenucount = 0;
75int gDeviceCount = 0;
76
77BVRef bvr;
78BVRef menuBVR;
79BVRef bvChain;
80bool useGUI;
81
82//static void selectBiosDevice(void);
83static unsigned long Adler32(unsigned char *buffer, long length);
84
85
86static bool gUnloadPXEOnExit = false;
87
88/*
89 * How long to wait (in seconds) to load the
90 * kernel after displaying the "boot:" prompt.
91 */
92#define kBootErrorTimeout 5
93
94/*
95 * Default path to kernel cache file
96 */
97#define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"
98
99//==========================================================================
100// Zero the BSS.
101
102static void zeroBSS(void)
103{
104extern char _DATA__bss__begin, _DATA__bss__end;
105extern char _DATA__common__begin, _DATA__common__end;
106
107bzero(&_DATA__bss__begin, (&_DATA__bss__end - &_DATA__bss__begin));
108bzero(&_DATA__common__begin, (&_DATA__common__end - &_DATA__common__begin));
109}
110
111//==========================================================================
112// Malloc error function
113
114static void malloc_error(char *addr, size_t size, const char *file, int line)
115{
116stop("\nMemory allocation error! Addr=0x%x, Size=0x%x, File=%s, Line=%d\n", (unsigned)addr, (unsigned)size, file, line);
117}
118
119//==========================================================================
120//Initializes the runtime. Right now this means zeroing the BSS and initializing malloc.
121//
122void initialize_runtime(void)
123{
124zeroBSS();
125malloc_init(0, 0, 0, malloc_error);
126}
127
128//==========================================================================
129// execKernel - Load the kernel image (mach-o) and jump to its entry point.
130
131static int ExecKernel(void *binary)
132{
133 entry_t kernelEntry;
134 int ret;
135
136 bootArgs->kaddr = bootArgs->ksize = 0;
137
138 ret = DecodeKernel(binary,
139 &kernelEntry,
140 (char **) &bootArgs->kaddr,
141 (int *)&bootArgs->ksize );
142
143 if ( ret != 0 )
144 return ret;
145
146 // Reserve space for boot args
147 reserveKernBootStruct();
148
149 // Load boot drivers from the specifed root path.
150
151 if (!gHaveKernelCache) {
152 LoadDrivers("/");
153 }
154
155 clearActivityIndicator();
156
157 if (gErrors) {
158 printf("Errors encountered while starting up the computer.\n");
159 printf("Pausing %d seconds...\n", kBootErrorTimeout);
160 sleep(kBootErrorTimeout);
161 }
162
163 setupFakeEfi();
164
165 md0Ramdisk();
166
167 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
168
169 // Cleanup the PXE base code.
170
171 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
172if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
173 {
174 printf("nbpUnloadBaseCode error %d\n", (int) ret);
175 sleep(2);
176 }
177 }
178
179 bool dummyVal;
180
181//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.
182// Works properly if typed in first place or used on Boot.plist.
183if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {
184pause();
185}
186
187usb_loop();
188
189// Notify modules that the kernel is about to be started
190execute_hook("Kernel Start", (void*)kernelEntry, (void*)bootArgs, NULL, NULL);
191
192 // If we were in text mode, switch to graphics mode.
193 // This will draw the boot graphics unless we are in
194 // verbose mode.
195
196 if(gVerboseMode)
197 setVideoMode( GRAPHICS_MODE, 0 );
198 else
199 drawBootGraphics();
200
201setupBooterLog();
202
203 finalizeBootStruct();
204
205 // Jump to kernel's entry point. There's no going back now.
206
207 startprog( kernelEntry, bootArgs );
208
209 // Not reached
210
211 return 0;
212}
213
214//==========================================================================
215// This is the entrypoint from real-mode which functions exactly as it did
216// before. Multiboot does its own runtime initialization, does some of its
217// own things, and then calls common_boot.
218void boot(int biosdev)
219{
220initialize_runtime();
221// Enable A20 gate before accessing memory above 1Mb.
222enableA20();
223common_boot(biosdev);
224}
225
226//==========================================================================
227// The 'main' function for the booter. Called by boot0 when booting
228// from a block device, or by the network booter.
229//
230// arguments:
231// biosdev - Value passed from boot1/NBP to specify the device
232// that the booter was loaded from.
233//
234// If biosdev is kBIOSDevNetwork, then this function will return if
235// booting was unsuccessful. This allows the PXE firmware to try the
236// next boot device on its list.
237void common_boot(int biosdev)
238{
239 int status;
240 char *bootFile;
241 unsigned long adler32;
242 bool quiet;
243 bool firstRun = true;
244 bool instantMenu;
245 bool rescanPrompt;
246 unsigned int allowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
247 unsigned int denyBVFlags = kBVFlagEFISystem;
248
249 // Set reminder to unload the PXE base code. Neglect to unload
250 // the base code will result in a hang or kernel panic.
251 gUnloadPXEOnExit = true;
252
253 // Record the device that the booter was loaded from.
254 gBIOSDev = biosdev & kBIOSDevMask;
255
256 // Initialize boot info structure.
257 initKernBootStruct();
258
259initBooterLog();
260
261 // Setup VGA text mode.
262 // Not sure if it is safe to call setVideoMode() before the
263 // config table has been loaded. Call video_mode() instead.
264#if DEBUG
265 printf("before video_mode\n");
266#endif
267 video_mode( 2 ); // 80x25 mono text mode.
268#if DEBUG
269 printf("after video_mode\n");
270#endif
271
272 // Scan and record the system's hardware information.
273 scan_platform();
274
275 // First get info for boot volume.
276 scanBootVolumes(gBIOSDev, 0);
277 bvChain = getBVChainForBIOSDev(gBIOSDev);
278 setBootGlobals(bvChain);
279
280 // Load boot.plist config file
281 status = loadSystemConfig(&bootInfo->bootConfig);
282
283 if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet) {
284 gBootMode |= kBootModeQuiet;
285 }
286
287 // Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
288 if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu) {
289 firstRun = false;
290 }
291
292// Intialize module system
293if(init_module_system())
294{
295load_all_modules();
296}
297
298// Loading preboot ramdisk if exists.
299loadPrebootRAMDisk();
300
301 // Disable rescan option by default
302 gEnableCDROMRescan = false;
303
304 // Enable it with Rescan=y in system config
305 if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan) {
306 gEnableCDROMRescan = true;
307 }
308
309 // Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
310 rescanPrompt = false;
311 if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig) && rescanPrompt && biosDevIsCDROM(gBIOSDev)) {
312 gEnableCDROMRescan = promptForRescanOption();
313 }
314
315 // Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
316 if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive) {
317 gScanSingleDrive = true;
318 }
319
320 // Create a list of partitions on device(s).
321 if (gScanSingleDrive) {
322 scanBootVolumes(gBIOSDev, &bvCount);
323 } else {
324 scanDisks(gBIOSDev, &bvCount);
325 }
326
327 // Create a separated bvr chain using the specified filters.
328 bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
329
330 gBootVolume = selectBootVolume(bvChain);
331
332#if DEBUG
333 printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
334 printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n", gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
335 getc();
336#endif
337
338 useGUI = true;
339 // Override useGUI default
340 getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);
341
342/*
343 * AutoResolution
344 */
345// Before initGui, patch the video bios with the correct resolution
346UInt32 params[4];
347params[3] = 0;
348
349// default to "false" as it doesn't work for everyone atm.
350// http://forum.voodooprojects.org/index.php/topic,1227.0.html
351gAutoResolution = false;
352
353getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
354
355//Open the VBios and store VBios or Tables
356map = openVbios(CT_UNKWN);
357
358if (gAutoResolution == true)
359{
360//Get Resolution from Graphics Mode key or EDID
361int count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);
362if (count < 3)
363getResolution(params);
364else
365{
366if ( params[2] == 256 ) params[2] = 8;
367if ( params[2] == 555 ) params[2] = 16;
368if ( params[2] == 888 ) params[2] = 32;
369}
370
371#ifdef AUTORES_DEBUG
372printf("Resolution: %dx%d\n",params[0], params[1]);
373#endif
374
375//perfom the actual VBIOS patching
376if (params[0] != 0 && params[1] != 0)
377patchVbios(map, params[0], params[1], params[2], 0, 0);
378}
379
380 if (useGUI && initGUI())
381{
382// initGUI() returned with an error, disabling GUI.
383useGUI = false;
384}
385
386 setBootGlobals(bvChain);
387
388 // Parse args, load and start kernel.
389 while (1) {
390 const char *val;
391 int len;
392 int trycache;
393 long flags, cachetime, kerneltime, exttime, sleeptime, time;
394 int ret = -1;
395 void *binary = (void *)kLoadAddr;
396 bool tryresume;
397 bool tryresumedefault;
398 bool forceresume;
399bool patchKernel = false; //Kpatcher - default value.
400
401 // additional variable for testing alternate kernel image locations on boot helper partitions.
402 char bootFileSpec[512];
403
404 // Initialize globals.
405
406 sysConfigValid = false;
407 gErrors = false;
408
409 status = getBootOptions(firstRun);
410 firstRun = false;
411 if (status == -1) continue;
412
413//Azi: i'm now almost sure that here is the right place to do this! - test (gBootVolume == NULL)
414// Turn off any GUI elements, draw background and update VRAM.
415if ( bootArgs->Video.v_display == GRAPHICS_MODE )
416{
417gui.devicelist.draw = false;
418gui.bootprompt.draw = false;
419gui.menu.draw = false;
420gui.infobox.draw = false;
421gui.logo.draw = false;
422drawBackground();
423updateVRAM();
424}
425
426//
427//AutoResolution - Reapply the patch or cancel if Graphics Mode was incorrect
428// or EDID Info was insane
429getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
430
431//Restore the vbios for Cancelation
432if ((gAutoResolution == false) && map)
433{
434restoreVbios(map);
435closeVbios(map);
436}
437
438if ((gAutoResolution == true) && map)
439{
440// If mode has been switched during boot menu
441// use the new resolution
442if (map->hasSwitched == true)
443{
444params[0] = map->currentX;
445params[1] = map->currentY;
446params[2] = 32;
447}
448else
449{
450//or get resolution from Graphics Mode or EDID
451int count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);
452if (count < 3)
453getResolution(params);
454else
455{
456if ( params[2] == 256 ) params[2] = 8;
457if ( params[2] == 555 ) params[2] = 16;
458if ( params[2] == 888 ) params[2] = 32;
459}
460}
461
462//Resolution has changed, reapply the patch
463if ((params[0] != 0) && (params[1] != 0) && (params[0] != map->currentX) &&
464(params[1] != map->currentY))
465{
466patchVbios(map, params[0], params[1], params[2], 0, 0);
467}
468closeVbios(map);
469}
470
471 status = processBootOptions();
472// Status == 1 means to chainboot
473if ( status ==1 ) break;
474// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore!
475if ( status == -1 )
476{
477// gBootVolume == NULL usually means the user hit escape.
478if (gBootVolume == NULL)
479{
480freeFilteredBVChain(bvChain);
481
482if (gEnableCDROMRescan)
483rescanBIOSDevice(gBIOSDev);
484
485bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
486setBootGlobals(bvChain);
487setupDeviceList(&bootInfo->themeConfig);
488}
489continue;
490}
491
492 // Other status (e.g. 0) means that we should proceed with boot.
493
494// If cpu doesn't handle 64 bit instructions,...
495if (!platformCPUFeature(CPU_FEATURE_EM64T) ||
496// ... user forced i386 kernel architecture on cpu with "em64t"...
497getValueForKey(kArchI386Flag, &val, &len, &bootInfo->bootConfig) ||
498// ... or forced Legacy Mode...
499getValueForKey(kLegacyModeFlag, &val, &len, &bootInfo->bootConfig))
500{
501// ... use i386 kernel arch.
502archCpuType = CPU_TYPE_I386;
503}
504else
505{
506// Else use x86_64 kernel arch.
507archCpuType = CPU_TYPE_X86_64;
508}
509// Override i386/-legacy, if flagged on Boot.plist.
510if (getValueForKey(kArchX86_64Flag, &val, &len, &bootInfo->bootConfig))
511{
512archCpuType = CPU_TYPE_X86_64;
513}
514
515if (!getBoolForKey (kWake, &tryresume, &bootInfo->bootConfig)) {
516tryresume = true;
517tryresumedefault = true;
518} else {
519tryresumedefault = false;
520}
521
522if (!getBoolForKey (kForceWake, &forceresume, &bootInfo->bootConfig)) {
523forceresume = false;
524}
525
526if (forceresume) {
527tryresume = true;
528tryresumedefault = false;
529}
530
531while (tryresume) {
532const char *tmp;
533BVRef bvr;
534if (!getValueForKey(kWakeImage, &val, &len, &bootInfo->bootConfig))
535val="/private/var/vm/sleepimage";
536
537// Do this first to be sure that root volume is mounted
538ret = GetFileInfo(0, val, &flags, &sleeptime);
539
540if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
541break;
542
543// Can't check if it was hibernation Wake=y is required
544if (bvr->modTime == 0 && tryresumedefault)
545break;
546
547if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
548break;
549
550if (!forceresume && ((sleeptime+3)<bvr->modTime)) {
551printf ("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",bvr->modTime-sleeptime);
552break;
553}
554
555HibernateBoot((char *)val);
556break;
557}
558
559 // Reset cache name.
560 bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
561
562 sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
563
564 adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
565
566 if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig)) {
567 strlcpy(gBootKernelCacheFile, val, len+1);
568 } else {
569 sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);
570 }
571
572 // Check for cache file.
573 trycache = (((gBootMode & kBootModeSafe) == 0) &&
574 !gOverrideKernel &&
575 (gBootFileType == kBlockDeviceType) &&
576 (gMKextName[0] == '\0') &&
577 (gBootKernelCacheFile[0] != '\0'));
578
579verbose("Loading Darwin %s\n", gMacOSVersion);
580
581 if (trycache) do {
582
583 // if we haven't found the kernel yet, don't use the cache
584 ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
585 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)) {
586 trycache = 0;
587 break;
588 }
589 ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
590 if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
591 || (cachetime < kerneltime)) {
592 trycache = 0;
593 break;
594 }
595 ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
596 if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
597 && (cachetime < exttime)) {
598 trycache = 0;
599 break;
600 }
601 if (kerneltime > exttime) {
602 exttime = kerneltime;
603 }
604 if (cachetime != (exttime + 1)) {
605 trycache = 0;
606 break;
607 }
608 } while (0);
609
610 do {
611 if (trycache) {
612 bootFile = gBootKernelCacheFile;
613 verbose("Loading kernel cache %s\n", bootFile);
614 ret = LoadFile(bootFile);
615 binary = (void *)kLoadAddr;
616 if (ret >= 0) {
617 break;
618 }
619 }
620 bootFile = bootInfo->bootFile;
621
622 // Try to load kernel image from alternate locations on boot helper partitions.
623 sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile);
624 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
625 if (ret == -1)
626 {
627 sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
628 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
629 if (ret == -1)
630 {
631 sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
632 ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
633 if (ret == -1)
634 {
635 // Not found any alternate locations, using the original kernel image path.
636 strcpy(bootFileSpec, bootFile);
637 }
638 }
639 }
640
641 verbose("Loading kernel %s\n", bootFileSpec);
642 ret = LoadThinFatFile(bootFileSpec, &binary);
643 if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
644 {
645 archCpuType = CPU_TYPE_I386;
646 ret = LoadThinFatFile(bootFileSpec, &binary);
647 }
648
649 } while (0);
650
651 clearActivityIndicator();
652#if DEBUG
653 printf("Pausing...");
654 sleep(8);
655#endif
656
657 if (ret <= 0) {
658printf("Can't find %s\n", bootFile);
659
660sleep(1);
661
662 if (gBootFileType == kNetworkDeviceType) {
663 // Return control back to PXE. Don't unload PXE base code.
664 gUnloadPXEOnExit = false;
665 break;
666 }
667 } else {
668 /* Won't return if successful. */
669// Notify modules that ExecKernel is about to be called
670getBoolForKey(kKPatcherKey, &patchKernel, &bootInfo->bootConfig);
671
672if (patchKernel == true)
673{
674execute_hook("ExecKernel", binary, NULL, NULL, NULL);
675}
676 ret = ExecKernel(binary);
677 }
678 }
679
680 // chainboot
681 if (status==1) {
682if (getVideoMode() == GRAPHICS_MODE) {// if we are already in graphics-mode,
683setVideoMode(VGA_TEXT_MODE, 0);// switch back to text mode
684}
685 }
686
687 if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit) {
688nbpUnloadBaseCode();
689 }
690}
691
692/*!
693 Selects a new BIOS device, taking care to update the global state appropriately.
694 */
695/*
696static void selectBiosDevice(void)
697{
698 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
699 CacheReset();
700 diskFreeMap(oldMap);
701 oldMap = NULL;
702
703 int dev = selectAlternateBootDevice(gBIOSDev);
704
705 BVRef bvchain = scanBootVolumes(dev, 0);
706 BVRef bootVol = selectBootVolume(bvchain);
707 gBootVolume = bootVol;
708 setRootVolume(bootVol);
709 gBIOSDev = dev;
710}
711*/
712
713#define BASE 65521L /* largest prime smaller than 65536 */
714#define NMAX 5000
715// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
716
717#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
718#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
719#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
720#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
721#define DO16(buf) DO8(buf,0); DO8(buf,8);
722
723unsigned long Adler32(unsigned char *buf, long len)
724{
725 unsigned long s1 = 1; // adler & 0xffff;
726 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
727 unsigned long result;
728 int k;
729
730 while (len > 0) {
731 k = len < NMAX ? len : NMAX;
732 len -= k;
733 while (k >= 16) {
734 DO16(buf);
735 buf += 16;
736 k -= 16;
737 }
738 if (k != 0) do {
739 s1 += *buf++;
740 s2 += s1;
741 } while (--k);
742 s1 %= BASE;
743 s2 %= BASE;
744 }
745 result = (s2 << 16) | s1;
746 return OSSwapHostToBigInt32(result);
747}
748

Archive Download this file

Revision: 497