1/*
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation.  Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above.  If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL.  If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
31 * GPL.
32 */
33
34/*
35 * secport.h - portability interfaces for security libraries
36 *
37 * This file abstracts out libc functionality that libsec depends on
38 *
39 * NOTE - These are not public interfaces
40 *
41 * $Id: secport.h,v 1.3 2004/10/22 19:11:36 dmitch Exp $
42 */
43
44#ifndef _SECPORT_H_
45#define _SECPORT_H_
46
47/*
48 * define XP_MAC, XP_WIN, XP_BEOS, or XP_UNIX, in case they are not defined
49 * by anyone else
50 */
51#ifdef macintosh
52# ifndef XP_MAC
53# define XP_MAC 1
54# endif
55#endif
56
57#ifdef _WINDOWS
58# ifndef XP_WIN
59# define XP_WIN
60# endif
61#if defined(_WIN32) || defined(WIN32)
62# ifndef XP_WIN32
63# define XP_WIN32
64# endif
65#else
66# ifndef XP_WIN16
67# define XP_WIN16
68# endif
69#endif
70#endif
71
72#ifdef __BEOS__
73# ifndef XP_BEOS
74# define XP_BEOS
75# endif
76#endif
77
78#ifdef unix
79# ifndef XP_UNIX
80# define XP_UNIX
81# endif
82#endif
83
84#if defined(__WATCOMC__) || defined(__WATCOM_CPLUSPLUS__)
85#include "watcomfx.h"
86#endif
87
88#if defined(_WIN32_WCE)
89#include <windef.h>
90#include <types.h>
91#elif defined( XP_MAC )
92#include <sys/types.h>
93#include <time.h> /* for time_t below */
94#else
95#include <sys/types.h>
96#endif
97
98#ifdef notdef
99#ifdef XP_MAC
100#include "NSString.h"
101#endif
102#endif
103
104#include <ctype.h>
105#include <string.h>
106#if defined(_WIN32_WCE)
107#include <stdlib.h>	/* WinCE puts some stddef symbols here. */
108#else
109#include <stddef.h>
110#endif
111#include <stdlib.h>
112#include "prtypes.h"
113#include "prlog.h"	/* for PR_ASSERT */
114#include "plarenas.h"
115#include "plstr.h"
116
117/*
118 * HACK for NSS 2.8 to allow Admin to compile without source changes.
119 */
120#ifndef SEC_BEGIN_PROTOS
121#include "seccomon.h"
122#endif
123
124SEC_BEGIN_PROTOS
125
126extern void *PORT_Alloc(size_t len);
127extern void *PORT_Realloc(void *old, size_t len);
128extern void *PORT_AllocBlock(size_t len);
129extern void *PORT_ReallocBlock(void *old, size_t len);
130extern void PORT_FreeBlock(void *ptr);
131extern void *PORT_ZAlloc(size_t len);
132extern void PORT_Free(void *ptr);
133extern void PORT_ZFree(void *ptr, size_t len);
134extern time_t PORT_Time(void);
135extern void PORT_SetError(int value);
136extern int PORT_GetError(void);
137
138extern PLArenaPool *PORT_NewArena(unsigned long chunksize);
139extern void *PORT_ArenaAlloc(PLArenaPool *arena, size_t size);
140extern void *PORT_ArenaZAlloc(PLArenaPool *arena, size_t size);
141extern void PORT_FreeArena(PLArenaPool *arena, PRBool zero);
142extern void *PORT_ArenaGrow(PLArenaPool *arena, void *ptr,
143			    size_t oldsize, size_t newsize);
144extern void *PORT_ArenaMark(PLArenaPool *arena);
145extern void PORT_ArenaRelease(PLArenaPool *arena, void *mark);
146extern void PORT_ArenaUnmark(PLArenaPool *arena, void *mark);
147
148extern char *PORT_ArenaStrdup(PLArenaPool *arena, const char *str);
149
150#ifdef __cplusplus
151}
152#endif
153
154#define PORT_Assert PR_ASSERT
155#define PORT_ZNew(type) (type*)PORT_ZAlloc(sizeof(type))
156#define PORT_New(type) (type*)PORT_Alloc(sizeof(type))
157#define PORT_ArenaNew(poolp, type)	\
158		(type*) PORT_ArenaAlloc(poolp, sizeof(type))
159#define PORT_ArenaZNew(poolp, type)	\
160		(type*) PORT_ArenaZAlloc(poolp, sizeof(type))
161#define PORT_NewArray(type, num)	\
162		(type*) PORT_Alloc (sizeof(type)*(num))
163#define PORT_ZNewArray(type, num)	\
164		(type*) PORT_ZAlloc (sizeof(type)*(num))
165#define PORT_ArenaNewArray(poolp, type, num)	\
166		(type*) PORT_ArenaAlloc (poolp, sizeof(type)*(num))
167#define PORT_ArenaZNewArray(poolp, type, num)	\
168		(type*) PORT_ArenaZAlloc (poolp, sizeof(type)*(num))
169
170/* Please, keep these defines sorted alphbetically.  Thanks! */
171
172#ifdef XP_STRING_FUNCS
173
174#define PORT_Atoi 	XP_ATOI
175
176#define PORT_Memcmp 	XP_MEMCMP
177#define PORT_Memcpy 	XP_MEMCPY
178#define PORT_Memmove 	XP_MEMMOVE
179#define PORT_Memset 	XP_MEMSET
180
181#define PORT_Strcasecmp XP_STRCASECMP
182#define PORT_Strcat 	XP_STRCAT
183#define PORT_Strchr 	XP_STRCHR
184#define PORT_Strrchr	XP_STRRCHR
185#define PORT_Strcmp 	XP_STRCMP
186#define PORT_Strcpy 	XP_STRCPY
187#define PORT_Strdup 	XP_STRDUP
188#define PORT_Strlen(s) 	XP_STRLEN(s)
189#define PORT_Strncasecmp XP_STRNCASECMP
190#define PORT_Strncat 	strncat
191#define PORT_Strncmp 	XP_STRNCMP
192#define PORT_Strncpy 	strncpy
193#define PORT_Strstr 	XP_STRSTR
194#define PORT_Strtok 	XP_STRTOK_R
195
196#define PORT_Tolower 	XP_TO_LOWER
197
198#else /* XP_STRING_FUNCS */
199
200#define PORT_Atoi 	atoi
201
202#define PORT_Memcmp 	memcmp
203#define PORT_Memcpy 	memcpy
204#ifndef SUNOS4
205#define PORT_Memmove 	memmove
206#else /*SUNOS4*/
207#define PORT_Memmove(s,ct,n)    bcopy ((ct), (s), (n))
208#endif/*SUNOS4*/
209#define PORT_Memset 	memset
210
211#define PORT_Strcasecmp PL_strcasecmp
212#define PORT_Strcat 	strcat
213#define PORT_Strchr 	strchr
214#define PORT_Strrchr    strrchr
215#define PORT_Strcmp 	strcmp
216#define PORT_Strcpy 	strcpy
217extern char *PORT_Strdup(const char *s);
218#define PORT_Strlen(s) 	strlen(s)
219#define PORT_Strncasecmp PL_strncasecmp
220#define PORT_Strncat 	strncat
221#define PORT_Strncmp 	strncmp
222#define PORT_Strncpy 	strncpy
223#define PORT_Strstr 	strstr
224#define PORT_Strtok 	strtok
225
226#define PORT_Tolower 	tolower
227
228#endif /* XP_STRING_FUNCS */
229
230#ifndef __APPLE__
231
232typedef PRBool (PR_CALLBACK * PORTCharConversionWSwapFunc) (PRBool toUnicode,
233			unsigned char *inBuf, unsigned int inBufLen,
234			unsigned char *outBuf, unsigned int maxOutBufLen,
235			unsigned int *outBufLen, PRBool swapBytes);
236
237typedef PRBool (PR_CALLBACK * PORTCharConversionFunc) (PRBool toUnicode,
238			unsigned char *inBuf, unsigned int inBufLen,
239			unsigned char *outBuf, unsigned int maxOutBufLen,
240			unsigned int *outBufLen);
241
242#ifdef __cplusplus
243extern "C" {
244#endif
245
246void PORT_SetUCS4_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
247void PORT_SetUCS2_ASCIIConversionFunction(PORTCharConversionWSwapFunc convFunc);
248PRBool PORT_UCS4_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
249			unsigned int inBufLen, unsigned char *outBuf,
250			unsigned int maxOutBufLen, unsigned int *outBufLen);
251PRBool PORT_UCS2_ASCIIConversion(PRBool toUnicode, unsigned char *inBuf,
252			unsigned int inBufLen, unsigned char *outBuf,
253			unsigned int maxOutBufLen, unsigned int *outBufLen,
254			PRBool swapBytes);
255void PORT_SetUCS2_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
256PRBool PORT_UCS2_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
257			unsigned int inBufLen, unsigned char *outBuf,
258			unsigned int maxOutBufLen, unsigned int *outBufLen);
259
260PR_EXTERN(PRBool)
261sec_port_ucs4_utf8_conversion_function
262(
263  PRBool toUnicode,
264  unsigned char *inBuf,
265  unsigned int inBufLen,
266  unsigned char *outBuf,
267  unsigned int maxOutBufLen,
268  unsigned int *outBufLen
269);
270
271PR_EXTERN(PRBool)
272sec_port_ucs2_utf8_conversion_function
273(
274  PRBool toUnicode,
275  unsigned char *inBuf,
276  unsigned int inBufLen,
277  unsigned char *outBuf,
278  unsigned int maxOutBufLen,
279  unsigned int *outBufLen
280);
281
282extern int NSS_PutEnv(const char * envVarName, const char * envValue);
283
284#ifdef __cplusplus
285}
286#endif
287
288#endif  /* __APPLE__ */
289
290#endif /* _SECPORT_H_ */
291