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

Archive Download this file

Revision: 515