Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 767