1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/*
6 * secport.h - portability interfaces for security libraries
7 */
8
9#ifndef _SECPORT_H_
10#define _SECPORT_H_
11
12#include "utilrename.h"
13#include "prlink.h"
14
15/*
16 * define XP_WIN, XP_BEOS, or XP_UNIX, in case they are not defined
17 * by anyone else
18 */
19#ifdef _WINDOWS
20#ifndef XP_WIN
21#define XP_WIN
22#endif
23#if defined(_WIN32) || defined(WIN32)
24#ifndef XP_WIN32
25#define XP_WIN32
26#endif
27#endif
28#endif
29
30#ifdef __BEOS__
31#ifndef XP_BEOS
32#define XP_BEOS
33#endif
34#endif
35
36#ifdef unix
37#ifndef XP_UNIX
38#define XP_UNIX
39#endif
40#endif
41
42#include <sys/types.h>
43
44#include <ctype.h>
45#include <string.h>
46#include <stddef.h>
47#include <stdlib.h>
48#include <stdint.h>
49#include "prtypes.h"
50#include "prlog.h" /* for PR_ASSERT */
51#include "plarena.h"
52#include "plstr.h"
53
54/*
55 * HACK for NSS 2.8 to allow Admin to compile without source changes.
56 */
57#ifndef SEC_BEGIN_PROTOS
58#include "seccomon.h"
59#endif
60
61/*
62 * The PORT_*Arena* function signatures mostly involve PLArenaPool* arguments.
63 * But this is misleading! It's not actually safe to use vanilla PLArenaPools
64 * with them. There are two "subclasses" of PLArenaPool that should be used
65 * instead.
66 *
67 * - PORTArenaPool (defined in secport.c): this "subclass" is always
68 * heap-allocated and uses a (heap-allocated) lock to protect all accesses.
69 * Use PORT_NewArena() and PORT_FreeArena() to create and destroy
70 * PORTArenaPools.
71 *
72 * - PORTCheapArenaPool (defined here): this "subclass" can be stack-allocated
73 * and does not use a lock to protect accesses. This makes it cheaper but
74 * less general. It is best used for arena pools that (a) are hot, (b) have
75 * lifetimes bounded within a single function, and (c) don't need locking.
76 * Use PORT_InitCheapArena() and PORT_DestroyCheapArena() to initialize and
77 * finalize PORTCheapArenaPools.
78 *
79 * All the other PORT_Arena* functions will operate safely with either
80 * subclass.
81 */
82typedef struct PORTCheapArenaPool_str {
83 PLArenaPool arena;
84 PRUint32 magic; /* This is used to distinguish the two subclasses. */
85} PORTCheapArenaPool;
86
87SEC_BEGIN_PROTOS
88
89extern void *PORT_Alloc(size_t len);
90extern void *PORT_Realloc(void *old, size_t len);
91extern void *PORT_ZAlloc(size_t len);
92extern void *PORT_ZAllocAligned(size_t bytes, size_t alignment, void **mem);
93extern void *PORT_ZAllocAlignedOffset(size_t bytes, size_t alignment,
94 size_t offset);
95extern void PORT_Free(void *ptr);
96extern void PORT_ZFree(void *ptr, size_t len);
97extern char *PORT_Strdup(const char *s);
98extern void PORT_SetError(int value);
99extern int PORT_GetError(void);
100
101/* These functions are for use with PORTArenaPools. */
102extern PLArenaPool *PORT_NewArena(unsigned long chunksize);
103extern void PORT_FreeArena(PLArenaPool *arena, PRBool zero);
104
105/* These functions are for use with PORTCheapArenaPools. */
106extern void PORT_InitCheapArena(PORTCheapArenaPool *arena,
107 unsigned long chunksize);
108extern void PORT_DestroyCheapArena(PORTCheapArenaPool *arena);
109
110/* These functions work with both kinds of arena pool. */
111extern void *PORT_ArenaAlloc(PLArenaPool *arena, size_t size);
112extern void *PORT_ArenaZAlloc(PLArenaPool *arena, size_t size);
113extern void *PORT_ArenaGrow(PLArenaPool *arena, void *ptr,
114 size_t oldsize, size_t newsize);
115extern void *PORT_ArenaMark(PLArenaPool *arena);
116extern void PORT_ArenaRelease(PLArenaPool *arena, void *mark);
117extern void PORT_ArenaZRelease(PLArenaPool *arena, void *mark);
118extern void PORT_ArenaUnmark(PLArenaPool *arena, void *mark);
119extern char *PORT_ArenaStrdup(PLArenaPool *arena, const char *str);
120
121SEC_END_PROTOS
122
123#define PORT_Assert PR_ASSERT
124/* This runs a function that should return SECSuccess.
125 * Intended for NSS internal use only.
126 * The return value is asserted in a debug build, otherwise it is ignored.
127 * This is no substitute for proper error handling. It is OK only if you
128 * have ensured that the function cannot fail by other means such as checking
129 * prerequisites. In that case this can be used as a safeguard against
130 * unexpected changes in a function.
131 */
132#ifdef DEBUG
133#define PORT_CheckSuccess(f) PR_ASSERT((f) == SECSuccess)
134#else
135#define PORT_CheckSuccess(f) (f)
136#endif
137#define PORT_ZNew(type) (type *)PORT_ZAlloc(sizeof(type))
138#define PORT_ZNewAligned(type, alignment, mem) \
139 (type *)PORT_ZAllocAlignedOffset(sizeof(type), alignment, offsetof(type, mem))
140#define PORT_New(type) (type *)PORT_Alloc(sizeof(type))
141#define PORT_ArenaNew(poolp, type) \
142 (type *)PORT_ArenaAlloc(poolp, sizeof(type))
143#define PORT_ArenaZNew(poolp, type) \
144 (type *)PORT_ArenaZAlloc(poolp, sizeof(type))
145#define PORT_NewArray(type, num) \
146 (type *)PORT_Alloc(sizeof(type) * (num))
147#define PORT_ZNewArray(type, num) \
148 (type *)PORT_ZAlloc(sizeof(type) * (num))
149#define PORT_ArenaNewArray(poolp, type, num) \
150 (type *)PORT_ArenaAlloc(poolp, sizeof(type) * (num))
151#define PORT_ArenaZNewArray(poolp, type, num) \
152 (type *)PORT_ArenaZAlloc(poolp, sizeof(type) * (num))
153
154/* Please, keep these defines sorted alphabetically. Thanks! */
155
156#define PORT_Atoi(buff) (int)strtol(buff, NULL, 10)
157
158/* Returns a UTF-8 encoded constant error string for err.
159 * Returns NULL if initialization of the error tables fails
160 * due to insufficient memory.
161 *
162 * This string must not be modified by the application.
163 */
164#define PORT_ErrorToString(err) PR_ErrorToString((err), PR_LANGUAGE_I_DEFAULT)
165
166#define PORT_ErrorToName PR_ErrorToName
167
168#define PORT_Memcmp memcmp
169#define PORT_Memcpy memcpy
170#ifndef SUNOS4
171#define PORT_Memmove memmove
172#else /*SUNOS4*/
173#define PORT_Memmove(s, ct, n) bcopy((ct), (s), (n))
174#endif /*SUNOS4*/
175#define PORT_Memset memset
176
177#define PORT_Strcasecmp PL_strcasecmp
178#define PORT_Strcat strcat
179#define PORT_Strchr strchr
180#define PORT_Strrchr strrchr
181#define PORT_Strcmp strcmp
182#define PORT_Strcpy strcpy
183#define PORT_Strlen(s) strlen(s)
184#define PORT_Strncasecmp PL_strncasecmp
185#define PORT_Strncat strncat
186#define PORT_Strncmp strncmp
187#define PORT_Strncpy strncpy
188#define PORT_Strpbrk strpbrk
189#define PORT_Strstr strstr
190#define PORT_Strtok strtok
191
192#define PORT_Tolower tolower
193
194typedef PRBool(PR_CALLBACK *PORTCharConversionWSwapFunc)(PRBool toUnicode,
195 unsigned char *inBuf, unsigned int inBufLen,
196 unsigned char *outBuf, unsigned int maxOutBufLen,
197 unsigned int *outBufLen, PRBool swapBytes);
198
199typedef PRBool(PR_CALLBACK *PORTCharConversionFunc)(PRBool toUnicode,
200 unsigned char *inBuf, unsigned int inBufLen,
201 unsigned char *outBuf, unsigned int maxOutBufLen,
202 unsigned int *outBufLen);
203
204SEC_BEGIN_PROTOS
205
206void PORT_SetUCS4_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
207void PORT_SetUCS2_ASCIIConversionFunction(PORTCharConversionWSwapFunc convFunc);
208PRBool PORT_UCS4_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
209 unsigned int inBufLen, unsigned char *outBuf,
210 unsigned int maxOutBufLen, unsigned int *outBufLen);
211PRBool PORT_UCS2_ASCIIConversion(PRBool toUnicode, unsigned char *inBuf,
212 unsigned int inBufLen, unsigned char *outBuf,
213 unsigned int maxOutBufLen, unsigned int *outBufLen,
214 PRBool swapBytes);
215void PORT_SetUCS2_UTF8ConversionFunction(PORTCharConversionFunc convFunc);
216PRBool PORT_UCS2_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
217 unsigned int inBufLen, unsigned char *outBuf,
218 unsigned int maxOutBufLen, unsigned int *outBufLen);
219
220/* One-way conversion from ISO-8859-1 to UTF-8 */
221PRBool PORT_ISO88591_UTF8Conversion(const unsigned char *inBuf,
222 unsigned int inBufLen, unsigned char *outBuf,
223 unsigned int maxOutBufLen, unsigned int *outBufLen);
224
225extern PRBool
226sec_port_ucs4_utf8_conversion_function(
227 PRBool toUnicode,
228 unsigned char *inBuf,
229 unsigned int inBufLen,
230 unsigned char *outBuf,
231 unsigned int maxOutBufLen,
232 unsigned int *outBufLen);
233
234extern PRBool
235sec_port_ucs2_utf8_conversion_function(
236 PRBool toUnicode,
237 unsigned char *inBuf,
238 unsigned int inBufLen,
239 unsigned char *outBuf,
240 unsigned int maxOutBufLen,
241 unsigned int *outBufLen);
242
243/* One-way conversion from ISO-8859-1 to UTF-8 */
244extern PRBool
245sec_port_iso88591_utf8_conversion_function(
246 const unsigned char *inBuf,
247 unsigned int inBufLen,
248 unsigned char *outBuf,
249 unsigned int maxOutBufLen,
250 unsigned int *outBufLen);
251
252extern int NSS_PutEnv(const char *envVarName, const char *envValue);
253
254extern int NSS_SecureMemcmp(const void *a, const void *b, size_t n);
255extern unsigned int NSS_SecureMemcmpZero(const void *mem, size_t n);
256
257/*
258 * Load a shared library called "newShLibName" in the same directory as
259 * a shared library that is already loaded, called existingShLibName.
260 * A pointer to a static function in that shared library,
261 * staticShLibFunc, is required.
262 *
263 * existingShLibName:
264 * The file name of the shared library that shall be used as the
265 * "reference library". The loader will attempt to load the requested
266 * library from the same directory as the reference library.
267 *
268 * staticShLibFunc:
269 * Pointer to a static function in the "reference library".
270 *
271 * newShLibName:
272 * The simple file name of the new shared library to be loaded.
273 *
274 * We use PR_GetLibraryFilePathname to get the pathname of the loaded
275 * shared lib that contains this function, and then do a
276 * PR_LoadLibraryWithFlags with an absolute pathname for the shared
277 * library to be loaded.
278 *
279 * On Windows, the "alternate search path" strategy is employed, if available.
280 * On Unix, if existingShLibName is a symbolic link, and no link exists for the
281 * new library, the original link will be resolved, and the new library loaded
282 * from the resolved location.
283 *
284 * If the new shared library is not found in the same location as the reference
285 * library, it will then be loaded from the normal system library path.
286 */
287PRLibrary *
288PORT_LoadLibraryFromOrigin(const char *existingShLibName,
289 PRFuncPtr staticShLibFunc,
290 const char *newShLibName);
291
292SEC_END_PROTOS
293
294#endif /* _SECPORT_H_ */
295