Diffstat (limited to 'libical/src/libical/vsnprintf.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libical/src/libical/vsnprintf.c | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/libical/src/libical/vsnprintf.c b/libical/src/libical/vsnprintf.c new file mode 100644 index 0000000..88a8c63 --- a/dev/null +++ b/libical/src/libical/vsnprintf.c | |||
@@ -0,0 +1,180 @@ | |||
1 | #ifdef __osf__ | ||
2 | /* | ||
3 | * Revision 12: http://theos.com/~deraadt/snprintf.c | ||
4 | * | ||
5 | * Copyright (c) 1997 Theo de Raadt | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <stdio.h> | ||
30 | |||
31 | #include <unistd.h> | ||
32 | #include <sys/param.h> | ||
33 | #include <sys/mman.h> | ||
34 | #include <signal.h> | ||
35 | |||
36 | #include <string.h> | ||
37 | #if __STDC__ | ||
38 | #include <stdarg.h> | ||
39 | #include <stdlib.h> | ||
40 | #else | ||
41 | #include <varargs.h> | ||
42 | #endif | ||
43 | #include <setjmp.h> | ||
44 | |||
45 | #ifndef roundup | ||
46 | #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) | ||
47 | #endif | ||
48 | |||
49 | static int pgsize; | ||
50 | static char *curobj; | ||
51 | static sigjmp_buf bail; | ||
52 | |||
53 | #define EXTRABYTES 2/* XXX: why 2? you don't want to know */ | ||
54 | |||
55 | static char * | ||
56 | msetup(str, n) | ||
57 | char *str; | ||
58 | size_t n; | ||
59 | { | ||
60 | char *e; | ||
61 | |||
62 | if (n == 0) | ||
63 | return NULL; | ||
64 | if (pgsize == 0) | ||
65 | pgsize = getpagesize(); | ||
66 | curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2); | ||
67 | if (curobj == NULL) | ||
68 | return NULL; | ||
69 | e = curobj + n + EXTRABYTES; | ||
70 | e = (char *)roundup((unsigned long)e, pgsize); | ||
71 | if (mprotect(e, pgsize, PROT_NONE) == -1) { | ||
72 | free(curobj); | ||
73 | curobj = NULL; | ||
74 | return NULL; | ||
75 | } | ||
76 | e = e - n - EXTRABYTES; | ||
77 | *e = '\0'; | ||
78 | return (e); | ||
79 | } | ||
80 | |||
81 | static void | ||
82 | mcatch(int i) | ||
83 | { | ||
84 | siglongjmp(bail, 1); | ||
85 | } | ||
86 | |||
87 | static void | ||
88 | mcleanup(str, n, p) | ||
89 | char *str; | ||
90 | size_t n; | ||
91 | char *p; | ||
92 | { | ||
93 | strncpy(str, p, n-1); | ||
94 | str[n-1] = '\0'; | ||
95 | if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, | ||
96 | PROT_READ|PROT_WRITE|PROT_EXEC) == -1) | ||
97 | mprotect((caddr_t)(p + n + EXTRABYTES), pgsize, | ||
98 | PROT_READ|PROT_WRITE); | ||
99 | free(curobj); | ||
100 | } | ||
101 | |||
102 | int | ||
103 | #if __STDC__ | ||
104 | vsnprintf(char *str, size_t n, char const *fmt, va_list ap) | ||
105 | #else | ||
106 | vsnprintf(str, n, fmt, ap) | ||
107 | char *str; | ||
108 | size_t n; | ||
109 | char *fmt; | ||
110 | char *ap; | ||
111 | #endif | ||
112 | { | ||
113 | struct sigaction osa, nsa; | ||
114 | char *p; | ||
115 | int ret = n + 1;/* if we bail, indicated we overflowed */ | ||
116 | |||
117 | memset(&nsa, 0, sizeof nsa); | ||
118 | nsa.sa_handler = mcatch; | ||
119 | sigemptyset(&nsa.sa_mask); | ||
120 | |||
121 | p = msetup(str, n); | ||
122 | if (p == NULL) { | ||
123 | *str = '\0'; | ||
124 | return 0; | ||
125 | } | ||
126 | if (sigsetjmp(bail, 1) == 0) { | ||
127 | if (sigaction(SIGSEGV, &nsa, &osa) == -1) { | ||
128 | mcleanup(str, n, p); | ||
129 | return (0); | ||
130 | } | ||
131 | ret = vsprintf(p, fmt, ap); | ||
132 | } | ||
133 | mcleanup(str, n, p); | ||
134 | (void) sigaction(SIGSEGV, &osa, NULL); | ||
135 | return (ret); | ||
136 | } | ||
137 | |||
138 | int | ||
139 | #if __STDC__ | ||
140 | snprintf(char *str, size_t n, char const *fmt, ...) | ||
141 | #else | ||
142 | snprintf(str, n, fmt, va_alist) | ||
143 | char *str; | ||
144 | size_t n; | ||
145 | char *fmt; | ||
146 | va_dcl | ||
147 | #endif | ||
148 | { | ||
149 | va_list ap; | ||
150 | #if __STDC__ | ||
151 | va_start(ap, fmt); | ||
152 | #else | ||
153 | va_start(ap); | ||
154 | #endif | ||
155 | |||
156 | return (vsnprintf(str, n, fmt, ap)); | ||
157 | va_end(ap); | ||
158 | } | ||
159 | |||
160 | #elif defined ( _WIN32 ) | ||
161 | |||
162 | #include <stdio.h> | ||
163 | #include <stdarg.h> | ||
164 | |||
165 | int snprintf(char *str, size_t n, char const *fmt, ...) | ||
166 | { | ||
167 | va_list ap; | ||
168 | va_start(ap, fmt); | ||
169 | |||
170 | return _snprintf(str, n, fmt, ap); | ||
171 | } | ||
172 | |||
173 | #else | ||
174 | /* ANSI C forbids an empty source file... */ | ||
175 | |||
176 | static void vsnprintf_dummy_func() { | ||
177 | vsnprintf_dummy_func(); | ||
178 | } | ||
179 | |||
180 | #endif | ||