File: SRuntime.h

package info (click to toggle)
hfsprogs 332.25-11
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 6,440 kB
  • ctags: 7,805
  • sloc: ansic: 58,120; makefile: 25
file content (428 lines) | stat: -rw-r--r-- 12,060 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (c) 1999, 2005 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
 * Reserved.  This file contains Original Code and/or Modifications of
 * Original Code as defined in and that are subject to the Apple Public
 * Source License Version 1.0 (the 'License').  You may not use this file
 * except in compliance with the License.  Please obtain a copy of the
 * License at http://www.apple.com/publicsource and read it before using
 * this file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
 * License for the specific language governing rights and limitations
 * under the License."
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
/* SRuntime.h */

#ifndef __SRUNTIME__
#define __SRUNTIME__

#if BSD
#if LINUX
#include "missing.h"
#else
#include <sys/types.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include <hfs/hfs_format.h>

#else

#include <MacTypes.h>
#include <MacMemory.h>
#include <HFSVolumes.h>
#include <Errors.h>

#endif

#if BSD
/* Classic Mac OS Types */
typedef int8_t		SInt8;
typedef int16_t		SInt16;
typedef int32_t		SInt32;
typedef int64_t		SInt64;

typedef u_int8_t	UInt8;
typedef u_int16_t	UInt16;
typedef u_int32_t	UInt32;
typedef u_int64_t	UInt64;

typedef void *		LogicalAddress;
typedef char *		Ptr;
typedef Ptr *		Handle;
typedef u_int8_t	Byte;
typedef long 		Size;
typedef unsigned char	Boolean;
typedef unsigned long 	ItemCount;
typedef unsigned long 	ByteCount;
typedef unsigned long	OptionBits;

typedef short 		OSErr;
typedef long 		OSStatus;

typedef u_int32_t	OSType;
typedef u_int32_t	ResType;

typedef u_int16_t	UniChar;
typedef u_int32_t	UniCharCount;
typedef UniChar *	UniCharArrayPtr;
typedef const UniChar *	ConstUniCharArrayPtr;
typedef u_int32_t	TextEncoding;

typedef unsigned char *	StringPtr;
typedef unsigned char	Str27[28];
typedef unsigned char	Str31[32];
typedef unsigned char	Str63[64];
typedef unsigned char	Str255[256];

typedef const unsigned char *	ConstStr31Param;
typedef const unsigned char *	ConstStr63Param;
typedef const unsigned char *	ConstStr255Param;

typedef u_int32_t	HFSCatalogNodeID;

#if !LINUX
enum {
	false		= 0,
	true		= 1
};
#endif

/* OS error codes */
enum {
	noErr		=     0,
	dskFulErr	=   -34,
	nsvErr		=   -35,
	ioErr		=   -36,
	eofErr		=   -39,
	fnfErr		=   -43,
	fBsyErr		=   -47,
	paramErr	=   -50,
	noMacDskErr	=   -57,
	badMDBErr	=   -60,
	memFullErr	=  -108,
	notBTree	=  -410,
	fileBoundsErr	= -1309,
};

/* Finder Flags */
enum {
    kIsOnDesk		= 0x0001,
    kColor		= 0x000E,
    kIsShared		= 0x0040,
    kHasBeenInited	= 0x0100,
    kHasCustomIcon	= 0x0400,
    kIsStationery	= 0x0800,
    kNameLocked		= 0x1000,
    kHasBundle		= 0x2000,
    kIsInvisible	= 0x4000,
    kIsAlias		= 0x8000
};

#define EXTERN_API(_type)	extern _type
#define EXTERN_API_C(_type)	extern _type

#define nil NULL

EXTERN_API( void )
DebugStr(ConstStr255Param debuggerMsg);

typedef void * QElemPtr;
typedef void * DrvQElPtr;


#endif



/* vcbFlags bits */
enum {
  kVCBFlagsIdleFlushBit           = 3,                                                    /* Set if volume should be flushed at idle time */
        kVCBFlagsIdleFlushMask          = 0x0008,
        kVCBFlagsHFSPlusAPIsBit         = 4,                                                    /* Set if volume implements HFS Plus APIs itself (not via emu\
												   lation) */
        kVCBFlagsHFSPlusAPIsMask        = 0x0010,
        kVCBFlagsHardwareGoneBit        = 5,                                                    /* Set if disk driver returned a hardwareGoneErr to Read or W\
												   rite */
        kVCBFlagsHardwareGoneMask       = 0x0020,
        kVCBFlagsVolumeDirtyBit         = 15,                                                   /* Set if volume information has changed since the last Flush\
												   Vol */
        kVCBFlagsVolumeDirtyMask        = 0x8000
};


/* Disk Cache constants */
/*
 * UTGetBlock options
 */
enum {
	gbDefault					= 0,							/* default value - read if not found */
																/*	bits and masks */
	gbReadBit					= 0,							/* read block from disk (forced read) */
	gbReadMask					= 0x0001,
	gbExistBit					= 1,							/* get existing cache block */
	gbExistMask					= 0x0002,
	gbNoReadBit					= 2,							/* don't read block from disk if not found in cache */
	gbNoReadMask				= 0x0004,
	gbReleaseBit				= 3,							/* release block immediately after GetBlock */
	gbReleaseMask				= 0x0008
};


/*
 * UTReleaseBlock options
 */
enum {
	rbDefault					= 0,							/* default value - just mark the buffer not in-use */
																/*	bits and masks */
	rbWriteBit					= 0,							/* force write buffer to disk */
	rbWriteMask					= 0x0001,
	rbTrashBit					= 1,							/* trash buffer contents after release */
	rbTrashMask					= 0x0002,
	rbDirtyBit					= 2,							/* mark buffer dirty */
	rbDirtyMask					= 0x0004,
	rbFreeBit					= 3,							/* free the buffer (save in the hash) */
	rbFreeMask					= 0x000A						/* rbFreeMask (rbFreeBit + rbTrashBit) works as rbTrash on < System 7.0 RamCache; on >= System 7.0, rbfreeMask overrides rbTrash */
};


/*
 * UTFlushCache options
 */
enum {
	fcDefault					= 0,							/* default value - pass this fcOption to just flush any dirty buffers */
																/*	bits and masks */
	fcTrashBit					= 0,							/* (don't pass this as fcOption, use only for testing bit) */
	fcTrashMask					= 0x0001,						/* pass this fcOption value to flush and trash cache blocks */
	fcFreeBit					= 1,							/* (don't pass this as fcOption, use only for testing bit) */
	fcFreeMask					= 0x0003						/* pass this fcOption to flush and free cache blocks (Note: both fcTrash and fcFree bits are set) */
};


/*
 * UTCacheReadIP and UTCacheWriteIP cacheOption bits and masks are the ioPosMode
 * bits and masks in Files.x
 */

/*
 * Cache routine internal error codes
 */
enum {
	chNoBuf						= 1,							/* no free cache buffers (all in use) */
	chInUse						= 2,							/* requested block in use */
	chnotfound					= 3,							/* requested block not found */
	chNotInUse					= 4								/* block being released was not in use */
};


/*
 * FCBRec.fcbFlags bits
 */
enum {
	fcbWriteBit					= 0,							/* Data can be written to this file */
	fcbWriteMask				= 0x01,
	fcbResourceBit				= 1,							/* This file is a resource fork */
	fcbResourceMask				= 0x02,
	fcbWriteLockedBit			= 2,							/* File has a locked byte range */
	fcbWriteLockedMask			= 0x04,
	fcbSharedWriteBit			= 4,							/* File is open for shared write access */
	fcbSharedWriteMask			= 0x10,
	fcbFileLockedBit			= 5,							/* File is locked (write-protected) */
	fcbFileLockedMask			= 0x20,
	fcbOwnClumpBit				= 6,							/* File has clump size specified in FCB */
	fcbOwnClumpMask				= 0x40,
	fcbModifiedBit				= 7,							/* File has changed since it was last flushed */
	fcbModifiedMask				= 0x80
};

enum {
	fcbLargeFileBit				= 3,							/* File may grow beyond 2GB; cache uses file blocks, not bytes */
	fcbLargeFileMask			= 0x08
};

#define kSectorShift  9   /* log2(kSectorSize); used for bit shifts */

/*
	Fork Level Access Method Block get options
*/
enum {
	kGetBlock	= 0x00000000,
	kForceReadBlock	= 0x00000002,
	kGetEmptyBlock	= 0x00000008,
	kSkipEndianSwap	= 0x00000010
};
typedef OptionBits  GetBlockOptions;

/*
	Fork Level Access Method Block release options
*/
enum {
	kReleaseBlock	 = 0x00000000,
	kForceWriteBlock = 0x00000001,
	kMarkBlockDirty	 = 0x00000002,
	kTrashBlock	 	 = 0x00000004
};
typedef OptionBits  ReleaseBlockOptions;


struct BlockDescriptor{
	void		*buffer;
	void		*blockHeader;
	UInt64		 blockNum;
	UInt32		 blockSize;
	Boolean		 blockReadFromDisk;
	Boolean		 fragmented;
};
typedef struct BlockDescriptor BlockDescriptor;
typedef BlockDescriptor *BlockDescPtr;



struct SFCB;

struct SVCB {
	UInt16		vcbSignature;
	UInt16		vcbVersion;
	UInt32		vcbAttributes;
	UInt32		vcbLastMountedVersion;
	UInt32		vcbReserved1;
	UInt32		vcbCreateDate;
	UInt32		vcbModifyDate;
	UInt32		vcbBackupDate;
	UInt32		vcbCheckedDate;
	UInt32		vcbFileCount;
	UInt32		vcbFolderCount;
	UInt32		vcbBlockSize;
	UInt32		vcbTotalBlocks;
	UInt32		vcbFreeBlocks;
	UInt32		vcbNextAllocation;
	UInt32 		vcbRsrcClumpSize;
	UInt32		vcbDataClumpSize;
	UInt32 		vcbNextCatalogID;
	UInt32		vcbWriteCount;
	UInt64		vcbEncodingsBitmap;
	UInt8		vcbFinderInfo[32];

	/* MDB-specific fields... */
	SInt16		vcbNmFls;	/* number of files in root folder */
	SInt16		vcbNmRtDirs;	/* number of directories in root folder */
	UInt16		vcbVBMSt;	/* first sector of HFS volume bitmap */
	UInt16		vcbAlBlSt;	/* first allocation block in HFS volume */
	UInt16		vcbVSeqNum;	/* volume backup sequence number */
	UInt16		vcbReserved2;
	Str27		vcbVN;		/* HFS volume name */

	/* runtime fields... */
	struct SFCB * 	vcbAllocationFile;
	struct SFCB * 	vcbExtentsFile;
	struct SFCB * 	vcbCatalogFile;
	struct SFCB * 	vcbAttributesFile;
	struct SFCB * 	vcbStartupFile;

	UInt32		vcbEmbeddedOffset;	/* Sector where HFS+ starts */
	UInt16		vcbFlags;
	SInt16		vcbDriveNumber;
	SInt16		vcbDriverReadRef;
	SInt16		vcbDriverWriteRef;

	void *		vcbBlockCache;

	struct SGlob *	vcbGPtr;

	/* deprecated fields... */
	SInt16		vcbVRefNum;
};
typedef struct SVCB SVCB;


struct SFCB {
	UInt32			fcbFileID;
	UInt32			fcbFlags;
	struct SVCB *		fcbVolume;
	void *			fcbBtree;
	HFSExtentRecord		fcbExtents16;
	HFSPlusExtentRecord	fcbExtents32;
	UInt32			fcbCatalogHint;
	UInt32			fcbClumpSize;
	UInt64 			fcbLogicalSize;
	UInt64			fcbPhysicalSize;
	UInt32			fcbBlockSize;
};
typedef struct SFCB SFCB;


extern OSErr GetDeviceSize(int driveRefNum, UInt64 *numBlocks, UInt32 *blockSize);

extern OSErr DeviceRead(int device, int drive, void* buffer, SInt64 offset, UInt32 reqBytes, UInt32 *actBytes);

extern OSErr DeviceWrite(int device, int drive, void* buffer, SInt64 offset, UInt32 reqBytes, UInt32 *actBytes);


/*
 *  Block Cache Interface
 */
extern void      InitBlockCache(SVCB *volume);

extern OSStatus  GetVolumeBlock (SVCB *volume, UInt64 blockNum, GetBlockOptions options,
				BlockDescriptor *block);

extern OSStatus  ReleaseVolumeBlock (SVCB *volume, BlockDescriptor *block,
				ReleaseBlockOptions options);

extern OSStatus  GetFileBlock (SFCB *file, UInt32 blockNum, GetBlockOptions options,
				BlockDescriptor *block);

extern OSStatus  ReleaseFileBlock (SFCB *file, BlockDescriptor *block,
				ReleaseBlockOptions options);

extern OSStatus  SetFileBlockSize (SFCB *file, ByteCount blockSize);



#if BSD

#define AllocateMemory(size)		malloc((size_t)(size))
#define	AllocateClearMemory(size)	calloc(1,(size_t)(size))
#define ReallocateMemory(ptr,newSize)	SetPtrSize((void*)(ptr),(size_t)(newSize))
#define MemorySize(ptr)			malloc_size((void*)(ptr))
#define DisposeMemory(ptr)		free((void *)(ptr))
#define CopyMemory(src,dst,len)		bcopy((void*)(src),(void*)(dst),(size_t)(len))
#define ClearMemory(start,len)	 	bzero((void*)(start),(size_t)(len))

extern UInt32	TickCount();
extern OSErr	MemError(void);
extern Handle	NewHandleClear(Size byteCount);
extern Handle	NewHandle(Size byteCount);
extern void	DisposeHandle(Handle h);
extern Size	GetHandleSize(Handle h);
extern void	SetHandleSize(Handle h, Size newSize);
extern OSErr	PtrAndHand(const void *ptr1, Handle hand2, long size);

#else

#define AllocateMemory(size)		NewPtr((Size)(size))
#define	AllocateClearMemory(size)	NewPtrClear((Size)(size))
#define ReallocateMemory(ptr,newSize)	SetPtrSize((Ptr)(ptr),(Size)(newSize))
#define MemorySize(ptr)			GetPtrSize((Ptr)(ptr))
#define DisposeMemory(ptr)		DisposePtr((Ptr)(ptr))
#define CopyMemory(src,dst,len)		BlockMoveData((void *)(src),(void *)(dst),(Size)(len))
void ClearMemory(void* start, long len);
#endif


#endif /* __SRUNTIME__ */