1 #ifndef CSF__IMPL_H
2 #define CSF__IMPL_H
3 
4 /******************************************************************/
5 /******************************************************************/
6 /**                                                               */
7 /**  RUU CROSS SYSTEM MAP FORMAT                                  */
8 /**                                                               */
9 /******************************************************************/
10 /* number of maps that can be open at one time
11  * FOPEN_MAX should be there in Ansi-C in <stdio.h>
12  * stdio.h is included in csf.h, check if csf.h is included first
13  */
14 #ifndef INCLUDED_CSF
15 # error csfimpl.h included before csf.h
16 #endif
17 
18 /******************************************************************/
19 /* CSFIMPL.H							  */
20 /******************************************************************/
21 
22 /******************************************************************/
23 /* Starting Addresses                                             */
24 /******************************************************************/
25 /* Constants of type CSF_FADDR */
26 
27 #define ADDR_MAIN_HEADER       ((CSF_FADDR)0)
28 #define ADDR_SECOND_HEADER    ((CSF_FADDR)64)
29 #define ADDR_DATA            ((CSF_FADDR)256)
30 
31 /* Padding of headers
32  */
33 #define RASTER_HEADER_FILL_SIZE ((size_t)124)
34 #define MAIN_HEADER_FILL_SIZE   ((size_t)14)
35 /* Used in mclose.c
36  */
37 #define MAX_HEADER_FILL_SIZE    (RASTER_HEADER_FILL_SIZE)
38 
39 /* values for MAIN_HEADER.byteOrder */
40 #define ORD_OK   0x00000001L
41 #define ORD_SWAB 0x01000000L
42 
43 /* INTERFACE with PCRaster software
44  */
45 #ifdef USE_IN_PCR
46 # include "stddefx.h"
47 # include "misc.h" /* malloc, free */
48 # define  CSF_MALLOC ChkMalloc
49 # define  CSF_FREE   Free
50 #else
51 # include <stdlib.h> /* malloc, free,abs */
52 # include <assert.h>
53 # define  CSF_MALLOC malloc
54 # define  CSF_FREE   free
55 # ifdef DEBUG
56 #  define  PRECOND(x)	assert(x)
57 #  define  POSTCOND(x)	assert(x)
58 # else
59 #  define  PRECOND(x)
60 #  define  POSTCOND(x)
61 # endif
62 #ifndef USE_IN_GDAL
63 # define  ABS(x)        abs(x)
64 #endif
65 # define  USED_UNINIT_ZERO 0
66 #endif
67 
68 /******************************************************************/
69 /* Definition of the main header                                  */
70 /******************************************************************/
71 
72 /* value for MAIN_HEADER.version */
73 #define CSF_VERSION_1 1
74 #define CSF_VERSION_2 2
75 
76 
77 #define IS_UNSIGNED(type) 	(!((type) & CSF_FLOAT_SIGN_MASK))
78 #define IS_SIGNED(type)   	((type) & CSF_SIGN_MASK)
79 #define IS_REAL(type)     	((type) & CSF_FLOAT_MASK)
80 
81 /******************************************************************/
82 /* Compiler conditions                                            */
83 /******************************************************************/
84 /*
85  sizeof(INT1)  == 1
86  sizeof(INT2)  == 2
87  sizeof(INT4)  == 4
88  sizeof(UINT1) == 1
89  sizeof(UINT2) == 2
90  sizeof(UINT4) == 4
91  sizeof(REAL4) == 4
92  sizeof(REAL8) == 8
93 */
94 
95 /******************************************************************/
96 /* Definition of an attribute control block	                  */
97 /******************************************************************/
98 
99 #define NR_ATTR_IN_BLOCK 	10
100 #define LAST_ATTR_IN_BLOCK 	(NR_ATTR_IN_BLOCK-1)
101 
102 
103 typedef struct ATTR_REC
104 {
105 		UINT2 attrId;	/* attribute identifier */
106 		CSF_FADDR32 attrOffset;   /* file-offset of attribute */
107 		UINT4 attrSize;	/* size of attribute in bytes */
108 } ATTR_REC;
109 
110 typedef struct ATTR_CNTRL_BLOCK
111 {
112 	ATTR_REC attrs[NR_ATTR_IN_BLOCK];
113 	CSF_FADDR32    next; /* file-offset of next block */
114 } ATTR_CNTRL_BLOCK;
115 
116 #define SIZE_OF_ATTR_CNTRL_BLOCK  \
117  ((NR_ATTR_IN_BLOCK * (sizeof(UINT2) + sizeof(CSF_FADDR32) + sizeof(UINT4))) \
118   + sizeof(CSF_FADDR32) )
119 
120 /* Note that two empty holes in the attribute area are never merged */
121 
122 
123 #define ATTR_NOT_USED 0x0
124 	/* value of attrId field if an attribute is deleted */
125 	/* attrOffset and attrSize must remain valid; so a new
126 	 * attribute can be inserted if its size is equal or
127 	 * smaller then attrSize
128 	 */
129 
130 #define END_OF_ATTRS 0xFFFF
131 	/* value of attrId field if there are no more attributes */
132 	/* INDEED: A BUG we wanted to use the highest value (0xFFFFFFFF)
133 	 *  but we made a mistake. Don't change, 1023  is just as
134 	 *  good as (2^16)-1
135 	 */
136 
137 /* does y decrements from
138  * top to bottom in this projection type?
139  * this will also hold for the old types
140  * since only PT_XY was increments from
141  * top to bottom, like PT_YINCT2B
142  * PT_XY and PT_YINCT2B are the only one that are
143  * 0, the others all have a nonzero value
144  */
145 #define PROJ_DEC_T2B(x)	(x != 0)
146 
147 #define MM_KEEPTRACK		0
148 #define MM_DONTKEEPTRACK	1
149 #define MM_WRONGVALUE		2
150 
151 #define M_ERROR(errorCode) Merrno = errorCode
152 #define PROG_ERROR(errorCode) Merrno = errorCode
153 
154 #define S_READ		"rb"   /* Open for read only */
155 #define S_WRITE	 	"r+b"  /* Open for write only  I don't know an */
156 				/* appropriate mode "r+b" seems most app. */
157 #define S_READ_WRITE  	"r+b"  /* Open for reading and writing */
158 #define S_CREATE  	"w+b"  /* Create new file for reading and writing */
159 
160 #define WRITE_ENABLE(m)	   (m->fileAccessMode & M_WRITE)
161 #define READ_ENABLE(m)	   (m->fileAccessMode & M_READ)
162 #define IS_BAD_ACCESS_MODE(mode) \
163 		 (mode >> 2) 	/* use only 2 bits for modes */
164 
165 #define READ_AS	 0 /* note that READ_AS is also used on procedures
166 			that implies write access, under the condition of
167 			write access both type bytes are equal, and the
168 			READ_AS byte is 0-alligned in the record, so this
169 			byte is quicker accessible */
170 	/* we will call READ_AS the ONLY_AS if write access is implied */
171 #define ONLY_AS		0
172 #define STORED_AS	1
173 
174 /* Typed zero values to keep lint happy
175  * mainly used in conversion macro's
176  */
177 #define ZERO_UINT1	((UINT1)0)
178 #define ZERO_UINT2	((UINT2)0)
179 #define ZERO_UINT4	((UINT4)0)
180 #define ZERO_INT1	((INT1) 0)
181 #define ZERO_INT2	((INT2) 0)
182 #define ZERO_INT4	((INT4) 0)
183 #define ZERO_REAL4	((REAL4)0)
184 #define	ZERO_REAL8	((REAL8)0)
185 
186 /* LIBRARY_INTERNAL's: */
187 /* OLD STUFF
188 void TransForm(const MAP *map, UINT4 nrCells, void *buf);
189  */
190 
191 void  CsfFinishMapInit(MAP *m);
192 void  CsfDummyConversion(size_t n, void *buf);
193 int   CsfIsValidMap(const MAP *m);
194 void  CsfUnloadMap(MAP *m);
195 void  CsfRegisterMap(MAP *m);
196 int   CsfIsBootedCsfKernel(void);
197 void  CsfBootCsfKernel(void);
198 void  CsfSetVarTypeMV( CSF_VAR_TYPE *var, CSF_CR cellRepr);
199 void  CsfGetVarType(void *dest, const CSF_VAR_TYPE *src, CSF_CR cellRepr);
200 void  CsfReadAttrBlock( MAP *m, CSF_FADDR32 pos, ATTR_CNTRL_BLOCK *b);
201 int   CsfWriteAttrBlock(MAP *m, CSF_FADDR32 pos, ATTR_CNTRL_BLOCK *b);
202 int   CsfGetAttrIndex(CSF_ATTR_ID id, const ATTR_CNTRL_BLOCK *b);
203 CSF_FADDR32 CsfGetAttrBlock(MAP *m, CSF_ATTR_ID id, ATTR_CNTRL_BLOCK *b);
204 CSF_FADDR32 CsfGetAttrPosSize(MAP *m, CSF_ATTR_ID id, size_t *size);
205 size_t CsfWriteSwapped(void *buf, size_t size, size_t n, FILE  *f);
206 size_t CsfReadSwapped(void *buf, size_t size, size_t n, FILE  *f);
207 size_t CsfWritePlain(void *buf, size_t size, size_t n, FILE  *f);
208 size_t CsfReadPlain(void *buf, size_t size, size_t n, FILE  *f);
209 void   CsfSwap(void *buf, size_t size, size_t n);
210 char *CsfStringPad(char *s, size_t reqSize);
211 
212 CSF_FADDR32 CsfSeekAttrSpace(MAP *m, CSF_ATTR_ID id, size_t size);
213 CSF_ATTR_ID CsfPutAttribute( MAP *m, CSF_ATTR_ID id, size_t size, size_t nitems, void *attr);
214 CSF_ATTR_ID CsfGetAttribute(MAP *m, CSF_ATTR_ID id, size_t elSize, size_t *nmemb, void *attr);
215 size_t      CsfAttributeSize(MAP *m, CSF_ATTR_ID id);
216 CSF_ATTR_ID CsfUpdateAttribute(MAP *m, CSF_ATTR_ID id, size_t itemSize, size_t nitems, void *attr);
217 
218 int CsfValidSize(size_t size);
219 
220 #define CHECKHANDLE_GOTO(m, label)	\
221 			if (! CsfIsValidMap(m))	\
222 		   	{			\
223 				M_ERROR(ILLHANDLE);	\
224 				goto label;		\
225 			}
226 #define CHECKHANDLE_RETURN(m, value)	\
227 			if (! CsfIsValidMap(m))	\
228 		   	{			\
229 				M_ERROR(ILLHANDLE);	\
230 				return value;		\
231 			}
232 #define CHECKHANDLE(m)	\
233 			if (! CsfIsValidMap(m))	\
234 		   	{			\
235 				M_ERROR(ILLHANDLE);	\
236 			}
237 
238 
239 int                csf_fseek           (FILE* file,
240                                         CSF_FADDR offset,
241                                         int origin);
242 
243 CSF_FADDR          csf_ftell           (FILE* file);
244 
245 #endif /* CSF__IMPL_H */
246