Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazileon/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//#define DEBUG 1 //Azi:temp
53
54//Azi:include
55//#include "boot.h" - included on graphics.h, which is included on gui.h
56//#include "bootstruct.h" - same as above
57#include "fake_efi.h"
58#include "gui.h"
59//#include "libsa.h" - included on libsaio.h, which is included on sl.h
60#include "platform.h"
61#include "ramdisk.h"
62#include "sl.h"
63
64#include "edid.h" // Autoresolution
65#include "autoresolution.h" // "was" included on boot.h, which is everywere!! -> gui.h -> graphics.h
66
67long gBootMode; /* defaults to 0 == kBootModeNormal */
68bool gOverrideKernel;
69static char gBootKernelCacheFile[512];
70static char gCacheNameAdler[64 + 256];
71//char *gPlatformName = gCacheNameAdler; disabled
72char gRootDevice[512];
73char gMKextName[512];
74bool gEnableCDROMRescan;
75bool gScanSingleDrive;
76
77int bvCount = 0;
78//intmenucount = 0;
79int gDeviceCount = 0;
80
81BVRef bvr;
82BVRef menuBVR;
83BVRef bvChain;
84bool useGUI;
85
86//static void selectBiosDevice(void);
87static unsigned long Adler32(unsigned char *buffer, long length);
88
89static bool gUnloadPXEOnExit = false;
90
91/*
92 * How long to wait (in seconds) to load the
93 * kernel after displaying the "boot:" prompt.
94 */
95#define kBootErrorTimeout 5
96
97/*
98 * Default path to kernel cache file
99 */
100#define kDefaultCachePath "/System/Library/Caches/com.apple.kernelcaches/kernelcache"
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.
153
154 if (!gHaveKernelCache) {
155 LoadDrivers("/");
156 }
157
158 clearActivityIndicator();
159
160 if (gErrors) {
161 printf("Errors encountered while starting up the computer.\n");
162 printf("Pausing %d seconds...\n", kBootErrorTimeout);
163 sleep(kBootErrorTimeout);
164 }
165
166 setupFakeEfi();
167
168 md0Ramdisk();
169
170 verbose("Starting Darwin %s\n",( archCpuType == CPU_TYPE_I386 ) ? "x86" : "x86_64");
171
172 // Cleanup the PXE base code.
173
174 if ( (gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit ) {
175if ( (ret = nbpUnloadBaseCode()) != nbpStatusSuccess )
176 {
177 printf("nbpUnloadBaseCode error %d\n", (int) ret);
178 sleep(2);
179 }
180 }
181
182 bool dummyVal;
183
184//Azi: Wait=y is breaking other keys when typed "after them" at boot prompt.
185// Works properly if typed in first place or used on Boot.plist.
186if (getBoolForKey(kWaitForKeypressKey, &dummyVal, &bootInfo->bootConfig) && dummyVal) {
187pause();
188}
189
190usb_loop();
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{
239boolinstantMenu, quiet, rescanPrompt;
240boolfirstRun = true;
241char*bootFile;
242intstatus;
243unsigned intallowBVFlags = kBVFlagSystemVolume|kBVFlagForeignBoot;
244unsigned intdenyBVFlags = kBVFlagEFISystem;
245unsigned longadler32;
246
247// Set reminder to unload the PXE base code. Neglect to unload
248// the base code will result in a hang or kernel panic.
249gUnloadPXEOnExit = true;
250
251// Record the device that the booter was loaded from.
252gBIOSDev = biosdev & kBIOSDevMask;
253
254// Initialize boot info structure.
255initKernBootStruct();
256
257initBooterLog();
258
259// Setup VGA text mode.
260// Not sure if it is safe to call setVideoMode() before the
261// config table has been loaded. Call video_mode() instead.
262#if DEBUG
263printf("before video_mode\n");
264#endif
265video_mode( 2 ); // 80x25 mono text mode.
266#if DEBUG
267printf("after video_mode\n");
268#endif
269
270// Scan and record the system's hardware information.
271scan_platform();
272
273// First get info for boot volume.
274scanBootVolumes(gBIOSDev, 0);
275bvChain = getBVChainForBIOSDev(gBIOSDev);
276setBootGlobals(bvChain);
277
278// Load boot.plist config file
279status = loadSystemConfig(&bootInfo->bootConfig);
280
281if (getBoolForKey(kQuietBootKey, &quiet, &bootInfo->bootConfig) && quiet)
282{
283gBootMode |= kBootModeQuiet;
284}
285
286// Override firstRun to get to the boot menu instantly by setting "Instant Menu"=y in system config
287if (getBoolForKey(kInstantMenuKey, &instantMenu, &bootInfo->bootConfig) && instantMenu)
288{
289firstRun = false;
290}
291
292// Loading preboot ramdisk if exists.
293loadPrebootRAMDisk();
294
295// Disable rescan option by default
296gEnableCDROMRescan = false;
297
298// Enable it with Rescan=y in system config
299if (getBoolForKey(kRescanKey, &gEnableCDROMRescan, &bootInfo->bootConfig) && gEnableCDROMRescan)
300{
301gEnableCDROMRescan = true;
302}
303
304// Ask the user for Rescan option by setting "Rescan Prompt"=y in system config.
305rescanPrompt = false;
306if (getBoolForKey(kRescanPromptKey, &rescanPrompt , &bootInfo->bootConfig)
307&& rescanPrompt && biosDevIsCDROM(gBIOSDev))
308{
309gEnableCDROMRescan = promptForRescanOption();
310}
311
312// Enable touching a single BIOS device only if "Scan Single Drive"=y is set in system config.
313if (getBoolForKey(kScanSingleDriveKey, &gScanSingleDrive, &bootInfo->bootConfig) && gScanSingleDrive)
314{
315gScanSingleDrive = true;
316}
317
318// Create a list of partitions on device(s).
319if (gScanSingleDrive)
320{
321scanBootVolumes(gBIOSDev, &bvCount);
322}
323else
324{
325scanDisks(gBIOSDev, &bvCount);
326}
327
328// Create a separated bvr chain using the specified filters.
329bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
330
331gBootVolume = selectBootVolume(bvChain);
332
333#if DEBUG
334printf(" Default: %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
335gBootVolume, gBootVolume->biosdev, gBootVolume->part_no, gBootVolume->flags);
336printf(" bt(0,0): %d, ->biosdev: %d, ->part_no: %d ->flags: %d\n",
337gBIOSBootVolume, gBIOSBootVolume->biosdev, gBIOSBootVolume->part_no, gBIOSBootVolume->flags);
338pause(); //getc();
339#endif
340
341useGUI = true;
342
343// Override useGUI default
344getBoolForKey(kGUIKey, &useGUI, &bootInfo->bootConfig);
345
346//Azi:autoresolution begin
347// Before initGui, patch the video bios with the correct resolution
348UInt32 params[4];
349params[3] = 0;
350
351// default to "false" as it doesn't work for everyone atm.
352// http://forum.voodooprojects.org/index.php/topic,1227.0.html
353gAutoResolution = false;
354
355getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
356
357//Open the VBios and store VBios or Tables
358map = openVbios(CT_UNKWN);
359
360if (gAutoResolution == true)
361{
362//Get Resolution from Graphics Mode key or EDID
363int count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);
364if (count < 3)
365getResolution(params);
366else
367{
368if ( params[2] == 256 ) params[2] = 8;
369if ( params[2] == 555 ) params[2] = 16;
370if ( params[2] == 888 ) params[2] = 32;
371}
372
373#ifdef AUTORES_DEBUG
374printf("Resolution: %dx%d\n",params[0], params[1]);
375#endif
376
377//perfom the actual VBIOS patching
378if (params[0] != 0 && params[1] != 0)
379patchVbios(map, params[0], params[1], params[2], 0, 0);
380}
381//Azi:autoresolution end
382
383if (useGUI && initGUI())
384{
385// initGUI() returned with an error, disabling GUI.
386useGUI = false;
387}
388
389setBootGlobals(bvChain);
390
391// Parse args, load and start kernel.
392while (1)
393{
394boolforceresume, tryresume, tryresumedefault;
395const char*val;
396intlen, trycache;
397intret = -1;
398longflags, cachetime, kerneltime, exttime, sleeptime, time;
399void*binary = (void *)kLoadAddr;
400
401// additional variable for testing alternate kernel image locations on boot helper partitions.
402charbootFileSpec[512]; //Azi:HelperConfig - kernel
403
404// Initialize globals.
405
406sysConfigValid = false;
407gErrors = false;
408
409status = getBootOptions(firstRun);
410firstRun = false;
411if ( 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//Azi:autoresolution begin
427
428//
429//AutoResolution - Reapply the patch or cancel if Graphics Mode was incorrect
430//or EDID Info was insane
431getBoolForKey(kAutoResolutionKey, &gAutoResolution, &bootInfo->bootConfig);
432
433//Restore the vbios for Cancelation
434if ((gAutoResolution == false) && map)
435{
436restoreVbios(map);
437closeVbios(map);
438}
439
440if ((gAutoResolution == true) && map)
441{
442// If mode has been switched during boot menu
443// use the new resolution
444if (map->hasSwitched == true)
445{
446params[0] = map->currentX;
447params[1] = map->currentY;
448params[2] = 32;
449}
450else
451{
452//or get resolution from Graphics Mode or EDID
453int count = getNumberArrayFromProperty(kGraphicsModeKey, params, 4);
454if (count < 3)
455getResolution(params);
456else
457{
458if ( params[2] == 256 ) params[2] = 8;
459if ( params[2] == 555 ) params[2] = 16;
460if ( params[2] == 888 ) params[2] = 32;
461}
462}
463
464//Resolution has changed, reapply the patch
465if ((params[0] != 0) && (params[1] != 0) && (params[0] != map->currentX) &&
466(params[1] != map->currentY))
467{
468patchVbios(map, params[0], params[1], params[2], 0, 0);
469}
470closeVbios(map);
471}
472
473//Azi:autoresolution end
474
475status = processBootOptions();
476// Status == 1 means to chainboot
477if ( status ==1 ) break;
478// Status == -1 means that gBootVolume is NULL. Config file is not mandatory anymore!
479if ( status == -1 )
480{
481// gBootVolume == NULL usually means the user hit escape.
482if (gBootVolume == NULL) //Azi: reminder - hitting escape makes me boot when "at" boot prompt.
483{
484freeFilteredBVChain(bvChain);
485
486if (gEnableCDROMRescan)
487rescanBIOSDevice(gBIOSDev);
488
489bvChain = newFilteredBVChain(0x80, 0xFF, allowBVFlags, denyBVFlags, &gDeviceCount);
490setBootGlobals(bvChain);
491setupDeviceList(&bootInfo->themeConfig); //Azi: test this!!!
492}
493continue;
494}
495
496// Other status (e.g. 0) means that we should proceed with boot.
497
498// If cpu doesn't handle 64 bit instructions,...
499if (!platformCPUFeature(CPU_FEATURE_EM64T) ||
500// ... user forced i386 kernel architecture on cpu with "em64t"...
501getValueForKey(kArchI386Flag, &val, &len, &bootInfo->bootConfig) ||
502// ... or forced Legacy Mode...
503getValueForKey(kLegacyModeFlag, &val, &len, &bootInfo->bootConfig))
504{
505// ... use i386 kernel arch.
506archCpuType = CPU_TYPE_I386;
507}
508else
509{
510// Else use x86_64 kernel arch.
511archCpuType = CPU_TYPE_X86_64;
512}
513// Override i386/-legacy, if flagged on Boot.plist.
514if (getValueForKey(kArchX86_64Flag, &val, &len, &bootInfo->bootConfig))
515{
516archCpuType = CPU_TYPE_X86_64;
517}
518
519if (!getBoolForKey (kWakeKey, &tryresume, &bootInfo->bootConfig))
520{
521tryresume = true;
522tryresumedefault = true;
523}
524else
525{
526tryresumedefault = false;
527}
528
529if (!getBoolForKey (kForceWakeKey, &forceresume, &bootInfo->bootConfig))
530{
531forceresume = false;
532}
533
534if (forceresume)
535{
536tryresume = true;
537tryresumedefault = false;
538}
539
540while (tryresume)
541{
542const char *tmp;
543BVRef bvr;
544if (!getValueForKey(kWakeImageKey, &val, &len, &bootInfo->bootConfig))
545val="/private/var/vm/sleepimage";
546
547// Do this first to be sure that root volume is mounted
548ret = GetFileInfo(0, val, &flags, &sleeptime);
549
550if ((bvr = getBootVolumeRef(val, &tmp)) == NULL)
551break;
552
553// Can't check if it was hibernation Wake=y is required
554if (bvr->modTime == 0 && tryresumedefault)
555break;
556
557if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
558break;
559
560if (!forceresume && ((sleeptime+3)<bvr->modTime))
561{
562printf("Hibernate image is too old by %d seconds. Use ForceWake=y to override\n",
563bvr->modTime-sleeptime);
564break;
565}
566HibernateBoot((char *)val);
567break;
568}
569
570// Reset cache name.
571bzero(gCacheNameAdler + 64, sizeof(gCacheNameAdler) - 64);
572
573sprintf(gCacheNameAdler + 64, "%s,%s", gRootDevice, bootInfo->bootFile);
574
575adler32 = Adler32((unsigned char *)gCacheNameAdler, sizeof(gCacheNameAdler));
576
577if (getValueForKey(kKernelCacheKey, &val, &len, &bootInfo->bootConfig))
578{
579strlcpy(gBootKernelCacheFile, val, len+1);
580}
581else
582{
583sprintf(gBootKernelCacheFile, "%s.%08lX", kDefaultCachePath, adler32);
584}
585
586// Check for cache file.
587trycache = (((gBootMode & kBootModeSafe) == 0) &&
588!gOverrideKernel &&
589(gBootFileType == kBlockDeviceType) &&
590(gMKextName[0] == '\0') &&
591(gBootKernelCacheFile[0] != '\0'));
592
593verbose("Loading Darwin %s\n", gMacOSVersion);
594
595if (trycache) do
596{
597// if we haven't found the kernel yet, don't use the cache
598ret = GetFileInfo(NULL, bootInfo->bootFile, &flags, &kerneltime);
599if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat))
600{
601trycache = 0;
602break;
603}
604
605ret = GetFileInfo(NULL, gBootKernelCacheFile, &flags, &cachetime);
606if ((ret != 0) || ((flags & kFileTypeMask) != kFileTypeFlat)
607|| (cachetime < kerneltime))
608{
609trycache = 0;
610break;
611}
612
613ret = GetFileInfo("/System/Library/", "Extensions", &flags, &exttime);
614if ((ret == 0) && ((flags & kFileTypeMask) == kFileTypeDirectory)
615&& (cachetime < exttime))
616{
617trycache = 0;
618break;
619}
620
621if (kerneltime > exttime)
622{
623exttime = kerneltime;
624}
625
626if (cachetime != (exttime + 1))
627{
628trycache = 0;
629break;
630}
631} while (0);
632
633do
634{
635if (trycache)
636{
637bootFile = gBootKernelCacheFile;
638verbose("Loading kernel cache %s\n", bootFile); //Azi: i never saw this!! check!!!
639ret = LoadFile(bootFile);
640binary = (void *)kLoadAddr;
641if (ret >= 0)
642{
643break;
644}
645}
646
647bootFile = bootInfo->bootFile;
648
649// Try to load kernel image from alternate locations on boot helper partitions.
650sprintf(bootFileSpec, "com.apple.boot.P/%s", bootFile); //Azi:HelperConfig - kernel
651
652ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
653if (ret == -1)
654{
655sprintf(bootFileSpec, "com.apple.boot.R/%s", bootFile);
656
657ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
658if (ret == -1)
659{
660sprintf(bootFileSpec, "com.apple.boot.S/%s", bootFile);
661
662ret = GetFileInfo(NULL, bootFileSpec, &flags, &time);
663if (ret == -1)
664{
665// Not found any alternate locations, using the original kernel image path.
666strcpy(bootFileSpec, bootFile);
667}
668}
669}
670
671verbose("Loading kernel %s\n", bootFileSpec);
672
673ret = LoadThinFatFile(bootFileSpec, &binary);
674if (ret <= 0 && archCpuType == CPU_TYPE_X86_64)
675{
676archCpuType = CPU_TYPE_I386;
677ret = LoadThinFatFile(bootFileSpec, &binary);
678}
679
680} while (0);
681
682clearActivityIndicator();
683/*#if DEBUG
684printf("Pausing...");
685sleep(8);
686#endif Azi: annoying! Can't see the point atm... */
687
688if (ret <= 0)
689{
690printf("Can't find %s\n", bootFile);
691
692sleep(1);
693
694if (gBootFileType == kNetworkDeviceType) {
695// Return control back to PXE. Don't unload PXE base code.
696gUnloadPXEOnExit = false;
697break;
698}
699}
700else
701{
702/* Won't return if successful. */
703ret = ExecKernel(binary);
704}
705}
706
707// chainboot
708if (status==1)
709{
710if (getVideoMode() == GRAPHICS_MODE)
711{// if we are already in graphics-mode,
712setVideoMode(VGA_TEXT_MODE, 0); // switch back to text mode
713}
714}
715
716if ((gBootFileType == kNetworkDeviceType) && gUnloadPXEOnExit)
717{
718nbpUnloadBaseCode();
719}
720}
721
722/*!
723 Selects a new BIOS device, taking care to update the global state appropriately.
724 */
725/*
726static void selectBiosDevice(void)
727{
728 struct DiskBVMap *oldMap = diskResetBootVolumes(gBIOSDev);
729 CacheReset();
730 diskFreeMap(oldMap);
731 oldMap = NULL;
732
733 int dev = selectAlternateBootDevice(gBIOSDev);
734
735 BVRef bvchain = scanBootVolumes(dev, 0);
736 BVRef bootVol = selectBootVolume(bvchain);
737 gBootVolume = bootVol;
738 setRootVolume(bootVol);
739 gBIOSDev = dev;
740}
741*/
742
743#define BASE 65521L /* largest prime smaller than 65536 */
744#define NMAX 5000
745// NMAX (was 5521) the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
746
747#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
748#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
749#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
750#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
751#define DO16(buf) DO8(buf,0); DO8(buf,8);
752
753unsigned long Adler32(unsigned char *buf, long len)
754{
755 unsigned long s1 = 1; // adler & 0xffff;
756 unsigned long s2 = 0; // (adler >> 16) & 0xffff;
757 unsigned long result;
758 int k;
759
760 while (len > 0) {
761 k = len < NMAX ? len : NMAX;
762 len -= k;
763 while (k >= 16) {
764 DO16(buf);
765 buf += 16;
766 k -= 16;
767 }
768 if (k != 0) do {
769 s1 += *buf++;
770 s2 += s1;
771 } while (--k);
772 s1 %= BASE;
773 s2 %= BASE;
774 }
775 result = (s2 << 16) | s1;
776 return OSSwapHostToBigInt32(result);
777}
778

Archive Download this file

Revision: 451