~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/include/impl/.svn/text-base/security.h.svn-base

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 
2
 *
 
3
 * portable security-enhanced CRT functions.
 
4
 *
 
5
 * Copyright (c) 2010 Miru Limited.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#if !defined (__PGM_IMPL_FRAMEWORK_H_INSIDE__) && !defined (PGM_COMPILATION)
 
23
#       error "Only <framework.h> can be included directly."
 
24
#endif
 
25
 
 
26
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 
27
#       pragma once
 
28
#endif
 
29
#ifndef __PGM_IMPL_SECURITY_H__
 
30
#define __PGM_IMPL_SECURITY_H__
 
31
 
 
32
#include <errno.h>
 
33
#include <stdio.h>
 
34
#include <stdarg.h>
 
35
#include <sys/types.h>
 
36
#include <sys/timeb.h>
 
37
#include <impl/i18n.h>
 
38
#include <impl/errno.h>
 
39
#include <impl/string.h>
 
40
 
 
41
PGM_BEGIN_DECLS
 
42
 
 
43
#ifdef CONFIG_HAVE_FTIME
 
44
static inline
 
45
errno_t
 
46
#       ifndef _WIN32
 
47
pgm_ftime_s (struct timeb *timeptr)
 
48
#       elif !defined( _MSC_VER )
 
49
pgm_ftime_s (struct _timeb *timeptr)
 
50
#       else
 
51
pgm_ftime_s (struct __timeb64 *timeptr)
 
52
#       endif
 
53
{
 
54
#       ifndef _WIN32
 
55
        return ftime (timeptr);
 
56
#       elif !defined( _MSC_VER )
 
57
        _ftime (timeptr);
 
58
        return 0;
 
59
#       elif  defined( CONFIG_HAVE_SECURITY_ENHANCED_CRT )
 
60
        return _ftime64_s (timeptr);
 
61
#       else
 
62
        _ftime64 (timeptr);
 
63
        return 0;
 
64
#       endif
 
65
}
 
66
#endif /* CONFIG_HAVE_FTIME */
 
67
 
 
68
#ifndef _TRUNCATE
 
69
#       define _TRUNCATE        (size_t)-1
 
70
#endif
 
71
 
 
72
static inline
 
73
errno_t
 
74
pgm_strncpy_s (char *dest, size_t size, const char *src, size_t count)
 
75
{
 
76
#ifndef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
77
        if (_TRUNCATE == count) {
 
78
                strncpy (dest, src, size);
 
79
                if (size > 0)
 
80
                        dest[size - 1] = 0;
 
81
                return 0;
 
82
        }
 
83
        strncpy (dest, src, count + 1);
 
84
        dest[count] = 0;
 
85
        return 0;
 
86
#else
 
87
        return strncpy_s (dest, size, src, count);
 
88
#endif
 
89
}
 
90
 
 
91
static inline int pgm_vsnprintf_s (char*, size_t, size_t, const char*, va_list) PGM_GNUC_PRINTF(4, 0);
 
92
 
 
93
static inline
 
94
int
 
95
pgm_vsnprintf_s (char *str, size_t size, size_t count, const char *format, va_list ap)
 
96
{
 
97
#ifndef _WIN32
 
98
        if (_TRUNCATE == count) {
 
99
                const int retval = vsnprintf (str, size, format, ap);
 
100
                if (size > 0)
 
101
                        str[size - 1] = 0;
 
102
                return retval;
 
103
        }
 
104
        const int retval = vsnprintf (str, count + 1, format, ap);
 
105
        str[count] = 0;
 
106
        return retval;
 
107
#elif !defined( CONFIG_HAVE_SECURITY_ENHANCED_CRT )
 
108
        if (_TRUNCATE == count) {
 
109
                const int retval = _vsnprintf (str, size, format, ap);
 
110
                if (size > 0)
 
111
                        str[size - 1] = 0;
 
112
                return retval;
 
113
        }
 
114
        const int retval = _vsnprintf (str, count + 1, format, ap);
 
115
        str[count] = 0;
 
116
        return retval;
 
117
#else
 
118
        return _vsnprintf_s (str, size, count, format, ap);
 
119
#endif
 
120
}
 
121
 
 
122
#ifndef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
123
static inline int pgm_snprintf_s (char*, size_t, size_t, const char*, ...) PGM_GNUC_PRINTF(4, 5);
 
124
static inline int pgm_sscanf_s (const char*, const char*, ...) PGM_GNUC_SCANF(2, 3);
 
125
 
 
126
static inline
 
127
int
 
128
pgm_snprintf_s (char *str, size_t size, size_t count, const char *format, ...)
 
129
{
 
130
        va_list ap;
 
131
        int retval;
 
132
 
 
133
        va_start (ap, format);
 
134
        retval = pgm_vsnprintf_s (str, size, count, format, ap);
 
135
        va_end (ap);
 
136
        return retval;
 
137
}
 
138
 
 
139
static inline
 
140
int
 
141
pgm_sscanf_s (const char *buffer, const char *format, ...)
 
142
{
 
143
        va_list ap;
 
144
        int retval;
 
145
 
 
146
        va_start (ap, format);
 
147
        retval = vsscanf (buffer, format, ap);
 
148
        va_end (ap);
 
149
        return retval;
 
150
}
 
151
#else
 
152
#       define pgm_snprintf_s           _snprintf_s
 
153
#       define pgm_sscanf_s             sscanf_s
 
154
#endif /* CONFIG_HAVE_SECURITY_ENHANCED_CRT */
 
155
 
 
156
static inline
 
157
char*
 
158
pgm_strerror_s (char *buffer, size_t size, int errnum)
 
159
{
 
160
#ifdef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
161
        if (0 != strerror_s (buffer, size, errnum))
 
162
                pgm_snprintf_s (buffer, size, _TRUNCATE, _("Unknown error %d"), errnum);
 
163
        return buffer;
 
164
#elif defined( _WIN32 )
 
165
        pgm_strncpy_s (buffer, size, strerror (errnum), _TRUNCATE);
 
166
        return buffer;
 
167
#elif defined( CONFIG_HAVE_GNU_STRERROR_R )
 
168
/* GNU-specific, failure is noted within buffer contents */
 
169
        return strerror_r (errnum, buffer, size);
 
170
#else
 
171
/* XSI-compliant */
 
172
        if (0 != strerror_r (errnum, buffer, size))
 
173
                pgm_snprintf_s (buffer, size, _TRUNCATE, _("Unknown error %d"), errnum);
 
174
        return buffer;
 
175
#endif
 
176
}
 
177
 
 
178
static inline
 
179
errno_t
 
180
pgm_fopen_s (FILE **pFile, const char *filename, const char *mode)
 
181
{
 
182
#ifndef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
183
        FILE* stream;
 
184
 
 
185
        if (NULL == (stream = fopen (filename, mode)))
 
186
                return errno;
 
187
        *pFile = stream;
 
188
        return 0;
 
189
#else
 
190
        return fopen_s (pFile, filename, mode);
 
191
#endif
 
192
}
 
193
 
 
194
/* Security-only APIs */
 
195
 
 
196
static inline
 
197
errno_t
 
198
pgm_dupenv_s (char **buffer, size_t *count, const char* name)
 
199
{
 
200
#ifndef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
201
        const char *val = getenv (name);
 
202
/* not found */
 
203
        if (NULL == val) {
 
204
                *buffer = NULL;
 
205
                *count = 0;
 
206
                return 0;
 
207
        }
 
208
        *buffer = pgm_strdup (val);
 
209
/* out of memory */
 
210
        if (NULL == *buffer) {
 
211
                *buffer = NULL;
 
212
                *count = 0;
 
213
                return errno;   /* ENOMEM */
 
214
        }
 
215
        *count = strlen (*buffer) + 1;
 
216
        return 0;
 
217
#else
 
218
        char *pValue;
 
219
        const errno_t err = _dupenv_s (&pValue, count, name);
 
220
        if (err) return err;
 
221
        *buffer = pgm_strdup (pValue);
 
222
        free (pValue);
 
223
        return err;
 
224
#endif
 
225
}
 
226
 
 
227
/* Win32 specific APIs */
 
228
 
 
229
#ifdef _WIN32
 
230
static inline
 
231
errno_t
 
232
pgm_wcstombs_s (size_t *retval, char *dest, size_t size, const wchar_t *src, size_t count)
 
233
{
 
234
#       ifndef CONFIG_HAVE_SECURITY_ENHANCED_CRT
 
235
        size_t characters;
 
236
        if (_TRUNCATE == count) {
 
237
                characters = wcstombs (dest, src, size);
 
238
/* may invalidate last multi-byte character */
 
239
                if (size > 0)
 
240
                        dest[size - 1] = 0;
 
241
        } else {
 
242
                characters = wcstombs (dest, src, count + 1);
 
243
                dest[count] = 0;
 
244
        }
 
245
        if ((size_t)-1 == characters) {
 
246
                *retval = 0;
 
247
                return errno;
 
248
        }
 
249
        *retval = characters;
 
250
        return 0;
 
251
#       else
 
252
        return wcstombs_s (retval, dest, size, src, count);
 
253
#       endif
 
254
}
 
255
 
 
256
#endif /* _WIN32 */
 
257
 
 
258
PGM_END_DECLS
 
259
 
 
260
#endif /* __PGM_IMPL_SECURITY_H__ */