1/*
2 * Copyright (c) 1999, 2005-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/* SRuntime.h */
24
25#ifndef __SRUNTIME__
26#define __SRUNTIME__
27
28#if BSD
29
30#include <sys/types.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdio.h>
34#include <stdarg.h>
35
36#include <hfs/hfs_format.h>
37#else
38
39#include <MacTypes.h>
40#include <MacMemory.h>
41#include <HFSVolumes.h>
42#include <Errors.h>
43
44#endif
45
46#if BSD
47/* Classic Mac OS Types */
48typedef int8_t		SInt8;
49typedef int16_t		SInt16;
50typedef int32_t		SInt32;
51typedef int64_t		SInt64;
52
53typedef u_int8_t	UInt8;
54typedef u_int16_t	UInt16;
55typedef u_int32_t	UInt32;
56typedef u_int64_t	UInt64;
57
58typedef void *		LogicalAddress;
59typedef char *		Ptr;
60typedef Ptr *		Handle;
61typedef u_int8_t	Byte;
62typedef size_t 		Size;
63typedef unsigned char	Boolean;
64typedef u_int32_t	ItemCount;
65typedef u_int32_t	ByteCount;
66typedef u_int32_t	OptionBits;
67
68typedef int16_t 	OSErr;
69typedef int32_t 	OSStatus;
70
71typedef u_int32_t	OSType;
72typedef u_int32_t	ResType;
73
74typedef u_int16_t	UniChar;
75typedef u_int32_t	UniCharCount;
76typedef UniChar *	UniCharArrayPtr;
77typedef const UniChar *	ConstUniCharArrayPtr;
78typedef u_int32_t	TextEncoding;
79
80typedef unsigned char *	StringPtr;
81typedef unsigned char	Str27[28];
82typedef unsigned char	Str31[32];
83typedef unsigned char	Str63[64];
84typedef unsigned char	Str255[256];
85
86typedef const unsigned char *	ConstStr31Param;
87typedef const unsigned char *	ConstStr63Param;
88typedef const unsigned char *	ConstStr255Param;
89
90typedef u_int32_t	HFSCatalogNodeID;
91
92enum {
93	false		= 0,
94	true		= 1
95};
96
97/* OS error codes */
98enum {
99	noErr		=     0,
100	dskFulErr	=   -34,
101	nsvErr		=   -35,
102	ioErr		=   -36,
103	eofErr		=   -39,
104	fnfErr		=   -43,
105	fBsyErr		=   -47,
106	paramErr	=   -50,
107	noMacDskErr	=   -57,
108	badMDBErr	=   -60,
109	memFullErr	=  -108,
110	notBTree	=  -410,
111	fileBoundsErr	= -1309,
112};
113
114/* Finder Flags */
115enum {
116    kIsOnDesk		= 0x0001,
117    kColor		= 0x000E,
118    kIsShared		= 0x0040,
119    kHasBeenInited	= 0x0100,
120    kHasCustomIcon	= 0x0400,
121    kIsStationery	= 0x0800,
122    kNameLocked		= 0x1000,
123    kHasBundle		= 0x2000,
124    kIsInvisible	= 0x4000,
125    kIsAlias		= 0x8000
126};
127
128#define EXTERN_API(_type)	extern _type
129#define EXTERN_API_C(_type)	extern _type
130
131#define nil NULL
132
133EXTERN_API( void )
134DebugStr(ConstStr255Param debuggerMsg);
135
136typedef void * QElemPtr;
137typedef void * DrvQElPtr;
138
139
140#endif
141
142
143
144/* vcbFlags bits */
145enum {
146  kVCBFlagsIdleFlushBit           = 3,                                                    /* Set if volume should be flushed at idle time */
147        kVCBFlagsIdleFlushMask          = 0x0008,
148        kVCBFlagsHFSPlusAPIsBit         = 4,                                                    /* Set if volume implements HFS Plus APIs itself (not via emu\
149												   lation) */
150        kVCBFlagsHFSPlusAPIsMask        = 0x0010,
151        kVCBFlagsHardwareGoneBit        = 5,                                                    /* Set if disk driver returned a hardwareGoneErr to Read or W\
152												   rite */
153        kVCBFlagsHardwareGoneMask       = 0x0020,
154        kVCBFlagsVolumeDirtyBit         = 15,                                                   /* Set if volume information has changed since the last Flush\
155												   Vol */
156        kVCBFlagsVolumeDirtyMask        = 0x8000
157};
158
159
160/* Disk Cache constants */
161/*
162 * UTGetBlock options
163 */
164enum {
165	gbDefault					= 0,							/* default value - read if not found */
166																/*	bits and masks */
167	gbReadBit					= 0,							/* read block from disk (forced read) */
168	gbReadMask					= 0x0001,
169	gbExistBit					= 1,							/* get existing cache block */
170	gbExistMask					= 0x0002,
171	gbNoReadBit					= 2,							/* don't read block from disk if not found in cache */
172	gbNoReadMask				= 0x0004,
173	gbReleaseBit				= 3,							/* release block immediately after GetBlock */
174	gbReleaseMask				= 0x0008
175};
176
177
178/*
179 * UTReleaseBlock options
180 */
181enum {
182	rbDefault					= 0,							/* default value - just mark the buffer not in-use */
183																/*	bits and masks */
184	rbWriteBit					= 0,							/* force write buffer to disk */
185	rbWriteMask					= 0x0001,
186	rbTrashBit					= 1,							/* trash buffer contents after release */
187	rbTrashMask					= 0x0002,
188	rbDirtyBit					= 2,							/* mark buffer dirty */
189	rbDirtyMask					= 0x0004,
190	rbFreeBit					= 3,							/* free the buffer (save in the hash) */
191	rbFreeMask					= 0x000A						/* rbFreeMask (rbFreeBit + rbTrashBit) works as rbTrash on < System 7.0 RamCache; on >= System 7.0, rbfreeMask overrides rbTrash */
192};
193
194
195/*
196 * UTFlushCache options
197 */
198enum {
199	fcDefault					= 0,							/* default value - pass this fcOption to just flush any dirty buffers */
200																/*	bits and masks */
201	fcTrashBit					= 0,							/* (don't pass this as fcOption, use only for testing bit) */
202	fcTrashMask					= 0x0001,						/* pass this fcOption value to flush and trash cache blocks */
203	fcFreeBit					= 1,							/* (don't pass this as fcOption, use only for testing bit) */
204	fcFreeMask					= 0x0003						/* pass this fcOption to flush and free cache blocks (Note: both fcTrash and fcFree bits are set) */
205};
206
207
208/*
209 * UTCacheReadIP and UTCacheWriteIP cacheOption bits and masks are the ioPosMode
210 * bits and masks in Files.x
211 */
212
213/*
214 * Cache routine internal error codes
215 */
216enum {
217	chNoBuf						= 1,							/* no free cache buffers (all in use) */
218	chInUse						= 2,							/* requested block in use */
219	chnotfound					= 3,							/* requested block not found */
220	chNotInUse					= 4								/* block being released was not in use */
221};
222
223
224/*
225 * FCBRec.fcbFlags bits
226 */
227enum {
228	fcbWriteBit					= 0,							/* Data can be written to this file */
229	fcbWriteMask				= 0x01,
230	fcbResourceBit				= 1,							/* This file is a resource fork */
231	fcbResourceMask				= 0x02,
232	fcbWriteLockedBit			= 2,							/* File has a locked byte range */
233	fcbWriteLockedMask			= 0x04,
234	fcbSharedWriteBit			= 4,							/* File is open for shared write access */
235	fcbSharedWriteMask			= 0x10,
236	fcbFileLockedBit			= 5,							/* File is locked (write-protected) */
237	fcbFileLockedMask			= 0x20,
238	fcbOwnClumpBit				= 6,							/* File has clump size specified in FCB */
239	fcbOwnClumpMask				= 0x40,
240	fcbModifiedBit				= 7,							/* File has changed since it was last flushed */
241	fcbModifiedMask				= 0x80
242};
243
244enum {
245	fcbLargeFileBit				= 3,							/* File may grow beyond 2GB; cache uses file blocks, not bytes */
246	fcbLargeFileMask			= 0x08
247};
248
249#define kSectorShift  9   /* log2(kSectorSize); used for bit shifts */
250
251/*
252	Fork Level Access Method Block get options
253*/
254enum {
255	kGetBlock	= 0x00000000,
256	kForceReadBlock	= 0x00000002,
257	kGetEmptyBlock	= 0x00000008,
258	kSkipEndianSwap	= 0x00000010
259};
260typedef OptionBits  GetBlockOptions;
261
262/*
263	Fork Level Access Method Block release options
264*/
265enum {
266	kReleaseBlock	 = 0x00000000,
267	kForceWriteBlock = 0x00000001,
268	kMarkBlockDirty	 = 0x00000002,
269	kTrashBlock	 	 = 0x00000004
270};
271typedef OptionBits  ReleaseBlockOptions;
272
273struct BlockDescriptor{
274	void		*buffer;
275	void		*blockHeader;
276	UInt64		 blockNum;
277	UInt32		 blockSize;
278	Boolean		 blockReadFromDisk;
279	Boolean		 fragmented;
280};
281typedef struct BlockDescriptor BlockDescriptor;
282typedef BlockDescriptor *BlockDescPtr;
283
284
285
286struct SFCB;
287
288struct SVCB {
289	UInt16		vcbSignature;
290	UInt16		vcbVersion;
291	UInt32		vcbAttributes;
292	UInt32		vcbLastMountedVersion;
293	UInt32		vcbReserved1;
294	UInt32		vcbCreateDate;
295	UInt32		vcbModifyDate;
296	UInt32		vcbBackupDate;
297	UInt32		vcbCheckedDate;
298	UInt32		vcbFileCount;
299	UInt32		vcbFolderCount;
300	UInt32		vcbBlockSize;
301	UInt32		vcbTotalBlocks;
302	UInt32		vcbFreeBlocks;
303	UInt32		vcbNextAllocation;
304	UInt32 		vcbRsrcClumpSize;
305	UInt32		vcbDataClumpSize;
306	UInt32 		vcbNextCatalogID;
307	UInt32		vcbWriteCount;
308	UInt64		vcbEncodingsBitmap;
309	UInt8		vcbFinderInfo[32];
310
311	/* MDB-specific fields... */
312	SInt16		vcbNmFls;	/* number of files in root folder */
313	SInt16		vcbNmRtDirs;	/* number of directories in root folder */
314	UInt16		vcbVBMSt;	/* first sector of HFS volume bitmap */
315	UInt16		vcbAlBlSt;	/* first allocation block in HFS volume */
316	UInt16		vcbVSeqNum;	/* volume backup sequence number */
317	UInt16		vcbReserved2;
318	Str27		vcbVN;		/* HFS volume name */
319
320	/* runtime fields... */
321	struct SFCB * 	vcbAllocationFile;
322	struct SFCB * 	vcbExtentsFile;
323	struct SFCB * 	vcbCatalogFile;
324	struct SFCB * 	vcbAttributesFile;
325	struct SFCB * 	vcbStartupFile;
326
327	UInt32		vcbEmbeddedOffset;	/* Byte offset where HFS+ starts */
328	UInt16		vcbFlags;
329	SInt16		vcbDriveNumber;
330	SInt16		vcbDriverReadRef;
331	SInt16		vcbDriverWriteRef;
332
333	void *		vcbBlockCache;
334
335	struct SGlob *	vcbGPtr;
336
337	/* deprecated fields... */
338	SInt16		vcbVRefNum;
339};
340typedef struct SVCB SVCB;
341
342
343struct SFCB {
344	UInt32			fcbFileID;
345	UInt32			fcbFlags;
346	struct SVCB *		fcbVolume;
347	void *			fcbBtree;
348	HFSExtentRecord		fcbExtents16;
349	HFSPlusExtentRecord	fcbExtents32;
350	UInt32			fcbCatalogHint;
351	UInt32			fcbClumpSize;
352	UInt64 			fcbLogicalSize;
353	UInt64			fcbPhysicalSize;
354	UInt32			fcbBlockSize;
355};
356typedef struct SFCB SFCB;
357
358
359extern OSErr GetDeviceSize(int driveRefNum, UInt64 *numBlocks, UInt32 *blockSize);
360
361extern OSErr DeviceRead(int device, int drive, void* buffer, SInt64 offset, UInt32 reqBytes, UInt32 *actBytes);
362
363extern OSErr DeviceWrite(int device, int drive, void* buffer, SInt64 offset, UInt32 reqBytes, UInt32 *actBytes);
364
365
366/*
367 *  Block Cache Interface
368 */
369extern void      InitBlockCache(SVCB *volume);
370
371extern OSStatus  GetVolumeBlock (SVCB *volume, UInt64 blockNum, GetBlockOptions options,
372				BlockDescriptor *block);
373
374extern OSStatus  ReleaseVolumeBlock (SVCB *volume, BlockDescriptor *block,
375				ReleaseBlockOptions options);
376
377extern OSStatus  GetFileBlock (SFCB *file, UInt32 blockNum, GetBlockOptions options,
378				BlockDescriptor *block);
379
380extern OSStatus  ReleaseFileBlock (SFCB *file, BlockDescriptor *block,
381				ReleaseBlockOptions options);
382
383extern OSStatus  SetFileBlockSize (SFCB *file, ByteCount blockSize);
384
385
386
387#if BSD
388
389#define AllocateMemory(size)		malloc((size_t)(size))
390#define	AllocateClearMemory(size)	calloc(1,(size_t)(size))
391#define ReallocateMemory(ptr,newSize)	SetPtrSize((void*)(ptr),(size_t)(newSize))
392#define MemorySize(ptr)			malloc_size((void*)(ptr))
393#define DisposeMemory(ptr)		free((void *)(ptr))
394#define CopyMemory(src,dst,len)		bcopy((void*)(src),(void*)(dst),(size_t)(len))
395#define ClearMemory(start,len)	 	bzero((void*)(start),(size_t)(len))
396
397extern UInt32	TickCount();
398extern OSErr	MemError(void);
399extern Handle	NewHandleClear(Size byteCount);
400extern Handle	NewHandle(Size byteCount);
401extern void	DisposeHandle(Handle h);
402extern Size	GetHandleSize(Handle h);
403extern void	SetHandleSize(Handle h, Size newSize);
404extern OSErr	PtrAndHand(const void *ptr1, Handle hand2, long size);
405
406#else
407
408#define AllocateMemory(size)		NewPtr((Size)(size))
409#define	AllocateClearMemory(size)	NewPtrClear((Size)(size))
410#define ReallocateMemory(ptr,newSize)	SetPtrSize((Ptr)(ptr),(Size)(newSize))
411#define MemorySize(ptr)			GetPtrSize((Ptr)(ptr))
412#define DisposeMemory(ptr)		DisposePtr((Ptr)(ptr))
413#define CopyMemory(src,dst,len)		BlockMoveData((void *)(src),(void *)(dst),(Size)(len))
414void ClearMemory(void* start, long len);
415#endif
416
417
418#endif /* __SRUNTIME__ */
419
420
421