aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/lgl
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon/https/lgl')
-rw-r--r--src/daemon/https/lgl/Makefile.am37
-rw-r--r--src/daemon/https/lgl/asnprintf.c35
-rw-r--r--src/daemon/https/lgl/asprintf.c39
-rw-r--r--src/daemon/https/lgl/des.c662
-rw-r--r--src/daemon/https/lgl/des.h121
-rw-r--r--src/daemon/https/lgl/float+.h148
-rw-r--r--src/daemon/https/lgl/gc-gnulib.c791
-rw-r--r--src/daemon/https/lgl/gc-libgcrypt.c627
-rw-r--r--src/daemon/https/lgl/gc-pbkdf2-sha1.c185
-rw-r--r--src/daemon/https/lgl/gc.h347
-rw-r--r--src/daemon/https/lgl/gettext.h270
-rw-r--r--src/daemon/https/lgl/hmac-md5.c81
-rw-r--r--src/daemon/https/lgl/hmac-sha1.c81
-rw-r--r--src/daemon/https/lgl/hmac.h41
-rw-r--r--src/daemon/https/lgl/md5.c451
-rw-r--r--src/daemon/https/lgl/md5.h124
-rw-r--r--src/daemon/https/lgl/memmem.c61
-rw-r--r--src/daemon/https/lgl/memmove.c26
-rw-r--r--src/daemon/https/lgl/memxor.c35
-rw-r--r--src/daemon/https/lgl/memxor.h31
-rw-r--r--src/daemon/https/lgl/printf-args.c185
-rw-r--r--src/daemon/https/lgl/printf-args.h154
-rw-r--r--src/daemon/https/lgl/printf-parse.c599
-rw-r--r--src/daemon/https/lgl/printf-parse.h178
-rw-r--r--src/daemon/https/lgl/read-file.c136
-rw-r--r--src/daemon/https/lgl/read-file.h34
-rw-r--r--src/daemon/https/lgl/realloc.c87
-rw-r--r--src/daemon/https/lgl/rijndael-alg-fst.c1083
-rw-r--r--src/daemon/https/lgl/rijndael-alg-fst.h67
-rw-r--r--src/daemon/https/lgl/rijndael-api-fst.c517
-rw-r--r--src/daemon/https/lgl/rijndael-api-fst.h207
-rw-r--r--src/daemon/https/lgl/sha1.c416
-rw-r--r--src/daemon/https/lgl/sha1.h87
-rw-r--r--src/daemon/https/lgl/snprintf.c77
-rw-r--r--src/daemon/https/lgl/strverscmp.c130
-rw-r--r--src/daemon/https/lgl/strverscmp.h24
-rw-r--r--src/daemon/https/lgl/time_r.c46
-rw-r--r--src/daemon/https/lgl/vasnprintf.c4825
-rw-r--r--src/daemon/https/lgl/vasnprintf.h81
-rw-r--r--src/daemon/https/lgl/vasprintf.c56
-rw-r--r--src/daemon/https/lgl/xsize.h109
41 files changed, 13291 insertions, 0 deletions
diff --git a/src/daemon/https/lgl/Makefile.am b/src/daemon/https/lgl/Makefile.am
new file mode 100644
index 00000000..1edb75ae
--- /dev/null
+++ b/src/daemon/https/lgl/Makefile.am
@@ -0,0 +1,37 @@
1SUBDIRS = .
2
3AM_CPPFLAGS = -std=c99 \
4-I$(GCRYPT_CPPFLAGS)
5
6# gc-gnulib.c
7
8noinst_LTLIBRARIES = liblgl.la
9
10liblgl_la_LDFLAGS = -lgcrypt
11# liblgl_la_LIBADD = ./gc-libgcrypt.lo
12
13liblgl_la_SOURCES = \
14asnprintf.c \
15sha1.c \
16gc-libgcrypt.c \
17time_r.c \
18rijndael-api-fst.c \
19gc-pbkdf2-sha1.c \
20read-file.c \
21rijndael-alg-fst.c \
22hmac-md5.c \
23hmac-sha1.c \
24realloc.c \
25memmem.c \
26memmove.c \
27memxor.c \
28printf-args.c \
29strverscmp.c \
30snprintf.c \
31asprintf.c \
32vasprintf.c \
33vasnprintf.c \
34md5.c \
35printf-parse.c \
36des.c
37 \ No newline at end of file
diff --git a/src/daemon/https/lgl/asnprintf.c b/src/daemon/https/lgl/asnprintf.c
new file mode 100644
index 00000000..a9114977
--- /dev/null
+++ b/src/daemon/https/lgl/asnprintf.c
@@ -0,0 +1,35 @@
1/* Formatted output to strings.
2 Copyright (C) 1999, 2002, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#include <config.h>
19
20/* Specification. */
21#include "vasnprintf.h"
22
23#include <stdarg.h>
24
25char *
26asnprintf (char *resultbuf, size_t * lengthp, const char *format, ...)
27{
28 va_list args;
29 char *result;
30
31 va_start (args, format);
32 result = vasnprintf (resultbuf, lengthp, format, args);
33 va_end (args);
34 return result;
35}
diff --git a/src/daemon/https/lgl/asprintf.c b/src/daemon/https/lgl/asprintf.c
new file mode 100644
index 00000000..2df1d4b0
--- /dev/null
+++ b/src/daemon/https/lgl/asprintf.c
@@ -0,0 +1,39 @@
1/* Formatted output to strings.
2 Copyright (C) 1999, 2002, 2006-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#include <config.h>
19
20/* Specification. */
21#ifdef IN_LIBASPRINTF
22# include "vasprintf.h"
23#else
24# include <stdio.h>
25#endif
26
27#include <stdarg.h>
28
29int
30asprintf (char **resultp, const char *format, ...)
31{
32 va_list args;
33 int result;
34
35 va_start (args, format);
36 result = vasprintf (resultp, format, args);
37 va_end (args);
38 return result;
39}
diff --git a/src/daemon/https/lgl/des.c b/src/daemon/https/lgl/des.c
new file mode 100644
index 00000000..fd70bc7e
--- /dev/null
+++ b/src/daemon/https/lgl/des.c
@@ -0,0 +1,662 @@
1/* des.c --- DES and Triple-DES encryption/decryption Algorithm
2 * Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 * Free Software Foundation, Inc.
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1, or (at your
8 * option) any later version.
9 *
10 * This file is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this file; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
19 *
20 */
21
22/* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. */
23
24/*
25 * For a description of triple encryption, see:
26 * Bruce Schneier: Applied Cryptography. Second Edition.
27 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
28 * This implementation is according to the definition of DES in FIPS
29 * PUB 46-2 from December 1993.
30 *
31 * Written by Michael Roth <mroth@nessie.de>, September 1998
32 */
33
34/*
35 * U S A G E
36 * ===========
37 *
38 * For DES or Triple-DES encryption/decryption you must initialize a proper
39 * encryption context with a key.
40 *
41 * A DES key is 64bit wide but only 56bits of the key are used. The remaining
42 * bits are parity bits and they will _not_ checked in this implementation, but
43 * simply ignored.
44 *
45 * For Triple-DES you could use either two 64bit keys or three 64bit keys.
46 * The parity bits will _not_ checked, too.
47 *
48 * After initializing a context with a key you could use this context to
49 * encrypt or decrypt data in 64bit blocks in Electronic Codebook Mode.
50 *
51 * DES Example
52 * -----------
53 * unsigned char key[8];
54 * unsigned char plaintext[8];
55 * unsigned char ciphertext[8];
56 * unsigned char recoverd[8];
57 * gl_des_ctx context;
58 *
59 * // Fill 'key' and 'plaintext' with some data
60 * ....
61 *
62 * // Set up the DES encryption context
63 * gl_des_setkey(&context, key);
64 *
65 * // Encrypt the plaintext
66 * des_ecb_encrypt(&context, plaintext, ciphertext);
67 *
68 * // To recover the orginal plaintext from ciphertext use:
69 * des_ecb_decrypt(&context, ciphertext, recoverd);
70 *
71 *
72 * Triple-DES Example
73 * ------------------
74 * unsigned char key1[8];
75 * unsigned char key2[8];
76 * unsigned char key3[8];
77 * unsigned char plaintext[8];
78 * unsigned char ciphertext[8];
79 * unsigned char recoverd[8];
80 * gl_3des_ctx context;
81 *
82 * // If you would like to use two 64bit keys, fill 'key1' and'key2'
83 * // then setup the encryption context:
84 * gl_3des_set2keys(&context, key1, key2);
85 *
86 * // To use three 64bit keys with Triple-DES use:
87 * gl_3des_set3keys(&context, key1, key2, key3);
88 *
89 * // Encrypting plaintext with Triple-DES
90 * gl_3des_ecb_encrypt(&context, plaintext, ciphertext);
91 *
92 * // Decrypting ciphertext to recover the plaintext with Triple-DES
93 * gl_3des_ecb_decrypt(&context, ciphertext, recoverd);
94 */
95
96
97#include <config.h>
98
99#include "des.h"
100
101#include <stdio.h>
102#include <string.h> /* memcpy, memcmp */
103
104/*
105 * The s-box values are permuted according to the 'primitive function P'
106 * and are rotated one bit to the left.
107 */
108static const uint32_t sbox1[64] = {
109 0x01010400, 0x00000000, 0x00010000, 0x01010404, 0x01010004, 0x00010404,
110 0x00000004, 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
111 0x01000404, 0x01010004, 0x01000000, 0x00000004, 0x00000404, 0x01000400,
112 0x01000400, 0x00010400, 0x00010400, 0x01010000, 0x01010000, 0x01000404,
113 0x00010004, 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
114 0x00010404, 0x01000000, 0x00010000, 0x01010404, 0x00000004, 0x01010000,
115 0x01010400, 0x01000000, 0x01000000, 0x00000400, 0x01010004, 0x00010000,
116 0x00010400, 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
117 0x01010404, 0x00010004, 0x01010000, 0x01000404, 0x01000004, 0x00000404,
118 0x00010404, 0x01010400, 0x00000404, 0x01000400, 0x01000400, 0x00000000,
119 0x00010004, 0x00010400, 0x00000000, 0x01010004
120};
121
122static const uint32_t sbox2[64] = {
123 0x80108020, 0x80008000, 0x00008000, 0x00108020, 0x00100000, 0x00000020,
124 0x80100020, 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
125 0x80008000, 0x00100000, 0x00000020, 0x80100020, 0x00108000, 0x00100020,
126 0x80008020, 0x00000000, 0x80000000, 0x00008000, 0x00108020, 0x80100000,
127 0x00100020, 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
128 0x80100000, 0x00008020, 0x00000000, 0x00108020, 0x80100020, 0x00100000,
129 0x80008020, 0x80100000, 0x80108000, 0x00008000, 0x80100000, 0x80008000,
130 0x00000020, 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
131 0x00008020, 0x80108000, 0x00100000, 0x80000020, 0x00100020, 0x80008020,
132 0x80000020, 0x00100020, 0x00108000, 0x00000000, 0x80008000, 0x00008020,
133 0x80000000, 0x80100020, 0x80108020, 0x00108000
134};
135
136static const uint32_t sbox3[64] = {
137 0x00000208, 0x08020200, 0x00000000, 0x08020008, 0x08000200, 0x00000000,
138 0x00020208, 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
139 0x08020208, 0x00020008, 0x08020000, 0x00000208, 0x08000000, 0x00000008,
140 0x08020200, 0x00000200, 0x00020200, 0x08020000, 0x08020008, 0x00020208,
141 0x08000208, 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
142 0x00000200, 0x08000000, 0x08020200, 0x08000000, 0x00020008, 0x00000208,
143 0x00020000, 0x08020200, 0x08000200, 0x00000000, 0x00000200, 0x00020008,
144 0x08020208, 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
145 0x08000208, 0x00020000, 0x08000000, 0x08020208, 0x00000008, 0x00020208,
146 0x00020200, 0x08000008, 0x08020000, 0x08000208, 0x00000208, 0x08020000,
147 0x00020208, 0x00000008, 0x08020008, 0x00020200
148};
149
150static const uint32_t sbox4[64] = {
151 0x00802001, 0x00002081, 0x00002081, 0x00000080, 0x00802080, 0x00800081,
152 0x00800001, 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
153 0x00000081, 0x00000000, 0x00800080, 0x00800001, 0x00000001, 0x00002000,
154 0x00800000, 0x00802001, 0x00000080, 0x00800000, 0x00002001, 0x00002080,
155 0x00800081, 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
156 0x00802081, 0x00000081, 0x00800080, 0x00800001, 0x00802000, 0x00802081,
157 0x00000081, 0x00000000, 0x00000000, 0x00802000, 0x00002080, 0x00800080,
158 0x00800081, 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
159 0x00802081, 0x00000081, 0x00000001, 0x00002000, 0x00800001, 0x00002001,
160 0x00802080, 0x00800081, 0x00002001, 0x00002080, 0x00800000, 0x00802001,
161 0x00000080, 0x00800000, 0x00002000, 0x00802080
162};
163
164static const uint32_t sbox5[64] = {
165 0x00000100, 0x02080100, 0x02080000, 0x42000100, 0x00080000, 0x00000100,
166 0x40000000, 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
167 0x42000100, 0x42080000, 0x00080100, 0x40000000, 0x02000000, 0x40080000,
168 0x40080000, 0x00000000, 0x40000100, 0x42080100, 0x42080100, 0x02000100,
169 0x42080000, 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
170 0x42000000, 0x00080100, 0x00080000, 0x42000100, 0x00000100, 0x02000000,
171 0x40000000, 0x02080000, 0x42000100, 0x40080100, 0x02000100, 0x40000000,
172 0x42080000, 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
173 0x42080100, 0x00080100, 0x42000000, 0x42080100, 0x02080000, 0x00000000,
174 0x40080000, 0x42000000, 0x00080100, 0x02000100, 0x40000100, 0x00080000,
175 0x00000000, 0x40080000, 0x02080100, 0x40000100
176};
177
178static const uint32_t sbox6[64] = {
179 0x20000010, 0x20400000, 0x00004000, 0x20404010, 0x20400000, 0x00000010,
180 0x20404010, 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
181 0x00400010, 0x20004000, 0x20000000, 0x00004010, 0x00000000, 0x00400010,
182 0x20004010, 0x00004000, 0x00404000, 0x20004010, 0x00000010, 0x20400010,
183 0x20400010, 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
184 0x20404000, 0x20000000, 0x20004000, 0x00000010, 0x20400010, 0x00404000,
185 0x20404010, 0x00400000, 0x00004010, 0x20000010, 0x00400000, 0x20004000,
186 0x20000000, 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
187 0x00404010, 0x20404000, 0x00000000, 0x20400010, 0x00000010, 0x00004000,
188 0x20400000, 0x00404010, 0x00004000, 0x00400010, 0x20004010, 0x00000000,
189 0x20404000, 0x20000000, 0x00400010, 0x20004010
190};
191
192static const uint32_t sbox7[64] = {
193 0x00200000, 0x04200002, 0x04000802, 0x00000000, 0x00000800, 0x04000802,
194 0x00200802, 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
195 0x00000002, 0x04000000, 0x04200002, 0x00000802, 0x04000800, 0x00200802,
196 0x00200002, 0x04000800, 0x04000002, 0x04200000, 0x04200800, 0x00200002,
197 0x04200000, 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
198 0x04000000, 0x00200800, 0x04000000, 0x00200800, 0x00200000, 0x04000802,
199 0x04000802, 0x04200002, 0x04200002, 0x00000002, 0x00200002, 0x04000000,
200 0x04000800, 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
201 0x00000802, 0x04000002, 0x04200802, 0x04200000, 0x00200800, 0x00000000,
202 0x00000002, 0x04200802, 0x00000000, 0x00200802, 0x04200000, 0x00000800,
203 0x04000002, 0x04000800, 0x00000800, 0x00200002
204};
205
206static const uint32_t sbox8[64] = {
207 0x10001040, 0x00001000, 0x00040000, 0x10041040, 0x10000000, 0x10001040,
208 0x00000040, 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
209 0x10041000, 0x00041040, 0x00001000, 0x00000040, 0x10040000, 0x10000040,
210 0x10001000, 0x00001040, 0x00041000, 0x00040040, 0x10040040, 0x10041000,
211 0x00001040, 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
212 0x00041040, 0x00040000, 0x00041040, 0x00040000, 0x10041000, 0x00001000,
213 0x00000040, 0x10040040, 0x00001000, 0x00041040, 0x10001000, 0x00000040,
214 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
215 0x00000000, 0x10041040, 0x00040040, 0x10000040, 0x10040000, 0x10001000,
216 0x10001040, 0x00000000, 0x10041040, 0x00041000, 0x00041000, 0x00001040,
217 0x00001040, 0x00040040, 0x10000000, 0x10041000
218};
219
220/*
221 * These two tables are part of the 'permuted choice 1' function.
222 * In this implementation several speed improvements are done.
223 */
224static const uint32_t leftkey_swap[16] = {
225 0x00000000, 0x00000001, 0x00000100, 0x00000101,
226 0x00010000, 0x00010001, 0x00010100, 0x00010101,
227 0x01000000, 0x01000001, 0x01000100, 0x01000101,
228 0x01010000, 0x01010001, 0x01010100, 0x01010101
229};
230
231static const uint32_t rightkey_swap[16] = {
232 0x00000000, 0x01000000, 0x00010000, 0x01010000,
233 0x00000100, 0x01000100, 0x00010100, 0x01010100,
234 0x00000001, 0x01000001, 0x00010001, 0x01010001,
235 0x00000101, 0x01000101, 0x00010101, 0x01010101,
236};
237
238/*
239 * Numbers of left shifts per round for encryption subkeys. To
240 * calculate the decryption subkeys we just reverse the ordering of
241 * the calculated encryption subkeys, so there is no need for a
242 * decryption rotate tab.
243 */
244static const unsigned char encrypt_rotate_tab[16] = {
245 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
246};
247
248/*
249 * Table with weak DES keys sorted in ascending order. In DES there
250 * are 64 known keys which are weak. They are weak because they
251 * produce only one, two or four different subkeys in the subkey
252 * scheduling process. The keys in this table have all their parity
253 * bits cleared.
254 */
255static const unsigned char weak_keys[64][8] = {
256 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*w */
257 {0x00, 0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e},
258 {0x00, 0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0},
259 {0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe},
260 {0x00, 0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e}, /*sw */
261 {0x00, 0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00},
262 {0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe},
263 {0x00, 0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0},
264 {0x00, 0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0}, /*sw */
265 {0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe},
266 {0x00, 0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00},
267 {0x00, 0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e},
268 {0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe}, /*sw */
269 {0x00, 0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0},
270 {0x00, 0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e},
271 {0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00},
272 {0x1e, 0x00, 0x00, 0x1e, 0x0e, 0x00, 0x00, 0x0e},
273 {0x1e, 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x0e, 0x00}, /*sw */
274 {0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0, 0xfe},
275 {0x1e, 0x00, 0xfe, 0xe0, 0x0e, 0x00, 0xfe, 0xf0},
276 {0x1e, 0x1e, 0x00, 0x00, 0x0e, 0x0e, 0x00, 0x00},
277 {0x1e, 0x1e, 0x1e, 0x1e, 0x0e, 0x0e, 0x0e, 0x0e}, /*w */
278 {0x1e, 0x1e, 0xe0, 0xe0, 0x0e, 0x0e, 0xf0, 0xf0},
279 {0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe, 0xfe},
280 {0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00, 0xfe},
281 {0x1e, 0xe0, 0x1e, 0xe0, 0x0e, 0xf0, 0x0e, 0xf0}, /*sw */
282 {0x1e, 0xe0, 0xe0, 0x1e, 0x0e, 0xf0, 0xf0, 0x0e},
283 {0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0, 0xfe, 0x00},
284 {0x1e, 0xfe, 0x00, 0xe0, 0x0e, 0xfe, 0x00, 0xf0},
285 {0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e, 0xfe}, /*sw */
286 {0x1e, 0xfe, 0xe0, 0x00, 0x0e, 0xfe, 0xf0, 0x00},
287 {0x1e, 0xfe, 0xfe, 0x1e, 0x0e, 0xfe, 0xfe, 0x0e},
288 {0xe0, 0x00, 0x00, 0xe0, 0xf0, 0x00, 0x00, 0xf0},
289 {0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e, 0xfe},
290 {0xe0, 0x00, 0xe0, 0x00, 0xf0, 0x00, 0xf0, 0x00}, /*sw */
291 {0xe0, 0x00, 0xfe, 0x1e, 0xf0, 0x00, 0xfe, 0x0e},
292 {0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00, 0xfe},
293 {0xe0, 0x1e, 0x1e, 0xe0, 0xf0, 0x0e, 0x0e, 0xf0},
294 {0xe0, 0x1e, 0xe0, 0x1e, 0xf0, 0x0e, 0xf0, 0x0e}, /*sw */
295 {0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e, 0xfe, 0x00},
296 {0xe0, 0xe0, 0x00, 0x00, 0xf0, 0xf0, 0x00, 0x00},
297 {0xe0, 0xe0, 0x1e, 0x1e, 0xf0, 0xf0, 0x0e, 0x0e},
298 {0xe0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf0, 0xf0}, /*w */
299 {0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe, 0xfe},
300 {0xe0, 0xfe, 0x00, 0x1e, 0xf0, 0xfe, 0x00, 0x0e},
301 {0xe0, 0xfe, 0x1e, 0x00, 0xf0, 0xfe, 0x0e, 0x00},
302 {0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0, 0xfe}, /*sw */
303 {0xe0, 0xfe, 0xfe, 0xe0, 0xf0, 0xfe, 0xfe, 0xf0},
304 {0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00, 0xfe},
305 {0xfe, 0x00, 0x1e, 0xe0, 0xfe, 0x00, 0x0e, 0xf0},
306 {0xfe, 0x00, 0xe0, 0x1e, 0xfe, 0x00, 0xf0, 0x0e},
307 {0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00, 0xfe, 0x00}, /*sw */
308 {0xfe, 0x1e, 0x00, 0xe0, 0xfe, 0x0e, 0x00, 0xf0},
309 {0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e, 0xfe},
310 {0xfe, 0x1e, 0xe0, 0x00, 0xfe, 0x0e, 0xf0, 0x00},
311 {0xfe, 0x1e, 0xfe, 0x1e, 0xfe, 0x0e, 0xfe, 0x0e}, /*sw */
312 {0xfe, 0xe0, 0x00, 0x1e, 0xfe, 0xf0, 0x00, 0x0e},
313 {0xfe, 0xe0, 0x1e, 0x00, 0xfe, 0xf0, 0x0e, 0x00},
314 {0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0, 0xfe},
315 {0xfe, 0xe0, 0xfe, 0xe0, 0xfe, 0xf0, 0xfe, 0xf0}, /*sw */
316 {0xfe, 0xfe, 0x00, 0x00, 0xfe, 0xfe, 0x00, 0x00},
317 {0xfe, 0xfe, 0x1e, 0x1e, 0xfe, 0xfe, 0x0e, 0x0e},
318 {0xfe, 0xfe, 0xe0, 0xe0, 0xfe, 0xfe, 0xf0, 0xf0},
319 {0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe} /*w */
320};
321
322bool
323gl_des_is_weak_key (const char *key)
324{
325 char work[8];
326 int i, left, right, middle, cmp_result;
327
328 /* clear parity bits */
329 for (i = 0; i < 8; ++i)
330 work[i] = ((unsigned char) key[i]) & 0xfe;
331
332 /* binary search in the weak key table */
333 left = 0;
334 right = 63;
335 while (left <= right)
336 {
337 middle = (left + right) / 2;
338
339 if (!(cmp_result = memcmp (work, weak_keys[middle], 8)))
340 return -1;
341
342 if (cmp_result > 0)
343 left = middle + 1;
344 else
345 right = middle - 1;
346 }
347
348 return 0;
349}
350
351/*
352 * Macro to swap bits across two words.
353 */
354#define DO_PERMUTATION(a, temp, b, offset, mask) \
355 temp = ((a>>offset) ^ b) & mask; \
356 b ^= temp; \
357 a ^= temp<<offset;
358
359
360/*
361 * This performs the 'initial permutation' of the data to be encrypted
362 * or decrypted. Additionally the resulting two words are rotated one bit
363 * to the left.
364 */
365#define INITIAL_PERMUTATION(left, temp, right) \
366 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f) \
367 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
368 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
369 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
370 right = (right << 1) | (right >> 31); \
371 temp = (left ^ right) & 0xaaaaaaaa; \
372 right ^= temp; \
373 left ^= temp; \
374 left = (left << 1) | (left >> 31);
375
376/*
377 * The 'inverse initial permutation'.
378 */
379#define FINAL_PERMUTATION(left, temp, right) \
380 left = (left << 31) | (left >> 1); \
381 temp = (left ^ right) & 0xaaaaaaaa; \
382 left ^= temp; \
383 right ^= temp; \
384 right = (right << 31) | (right >> 1); \
385 DO_PERMUTATION(right, temp, left, 8, 0x00ff00ff) \
386 DO_PERMUTATION(right, temp, left, 2, 0x33333333) \
387 DO_PERMUTATION(left, temp, right, 16, 0x0000ffff) \
388 DO_PERMUTATION(left, temp, right, 4, 0x0f0f0f0f)
389
390
391/*
392 * A full DES round including 'expansion function', 'sbox substitution'
393 * and 'primitive function P' but without swapping the left and right word.
394 * Please note: The data in 'from' and 'to' is already rotated one bit to
395 * the left, done in the initial permutation.
396 */
397#define DES_ROUND(from, to, work, subkey) \
398 work = from ^ *subkey++; \
399 to ^= sbox8[ work & 0x3f ]; \
400 to ^= sbox6[ (work>>8) & 0x3f ]; \
401 to ^= sbox4[ (work>>16) & 0x3f ]; \
402 to ^= sbox2[ (work>>24) & 0x3f ]; \
403 work = ((from << 28) | (from >> 4)) ^ *subkey++; \
404 to ^= sbox7[ work & 0x3f ]; \
405 to ^= sbox5[ (work>>8) & 0x3f ]; \
406 to ^= sbox3[ (work>>16) & 0x3f ]; \
407 to ^= sbox1[ (work>>24) & 0x3f ];
408
409/*
410 * Macros to convert 8 bytes from/to 32bit words.
411 */
412#define READ_64BIT_DATA(data, left, right) \
413 left = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
414 right = (data[4] << 24) | (data[5] << 16) | (data[6] << 8) | data[7];
415
416#define WRITE_64BIT_DATA(data, left, right) \
417 data[0] = (left >> 24) &0xff; data[1] = (left >> 16) &0xff; \
418 data[2] = (left >> 8) &0xff; data[3] = left &0xff; \
419 data[4] = (right >> 24) &0xff; data[5] = (right >> 16) &0xff; \
420 data[6] = (right >> 8) &0xff; data[7] = right &0xff;
421
422/*
423 * des_key_schedule(): Calculate 16 subkeys pairs (even/odd) for
424 * 16 encryption rounds.
425 * To calculate subkeys for decryption the caller
426 * have to reorder the generated subkeys.
427 *
428 * rawkey: 8 Bytes of key data
429 * subkey: Array of at least 32 uint32_ts. Will be filled
430 * with calculated subkeys.
431 *
432 */
433static void
434des_key_schedule (const char *_rawkey, uint32_t * subkey)
435{
436 const unsigned char *rawkey = (const unsigned char *) _rawkey;
437 uint32_t left, right, work;
438 int round;
439
440 READ_64BIT_DATA (rawkey, left, right)
441 DO_PERMUTATION (right, work, left, 4, 0x0f0f0f0f)
442 DO_PERMUTATION (right, work, left, 0, 0x10101010)
443 left = ((leftkey_swap[(left >> 0) & 0xf] << 3)
444 | (leftkey_swap[(left >> 8) & 0xf] << 2)
445 | (leftkey_swap[(left >> 16) & 0xf] << 1)
446 | (leftkey_swap[(left >> 24) & 0xf])
447 | (leftkey_swap[(left >> 5) & 0xf] << 7)
448 | (leftkey_swap[(left >> 13) & 0xf] << 6)
449 | (leftkey_swap[(left >> 21) & 0xf] << 5)
450 | (leftkey_swap[(left >> 29) & 0xf] << 4));
451
452 left &= 0x0fffffff;
453
454 right = ((rightkey_swap[(right >> 1) & 0xf] << 3)
455 | (rightkey_swap[(right >> 9) & 0xf] << 2)
456 | (rightkey_swap[(right >> 17) & 0xf] << 1)
457 | (rightkey_swap[(right >> 25) & 0xf])
458 | (rightkey_swap[(right >> 4) & 0xf] << 7)
459 | (rightkey_swap[(right >> 12) & 0xf] << 6)
460 | (rightkey_swap[(right >> 20) & 0xf] << 5)
461 | (rightkey_swap[(right >> 28) & 0xf] << 4));
462
463 right &= 0x0fffffff;
464
465 for (round = 0; round < 16; ++round)
466 {
467 left = ((left << encrypt_rotate_tab[round])
468 | (left >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
469 right = ((right << encrypt_rotate_tab[round])
470 | (right >> (28 - encrypt_rotate_tab[round]))) & 0x0fffffff;
471
472 *subkey++ = (((left << 4) & 0x24000000)
473 | ((left << 28) & 0x10000000)
474 | ((left << 14) & 0x08000000)
475 | ((left << 18) & 0x02080000)
476 | ((left << 6) & 0x01000000)
477 | ((left << 9) & 0x00200000)
478 | ((left >> 1) & 0x00100000)
479 | ((left << 10) & 0x00040000)
480 | ((left << 2) & 0x00020000)
481 | ((left >> 10) & 0x00010000)
482 | ((right >> 13) & 0x00002000)
483 | ((right >> 4) & 0x00001000)
484 | ((right << 6) & 0x00000800)
485 | ((right >> 1) & 0x00000400)
486 | ((right >> 14) & 0x00000200)
487 | (right & 0x00000100)
488 | ((right >> 5) & 0x00000020)
489 | ((right >> 10) & 0x00000010)
490 | ((right >> 3) & 0x00000008)
491 | ((right >> 18) & 0x00000004)
492 | ((right >> 26) & 0x00000002)
493 | ((right >> 24) & 0x00000001));
494
495 *subkey++ = (((left << 15) & 0x20000000)
496 | ((left << 17) & 0x10000000)
497 | ((left << 10) & 0x08000000)
498 | ((left << 22) & 0x04000000)
499 | ((left >> 2) & 0x02000000)
500 | ((left << 1) & 0x01000000)
501 | ((left << 16) & 0x00200000)
502 | ((left << 11) & 0x00100000)
503 | ((left << 3) & 0x00080000)
504 | ((left >> 6) & 0x00040000)
505 | ((left << 15) & 0x00020000)
506 | ((left >> 4) & 0x00010000)
507 | ((right >> 2) & 0x00002000)
508 | ((right << 8) & 0x00001000)
509 | ((right >> 14) & 0x00000808)
510 | ((right >> 9) & 0x00000400)
511 | ((right) & 0x00000200)
512 | ((right << 7) & 0x00000100)
513 | ((right >> 7) & 0x00000020)
514 | ((right >> 3) & 0x00000011)
515 | ((right << 2) & 0x00000004)
516 | ((right >> 21) & 0x00000002));
517 }
518}
519
520void
521gl_des_setkey (gl_des_ctx * ctx, const char *key)
522{
523 int i;
524
525 des_key_schedule (key, ctx->encrypt_subkeys);
526
527 for (i = 0; i < 32; i += 2)
528 {
529 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30 - i];
530 ctx->decrypt_subkeys[i + 1] = ctx->encrypt_subkeys[31 - i];
531 }
532}
533
534bool
535gl_des_makekey (gl_des_ctx * ctx, const char *key, size_t keylen)
536{
537 if (keylen != 8)
538 return false;
539
540 gl_des_setkey (ctx, key);
541
542 return !gl_des_is_weak_key (key);
543}
544
545void
546gl_des_ecb_crypt (gl_des_ctx * ctx, const char *_from, char *_to, int mode)
547{
548 const unsigned char *from = (const unsigned char *) _from;
549 unsigned char *to = (unsigned char *) _to;
550 uint32_t left, right, work;
551 uint32_t *keys;
552
553 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
554
555READ_64BIT_DATA (from, left, right)
556 INITIAL_PERMUTATION (left, work, right)
557 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
558 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
559 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
560 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
561 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
562 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
563 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
564 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
565 FINAL_PERMUTATION (right, work, left) WRITE_64BIT_DATA (to, right, left)}
566
567void
568gl_3des_set2keys (gl_3des_ctx * ctx, const char *key1, const char *key2)
569{
570 int i;
571
572 des_key_schedule (key1, ctx->encrypt_subkeys);
573 des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
574
575 for (i = 0; i < 32; i += 2)
576 {
577 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[30 - i];
578 ctx->decrypt_subkeys[i + 1] = ctx->encrypt_subkeys[31 - i];
579
580 ctx->encrypt_subkeys[i + 32] = ctx->decrypt_subkeys[62 - i];
581 ctx->encrypt_subkeys[i + 33] = ctx->decrypt_subkeys[63 - i];
582
583 ctx->encrypt_subkeys[i + 64] = ctx->encrypt_subkeys[i];
584 ctx->encrypt_subkeys[i + 65] = ctx->encrypt_subkeys[i + 1];
585
586 ctx->decrypt_subkeys[i + 64] = ctx->decrypt_subkeys[i];
587 ctx->decrypt_subkeys[i + 65] = ctx->decrypt_subkeys[i + 1];
588 }
589}
590
591void
592gl_3des_set3keys (gl_3des_ctx * ctx, const char *key1,
593 const char *key2, const char *key3)
594{
595 int i;
596
597 des_key_schedule (key1, ctx->encrypt_subkeys);
598 des_key_schedule (key2, &(ctx->decrypt_subkeys[32]));
599 des_key_schedule (key3, &(ctx->encrypt_subkeys[64]));
600
601 for (i = 0; i < 32; i += 2)
602 {
603 ctx->decrypt_subkeys[i] = ctx->encrypt_subkeys[94 - i];
604 ctx->decrypt_subkeys[i + 1] = ctx->encrypt_subkeys[95 - i];
605
606 ctx->encrypt_subkeys[i + 32] = ctx->decrypt_subkeys[62 - i];
607 ctx->encrypt_subkeys[i + 33] = ctx->decrypt_subkeys[63 - i];
608
609 ctx->decrypt_subkeys[i + 64] = ctx->encrypt_subkeys[30 - i];
610 ctx->decrypt_subkeys[i + 65] = ctx->encrypt_subkeys[31 - i];
611 }
612}
613
614void
615gl_3des_ecb_crypt (gl_3des_ctx * ctx, const char *_from, char *_to, int mode)
616{
617 const unsigned char *from = (const unsigned char *) _from;
618 unsigned char *to = (unsigned char *) _to;
619 uint32_t left, right, work;
620 uint32_t *keys;
621
622 keys = mode ? ctx->decrypt_subkeys : ctx->encrypt_subkeys;
623
624READ_64BIT_DATA (from, left, right)
625 INITIAL_PERMUTATION (left, work, right)
626 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
627 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
628 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
629 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
630 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
631 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
632 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
633 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
634 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
635 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
636 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
637 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
638 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
639 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
640 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
641 DES_ROUND (left, right, work, keys) DES_ROUND (right, left, work, keys)
642 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
643 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
644 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
645 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
646 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
647 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
648 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
649 DES_ROUND (right, left, work, keys) DES_ROUND (left, right, work, keys)
650 FINAL_PERMUTATION (right, work, left) WRITE_64BIT_DATA (to, right, left)}
651
652bool
653gl_3des_makekey (gl_3des_ctx * ctx, const char *key, size_t keylen)
654{
655 if (keylen != 24)
656 return false;
657
658 gl_3des_set3keys (ctx, key, key + 8, key + 16);
659
660 return !(gl_des_is_weak_key (key)
661 || gl_des_is_weak_key (key + 8) || gl_des_is_weak_key (key + 16));
662}
diff --git a/src/daemon/https/lgl/des.h b/src/daemon/https/lgl/des.h
new file mode 100644
index 00000000..fdc8686f
--- /dev/null
+++ b/src/daemon/https/lgl/des.h
@@ -0,0 +1,121 @@
1/* des.h --- DES cipher implementation.
2 * Copyright (C) 2005, 2007 Free Software Foundation, Inc.
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Adapted for gnulib by Simon Josefsson, based on Libgcrypt. */
22
23#ifndef DES_H
24# define DES_H
25
26#include <stddef.h>
27#include <stdint.h>
28#include <stdbool.h>
29
30/*
31 * Encryption/Decryption context of DES
32 */
33typedef struct
34{
35 uint32_t encrypt_subkeys[32];
36 uint32_t decrypt_subkeys[32];
37} gl_des_ctx;
38
39/*
40 * Encryption/Decryption context of Triple-DES
41 */
42typedef struct
43{
44 uint32_t encrypt_subkeys[96];
45 uint32_t decrypt_subkeys[96];
46} gl_3des_ctx;
47
48/* Check whether the 8 byte key is weak. Does not check the parity
49 * bits of the key but simple ignore them. */
50extern bool
51gl_des_is_weak_key (const char * key);
52
53/*
54 * DES
55 * ---
56 */
57
58/* Fill a DES context CTX with subkeys calculated from 64bit KEY.
59 * Does not check parity bits, but simply ignore them. Does not check
60 * for weak keys. */
61extern void
62gl_des_setkey (gl_des_ctx *ctx, const char * key);
63
64/* Fill a DES context CTX with subkeys calculated from 64bit KEY, with
65 * weak key checking. Does not check parity bits, but simply ignore
66 * them. */
67extern bool
68gl_des_makekey (gl_des_ctx *ctx, const char * key, size_t keylen);
69
70/* Electronic Codebook Mode DES encryption/decryption of data
71 * according to 'mode'. */
72extern void
73gl_des_ecb_crypt (gl_des_ctx *ctx, const char * from, char * to, int mode);
74
75#define gl_des_ecb_encrypt(ctx, from, to) gl_des_ecb_crypt(ctx, from, to, 0)
76#define gl_des_ecb_decrypt(ctx, from, to) gl_des_ecb_crypt(ctx, from, to, 1)
77
78/* Triple-DES
79 * ----------
80 */
81
82/* Fill a Triple-DES context CTX with subkeys calculated from two
83 * 64bit keys in KEY1 and KEY2. Does not check the parity bits of the
84 * keys, but simply ignore them. Does not check for weak keys. */
85extern void
86gl_3des_set2keys (gl_3des_ctx *ctx,
87 const char * key1,
88 const char * key2);
89
90/*
91 * Fill a Triple-DES context CTX with subkeys calculated from three
92 * 64bit keys in KEY1, KEY2 and KEY3. Does not check the parity bits
93 * of the keys, but simply ignore them. Does not check for weak
94 * keys. */
95extern void
96gl_3des_set3keys (gl_3des_ctx *ctx,
97 const char * key1,
98 const char * key2,
99 const char * key3);
100
101/* Fill a Triple-DES context CTX with subkeys calculated from three
102 * concatenated 64bit keys in KEY, with weak key checking. Does not
103 * check the parity bits of the keys, but simply ignore them. */
104extern bool
105gl_3des_makekey (gl_3des_ctx *ctx,
106 const char * key,
107 size_t keylen);
108
109/* Electronic Codebook Mode Triple-DES encryption/decryption of data
110 * according to 'mode'. Sometimes this mode is named 'EDE' mode
111 * (Encryption-Decryption-Encryption). */
112extern void
113gl_3des_ecb_crypt (gl_3des_ctx *ctx,
114 const char * from,
115 char * to,
116 int mode);
117
118#define gl_3des_ecb_encrypt(ctx, from, to) gl_3des_ecb_crypt(ctx,from,to,0)
119#define gl_3des_ecb_decrypt(ctx, from, to) gl_3des_ecb_crypt(ctx,from,to,1)
120
121#endif /* DES_H */
diff --git a/src/daemon/https/lgl/float+.h b/src/daemon/https/lgl/float+.h
new file mode 100644
index 00000000..f0c0c189
--- /dev/null
+++ b/src/daemon/https/lgl/float+.h
@@ -0,0 +1,148 @@
1/* Supplemental information about the floating-point formats.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2007.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef _FLOATPLUS_H
20#define _FLOATPLUS_H
21
22#include <float.h>
23#include <limits.h>
24
25/* Number of bits in the mantissa of a floating-point number, including the
26 "hidden bit". */
27#if FLT_RADIX == 2
28# define FLT_MANT_BIT FLT_MANT_DIG
29# define DBL_MANT_BIT DBL_MANT_DIG
30# define LDBL_MANT_BIT LDBL_MANT_DIG
31#elif FLT_RADIX == 4
32# define FLT_MANT_BIT (FLT_MANT_DIG * 2)
33# define DBL_MANT_BIT (DBL_MANT_DIG * 2)
34# define LDBL_MANT_BIT (LDBL_MANT_DIG * 2)
35#elif FLT_RADIX == 16
36# define FLT_MANT_BIT (FLT_MANT_DIG * 4)
37# define DBL_MANT_BIT (DBL_MANT_DIG * 4)
38# define LDBL_MANT_BIT (LDBL_MANT_DIG * 4)
39#endif
40
41/* Bit mask that can be used to mask the exponent, as an unsigned number. */
42#define FLT_EXP_MASK ((FLT_MAX_EXP - FLT_MIN_EXP) | 7)
43#define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
44#define LDBL_EXP_MASK ((LDBL_MAX_EXP - LDBL_MIN_EXP) | 7)
45
46/* Number of bits used for the exponent of a floating-point number, including
47 the exponent's sign. */
48#define FLT_EXP_BIT \
49 (FLT_EXP_MASK < 0x100 ? 8 : \
50 FLT_EXP_MASK < 0x200 ? 9 : \
51 FLT_EXP_MASK < 0x400 ? 10 : \
52 FLT_EXP_MASK < 0x800 ? 11 : \
53 FLT_EXP_MASK < 0x1000 ? 12 : \
54 FLT_EXP_MASK < 0x2000 ? 13 : \
55 FLT_EXP_MASK < 0x4000 ? 14 : \
56 FLT_EXP_MASK < 0x8000 ? 15 : \
57 FLT_EXP_MASK < 0x10000 ? 16 : \
58 FLT_EXP_MASK < 0x20000 ? 17 : \
59 FLT_EXP_MASK < 0x40000 ? 18 : \
60 FLT_EXP_MASK < 0x80000 ? 19 : \
61 FLT_EXP_MASK < 0x100000 ? 20 : \
62 FLT_EXP_MASK < 0x200000 ? 21 : \
63 FLT_EXP_MASK < 0x400000 ? 22 : \
64 FLT_EXP_MASK < 0x800000 ? 23 : \
65 FLT_EXP_MASK < 0x1000000 ? 24 : \
66 FLT_EXP_MASK < 0x2000000 ? 25 : \
67 FLT_EXP_MASK < 0x4000000 ? 26 : \
68 FLT_EXP_MASK < 0x8000000 ? 27 : \
69 FLT_EXP_MASK < 0x10000000 ? 28 : \
70 FLT_EXP_MASK < 0x20000000 ? 29 : \
71 FLT_EXP_MASK < 0x40000000 ? 30 : \
72 FLT_EXP_MASK <= 0x7fffffff ? 31 : \
73 32)
74#define DBL_EXP_BIT \
75 (DBL_EXP_MASK < 0x100 ? 8 : \
76 DBL_EXP_MASK < 0x200 ? 9 : \
77 DBL_EXP_MASK < 0x400 ? 10 : \
78 DBL_EXP_MASK < 0x800 ? 11 : \
79 DBL_EXP_MASK < 0x1000 ? 12 : \
80 DBL_EXP_MASK < 0x2000 ? 13 : \
81 DBL_EXP_MASK < 0x4000 ? 14 : \
82 DBL_EXP_MASK < 0x8000 ? 15 : \
83 DBL_EXP_MASK < 0x10000 ? 16 : \
84 DBL_EXP_MASK < 0x20000 ? 17 : \
85 DBL_EXP_MASK < 0x40000 ? 18 : \
86 DBL_EXP_MASK < 0x80000 ? 19 : \
87 DBL_EXP_MASK < 0x100000 ? 20 : \
88 DBL_EXP_MASK < 0x200000 ? 21 : \
89 DBL_EXP_MASK < 0x400000 ? 22 : \
90 DBL_EXP_MASK < 0x800000 ? 23 : \
91 DBL_EXP_MASK < 0x1000000 ? 24 : \
92 DBL_EXP_MASK < 0x2000000 ? 25 : \
93 DBL_EXP_MASK < 0x4000000 ? 26 : \
94 DBL_EXP_MASK < 0x8000000 ? 27 : \
95 DBL_EXP_MASK < 0x10000000 ? 28 : \
96 DBL_EXP_MASK < 0x20000000 ? 29 : \
97 DBL_EXP_MASK < 0x40000000 ? 30 : \
98 DBL_EXP_MASK <= 0x7fffffff ? 31 : \
99 32)
100#define LDBL_EXP_BIT \
101 (LDBL_EXP_MASK < 0x100 ? 8 : \
102 LDBL_EXP_MASK < 0x200 ? 9 : \
103 LDBL_EXP_MASK < 0x400 ? 10 : \
104 LDBL_EXP_MASK < 0x800 ? 11 : \
105 LDBL_EXP_MASK < 0x1000 ? 12 : \
106 LDBL_EXP_MASK < 0x2000 ? 13 : \
107 LDBL_EXP_MASK < 0x4000 ? 14 : \
108 LDBL_EXP_MASK < 0x8000 ? 15 : \
109 LDBL_EXP_MASK < 0x10000 ? 16 : \
110 LDBL_EXP_MASK < 0x20000 ? 17 : \
111 LDBL_EXP_MASK < 0x40000 ? 18 : \
112 LDBL_EXP_MASK < 0x80000 ? 19 : \
113 LDBL_EXP_MASK < 0x100000 ? 20 : \
114 LDBL_EXP_MASK < 0x200000 ? 21 : \
115 LDBL_EXP_MASK < 0x400000 ? 22 : \
116 LDBL_EXP_MASK < 0x800000 ? 23 : \
117 LDBL_EXP_MASK < 0x1000000 ? 24 : \
118 LDBL_EXP_MASK < 0x2000000 ? 25 : \
119 LDBL_EXP_MASK < 0x4000000 ? 26 : \
120 LDBL_EXP_MASK < 0x8000000 ? 27 : \
121 LDBL_EXP_MASK < 0x10000000 ? 28 : \
122 LDBL_EXP_MASK < 0x20000000 ? 29 : \
123 LDBL_EXP_MASK < 0x40000000 ? 30 : \
124 LDBL_EXP_MASK <= 0x7fffffff ? 31 : \
125 32)
126
127/* Number of bits used for a floating-point number: the mantissa (not
128 counting the "hidden bit", since it may or may not be explicit), the
129 exponent, and the sign. */
130#define FLT_TOTAL_BIT ((FLT_MANT_BIT - 1) + FLT_EXP_BIT + 1)
131#define DBL_TOTAL_BIT ((DBL_MANT_BIT - 1) + DBL_EXP_BIT + 1)
132#define LDBL_TOTAL_BIT ((LDBL_MANT_BIT - 1) + LDBL_EXP_BIT + 1)
133
134/* Number of bytes used for a floating-point number.
135 This can be smaller than the 'sizeof'. For example, on i386 systems,
136 'long double' most often have LDBL_MANT_BIT = 64, LDBL_EXP_BIT = 16, hence
137 LDBL_TOTAL_BIT = 80 bits, i.e. 10 bytes of consecutive memory, but
138 sizeof (long double) = 12 or = 16. */
139#define SIZEOF_FLT ((FLT_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
140#define SIZEOF_DBL ((DBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
141#define SIZEOF_LDBL ((LDBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
142
143/* Verify that SIZEOF_FLT <= sizeof (float) etc. */
144typedef int verify_sizeof_flt[2 * (SIZEOF_FLT <= sizeof (float)) - 1];
145typedef int verify_sizeof_dbl[2 * (SIZEOF_DBL <= sizeof (double)) - 1];
146typedef int verify_sizeof_ldbl[2 * (SIZEOF_LDBL <= sizeof (long double)) - 1];
147
148#endif /* _FLOATPLUS_H */
diff --git a/src/daemon/https/lgl/gc-gnulib.c b/src/daemon/https/lgl/gc-gnulib.c
new file mode 100644
index 00000000..05b12781
--- /dev/null
+++ b/src/daemon/https/lgl/gc-gnulib.c
@@ -0,0 +1,791 @@
1/* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Note: This file is only built if GC uses internal functions. */
22
23#include <config.h>
24
25/* Get prototype. */
26#include "gc.h"
27
28#include <stdlib.h>
29#include <string.h>
30
31/* For randomize. */
32#ifdef GNULIB_GC_RANDOM
33# include <unistd.h>
34# include <sys/types.h>
35# include <sys/stat.h>
36# include <fcntl.h>
37# include <errno.h>
38#endif
39
40/* Hashes. */
41#ifdef GNULIB_GC_MD5
42# include "md5.h"
43#endif
44#ifdef GNULIB_GC_SHA1
45# include "sha1.h"
46#endif
47#if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1)
48# include "hmac.h"
49#endif
50
51/* Ciphers. */
52#ifdef GNULIB_GC_ARCFOUR
53# include "arcfour.h"
54#endif
55#ifdef GNULIB_GC_ARCTWO
56# include "arctwo.h"
57#endif
58#ifdef GNULIB_GC_DES
59# include "des.h"
60#endif
61#ifdef GNULIB_GC_RIJNDAEL
62# include "rijndael-api-fst.h"
63#endif
64
65/* The results of open() in this file are not used with fchdir,
66 therefore save some unnecessary work in fchdir.c. */
67#undef open
68#undef close
69
70Gc_rc gc_init(void)
71{
72 return GC_OK;
73}
74
75void gc_done(void)
76{
77 return;
78}
79
80#ifdef GNULIB_GC_RANDOM
81
82/* Randomness. */
83
84static Gc_rc
85randomize (int level, char *data, size_t datalen)
86 {
87 int fd;
88 const char *device;
89 size_t len = 0;
90 int rc;
91
92 switch (level)
93 {
94 case 0:
95 device = NAME_OF_NONCE_DEVICE;
96 break;
97
98 case 1:
99 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
100 break;
101
102 default:
103 device = NAME_OF_RANDOM_DEVICE;
104 break;
105 }
106
107 if (strcmp (device, "no") == 0)
108 return GC_RANDOM_ERROR;
109
110 fd = open (device, O_RDONLY);
111 if (fd < 0)
112 return GC_RANDOM_ERROR;
113
114 do
115 {
116 ssize_t tmp;
117
118 tmp = read (fd, data, datalen);
119
120 if (tmp < 0)
121 {
122 int save_errno = errno;
123 close (fd);
124 errno = save_errno;
125 return GC_RANDOM_ERROR;
126 }
127
128 len += tmp;
129 }
130 while (len < datalen);
131
132 rc = close (fd);
133 if (rc < 0)
134 return GC_RANDOM_ERROR;
135
136 return GC_OK;
137 }
138
139Gc_rc
140gc_nonce (char *data, size_t datalen)
141 {
142 return randomize (0, data, datalen);
143 }
144
145Gc_rc
146gc_pseudo_random (char *data, size_t datalen)
147 {
148 return randomize (1, data, datalen);
149 }
150
151Gc_rc
152gc_random (char *data, size_t datalen)
153 {
154 return randomize (2, data, datalen);
155 }
156
157#endif
158
159/* Memory allocation. */
160void gc_set_allocators(gc_malloc_t func_malloc,
161 gc_malloc_t secure_malloc,
162 gc_secure_check_t secure_check,
163 gc_realloc_t func_realloc,
164 gc_free_t func_free)
165{
166 return;
167}
168
169/* Ciphers. */
170
171typedef struct _gc_cipher_ctx
172 {
173 Gc_cipher alg;
174 Gc_cipher_mode mode;
175#ifdef GNULIB_GC_ARCTWO
176 arctwo_context arctwoContext;
177 char arctwoIV[ARCTWO_BLOCK_SIZE];
178#endif
179#ifdef GNULIB_GC_ARCFOUR
180 arcfour_context arcfourContext;
181#endif
182#ifdef GNULIB_GC_DES
183 gl_des_ctx desContext;
184#endif
185#ifdef GNULIB_GC_RIJNDAEL
186 rijndaelKeyInstance aesEncKey;
187 rijndaelKeyInstance aesDecKey;
188 rijndaelCipherInstance aesContext;
189#endif
190 } _gc_cipher_ctx;
191
192Gc_rc gc_cipher_open(Gc_cipher alg,
193 Gc_cipher_mode mode,
194 gc_cipher_handle * outhandle)
195{
196 _gc_cipher_ctx *ctx;
197 Gc_rc rc = GC_OK;
198
199 ctx = calloc(sizeof (*ctx), 1);
200 if (!ctx)
201 return GC_MALLOC_ERROR;
202
203 ctx->alg = alg;
204 ctx->mode = mode;
205
206 switch (alg)
207 {
208#ifdef GNULIB_GC_ARCTWO
209 case GC_ARCTWO40:
210 switch (mode)
211 {
212 case GC_ECB:
213 case GC_CBC:
214 break;
215
216 default:
217 rc = GC_INVALID_CIPHER;
218 }
219 break;
220#endif
221
222#ifdef GNULIB_GC_ARCFOUR
223 case GC_ARCFOUR128:
224 case GC_ARCFOUR40:
225 switch (mode)
226 {
227 case GC_STREAM:
228 break;
229
230 default:
231 rc = GC_INVALID_CIPHER;
232 }
233 break;
234#endif
235
236#ifdef GNULIB_GC_DES
237 case GC_DES:
238 switch (mode)
239 {
240 case GC_ECB:
241 break;
242
243 default:
244 rc = GC_INVALID_CIPHER;
245 }
246 break;
247#endif
248
249#ifdef GNULIB_GC_RIJNDAEL
250 case GC_AES128:
251 case GC_AES192:
252 case GC_AES256:
253 switch (mode)
254 {
255 case GC_ECB:
256 case GC_CBC:
257 break;
258
259 default:
260 rc = GC_INVALID_CIPHER;
261 }
262 break;
263#endif
264
265 default:
266 rc = GC_INVALID_CIPHER;
267 }
268
269 if (rc == GC_OK)
270 *outhandle = ctx;
271 else
272 free(ctx);
273
274 return rc;
275}
276
277Gc_rc gc_cipher_setkey(gc_cipher_handle handle,
278 size_t keylen,
279 const char *key)
280{
281 _gc_cipher_ctx *ctx = handle;
282
283 switch (ctx->alg)
284 {
285#ifdef GNULIB_GC_ARCTWO
286 case GC_ARCTWO40:
287 arctwo_setkey (&ctx->arctwoContext, keylen, key);
288 break;
289#endif
290
291#ifdef GNULIB_GC_ARCFOUR
292 case GC_ARCFOUR128:
293 case GC_ARCFOUR40:
294 arcfour_setkey (&ctx->arcfourContext, key, keylen);
295 break;
296#endif
297
298#ifdef GNULIB_GC_DES
299 case GC_DES:
300 if (keylen != 8)
301 return GC_INVALID_CIPHER;
302 gl_des_setkey (&ctx->desContext, key);
303 break;
304#endif
305
306#ifdef GNULIB_GC_RIJNDAEL
307 case GC_AES128:
308 case GC_AES192:
309 case GC_AES256:
310 {
311 rijndael_rc rc;
312 size_t i;
313 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
314
315 for (i = 0; i < keylen; i++)
316 sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
317
318 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
319 keylen * 8, keyMaterial);
320 if (rc < 0)
321 return GC_INVALID_CIPHER;
322
323 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
324 keylen * 8, keyMaterial);
325 if (rc < 0)
326 return GC_INVALID_CIPHER;
327
328 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
329 if (rc < 0)
330 return GC_INVALID_CIPHER;
331 }
332 break;
333#endif
334
335 default:
336 return GC_INVALID_CIPHER;
337 }
338
339 return GC_OK;
340}
341
342Gc_rc gc_cipher_setiv(gc_cipher_handle handle,
343 size_t ivlen,
344 const char *iv)
345{
346 _gc_cipher_ctx *ctx = handle;
347
348 switch (ctx->alg)
349 {
350#ifdef GNULIB_GC_ARCTWO
351 case GC_ARCTWO40:
352 if (ivlen != ARCTWO_BLOCK_SIZE)
353 return GC_INVALID_CIPHER;
354 memcpy (ctx->arctwoIV, iv, ivlen);
355 break;
356#endif
357
358#ifdef GNULIB_GC_RIJNDAEL
359 case GC_AES128:
360 case GC_AES192:
361 case GC_AES256:
362 switch (ctx->mode)
363 {
364 case GC_ECB:
365 /* Doesn't use IV. */
366 break;
367
368 case GC_CBC:
369 {
370 rijndael_rc rc;
371 size_t i;
372 char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1];
373
374 for (i = 0; i < ivlen; i++)
375 sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF);
376
377 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC,
378 ivMaterial);
379 if (rc < 0)
380 return GC_INVALID_CIPHER;
381 }
382 break;
383
384 default:
385 return GC_INVALID_CIPHER;
386 }
387 break;
388#endif
389
390 default:
391 return GC_INVALID_CIPHER;
392 }
393
394 return GC_OK;
395}
396
397Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle,
398 size_t len,
399 char *data)
400{
401 _gc_cipher_ctx *ctx = handle;
402
403 switch (ctx->alg)
404 {
405#ifdef GNULIB_GC_ARCTWO
406 case GC_ARCTWO40:
407 switch (ctx->mode)
408 {
409 case GC_ECB:
410 arctwo_encrypt (&ctx->arctwoContext, data, data, len);
411 break;
412
413 case GC_CBC:
414 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
415 data += ARCTWO_BLOCK_SIZE)
416 {
417 size_t i;
418 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
419 data[i] ^= ctx->arctwoIV[i];
420 arctwo_encrypt (&ctx->arctwoContext, data, data,
421 ARCTWO_BLOCK_SIZE);
422 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
423 }
424 break;
425
426 default:
427 return GC_INVALID_CIPHER;
428 }
429 break;
430#endif
431
432#ifdef GNULIB_GC_ARCFOUR
433 case GC_ARCFOUR128:
434 case GC_ARCFOUR40:
435 arcfour_stream (&ctx->arcfourContext, data, data, len);
436 break;
437#endif
438
439#ifdef GNULIB_GC_DES
440 case GC_DES:
441 for (; len >= 8; len -= 8, data += 8)
442 gl_des_ecb_encrypt (&ctx->desContext, data, data);
443 break;
444#endif
445
446#ifdef GNULIB_GC_RIJNDAEL
447 case GC_AES128:
448 case GC_AES192:
449 case GC_AES256:
450 {
451 int nblocks;
452
453 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
454 data, 8 * len, data);
455 if (nblocks < 0)
456 return GC_INVALID_CIPHER;
457 }
458 break;
459#endif
460
461 default:
462 return GC_INVALID_CIPHER;
463 }
464
465 return GC_OK;
466}
467
468Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle,
469 size_t len,
470 char *data)
471{
472 _gc_cipher_ctx *ctx = handle;
473
474 switch (ctx->alg)
475 {
476#ifdef GNULIB_GC_ARCTWO
477 case GC_ARCTWO40:
478 switch (ctx->mode)
479 {
480 case GC_ECB:
481 arctwo_decrypt (&ctx->arctwoContext, data, data, len);
482 break;
483
484 case GC_CBC:
485 for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE,
486 data += ARCTWO_BLOCK_SIZE)
487 {
488 char tmpIV[ARCTWO_BLOCK_SIZE];
489 size_t i;
490 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
491 arctwo_decrypt (&ctx->arctwoContext, data, data,
492 ARCTWO_BLOCK_SIZE);
493 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
494 data[i] ^= ctx->arctwoIV[i];
495 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
496 }
497 break;
498
499 default:
500 return GC_INVALID_CIPHER;
501 }
502 break;
503#endif
504
505#ifdef GNULIB_GC_ARCFOUR
506 case GC_ARCFOUR128:
507 case GC_ARCFOUR40:
508 arcfour_stream (&ctx->arcfourContext, data, data, len);
509 break;
510#endif
511
512#ifdef GNULIB_GC_DES
513 case GC_DES:
514 for (; len >= 8; len -= 8, data += 8)
515 gl_des_ecb_decrypt (&ctx->desContext, data, data);
516 break;
517#endif
518
519#ifdef GNULIB_GC_RIJNDAEL
520 case GC_AES128:
521 case GC_AES192:
522 case GC_AES256:
523 {
524 int nblocks;
525
526 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
527 data, 8 * len, data);
528 if (nblocks < 0)
529 return GC_INVALID_CIPHER;
530 }
531 break;
532#endif
533
534 default:
535 return GC_INVALID_CIPHER;
536 }
537
538 return GC_OK;
539}
540
541Gc_rc gc_cipher_close(gc_cipher_handle handle)
542{
543 _gc_cipher_ctx *ctx = handle;
544
545 if (ctx)
546 free(ctx);
547
548 return GC_OK;
549}
550
551/* Hashes. */
552
553#define MAX_DIGEST_SIZE 20
554
555typedef struct _gc_hash_ctx
556 {
557 Gc_hash alg;
558 Gc_hash_mode mode;
559 char hash[MAX_DIGEST_SIZE];
560#ifdef GNULIB_GC_MD5
561 struct md5_ctx md5Context;
562#endif
563#ifdef GNULIB_GC_SHA1
564 struct sha1_ctx sha1Context;
565#endif
566 } _gc_hash_ctx;
567
568Gc_rc gc_hash_open(Gc_hash hash,
569 Gc_hash_mode mode,
570 gc_hash_handle * outhandle)
571{
572 _gc_hash_ctx *ctx;
573 Gc_rc rc = GC_OK;
574
575 ctx = calloc(sizeof (*ctx), 1);
576 if (!ctx)
577 return GC_MALLOC_ERROR;
578
579 ctx->alg = hash;
580 ctx->mode = mode;
581
582 switch (hash)
583 {
584#ifdef GNULIB_GC_MD5
585 case GC_MD5:
586 md5_init_ctx (&ctx->md5Context);
587 break;
588#endif
589
590#ifdef GNULIB_GC_SHA1
591 case GC_SHA1:
592 sha1_init_ctx (&ctx->sha1Context);
593 break;
594#endif
595
596 default:
597 rc = GC_INVALID_HASH;
598 break;
599 }
600
601 switch (mode)
602 {
603 case 0:
604 break;
605
606 default:
607 rc = GC_INVALID_HASH;
608 break;
609 }
610
611 if (rc == GC_OK)
612 *outhandle = ctx;
613 else
614 free(ctx);
615
616 return rc;
617}
618
619Gc_rc gc_hash_clone(gc_hash_handle handle,
620 gc_hash_handle * outhandle)
621{
622 _gc_hash_ctx *in = handle;
623 _gc_hash_ctx *out;
624
625 *outhandle = out = calloc(sizeof (*out), 1);
626 if (!out)
627 return GC_MALLOC_ERROR;
628
629 memcpy(out, in, sizeof (*out));
630
631 return GC_OK;
632}
633
634size_t gc_hash_digest_length(Gc_hash hash)
635{
636 size_t len;
637
638 switch (hash)
639 {
640 case GC_MD2:
641 len = GC_MD2_DIGEST_SIZE;
642 break;
643
644 case GC_MD4:
645 len = GC_MD4_DIGEST_SIZE;
646 break;
647
648 case GC_MD5:
649 len = GC_MD5_DIGEST_SIZE;
650 break;
651
652 case GC_RMD160:
653 len = GC_RMD160_DIGEST_SIZE;
654 break;
655
656 case GC_SHA1:
657 len = GC_SHA1_DIGEST_SIZE;
658 break;
659
660 default:
661 return 0;
662 }
663
664 return len;
665}
666
667void gc_hash_write(gc_hash_handle handle,
668 size_t len,
669 const char *data)
670{
671 _gc_hash_ctx *ctx = handle;
672
673 switch (ctx->alg)
674 {
675#ifdef GNULIB_GC_MD5
676 case GC_MD5:
677 md5_process_bytes (data, len, &ctx->md5Context);
678 break;
679#endif
680
681#ifdef GNULIB_GC_SHA1
682 case GC_SHA1:
683 sha1_process_bytes (data, len, &ctx->sha1Context);
684 break;
685#endif
686
687 default:
688 break;
689 }
690}
691
692const char * gc_hash_read(gc_hash_handle handle)
693{
694 _gc_hash_ctx *ctx = handle;
695 const char *ret= NULL;
696
697 switch (ctx->alg)
698 {
699#ifdef GNULIB_GC_MD5
700 case GC_MD5:
701 md5_finish_ctx (&ctx->md5Context, ctx->hash);
702 ret = ctx->hash;
703 break;
704#endif
705
706#ifdef GNULIB_GC_SHA1
707 case GC_SHA1:
708 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
709 ret = ctx->hash;
710 break;
711#endif
712
713 default:
714 return NULL;
715 }
716
717 return ret;
718}
719
720void gc_hash_close(gc_hash_handle handle)
721{
722 _gc_hash_ctx *ctx = handle;
723
724 free(ctx);
725}
726
727Gc_rc gc_hash_buffer(Gc_hash hash,
728 const void *in,
729 size_t inlen,
730 char *resbuf)
731{
732 switch (hash)
733 {
734#ifdef GNULIB_GC_MD5
735 case GC_MD5:
736 md5_buffer (in, inlen, resbuf);
737 break;
738#endif
739
740#ifdef GNULIB_GC_SHA1
741 case GC_SHA1:
742 sha1_buffer (in, inlen, resbuf);
743 break;
744#endif
745
746 default:
747 return GC_INVALID_HASH;
748 }
749
750 return GC_OK;
751}
752
753#ifdef GNULIB_GC_MD5
754Gc_rc
755gc_md5 (const void *in, size_t inlen, void *resbuf)
756 {
757 md5_buffer (in, inlen, resbuf);
758 return GC_OK;
759 }
760#endif
761
762#ifdef GNULIB_GC_SHA1
763Gc_rc
764gc_sha1 (const void *in, size_t inlen, void *resbuf)
765 {
766 sha1_buffer (in, inlen, resbuf);
767 return GC_OK;
768 }
769#endif
770
771#ifdef GNULIB_GC_HMAC_MD5
772Gc_rc
773gc_hmac_md5 (const void *key, size_t keylen,
774 const void *in, size_t inlen, char *resbuf)
775 {
776 hmac_md5 (key, keylen, in, inlen, resbuf);
777 return GC_OK;
778 }
779#endif
780
781#ifdef GNULIB_GC_HMAC_SHA1
782Gc_rc gc_hmac_sha1(const void *key,
783 size_t keylen,
784 const void *in,
785 size_t inlen,
786 char *resbuf)
787{
788 hmac_sha1(key, keylen, in, inlen, resbuf);
789 return GC_OK;
790}
791#endif
diff --git a/src/daemon/https/lgl/gc-libgcrypt.c b/src/daemon/https/lgl/gc-libgcrypt.c
new file mode 100644
index 00000000..da9c5e16
--- /dev/null
+++ b/src/daemon/https/lgl/gc-libgcrypt.c
@@ -0,0 +1,627 @@
1/* gc-libgcrypt.c --- Crypto wrappers around Libgcrypt for GC.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Note: This file is only built if GC uses Libgcrypt. */
22
23#include "config.h"
24
25/* Get prototype. */
26#include "gc.h"
27
28#include <stdlib.h>
29#include <string.h>
30
31/* Get libgcrypt API. */
32#include <gcrypt.h>
33
34#include <assert.h>
35
36/* Initialization. */
37
38Gc_rc gc_init(void)
39{
40 gcry_error_t err;
41
42 err = gcry_control(GCRYCTL_ANY_INITIALIZATION_P);
43 if (err == GPG_ERR_NO_ERROR)
44 {
45 if (gcry_check_version(GCRYPT_VERSION) == NULL)
46 return GC_INIT_ERROR;
47
48 err = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
49 if (err != GPG_ERR_NO_ERROR)
50 return GC_INIT_ERROR;
51 }
52 return GC_OK;
53}
54
55void gc_done(void)
56{
57 return;
58}
59
60#ifdef GNULIB_GC_RANDOM
61
62/* Randomness. */
63
64Gc_rc
65gc_nonce (char *data, size_t datalen)
66 {
67 gcry_create_nonce ((unsigned char *) data, datalen);
68 return GC_OK;
69 }
70
71Gc_rc
72gc_pseudo_random (char *data, size_t datalen)
73 {
74 gcry_randomize ((unsigned char *) data, datalen, GCRY_STRONG_RANDOM);
75 return GC_OK;
76 }
77
78Gc_rc
79gc_random (char *data, size_t datalen)
80 {
81 gcry_randomize ((unsigned char *) data, datalen, GCRY_VERY_STRONG_RANDOM);
82 return GC_OK;
83 }
84
85#endif
86
87/* Memory allocation. */
88
89void gc_set_allocators(gc_malloc_t func_malloc,
90 gc_malloc_t secure_malloc,
91 gc_secure_check_t secure_check,
92 gc_realloc_t func_realloc,
93 gc_free_t func_free)
94{
95 gcry_set_allocation_handler(func_malloc, secure_malloc, secure_check,
96 func_realloc, func_free);
97}
98
99/* Ciphers. */
100
101Gc_rc gc_cipher_open(Gc_cipher alg,
102 Gc_cipher_mode mode,
103 gc_cipher_handle * outhandle)
104{
105 int gcryalg, gcrymode;
106 gcry_error_t err;
107
108 switch (alg)
109 {
110 case GC_AES128:
111 gcryalg = GCRY_CIPHER_RIJNDAEL;
112 break;
113
114 case GC_AES192:
115 gcryalg = GCRY_CIPHER_RIJNDAEL;
116 break;
117
118 case GC_AES256:
119 gcryalg = GCRY_CIPHER_RIJNDAEL256;
120 break;
121
122 case GC_3DES:
123 gcryalg = GCRY_CIPHER_3DES;
124 break;
125
126 case GC_DES:
127 gcryalg = GCRY_CIPHER_DES;
128 break;
129
130 case GC_ARCFOUR128:
131 case GC_ARCFOUR40:
132 gcryalg = GCRY_CIPHER_ARCFOUR;
133 break;
134
135 case GC_ARCTWO40:
136 gcryalg = GCRY_CIPHER_RFC2268_40;
137 break;
138
139#ifdef ENABLE_CAMELLIA
140 case GC_CAMELLIA128:
141 gcryalg = GCRY_CIPHER_CAMELLIA128;
142 break;
143
144 case GC_CAMELLIA256:
145 gcryalg = GCRY_CIPHER_CAMELLIA256;
146 break;
147#endif
148
149 default:
150 return GC_INVALID_CIPHER;
151 }
152
153 switch (mode)
154 {
155 case GC_ECB:
156 gcrymode = GCRY_CIPHER_MODE_ECB;
157 break;
158
159 case GC_CBC:
160 gcrymode = GCRY_CIPHER_MODE_CBC;
161 break;
162
163 case GC_STREAM:
164 gcrymode = GCRY_CIPHER_MODE_STREAM;
165 break;
166
167 default:
168 return GC_INVALID_CIPHER;
169 }
170
171 err = gcry_cipher_open((gcry_cipher_hd_t *) outhandle, gcryalg, gcrymode, 0);
172 if (gcry_err_code(err))
173 return GC_INVALID_CIPHER;
174
175 return GC_OK;
176}
177
178Gc_rc gc_cipher_setkey(gc_cipher_handle handle,
179 size_t keylen,
180 const char *key)
181{
182 gcry_error_t err;
183
184 err = gcry_cipher_setkey ((gcry_cipher_hd_t) handle, key, keylen);
185 if (gcry_err_code(err))
186 return GC_INVALID_CIPHER;
187
188 return GC_OK;
189}
190
191Gc_rc gc_cipher_setiv(gc_cipher_handle handle,
192 size_t ivlen,
193 const char *iv)
194{
195 gcry_error_t err;
196
197 err = gcry_cipher_setiv ((gcry_cipher_hd_t) handle, iv, ivlen);
198 if (gcry_err_code(err))
199 return GC_INVALID_CIPHER;
200
201 return GC_OK;
202}
203
204Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle,
205 size_t len,
206 char *data)
207{
208 if (gcry_cipher_encrypt((gcry_cipher_hd_t) handle, data, len, NULL, len) != 0)
209 return GC_INVALID_CIPHER;
210
211 return GC_OK;
212}
213
214Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle,
215 size_t len,
216 char *data)
217{
218 if (gcry_cipher_decrypt((gcry_cipher_hd_t) handle, data, len, NULL, len) != 0)
219 return GC_INVALID_CIPHER;
220
221 return GC_OK;
222}
223
224Gc_rc gc_cipher_close(gc_cipher_handle handle)
225{
226 gcry_cipher_close(handle);
227
228 return GC_OK;
229}
230
231/* Hashes. */
232
233typedef struct _gc_hash_ctx
234 {
235 Gc_hash alg;
236 Gc_hash_mode mode;
237 gcry_md_hd_t gch;
238 } _gc_hash_ctx;
239
240Gc_rc gc_hash_open(Gc_hash hash,
241 Gc_hash_mode mode,
242 gc_hash_handle * outhandle)
243{
244 _gc_hash_ctx *ctx;
245 int gcryalg = 0, gcrymode = 0;
246 gcry_error_t err;
247 Gc_rc rc = GC_OK;
248
249 ctx = calloc(sizeof (*ctx), 1);
250 if (!ctx)
251 return GC_MALLOC_ERROR;
252
253 ctx->alg = hash;
254 ctx->mode = mode;
255
256 switch (hash)
257 {
258 case GC_MD2:
259 gcryalg = GCRY_MD_NONE;
260 break;
261
262 case GC_MD4:
263 gcryalg = GCRY_MD_MD4;
264 break;
265
266 case GC_MD5:
267 gcryalg = GCRY_MD_MD5;
268 break;
269
270 case GC_SHA1:
271 gcryalg = GCRY_MD_SHA1;
272 break;
273
274 case GC_SHA256:
275 gcryalg = GCRY_MD_SHA256;
276 break;
277
278 case GC_SHA384:
279 gcryalg = GCRY_MD_SHA384;
280 break;
281
282 case GC_SHA512:
283 gcryalg = GCRY_MD_SHA512;
284 break;
285
286 case GC_RMD160:
287 gcryalg = GCRY_MD_RMD160;
288 break;
289
290 default:
291 rc = GC_INVALID_HASH;
292 }
293
294 switch (mode)
295 {
296 case 0:
297 gcrymode = 0;
298 break;
299
300 case GC_HMAC:
301 gcrymode = GCRY_MD_FLAG_HMAC;
302 break;
303
304 default:
305 rc = GC_INVALID_HASH;
306 }
307
308 if (rc == GC_OK && gcryalg != GCRY_MD_NONE)
309 {
310 err = gcry_md_open(&ctx->gch, gcryalg, gcrymode);
311 if (gcry_err_code(err))
312 rc = GC_INVALID_HASH;
313 }
314
315 if (rc == GC_OK)
316 *outhandle = ctx;
317 else
318 free(ctx);
319
320 return rc;
321}
322
323Gc_rc gc_hash_clone(gc_hash_handle handle,
324 gc_hash_handle * outhandle)
325{
326 _gc_hash_ctx *in = handle;
327 _gc_hash_ctx *out;
328 int err;
329
330 *outhandle = out = calloc(sizeof (*out), 1);
331 if (!out)
332 return GC_MALLOC_ERROR;
333
334 memcpy(out, in, sizeof (*out));
335
336 err = gcry_md_copy(&out->gch, in->gch);
337 if (err)
338 {
339 free(out);
340 return GC_INVALID_HASH;
341 }
342
343 return GC_OK;
344}
345
346size_t gc_hash_digest_length(Gc_hash hash)
347{
348 size_t len;
349
350 switch (hash)
351 {
352 case GC_MD2:
353 len = GC_MD2_DIGEST_SIZE;
354 break;
355
356 case GC_MD4:
357 len = GC_MD4_DIGEST_SIZE;
358 break;
359
360 case GC_MD5:
361 len = GC_MD5_DIGEST_SIZE;
362 break;
363
364 case GC_RMD160:
365 len = GC_RMD160_DIGEST_SIZE;
366 break;
367
368 case GC_SHA1:
369 len = GC_SHA1_DIGEST_SIZE;
370 break;
371
372 case GC_SHA256:
373 len = GC_SHA256_DIGEST_SIZE;
374 break;
375
376 case GC_SHA384:
377 len = GC_SHA384_DIGEST_SIZE;
378 break;
379
380 case GC_SHA512:
381 len = GC_SHA512_DIGEST_SIZE;
382 break;
383
384 default:
385 return 0;
386 }
387
388 return len;
389}
390
391void gc_hash_hmac_setkey(gc_hash_handle handle,
392 size_t len,
393 const char *key)
394{
395 _gc_hash_ctx *ctx = handle;
396 gcry_md_setkey(ctx->gch, key, len);
397}
398
399void gc_hash_write(gc_hash_handle handle,
400 size_t len,
401 const char *data)
402{
403 _gc_hash_ctx *ctx = handle;
404 gcry_md_write(ctx->gch, data, len);
405}
406
407const char * gc_hash_read(gc_hash_handle handle)
408{
409 _gc_hash_ctx *ctx = handle;
410 const char *digest;
411 {
412 gcry_md_final (ctx->gch);
413 digest = gcry_md_read(ctx->gch, 0);
414 }
415
416 return digest;
417}
418
419void gc_hash_close(gc_hash_handle handle)
420{
421 _gc_hash_ctx *ctx = handle;
422
423 gcry_md_close(ctx->gch);
424
425 free(ctx);
426}
427
428Gc_rc gc_hash_buffer(Gc_hash hash,
429 const void *in,
430 size_t inlen,
431 char *resbuf)
432{
433 int gcryalg;
434
435 switch (hash)
436 {
437#ifdef GNULIB_GC_MD5
438 case GC_MD5:
439 gcryalg = GCRY_MD_MD5;
440 break;
441#endif
442
443#ifdef GNULIB_GC_SHA1
444 case GC_SHA1:
445 gcryalg = GCRY_MD_SHA1;
446 break;
447#endif
448
449#ifdef GNULIB_GC_SHA256
450 case GC_SHA256:
451 gcryalg = GCRY_MD_SHA256;
452 break;
453#endif
454
455#ifdef GNULIB_GC_SHA384
456 case GC_SHA384:
457 gcryalg = GCRY_MD_SHA384;
458 break;
459#endif
460
461#ifdef GNULIB_GC_SHA512
462 case GC_SHA512:
463 gcryalg = GCRY_MD_SHA512;
464 break;
465#endif
466
467#ifdef GNULIB_GC_RMD160
468 case GC_RMD160:
469 gcryalg = GCRY_MD_RMD160;
470 break;
471#endif
472
473 default:
474 return GC_INVALID_HASH;
475 }
476
477 gcry_md_hash_buffer(gcryalg, resbuf, in, inlen);
478
479 return GC_OK;
480}
481
482/* One-call interface. */
483#ifdef GNULIB_GC_MD5
484Gc_rc
485gc_md5 (const void *in, size_t inlen, void *resbuf)
486 {
487 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
488 gcry_md_hd_t hd;
489 gpg_error_t err;
490 unsigned char *p;
491
492 assert (outlen == GC_MD5_DIGEST_SIZE);
493
494 err = gcry_md_open (&hd, GCRY_MD_MD5, 0);
495 if (err != GPG_ERR_NO_ERROR)
496 return GC_INVALID_HASH;
497
498 gcry_md_write (hd, in, inlen);
499
500 p = gcry_md_read (hd, GCRY_MD_MD5);
501 if (p == NULL)
502 {
503 gcry_md_close (hd);
504 return GC_INVALID_HASH;
505 }
506
507 memcpy (resbuf, p, outlen);
508
509 gcry_md_close (hd);
510
511 return GC_OK;
512 }
513#endif
514
515#ifdef GNULIB_GC_SHA1
516Gc_rc
517gc_sha1 (const void *in, size_t inlen, void *resbuf)
518 {
519 size_t outlen = gcry_md_get_algo_dlen (GCRY_MD_SHA1);
520 gcry_md_hd_t hd;
521 gpg_error_t err;
522 unsigned char *p;
523
524 assert (outlen == GC_SHA1_DIGEST_SIZE);
525
526 err = gcry_md_open (&hd, GCRY_MD_SHA1, 0);
527 if (err != GPG_ERR_NO_ERROR)
528 return GC_INVALID_HASH;
529
530 gcry_md_write (hd, in, inlen);
531
532 p = gcry_md_read (hd, GCRY_MD_SHA1);
533 if (p == NULL)
534 {
535 gcry_md_close (hd);
536 return GC_INVALID_HASH;
537 }
538
539 memcpy (resbuf, p, outlen);
540
541 gcry_md_close (hd);
542
543 return GC_OK;
544 }
545#endif
546
547#ifdef GNULIB_GC_HMAC_MD5
548Gc_rc
549gc_hmac_md5 (const void *key, size_t keylen,
550 const void *in, size_t inlen, char *resbuf)
551 {
552 size_t hlen = gcry_md_get_algo_dlen (GCRY_MD_MD5);
553 gcry_md_hd_t mdh;
554 unsigned char *hash;
555 gpg_error_t err;
556
557 assert (hlen == 16);
558
559 err = gcry_md_open (&mdh, GCRY_MD_MD5, GCRY_MD_FLAG_HMAC);
560 if (err != GPG_ERR_NO_ERROR)
561 return GC_INVALID_HASH;
562
563 err = gcry_md_setkey (mdh, key, keylen);
564 if (err != GPG_ERR_NO_ERROR)
565 {
566 gcry_md_close (mdh);
567 return GC_INVALID_HASH;
568 }
569
570 gcry_md_write (mdh, in, inlen);
571
572 hash = gcry_md_read (mdh, GCRY_MD_MD5);
573 if (hash == NULL)
574 {
575 gcry_md_close (mdh);
576 return GC_INVALID_HASH;
577 }
578
579 memcpy (resbuf, hash, hlen);
580
581 gcry_md_close (mdh);
582
583 return GC_OK;
584 }
585#endif
586
587#ifdef GNULIB_GC_HMAC_SHA1
588Gc_rc gc_hmac_sha1(const void *key,
589 size_t keylen,
590 const void *in,
591 size_t inlen,
592 char *resbuf)
593{
594 size_t hlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
595 gcry_md_hd_t mdh;
596 unsigned char *hash;
597 gpg_error_t err;
598
599 assert (hlen == GC_SHA1_DIGEST_SIZE);
600
601 err = gcry_md_open(&mdh, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
602 if (err != GPG_ERR_NO_ERROR)
603 return GC_INVALID_HASH;
604
605 err = gcry_md_setkey(mdh, key, keylen);
606 if (err != GPG_ERR_NO_ERROR)
607 {
608 gcry_md_close(mdh);
609 return GC_INVALID_HASH;
610 }
611
612 gcry_md_write(mdh, in, inlen);
613
614 hash = gcry_md_read(mdh, GCRY_MD_SHA1);
615 if (hash == NULL)
616 {
617 gcry_md_close(mdh);
618 return GC_INVALID_HASH;
619 }
620
621 memcpy(resbuf, hash, hlen);
622
623 gcry_md_close(mdh);
624
625 return GC_OK;
626}
627#endif
diff --git a/src/daemon/https/lgl/gc-pbkdf2-sha1.c b/src/daemon/https/lgl/gc-pbkdf2-sha1.c
new file mode 100644
index 00000000..f5daf7b7
--- /dev/null
+++ b/src/daemon/https/lgl/gc-pbkdf2-sha1.c
@@ -0,0 +1,185 @@
1/* gc-pbkdf2-sha1.c --- Password-Based Key Derivation Function a'la PKCS#5
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. The comments in this file are taken
19 from RFC 2898. */
20
21#include <config.h>
22
23#include "gc.h"
24
25#include <stdlib.h>
26#include <string.h>
27
28/*
29 * 5.2 PBKDF2
30 *
31 * PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
32 * example) to derive keys. The length of the derived key is essentially
33 * unbounded. (However, the maximum effective search space for the
34 * derived key may be limited by the structure of the underlying
35 * pseudorandom function. See Appendix B.1 for further discussion.)
36 * PBKDF2 is recommended for new applications.
37 *
38 * PBKDF2 (P, S, c, dkLen)
39 *
40 * Options: PRF underlying pseudorandom function (hLen
41 * denotes the length in octets of the
42 * pseudorandom function output)
43 *
44 * Input: P password, an octet string (ASCII or UTF-8)
45 * S salt, an octet string
46 * c iteration count, a positive integer
47 * dkLen intended length in octets of the derived
48 * key, a positive integer, at most
49 * (2^32 - 1) * hLen
50 *
51 * Output: DK derived key, a dkLen-octet string
52 */
53
54Gc_rc
55gc_pbkdf2_sha1 (const char *P, size_t Plen,
56 const char *S, size_t Slen,
57 unsigned int c, char *DK, size_t dkLen)
58{
59 unsigned int hLen = 20;
60 char U[20];
61 char T[20];
62 unsigned int u;
63 unsigned int l;
64 unsigned int r;
65 unsigned int i;
66 unsigned int k;
67 int rc;
68 char *tmp;
69 size_t tmplen = Slen + 4;
70
71 if (c == 0)
72 return GC_PKCS5_INVALID_ITERATION_COUNT;
73
74 if (dkLen == 0)
75 return GC_PKCS5_INVALID_DERIVED_KEY_LENGTH;
76
77 /*
78 *
79 * Steps:
80 *
81 * 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
82 * stop.
83 */
84
85 if (dkLen > 4294967295U)
86 return GC_PKCS5_DERIVED_KEY_TOO_LONG;
87
88 /*
89 * 2. Let l be the number of hLen-octet blocks in the derived key,
90 * rounding up, and let r be the number of octets in the last
91 * block:
92 *
93 * l = CEIL (dkLen / hLen) ,
94 * r = dkLen - (l - 1) * hLen .
95 *
96 * Here, CEIL (x) is the "ceiling" function, i.e. the smallest
97 * integer greater than, or equal to, x.
98 */
99
100 l = ((dkLen - 1) / hLen) + 1;
101 r = dkLen - (l - 1) * hLen;
102
103 /*
104 * 3. For each block of the derived key apply the function F defined
105 * below to the password P, the salt S, the iteration count c, and
106 * the block index to compute the block:
107 *
108 * T_1 = F (P, S, c, 1) ,
109 * T_2 = F (P, S, c, 2) ,
110 * ...
111 * T_l = F (P, S, c, l) ,
112 *
113 * where the function F is defined as the exclusive-or sum of the
114 * first c iterates of the underlying pseudorandom function PRF
115 * applied to the password P and the concatenation of the salt S
116 * and the block index i:
117 *
118 * F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
119 *
120 * where
121 *
122 * U_1 = PRF (P, S || INT (i)) ,
123 * U_2 = PRF (P, U_1) ,
124 * ...
125 * U_c = PRF (P, U_{c-1}) .
126 *
127 * Here, INT (i) is a four-octet encoding of the integer i, most
128 * significant octet first.
129 *
130 * 4. Concatenate the blocks and extract the first dkLen octets to
131 * produce a derived key DK:
132 *
133 * DK = T_1 || T_2 || ... || T_l<0..r-1>
134 *
135 * 5. Output the derived key DK.
136 *
137 * Note. The construction of the function F follows a "belt-and-
138 * suspenders" approach. The iterates U_i are computed recursively to
139 * remove a degree of parallelism from an opponent; they are exclusive-
140 * ored together to reduce concerns about the recursion degenerating
141 * into a small set of values.
142 *
143 */
144
145 tmp = malloc (tmplen);
146 if (tmp == NULL)
147 return GC_MALLOC_ERROR;
148
149 memcpy (tmp, S, Slen);
150
151 for (i = 1; i <= l; i++)
152 {
153 memset (T, 0, hLen);
154
155 for (u = 1; u <= c; u++)
156 {
157 if (u == 1)
158 {
159 tmp[Slen + 0] = (i & 0xff000000) >> 24;
160 tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
161 tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
162 tmp[Slen + 3] = (i & 0x000000ff) >> 0;
163
164 rc = gc_hmac_sha1 (P, Plen, tmp, tmplen, U);
165 }
166 else
167 rc = gc_hmac_sha1 (P, Plen, U, hLen, U);
168
169 if (rc != GC_OK)
170 {
171 free (tmp);
172 return rc;
173 }
174
175 for (k = 0; k < hLen; k++)
176 T[k] ^= U[k];
177 }
178
179 memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
180 }
181
182 free (tmp);
183
184 return GC_OK;
185}
diff --git a/src/daemon/https/lgl/gc.h b/src/daemon/https/lgl/gc.h
new file mode 100644
index 00000000..688e624a
--- /dev/null
+++ b/src/daemon/https/lgl/gc.h
@@ -0,0 +1,347 @@
1/* gc.h --- Header file for implementation agnostic crypto wrapper API.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2007 Simon Josefsson
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21#ifndef GC_H
22#define GC_H
23
24/* Get size_t. */
25# include <stddef.h>
26
27enum Gc_rc
28 {
29 GC_OK = 0,
30 GC_MALLOC_ERROR,
31 GC_INIT_ERROR,
32 GC_RANDOM_ERROR,
33 GC_INVALID_CIPHER,
34 GC_INVALID_HASH,
35 GC_PKCS5_INVALID_ITERATION_COUNT,
36 GC_PKCS5_INVALID_DERIVED_KEY_LENGTH,
37 GC_PKCS5_DERIVED_KEY_TOO_LONG
38 };
39typedef enum Gc_rc Gc_rc;
40
41/* Hash types. */
42enum Gc_hash
43 {
44 GC_MD4,
45 GC_MD5,
46 GC_SHA1,
47 GC_MD2,
48 GC_RMD160,
49 GC_SHA256,
50 GC_SHA384,
51 GC_SHA512
52 };
53typedef enum Gc_hash Gc_hash;
54
55enum Gc_hash_mode
56 {
57 GC_HMAC = 1
58 };
59typedef enum Gc_hash_mode Gc_hash_mode;
60
61typedef void *gc_hash_handle;
62
63#define GC_MD2_DIGEST_SIZE 16
64#define GC_MD4_DIGEST_SIZE 16
65#define GC_MD5_DIGEST_SIZE 16
66#define GC_RMD160_DIGEST_SIZE 20
67#define GC_SHA1_DIGEST_SIZE 20
68#define GC_SHA256_DIGEST_SIZE 32
69#define GC_SHA384_DIGEST_SIZE 48
70#define GC_SHA512_DIGEST_SIZE 64
71
72/* Cipher types. */
73enum Gc_cipher
74 {
75 GC_AES128,
76 GC_AES192,
77 GC_AES256,
78 GC_3DES,
79 GC_DES,
80 GC_ARCFOUR128,
81 GC_ARCFOUR40,
82 GC_ARCTWO40,
83 GC_CAMELLIA128,
84 GC_CAMELLIA256
85 };
86typedef enum Gc_cipher Gc_cipher;
87
88enum Gc_cipher_mode
89 {
90 GC_ECB,
91 GC_CBC,
92 GC_STREAM
93 };
94typedef enum Gc_cipher_mode Gc_cipher_mode;
95
96typedef void * gc_cipher_handle;
97
98/* Call before respectively after any other functions. */
99Gc_rc gc_init(void);
100void gc_done(void);
101
102/* Memory allocation (avoid). */
103typedef void *(*gc_malloc_t)(size_t n);
104typedef int (*gc_secure_check_t)(const void *);
105typedef void *(*gc_realloc_t)(void *p,
106 size_t n);
107typedef void (*gc_free_t)(void *);
108void gc_set_allocators(gc_malloc_t func_malloc,
109 gc_malloc_t secure_malloc,
110 gc_secure_check_t secure_check,
111 gc_realloc_t func_realloc,
112 gc_free_t func_free);
113
114/* Randomness. */
115Gc_rc gc_nonce(char *data,
116 size_t datalen);
117Gc_rc gc_pseudo_random(char *data,
118 size_t datalen);
119Gc_rc gc_random(char *data,
120 size_t datalen);
121
122/* Ciphers. */
123Gc_rc gc_cipher_open(Gc_cipher cipher,
124 Gc_cipher_mode mode,
125 gc_cipher_handle *outhandle);
126Gc_rc gc_cipher_setkey(gc_cipher_handle handle,
127 size_t keylen,
128 const char *key);
129Gc_rc gc_cipher_setiv(gc_cipher_handle handle,
130 size_t ivlen,
131 const char *iv);
132Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle,
133 size_t len,
134 char *data);
135Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle,
136 size_t len,
137 char *data);
138Gc_rc gc_cipher_close(gc_cipher_handle handle);
139
140/* Hashes. */
141
142Gc_rc gc_hash_open(Gc_hash hash,
143 Gc_hash_mode mode,
144 gc_hash_handle *outhandle);
145Gc_rc gc_hash_clone(gc_hash_handle handle,
146 gc_hash_handle *outhandle);
147size_t gc_hash_digest_length(Gc_hash hash);
148void gc_hash_hmac_setkey(gc_hash_handle handle,
149 size_t len,
150 const char *key);
151void gc_hash_write(gc_hash_handle handle,
152 size_t len,
153 const char *data);
154const char *gc_hash_read(gc_hash_handle handle);
155void gc_hash_close(gc_hash_handle handle);
156
157/* Compute a hash value over buffer IN of INLEN bytes size using the
158 algorithm HASH, placing the result in the pre-allocated buffer OUT.
159 The required size of OUT depends on HASH, and is generally
160 GC_<HASH>_DIGEST_SIZE. For example, for GC_MD5 the output buffer
161 must be 16 bytes. The return value is 0 (GC_OK) on success, or
162 another Gc_rc error code. */
163Gc_rc gc_hash_buffer(Gc_hash hash,
164 const void *in,
165 size_t inlen,
166 char *out);
167
168/* One-call interface. */
169Gc_rc gc_md2(const void *in,
170 size_t inlen,
171 void *resbuf);
172Gc_rc gc_md4(const void *in,
173 size_t inlen,
174 void *resbuf);
175Gc_rc gc_md5(const void *in,
176 size_t inlen,
177 void *resbuf);
178Gc_rc gc_sha1(const void *in,
179 size_t inlen,
180 void *resbuf);
181Gc_rc gc_hmac_md5(const void *key,
182 size_t keylen,
183 const void *in,
184 size_t inlen,
185 char *resbuf);
186Gc_rc gc_hmac_sha1(const void *key,
187 size_t keylen,
188 const void *in,
189 size_t inlen,
190 char *resbuf);
191
192/* Derive cryptographic keys from a password P of length PLEN, with
193 salt S of length SLEN, placing the result in pre-allocated buffer
194 DK of length DKLEN. An iteration count is specified in C, where a
195 larger value means this function take more time (typical iteration
196 counts are 1000-20000). This function "stretches" the key to be
197 exactly dkLen bytes long. GC_OK is returned on success, otherwise
198 an Gc_rc error code is returned. */
199Gc_rc gc_pbkdf2_sha1(const char *P,
200 size_t Plen,
201 const char *S,
202 size_t Slen,
203 unsigned int c,
204 char *DK,
205 size_t dkLen);
206
207/*
208 TODO:
209
210 From: Simon Josefsson <jas@extundo.com>
211 Subject: Re: generic crypto
212 Newsgroups: gmane.comp.lib.gnulib.bugs
213 Cc: bug-gnulib@gnu.org
214 Date: Fri, 07 Oct 2005 12:50:57 +0200
215 Mail-Copies-To: nobody
216
217 Paul Eggert <eggert@CS.UCLA.EDU> writes:
218
219 > Simon Josefsson <jas@extundo.com> writes:
220 >
221 >> * Perhaps the /dev/?random reading should be separated into a separate
222 >> module? It might be useful outside of the gc layer too.
223 >
224 > Absolutely. I've been meaning to do that for months (for a "shuffle"
225 > program I want to add to coreutils), but hadn't gotten around to it.
226 > It would have to be generalized a bit. I'd like to have the file
227 > descriptor cached, for example.
228
229 I'll write a separate module for that part.
230
231 I think we should even add a good PRNG that is re-seeded from
232 /dev/?random frequently. GnuTLS can need a lot of random data on a
233 big server, more than /dev/random can supply. And /dev/urandom might
234 not be strong enough. Further, the security of /dev/?random can also
235 be questionable.
236
237 >> I'm also not sure about the names of those functions, they suggest
238 >> a more higher-level API than what is really offered (i.e., the
239 >> names "nonce" and "pseudo_random" and "random" imply certain
240 >> cryptographic properties).
241 >
242 > Could you expand a bit more on that? What is the relationship between
243 > nonce/pseudorandom/random and the /dev/ values you are using?
244
245 There is none, that is the problem.
246
247 Applications generally need different kind of "random" numbers.
248 Sometimes they just need some random data and doesn't care whether it
249 is possible for an attacker to compute the string (aka a "nonce").
250 Sometimes they need data that is very difficult to compute (i.e.,
251 computing it require inverting SHA1 or similar). Sometimes they need
252 data that is not possible to compute, i.e., it wants real entropy
253 collected over time on the system. Collecting the last kind of random
254 data is very expensive, so it must not be used too often. The second
255 kind of random data ("pseudo random") is typically generated by
256 seeding a good PRNG with a couple of hundred bytes of real entropy
257 from the "real random" data pool. The "nonce" is usually computed
258 using the PRNG as well, because PRNGs are usually fast.
259
260 Pseudo-random data is typically used for session keys. Strong random
261 data is often used to generate long-term keys (e.g., private RSA
262 keys).
263
264 Of course, there are many subtleties. There are several different
265 kind of nonce:s. Sometimes a nonce is just an ever-increasing
266 integer, starting from 0. Sometimes it is assumed to be unlikely to
267 be the same as previous nonces, but without a requirement that the
268 nonce is possible to guess. MD5(system clock) would thus suffice, if
269 it isn't called too often. You can guess what the next value will be,
270 but it will always be different.
271
272 The problem is that /dev/?random doesn't offer any kind of semantic
273 guarantees. But applications need an API that make that promise.
274
275 I think we should do this in several steps:
276
277 1) Write a module that can read from /dev/?random.
278
279 2) Add a module for a known-good PRNG suitable for random number
280 generation, that can be continuously re-seeded.
281
282 3) Add a high-level module that provide various different randomness
283 functions. One for nonces, perhaps even different kind of nonces,
284 one for pseudo random data, and one for strong random data. It is
285 not clear whether we can hope to achieve the last one in a portable
286 way.
287
288 Further, it would be useful to allow users to provide their own
289 entropy source as a file, used to seed the PRNG or initialize the
290 strong randomness pool. This is used on embedded platforms that
291 doesn't have enough interrupts to hope to generate good random data.
292
293 > For example, why not use OpenBSD's /dev/arandom?
294
295 I don't trust ARC4. For example, recent cryptographic efforts
296 indicate that you must throw away the first 512 bytes generated from
297 the PRNG for it to be secure. I don't know whether OpenBSD do this.
298 Further, I recall some eprint paper on RC4 security that didn't
299 inspire confidence.
300
301 While I trust the random devices in OpenBSD more than
302 Solaris/AIX/HPUX/etc, I think that since we need something better on
303 Solaris/AIX/HPUX we'd might as well use it on OpenBSD or even Linux
304 too.
305
306 > Here is one thought. The user could specify a desired quality level
307 > range, and the implementation then would supply random data that is at
308 > least as good as the lower bound of the range. I.e., ihe
309 > implementation refuses to produce any random data if it can't generate
310 > data that is at least as good as the lower end of the range. The
311 > upper bound of the range is advice from the user not to be any more
312 > expensive than that, but the implementation can ignore the advice if
313 > it doesn't have anything cheaper.
314
315 I'm not sure this is a good idea. Users can't really be expected to
316 understand this. Further, applications need many different kind of
317 random data. Selecting the randomness level for each by the user will
318 be too complicated.
319
320 I think it is better if the application decide, from its cryptographic
321 requirement, what entropy quality it require, and call the proper API.
322 Meeting the implied semantic properties should be the job for gnulib.
323
324 >> Perhaps gc_dev_random and gc_dev_urandom?
325 >
326 > To some extent. I'd rather insulate the user from the details of
327 > where the random numbers come from. On the other hand we need to
328 > provide a way for applications to specify a file that contains
329 > random bits, so that people can override the defaults.
330
331 Agreed.
332
333 This may require some thinking before it is finalized. Is it ok to
334 install the GC module as-is meanwhile? Then I can continue to add the
335 stuff that GnuTLS need, and then come back to re-working the
336 randomness module. That way, we have two different projects that use
337 the code. GnuTLS includes the same randomness code that was in GNU
338 SASL and that is in the current gc module. I feel much more
339 comfortable working in small steps at a time, rather then working on
340 this for a long time in gnulib and only later integrate the stuff in
341 GnuTLS.
342
343 Thanks,
344 Simon
345 */
346
347#endif /* GC_H */
diff --git a/src/daemon/https/lgl/gettext.h b/src/daemon/https/lgl/gettext.h
new file mode 100644
index 00000000..bd214d5c
--- /dev/null
+++ b/src/daemon/https/lgl/gettext.h
@@ -0,0 +1,270 @@
1/* Convenience header for conditional use of GNU <libintl.h>.
2 Copyright (C) 1995-1998, 2000-2002, 2004-2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _LIBGETTEXT_H
19#define _LIBGETTEXT_H 1
20
21/* NLS can be disabled through the configure --disable-nls option. */
22#if ENABLE_NLS
23
24/* Get declarations of GNU message catalog functions. */
25# include <libintl.h>
26
27/* You can set the DEFAULT_TEXT_DOMAIN macro to specify the domain used by
28 the gettext() and ngettext() macros. This is an alternative to calling
29 textdomain(), and is useful for libraries. */
30# ifdef DEFAULT_TEXT_DOMAIN
31# undef gettext
32# define gettext(Msgid) \
33 dgettext (DEFAULT_TEXT_DOMAIN, Msgid)
34# undef ngettext
35# define ngettext(Msgid1, Msgid2, N) \
36 dngettext (DEFAULT_TEXT_DOMAIN, Msgid1, Msgid2, N)
37# endif
38
39#else
40
41/* Solaris /usr/include/locale.h includes /usr/include/libintl.h, which
42 chokes if dcgettext is defined as a macro. So include it now, to make
43 later inclusions of <locale.h> a NOP. We don't include <libintl.h>
44 as well because people using "gettext.h" will not include <libintl.h>,
45 and also including <libintl.h> would fail on SunOS 4, whereas <locale.h>
46 is OK. */
47#if defined(__sun)
48# include <locale.h>
49#endif
50
51/* Many header files from the libstdc++ coming with g++ 3.3 or newer include
52 <libintl.h>, which chokes if dcgettext is defined as a macro. So include
53 it now, to make later inclusions of <libintl.h> a NOP. */
54#if defined(__cplusplus) && defined(__GNUG__) && (__GNUC__ >= 3)
55# include <cstdlib>
56# if (__GLIBC__ >= 2) || _GLIBCXX_HAVE_LIBINTL_H
57# include <libintl.h>
58# endif
59#endif
60
61/* Disabled NLS.
62 The casts to 'const char *' serve the purpose of producing warnings
63 for invalid uses of the value returned from these functions.
64 On pre-ANSI systems without 'const', the config.h file is supposed to
65 contain "#define const". */
66# define gettext(Msgid) ((const char *) (Msgid))
67# define dgettext(Domainname, Msgid) ((void) (Domainname), gettext (Msgid))
68# define dcgettext(Domainname, Msgid, Category) \
69 ((void) (Category), dgettext (Domainname, Msgid))
70# define ngettext(Msgid1, Msgid2, N) \
71 ((N) == 1 \
72 ? ((void) (Msgid2), (const char *) (Msgid1)) \
73 : ((void) (Msgid1), (const char *) (Msgid2)))
74# define dngettext(Domainname, Msgid1, Msgid2, N) \
75 ((void) (Domainname), ngettext (Msgid1, Msgid2, N))
76# define dcngettext(Domainname, Msgid1, Msgid2, N, Category) \
77 ((void) (Category), dngettext(Domainname, Msgid1, Msgid2, N))
78# define textdomain(Domainname) ((const char *) (Domainname))
79# define bindtextdomain(Domainname, Dirname) \
80 ((void) (Domainname), (const char *) (Dirname))
81# define bind_textdomain_codeset(Domainname, Codeset) \
82 ((void) (Domainname), (const char *) (Codeset))
83
84#endif
85
86/* A pseudo function call that serves as a marker for the automated
87 extraction of messages, but does not call gettext(). The run-time
88 translation is done at a different place in the code.
89 The argument, String, should be a literal string. Concatenated strings
90 and other string expressions won't work.
91 The macro's expansion is not parenthesized, so that it is suitable as
92 initializer for static 'char[]' or 'const char[]' variables. */
93#define gettext_noop(String) String
94
95/* The separator between msgctxt and msgid in a .mo file. */
96#define GETTEXT_CONTEXT_GLUE "\004"
97
98/* Pseudo function calls, taking a MSGCTXT and a MSGID instead of just a
99 MSGID. MSGCTXT and MSGID must be string literals. MSGCTXT should be
100 short and rarely need to change.
101 The letter 'p' stands for 'particular' or 'special'. */
102#ifdef DEFAULT_TEXT_DOMAIN
103# define pgettext(Msgctxt, Msgid) \
104 pgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
105#else
106# define pgettext(Msgctxt, Msgid) \
107 pgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
108#endif
109#define dpgettext(Domainname, Msgctxt, Msgid) \
110 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, LC_MESSAGES)
111#define dcpgettext(Domainname, Msgctxt, Msgid, Category) \
112 pgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, Category)
113#ifdef DEFAULT_TEXT_DOMAIN
114# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
115 npgettext_aux (DEFAULT_TEXT_DOMAIN, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
116#else
117# define npgettext(Msgctxt, Msgid, MsgidPlural, N) \
118 npgettext_aux (NULL, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
119#endif
120#define dnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
121 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, LC_MESSAGES)
122#define dcnpgettext(Domainname, Msgctxt, Msgid, MsgidPlural, N, Category) \
123 npgettext_aux (Domainname, Msgctxt GETTEXT_CONTEXT_GLUE Msgid, Msgid, MsgidPlural, N, Category)
124
125#ifdef __GNUC__
126__inline
127#else
128#ifdef __cplusplus
129inline
130#endif
131#endif
132static const char *
133pgettext_aux (const char *domain,
134 const char *msg_ctxt_id, const char *msgid,
135 int category)
136{
137 const char *translation = dcgettext (domain, msg_ctxt_id, category);
138 if (translation == msg_ctxt_id)
139 return msgid;
140 else
141 return translation;
142}
143
144#ifdef __GNUC__
145__inline
146#else
147#ifdef __cplusplus
148inline
149#endif
150#endif
151static const char *
152npgettext_aux (const char *domain,
153 const char *msg_ctxt_id, const char *msgid,
154 const char *msgid_plural, unsigned long int n,
155 int category)
156{
157 const char *translation =
158 dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
159 if (translation == msg_ctxt_id || translation == msgid_plural)
160 return (n == 1 ? msgid : msgid_plural);
161 else
162 return translation;
163}
164
165/* The same thing extended for non-constant arguments. Here MSGCTXT and MSGID
166 can be arbitrary expressions. But for string literals these macros are
167 less efficient than those above. */
168
169#include <string.h>
170
171#define _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS \
172 (((__GNUC__ >= 3 || __GNUG__ >= 2) && !__STRICT_ANSI__) \
173 /* || __STDC_VERSION__ >= 199901L */ )
174
175#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
176#include <stdlib.h>
177#endif
178
179#define pgettext_expr(Msgctxt, Msgid) \
180 dcpgettext_expr (NULL, Msgctxt, Msgid, LC_MESSAGES)
181#define dpgettext_expr(Domainname, Msgctxt, Msgid) \
182 dcpgettext_expr (Domainname, Msgctxt, Msgid, LC_MESSAGES)
183
184#ifdef __GNUC__
185__inline
186#else
187#ifdef __cplusplus
188inline
189#endif
190#endif
191static const char *
192dcpgettext_expr (const char *domain,
193 const char *msgctxt, const char *msgid,
194 int category)
195{
196 size_t msgctxt_len = strlen (msgctxt) + 1;
197 size_t msgid_len = strlen (msgid) + 1;
198 const char *translation;
199#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
200 char msg_ctxt_id[msgctxt_len + msgid_len];
201#else
202 char buf[1024];
203 char *msg_ctxt_id =
204 (msgctxt_len + msgid_len <= sizeof (buf)
205 ? buf
206 : (char *) malloc (msgctxt_len + msgid_len));
207 if (msg_ctxt_id != NULL)
208#endif
209 {
210 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
211 msg_ctxt_id[msgctxt_len - 1] = '\004';
212 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
213 translation = dcgettext (domain, msg_ctxt_id, category);
214#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
215 if (msg_ctxt_id != buf)
216 free (msg_ctxt_id);
217#endif
218 if (translation != msg_ctxt_id)
219 return translation;
220 }
221 return msgid;
222}
223
224#define npgettext_expr(Msgctxt, Msgid, MsgidPlural, N) \
225 dcnpgettext_expr (NULL, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
226#define dnpgettext_expr(Domainname, Msgctxt, Msgid, MsgidPlural, N) \
227 dcnpgettext_expr (Domainname, Msgctxt, Msgid, MsgidPlural, N, LC_MESSAGES)
228
229#ifdef __GNUC__
230__inline
231#else
232#ifdef __cplusplus
233inline
234#endif
235#endif
236static const char *
237dcnpgettext_expr (const char *domain,
238 const char *msgctxt, const char *msgid,
239 const char *msgid_plural, unsigned long int n,
240 int category)
241{
242 size_t msgctxt_len = strlen (msgctxt) + 1;
243 size_t msgid_len = strlen (msgid) + 1;
244 const char *translation;
245#if _LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
246 char msg_ctxt_id[msgctxt_len + msgid_len];
247#else
248 char buf[1024];
249 char *msg_ctxt_id =
250 (msgctxt_len + msgid_len <= sizeof (buf)
251 ? buf
252 : (char *) malloc (msgctxt_len + msgid_len));
253 if (msg_ctxt_id != NULL)
254#endif
255 {
256 memcpy (msg_ctxt_id, msgctxt, msgctxt_len - 1);
257 msg_ctxt_id[msgctxt_len - 1] = '\004';
258 memcpy (msg_ctxt_id + msgctxt_len, msgid, msgid_len);
259 translation = dcngettext (domain, msg_ctxt_id, msgid_plural, n, category);
260#if !_LIBGETTEXT_HAVE_VARIABLE_SIZE_ARRAYS
261 if (msg_ctxt_id != buf)
262 free (msg_ctxt_id);
263#endif
264 if (!(translation == msg_ctxt_id || translation == msgid_plural))
265 return translation;
266 }
267 return (n == 1 ? msgid : msgid_plural);
268}
269
270#endif /* _LIBGETTEXT_H */
diff --git a/src/daemon/https/lgl/hmac-md5.c b/src/daemon/https/lgl/hmac-md5.c
new file mode 100644
index 00000000..53cf2b10
--- /dev/null
+++ b/src/daemon/https/lgl/hmac-md5.c
@@ -0,0 +1,81 @@
1/* hmac-md5.c -- hashed message authentication codes
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. */
19
20#include <config.h>
21
22#include "hmac.h"
23
24#include "memxor.h"
25#include "md5.h"
26
27#include <string.h>
28
29#define IPAD 0x36
30#define OPAD 0x5c
31
32int
33hmac_md5 (const void *key, size_t keylen,
34 const void *in, size_t inlen, void *resbuf)
35{
36 struct md5_ctx inner;
37 struct md5_ctx outer;
38 char optkeybuf[16];
39 char block[64];
40 char innerhash[16];
41
42 /* Reduce the key's size, so that it becomes <= 64 bytes large. */
43
44 if (keylen > 64)
45 {
46 struct md5_ctx keyhash;
47
48 md5_init_ctx (&keyhash);
49 md5_process_bytes (key, keylen, &keyhash);
50 md5_finish_ctx (&keyhash, optkeybuf);
51
52 key = optkeybuf;
53 keylen = 16;
54 }
55
56 /* Compute INNERHASH from KEY and IN. */
57
58 md5_init_ctx (&inner);
59
60 memset (block, IPAD, sizeof (block));
61 memxor (block, key, keylen);
62
63 md5_process_block (block, 64, &inner);
64 md5_process_bytes (in, inlen, &inner);
65
66 md5_finish_ctx (&inner, innerhash);
67
68 /* Compute result from KEY and INNERHASH. */
69
70 md5_init_ctx (&outer);
71
72 memset (block, OPAD, sizeof (block));
73 memxor (block, key, keylen);
74
75 md5_process_block (block, 64, &outer);
76 md5_process_bytes (innerhash, 16, &outer);
77
78 md5_finish_ctx (&outer, resbuf);
79
80 return 0;
81}
diff --git a/src/daemon/https/lgl/hmac-sha1.c b/src/daemon/https/lgl/hmac-sha1.c
new file mode 100644
index 00000000..a9cf18cd
--- /dev/null
+++ b/src/daemon/https/lgl/hmac-sha1.c
@@ -0,0 +1,81 @@
1/* hmac-sha1.c -- hashed message authentication codes
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. */
19
20#include <config.h>
21
22#include "hmac.h"
23
24#include "memxor.h"
25#include "sha1.h"
26
27#include <string.h>
28
29#define IPAD 0x36
30#define OPAD 0x5c
31
32int
33hmac_sha1 (const void *key, size_t keylen,
34 const void *in, size_t inlen, void *resbuf)
35{
36 struct sha1_ctx inner;
37 struct sha1_ctx outer;
38 char optkeybuf[20];
39 char block[64];
40 char innerhash[20];
41
42 /* Reduce the key's size, so that it becomes <= 64 bytes large. */
43
44 if (keylen > 64)
45 {
46 struct sha1_ctx keyhash;
47
48 sha1_init_ctx (&keyhash);
49 sha1_process_bytes (key, keylen, &keyhash);
50 sha1_finish_ctx (&keyhash, optkeybuf);
51
52 key = optkeybuf;
53 keylen = 20;
54 }
55
56 /* Compute INNERHASH from KEY and IN. */
57
58 sha1_init_ctx (&inner);
59
60 memset (block, IPAD, sizeof (block));
61 memxor (block, key, keylen);
62
63 sha1_process_block (block, 64, &inner);
64 sha1_process_bytes (in, inlen, &inner);
65
66 sha1_finish_ctx (&inner, innerhash);
67
68 /* Compute result from KEY and INNERHASH. */
69
70 sha1_init_ctx (&outer);
71
72 memset (block, OPAD, sizeof (block));
73 memxor (block, key, keylen);
74
75 sha1_process_block (block, 64, &outer);
76 sha1_process_bytes (innerhash, 20, &outer);
77
78 sha1_finish_ctx (&outer, resbuf);
79
80 return 0;
81}
diff --git a/src/daemon/https/lgl/hmac.h b/src/daemon/https/lgl/hmac.h
new file mode 100644
index 00000000..5965b603
--- /dev/null
+++ b/src/daemon/https/lgl/hmac.h
@@ -0,0 +1,41 @@
1/* hmac.h -- hashed message authentication codes
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. */
19
20#ifndef HMAC_H
21# define HMAC_H 1
22
23#include <stddef.h>
24
25/* Compute Hashed Message Authentication Code with MD5, as described
26 in RFC 2104, over BUFFER data of BUFLEN bytes using the KEY of
27 KEYLEN bytes, writing the output to pre-allocated 16 byte minimum
28 RESBUF buffer. Return 0 on success. */
29int
30hmac_md5 (const void *key, size_t keylen,
31 const void *buffer, size_t buflen, void *resbuf);
32
33/* Compute Hashed Message Authentication Code with SHA-1, over BUFFER
34 data of BUFLEN bytes using the KEY of KEYLEN bytes, writing the
35 output to pre-allocated 20 byte minimum RESBUF buffer. Return 0 on
36 success. */
37int
38hmac_sha1 (const void *key, size_t keylen,
39 const void *in, size_t inlen, void *resbuf);
40
41#endif /* HMAC_H */
diff --git a/src/daemon/https/lgl/md5.c b/src/daemon/https/lgl/md5.c
new file mode 100644
index 00000000..9b3bfbe8
--- /dev/null
+++ b/src/daemon/https/lgl/md5.c
@@ -0,0 +1,451 @@
1/* Functions to compute MD5 message digest of files or memory blocks.
2 according to the definition of MD5 in RFC 1321 from April 1992.
3 Copyright (C) 1995,1996,1997,1999,2000,2001,2005,2006
4 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2.1, or (at your option) any
10 later version.
11
12 This program 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
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. */
22
23#include <config.h>
24
25#include "md5.h"
26
27#include <stddef.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/types.h>
31
32#if USE_UNLOCKED_IO
33# include "unlocked-io.h"
34#endif
35
36#ifdef _LIBC
37# include <endian.h>
38# if __BYTE_ORDER == __BIG_ENDIAN
39# define WORDS_BIGENDIAN 1
40# endif
41/* We need to keep the namespace clean so define the MD5 function
42 protected using leading __ . */
43# define md5_init_ctx __md5_init_ctx
44# define md5_process_block __md5_process_block
45# define md5_process_bytes __md5_process_bytes
46# define md5_finish_ctx __md5_finish_ctx
47# define md5_read_ctx __md5_read_ctx
48# define md5_stream __md5_stream
49# define md5_buffer __md5_buffer
50#endif
51
52#ifdef WORDS_BIGENDIAN
53# define SWAP(n) \
54 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
55#else
56# define SWAP(n) (n)
57#endif
58
59#define BLOCKSIZE 4096
60#if BLOCKSIZE % 64 != 0
61# error "invalid BLOCKSIZE"
62#endif
63
64/* This array contains the bytes used to pad the buffer to the next
65 64-byte boundary. (RFC 1321, 3.1: Step 1) */
66static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
67
68
69/* Initialize structure containing state of computation.
70 (RFC 1321, 3.3: Step 3) */
71void
72md5_init_ctx (struct md5_ctx *ctx)
73{
74 ctx->A = 0x67452301;
75 ctx->B = 0xefcdab89;
76 ctx->C = 0x98badcfe;
77 ctx->D = 0x10325476;
78
79 ctx->total[0] = ctx->total[1] = 0;
80 ctx->buflen = 0;
81}
82
83/* Put result from CTX in first 16 bytes following RESBUF. The result
84 must be in little endian byte order.
85
86 IMPORTANT: On some systems it is required that RESBUF is correctly
87 aligned for a 32-bit value. */
88void *
89md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
90{
91 ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
92 ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
93 ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
94 ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
95
96 return resbuf;
97}
98
99/* Process the remaining bytes in the internal buffer and the usual
100 prolog according to the standard and write the result to RESBUF.
101
102 IMPORTANT: On some systems it is required that RESBUF is correctly
103 aligned for a 32-bit value. */
104void *
105md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
106{
107 /* Take yet unprocessed bytes into account. */
108 uint32_t bytes = ctx->buflen;
109 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
110
111 /* Now count remaining bytes. */
112 ctx->total[0] += bytes;
113 if (ctx->total[0] < bytes)
114 ++ctx->total[1];
115
116 /* Put the 64-bit file length in *bits* at the end of the buffer. */
117 ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
118 ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
119
120 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
121
122 /* Process last bytes. */
123 md5_process_block (ctx->buffer, size * 4, ctx);
124
125 return md5_read_ctx (ctx, resbuf);
126}
127
128/* Compute MD5 message digest for bytes read from STREAM. The
129 resulting message digest number will be written into the 16 bytes
130 beginning at RESBLOCK. */
131int
132md5_stream (FILE * stream, void *resblock)
133{
134 struct md5_ctx ctx;
135 char buffer[BLOCKSIZE + 72];
136 size_t sum;
137
138 /* Initialize the computation context. */
139 md5_init_ctx (&ctx);
140
141 /* Iterate over full file contents. */
142 while (1)
143 {
144 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
145 computation function processes the whole buffer so that with the
146 next round of the loop another block can be read. */
147 size_t n;
148 sum = 0;
149
150 /* Read block. Take care for partial reads. */
151 while (1)
152 {
153 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
154
155 sum += n;
156
157 if (sum == BLOCKSIZE)
158 break;
159
160 if (n == 0)
161 {
162 /* Check for the error flag IFF N == 0, so that we don't
163 exit the loop after a partial read due to e.g., EAGAIN
164 or EWOULDBLOCK. */
165 if (ferror (stream))
166 return 1;
167 goto process_partial_block;
168 }
169
170 /* We've read at least one byte, so ignore errors. But always
171 check for EOF, since feof may be true even though N > 0.
172 Otherwise, we could end up calling fread after EOF. */
173 if (feof (stream))
174 goto process_partial_block;
175 }
176
177 /* Process buffer with BLOCKSIZE bytes. Note that
178 BLOCKSIZE % 64 == 0
179 */
180 md5_process_block (buffer, BLOCKSIZE, &ctx);
181 }
182
183process_partial_block:
184
185 /* Process any remaining bytes. */
186 if (sum > 0)
187 md5_process_bytes (buffer, sum, &ctx);
188
189 /* Construct result in desired memory. */
190 md5_finish_ctx (&ctx, resblock);
191 return 0;
192}
193
194/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
195 result is always in little endian byte order, so that a byte-wise
196 output yields to the wanted ASCII representation of the message
197 digest. */
198void *
199md5_buffer (const char *buffer, size_t len, void *resblock)
200{
201 struct md5_ctx ctx;
202
203 /* Initialize the computation context. */
204 md5_init_ctx (&ctx);
205
206 /* Process whole buffer but last len % 64 bytes. */
207 md5_process_bytes (buffer, len, &ctx);
208
209 /* Put result in desired memory area. */
210 return md5_finish_ctx (&ctx, resblock);
211}
212
213
214void
215md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
216{
217 /* When we already have some bits in our internal buffer concatenate
218 both inputs first. */
219 if (ctx->buflen != 0)
220 {
221 size_t left_over = ctx->buflen;
222 size_t add = 128 - left_over > len ? len : 128 - left_over;
223
224 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
225 ctx->buflen += add;
226
227 if (ctx->buflen > 64)
228 {
229 md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
230
231 ctx->buflen &= 63;
232 /* The regions in the following copy operation cannot overlap. */
233 memcpy (ctx->buffer,
234 &((char *) ctx->buffer)[(left_over + add) & ~63],
235 ctx->buflen);
236 }
237
238 buffer = (const char *) buffer + add;
239 len -= add;
240 }
241
242 /* Process available complete blocks. */
243 if (len >= 64)
244 {
245#if !_STRING_ARCH_unaligned
246# define alignof(type) offsetof (struct { char c; type x; }, x)
247# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
248 if (UNALIGNED_P (buffer))
249 while (len > 64)
250 {
251 md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
252 buffer = (const char *) buffer + 64;
253 len -= 64;
254 }
255 else
256#endif
257 {
258 md5_process_block (buffer, len & ~63, ctx);
259 buffer = (const char *) buffer + (len & ~63);
260 len &= 63;
261 }
262 }
263
264 /* Move remaining bytes in internal buffer. */
265 if (len > 0)
266 {
267 size_t left_over = ctx->buflen;
268
269 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
270 left_over += len;
271 if (left_over >= 64)
272 {
273 md5_process_block (ctx->buffer, 64, ctx);
274 left_over -= 64;
275 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
276 }
277 ctx->buflen = left_over;
278 }
279}
280
281
282/* These are the four functions used in the four steps of the MD5 algorithm
283 and defined in the RFC 1321. The first function is a little bit optimized
284 (as found in Colin Plumbs public domain implementation). */
285/* #define FF(b, c, d) ((b & c) | (~b & d)) */
286#define FF(b, c, d) (d ^ (b & (c ^ d)))
287#define FG(b, c, d) FF (d, b, c)
288#define FH(b, c, d) (b ^ c ^ d)
289#define FI(b, c, d) (c ^ (b | ~d))
290
291/* Process LEN bytes of BUFFER, accumulating context into CTX.
292 It is assumed that LEN % 64 == 0. */
293
294void
295md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
296{
297 uint32_t correct_words[16];
298 const uint32_t *words = buffer;
299 size_t nwords = len / sizeof (uint32_t);
300 const uint32_t *endp = words + nwords;
301 uint32_t A = ctx->A;
302 uint32_t B = ctx->B;
303 uint32_t C = ctx->C;
304 uint32_t D = ctx->D;
305
306 /* First increment the byte count. RFC 1321 specifies the possible
307 length of the file up to 2^64 bits. Here we only compute the
308 number of bytes. Do a double word increment. */
309 ctx->total[0] += len;
310 if (ctx->total[0] < len)
311 ++ctx->total[1];
312
313 /* Process all bytes in the buffer with 64 bytes in each round of
314 the loop. */
315 while (words < endp)
316 {
317 uint32_t *cwp = correct_words;
318 uint32_t A_save = A;
319 uint32_t B_save = B;
320 uint32_t C_save = C;
321 uint32_t D_save = D;
322
323 /* First round: using the given function, the context and a constant
324 the next context is computed. Because the algorithms processing
325 unit is a 32-bit word and it is determined to work on words in
326 little endian byte order we perhaps have to change the byte order
327 before the computation. To reduce the work for the next steps
328 we store the swapped words in the array CORRECT_WORDS. */
329
330#define OP(a, b, c, d, s, T) \
331 do \
332 { \
333 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
334 ++words; \
335 CYCLIC (a, s); \
336 a += b; \
337 } \
338 while (0)
339
340 /* It is unfortunate that C does not provide an operator for
341 cyclic rotation. Hope the C compiler is smart enough. */
342#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
343
344 /* Before we start, one word to the strange constants.
345 They are defined in RFC 1321 as
346
347 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
348
349 Here is an equivalent invocation using Perl:
350
351 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
352 */
353
354 /* Round 1. */
355 OP (A, B, C, D, 7, 0xd76aa478);
356 OP (D, A, B, C, 12, 0xe8c7b756);
357 OP (C, D, A, B, 17, 0x242070db);
358 OP (B, C, D, A, 22, 0xc1bdceee);
359 OP (A, B, C, D, 7, 0xf57c0faf);
360 OP (D, A, B, C, 12, 0x4787c62a);
361 OP (C, D, A, B, 17, 0xa8304613);
362 OP (B, C, D, A, 22, 0xfd469501);
363 OP (A, B, C, D, 7, 0x698098d8);
364 OP (D, A, B, C, 12, 0x8b44f7af);
365 OP (C, D, A, B, 17, 0xffff5bb1);
366 OP (B, C, D, A, 22, 0x895cd7be);
367 OP (A, B, C, D, 7, 0x6b901122);
368 OP (D, A, B, C, 12, 0xfd987193);
369 OP (C, D, A, B, 17, 0xa679438e);
370 OP (B, C, D, A, 22, 0x49b40821);
371
372 /* For the second to fourth round we have the possibly swapped words
373 in CORRECT_WORDS. Redefine the macro to take an additional first
374 argument specifying the function to use. */
375#undef OP
376#define OP(f, a, b, c, d, k, s, T) \
377 do \
378 { \
379 a += f (b, c, d) + correct_words[k] + T; \
380 CYCLIC (a, s); \
381 a += b; \
382 } \
383 while (0)
384
385 /* Round 2. */
386 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
387 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
388 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
389 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
390 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
391 OP (FG, D, A, B, C, 10, 9, 0x02441453);
392 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
393 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
394 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
395 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
396 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
397 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
398 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
399 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
400 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
401 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
402
403 /* Round 3. */
404 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
405 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
406 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
407 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
408 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
409 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
410 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
411 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
412 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
413 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
414 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
415 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
416 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
417 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
418 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
419 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
420
421 /* Round 4. */
422 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
423 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
424 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
425 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
426 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
427 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
428 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
429 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
430 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
431 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
432 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
433 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
434 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
435 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
436 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
437 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
438
439 /* Add the starting values of the context. */
440 A += A_save;
441 B += B_save;
442 C += C_save;
443 D += D_save;
444 }
445
446 /* Put checksum in context given as argument. */
447 ctx->A = A;
448 ctx->B = B;
449 ctx->C = C;
450 ctx->D = D;
451}
diff --git a/src/daemon/https/lgl/md5.h b/src/daemon/https/lgl/md5.h
new file mode 100644
index 00000000..6018a6f6
--- /dev/null
+++ b/src/daemon/https/lgl/md5.h
@@ -0,0 +1,124 @@
1/* Declaration of functions and data types used for MD5 sum computing
2 library functions.
3 Copyright (C) 1995-1997,1999,2000,2001,2004,2005,2006
4 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2.1, or (at your option) any
10 later version.
11
12 This program 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
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21#ifndef _MD5_H
22#define _MD5_H 1
23
24#include <stdio.h>
25#include <stdint.h>
26
27#define MD5_DIGEST_SIZE 16
28#define MD5_BLOCK_SIZE 64
29
30#ifndef __GNUC_PREREQ
31# if defined __GNUC__ && defined __GNUC_MINOR__
32# define __GNUC_PREREQ(maj, min) \
33 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
34# else
35# define __GNUC_PREREQ(maj, min) 0
36# endif
37#endif
38
39#ifndef __THROW
40# if defined __cplusplus && __GNUC_PREREQ (2,8)
41# define __THROW throw ()
42# else
43# define __THROW
44# endif
45#endif
46
47#ifndef _LIBC
48# define __md5_buffer md5_buffer
49# define __md5_finish_ctx md5_finish_ctx
50# define __md5_init_ctx md5_init_ctx
51# define __md5_process_block md5_process_block
52# define __md5_process_bytes md5_process_bytes
53# define __md5_read_ctx md5_read_ctx
54# define __md5_stream md5_stream
55#endif
56
57/* Structure to save state of computation between the single steps. */
58struct md5_ctx
59{
60 uint32_t A;
61 uint32_t B;
62 uint32_t C;
63 uint32_t D;
64
65 uint32_t total[2];
66 uint32_t buflen;
67 uint32_t buffer[32];
68};
69
70/*
71 * The following three functions are build up the low level used in
72 * the functions `md5_stream' and `md5_buffer'.
73 */
74
75/* Initialize structure containing state of computation.
76 (RFC 1321, 3.3: Step 3) */
77extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
78
79/* Starting with the result of former calls of this function (or the
80 initialization function update the context for the next LEN bytes
81 starting at BUFFER.
82 It is necessary that LEN is a multiple of 64!!! */
83extern void __md5_process_block (const void *buffer, size_t len,
84 struct md5_ctx *ctx) __THROW;
85
86/* Starting with the result of former calls of this function (or the
87 initialization function update the context for the next LEN bytes
88 starting at BUFFER.
89 It is NOT required that LEN is a multiple of 64. */
90extern void __md5_process_bytes (const void *buffer, size_t len,
91 struct md5_ctx *ctx) __THROW;
92
93/* Process the remaining bytes in the buffer and put result from CTX
94 in first 16 bytes following RESBUF. The result is always in little
95 endian byte order, so that a byte-wise output yields to the wanted
96 ASCII representation of the message digest.
97
98 IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
99 boundary. */
100extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
101
102
103/* Put result from CTX in first 16 bytes following RESBUF. The result is
104 always in little endian byte order, so that a byte-wise output yields
105 to the wanted ASCII representation of the message digest.
106
107 IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
108 boundary. */
109extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
110
111
112/* Compute MD5 message digest for bytes read from STREAM. The
113 resulting message digest number will be written into the 16 bytes
114 beginning at RESBLOCK. */
115extern int __md5_stream (FILE *stream, void *resblock) __THROW;
116
117/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
118 result is always in little endian byte order, so that a byte-wise
119 output yields to the wanted ASCII representation of the message
120 digest. */
121extern void *__md5_buffer (const char *buffer, size_t len,
122 void *resblock) __THROW;
123
124#endif /* md5.h */
diff --git a/src/daemon/https/lgl/memmem.c b/src/daemon/https/lgl/memmem.c
new file mode 100644
index 00000000..1b66ed56
--- /dev/null
+++ b/src/daemon/https/lgl/memmem.c
@@ -0,0 +1,61 @@
1/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _LIBC
19# include <config.h>
20#endif
21
22#include <stddef.h>
23#include <string.h>
24
25#ifndef _LIBC
26# define __builtin_expect(expr, val) (expr)
27#endif
28
29#undef memmem
30
31/* Return the first occurrence of NEEDLE in HAYSTACK. */
32void *
33memmem (haystack, haystack_len, needle, needle_len)
34 const void *haystack;
35 size_t haystack_len;
36 const void *needle;
37 size_t needle_len;
38{
39 const char *begin;
40 const char *const last_possible = (const char *) haystack + haystack_len
41 - needle_len;
42
43 if (needle_len == 0)
44 /* The first occurrence of the empty string is deemed to occur at
45 the beginning of the string. */
46 return (void *) haystack;
47
48 /* Sanity check, otherwise the loop might search through the whole
49 memory. */
50 if (__builtin_expect (haystack_len < needle_len, 0))
51 return NULL;
52
53 for (begin = (const char *) haystack; begin <= last_possible; ++begin)
54 if (begin[0] == ((const char *) needle)[0]
55 && !memcmp ((const void *) &begin[1],
56 (const void *) ((const char *) needle + 1),
57 needle_len - 1))
58 return (void *) begin;
59
60 return NULL;
61}
diff --git a/src/daemon/https/lgl/memmove.c b/src/daemon/https/lgl/memmove.c
new file mode 100644
index 00000000..0f040540
--- /dev/null
+++ b/src/daemon/https/lgl/memmove.c
@@ -0,0 +1,26 @@
1/* memmove.c -- copy memory.
2 Copy LENGTH bytes from SOURCE to DEST. Does not null-terminate.
3 In the public domain.
4 By David MacKenzie <djm@gnu.ai.mit.edu>. */
5
6#include <config.h>
7
8#include <stddef.h>
9
10void *
11memmove (void *dest0, void const *source0, size_t length)
12{
13 char *dest = dest0;
14 char const *source = source0;
15 if (source < dest)
16 /* Moving from low mem to hi mem; start at end. */
17 for (source += length, dest += length; length; --length)
18 *--dest = *--source;
19 else if (source != dest)
20 {
21 /* Moving from hi mem to low mem; start at beginning. */
22 for (; length; --length)
23 *dest++ = *source++;
24 }
25 return dest0;
26}
diff --git a/src/daemon/https/lgl/memxor.c b/src/daemon/https/lgl/memxor.c
new file mode 100644
index 00000000..7b0b6ae9
--- /dev/null
+++ b/src/daemon/https/lgl/memxor.c
@@ -0,0 +1,35 @@
1/* memxor.c -- perform binary exclusive OR operation of two memory blocks.
2 Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. The interface was inspired by memxor
19 in Niels Möller's Nettle. */
20
21#include <config.h>
22
23#include "memxor.h"
24
25void *
26memxor (void *restrict dest, const void *restrict src, size_t n)
27{
28 char const *s = src;
29 char *d = dest;
30
31 for (; n > 0; n--)
32 *d++ ^= *s++;
33
34 return dest;
35}
diff --git a/src/daemon/https/lgl/memxor.h b/src/daemon/https/lgl/memxor.h
new file mode 100644
index 00000000..4f85f2d7
--- /dev/null
+++ b/src/daemon/https/lgl/memxor.h
@@ -0,0 +1,31 @@
1/* memxor.h -- perform binary exclusive OR operation on memory blocks.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* Written by Simon Josefsson. The interface was inspired by memxor
19 in Niels Möller's Nettle. */
20
21#ifndef MEMXOR_H
22# define MEMXOR_H
23
24#include <stddef.h>
25
26/* Compute binary exclusive OR of memory areas DEST and SRC, putting
27 the result in DEST, of length N bytes. Returns a pointer to
28 DEST. */
29void *memxor (void *restrict dest, const void *restrict src, size_t n);
30
31#endif /* MEMXOR_H */
diff --git a/src/daemon/https/lgl/printf-args.c b/src/daemon/https/lgl/printf-args.c
new file mode 100644
index 00000000..55a5c439
--- /dev/null
+++ b/src/daemon/https/lgl/printf-args.c
@@ -0,0 +1,185 @@
1/* Decomposed printf argument list.
2 Copyright (C) 1999, 2002-2003, 2005-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* This file can be parametrized with the following macros:
19 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
20 PRINTF_FETCHARGS Name of the function to be defined.
21 STATIC Set to 'static' to declare the function static. */
22
23#ifndef PRINTF_FETCHARGS
24# include <config.h>
25#endif
26
27/* Specification. */
28#ifndef PRINTF_FETCHARGS
29# include "printf-args.h"
30#endif
31
32#ifdef STATIC
33STATIC
34#endif
35 int
36PRINTF_FETCHARGS (va_list args, arguments * a)
37{
38 size_t i;
39 argument *ap;
40
41 for (i = 0, ap = &a->arg[0]; i < a->count; i++, ap++)
42 switch (ap->type)
43 {
44 case TYPE_SCHAR:
45 ap->a.a_schar = va_arg (args, /*signed char */ int);
46 break;
47 case TYPE_UCHAR:
48 ap->a.a_uchar = va_arg (args, /*unsigned char */ int);
49 break;
50 case TYPE_SHORT:
51 ap->a.a_short = va_arg (args, /*short */ int);
52 break;
53 case TYPE_USHORT:
54 ap->a.a_ushort = va_arg (args, /*unsigned short */ int);
55 break;
56 case TYPE_INT:
57 ap->a.a_int = va_arg (args, int);
58 break;
59 case TYPE_UINT:
60 ap->a.a_uint = va_arg (args, unsigned int);
61 break;
62 case TYPE_LONGINT:
63 ap->a.a_longint = va_arg (args, long int);
64 break;
65 case TYPE_ULONGINT:
66 ap->a.a_ulongint = va_arg (args, unsigned long int);
67 break;
68#if HAVE_LONG_LONG_INT
69 case TYPE_LONGLONGINT:
70 ap->a.a_longlongint = va_arg (args, long long int);
71 break;
72 case TYPE_ULONGLONGINT:
73 ap->a.a_ulonglongint = va_arg (args, unsigned long long int);
74 break;
75#endif
76 case TYPE_DOUBLE:
77 ap->a.a_double = va_arg (args, double);
78 break;
79 case TYPE_LONGDOUBLE:
80 ap->a.a_longdouble = va_arg (args, long double);
81 break;
82 case TYPE_CHAR:
83 ap->a.a_char = va_arg (args, int);
84 break;
85#if HAVE_WINT_T
86 case TYPE_WIDE_CHAR:
87 /* Although ISO C 99 7.24.1.(2) says that wint_t is "unchanged by
88 default argument promotions", this is not the case in mingw32,
89 where wint_t is 'unsigned short'. */
90 ap->a.a_wide_char =
91 (sizeof (wint_t) < sizeof (int)
92 ? va_arg (args, int) : va_arg (args, wint_t));
93 break;
94#endif
95 case TYPE_STRING:
96 ap->a.a_string = va_arg (args, const char *);
97 /* A null pointer is an invalid argument for "%s", but in practice
98 it occurs quite frequently in printf statements that produce
99 debug output. Use a fallback in this case. */
100 if (ap->a.a_string == NULL)
101 ap->a.a_string = "(NULL)";
102 break;
103#if HAVE_WCHAR_T
104 case TYPE_WIDE_STRING:
105 ap->a.a_wide_string = va_arg (args, const wchar_t *);
106 /* A null pointer is an invalid argument for "%ls", but in practice
107 it occurs quite frequently in printf statements that produce
108 debug output. Use a fallback in this case. */
109 if (ap->a.a_wide_string == NULL)
110 {
111 static const wchar_t wide_null_string[] = {
112 (wchar_t) '(',
113 (wchar_t) 'N', (wchar_t) 'U', (wchar_t) 'L', (wchar_t) 'L',
114 (wchar_t) ')',
115 (wchar_t) 0
116 };
117 ap->a.a_wide_string = wide_null_string;
118 }
119 break;
120#endif
121 case TYPE_POINTER:
122 ap->a.a_pointer = va_arg (args, void *);
123 break;
124 case TYPE_COUNT_SCHAR_POINTER:
125 ap->a.a_count_schar_pointer = va_arg (args, signed char *);
126 break;
127 case TYPE_COUNT_SHORT_POINTER:
128 ap->a.a_count_short_pointer = va_arg (args, short *);
129 break;
130 case TYPE_COUNT_INT_POINTER:
131 ap->a.a_count_int_pointer = va_arg (args, int *);
132 break;
133 case TYPE_COUNT_LONGINT_POINTER:
134 ap->a.a_count_longint_pointer = va_arg (args, long int *);
135 break;
136#if HAVE_LONG_LONG_INT
137 case TYPE_COUNT_LONGLONGINT_POINTER:
138 ap->a.a_count_longlongint_pointer = va_arg (args, long long int *);
139 break;
140#endif
141#if ENABLE_UNISTDIO
142 /* The unistdio extensions. */
143 case TYPE_U8_STRING:
144 ap->a.a_u8_string = va_arg (args, const uint8_t *);
145 /* A null pointer is an invalid argument for "%U", but in practice
146 it occurs quite frequently in printf statements that produce
147 debug output. Use a fallback in this case. */
148 if (ap->a.a_u8_string == NULL)
149 {
150 static const uint8_t u8_null_string[] =
151 { '(', 'N', 'U', 'L', 'L', ')', 0 };
152 ap->a.a_u8_string = u8_null_string;
153 }
154 break;
155 case TYPE_U16_STRING:
156 ap->a.a_u16_string = va_arg (args, const uint16_t *);
157 /* A null pointer is an invalid argument for "%lU", but in practice
158 it occurs quite frequently in printf statements that produce
159 debug output. Use a fallback in this case. */
160 if (ap->a.a_u16_string == NULL)
161 {
162 static const uint16_t u16_null_string[] =
163 { '(', 'N', 'U', 'L', 'L', ')', 0 };
164 ap->a.a_u16_string = u16_null_string;
165 }
166 break;
167 case TYPE_U32_STRING:
168 ap->a.a_u32_string = va_arg (args, const uint32_t *);
169 /* A null pointer is an invalid argument for "%llU", but in practice
170 it occurs quite frequently in printf statements that produce
171 debug output. Use a fallback in this case. */
172 if (ap->a.a_u32_string == NULL)
173 {
174 static const uint32_t u32_null_string[] =
175 { '(', 'N', 'U', 'L', 'L', ')', 0 };
176 ap->a.a_u32_string = u32_null_string;
177 }
178 break;
179#endif
180 default:
181 /* Unknown type. */
182 return -1;
183 }
184 return 0;
185}
diff --git a/src/daemon/https/lgl/printf-args.h b/src/daemon/https/lgl/printf-args.h
new file mode 100644
index 00000000..b663a63b
--- /dev/null
+++ b/src/daemon/https/lgl/printf-args.h
@@ -0,0 +1,154 @@
1/* Decomposed printf argument list.
2 Copyright (C) 1999, 2002-2003, 2006-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _PRINTF_ARGS_H
19#define _PRINTF_ARGS_H
20
21/* This file can be parametrized with the following macros:
22 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
23 PRINTF_FETCHARGS Name of the function to be declared.
24 STATIC Set to 'static' to declare the function static. */
25
26/* Default parameters. */
27#ifndef PRINTF_FETCHARGS
28# define PRINTF_FETCHARGS printf_fetchargs
29#endif
30
31/* Get size_t. */
32#include <stddef.h>
33
34/* Get wchar_t. */
35#if HAVE_WCHAR_T
36# include <stddef.h>
37#endif
38
39/* Get wint_t. */
40#if HAVE_WINT_T
41# include <wchar.h>
42#endif
43
44/* Get va_list. */
45#include <stdarg.h>
46
47
48/* Argument types */
49typedef enum
50{
51 TYPE_NONE,
52 TYPE_SCHAR,
53 TYPE_UCHAR,
54 TYPE_SHORT,
55 TYPE_USHORT,
56 TYPE_INT,
57 TYPE_UINT,
58 TYPE_LONGINT,
59 TYPE_ULONGINT,
60#if HAVE_LONG_LONG_INT
61 TYPE_LONGLONGINT,
62 TYPE_ULONGLONGINT,
63#endif
64 TYPE_DOUBLE,
65 TYPE_LONGDOUBLE,
66 TYPE_CHAR,
67#if HAVE_WINT_T
68 TYPE_WIDE_CHAR,
69#endif
70 TYPE_STRING,
71#if HAVE_WCHAR_T
72 TYPE_WIDE_STRING,
73#endif
74 TYPE_POINTER,
75 TYPE_COUNT_SCHAR_POINTER,
76 TYPE_COUNT_SHORT_POINTER,
77 TYPE_COUNT_INT_POINTER,
78 TYPE_COUNT_LONGINT_POINTER
79#if HAVE_LONG_LONG_INT
80, TYPE_COUNT_LONGLONGINT_POINTER
81#endif
82#if ENABLE_UNISTDIO
83 /* The unistdio extensions. */
84, TYPE_U8_STRING
85, TYPE_U16_STRING
86, TYPE_U32_STRING
87#endif
88} arg_type;
89
90/* Polymorphic argument */
91typedef struct
92{
93 arg_type type;
94 union
95 {
96 signed char a_schar;
97 unsigned char a_uchar;
98 short a_short;
99 unsigned short a_ushort;
100 int a_int;
101 unsigned int a_uint;
102 long int a_longint;
103 unsigned long int a_ulongint;
104#if HAVE_LONG_LONG_INT
105 long long int a_longlongint;
106 unsigned long long int a_ulonglongint;
107#endif
108 float a_float;
109 double a_double;
110 long double a_longdouble;
111 int a_char;
112#if HAVE_WINT_T
113 wint_t a_wide_char;
114#endif
115 const char* a_string;
116#if HAVE_WCHAR_T
117 const wchar_t* a_wide_string;
118#endif
119 void* a_pointer;
120 signed char * a_count_schar_pointer;
121 short * a_count_short_pointer;
122 int * a_count_int_pointer;
123 long int * a_count_longint_pointer;
124#if HAVE_LONG_LONG_INT
125 long long int * a_count_longlongint_pointer;
126#endif
127#if ENABLE_UNISTDIO
128 /* The unistdio extensions. */
129 const uint8_t * a_u8_string;
130 const uint16_t * a_u16_string;
131 const uint32_t * a_u32_string;
132#endif
133 }
134 a;
135}
136argument;
137
138typedef struct
139{
140 size_t count;
141 argument *arg;
142}
143arguments;
144
145
146/* Fetch the arguments, putting them into a. */
147#ifdef STATIC
148STATIC
149#else
150extern
151#endif
152int PRINTF_FETCHARGS (va_list args, arguments *a);
153
154#endif /* _PRINTF_ARGS_H */
diff --git a/src/daemon/https/lgl/printf-parse.c b/src/daemon/https/lgl/printf-parse.c
new file mode 100644
index 00000000..e0b83a39
--- /dev/null
+++ b/src/daemon/https/lgl/printf-parse.c
@@ -0,0 +1,599 @@
1/* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003, 2006-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* This file can be parametrized with the following macros:
19 CHAR_T The element type of the format string.
20 CHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
21 in the format string are ASCII.
22 DIRECTIVE Structure denoting a format directive.
23 Depends on CHAR_T.
24 DIRECTIVES Structure denoting the set of format directives of a
25 format string. Depends on CHAR_T.
26 PRINTF_PARSE Function that parses a format string.
27 Depends on CHAR_T.
28 STATIC Set to 'static' to declare the function static.
29 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions. */
30
31#ifndef PRINTF_PARSE
32# include <config.h>
33#endif
34
35/* Specification. */
36#ifndef PRINTF_PARSE
37# include "printf-parse.h"
38#endif
39
40/* Default parameters. */
41#ifndef PRINTF_PARSE
42# define PRINTF_PARSE printf_parse
43# define CHAR_T char
44# define DIRECTIVE char_directive
45# define DIRECTIVES char_directives
46#endif
47
48/* Get size_t, NULL. */
49#include <stddef.h>
50
51/* Get intmax_t. */
52#if defined IN_LIBINTL || defined IN_LIBASPRINTF
53# if HAVE_STDINT_H_WITH_UINTMAX
54# include <stdint.h>
55# endif
56# if HAVE_INTTYPES_H_WITH_UINTMAX
57# include <inttypes.h>
58# endif
59#else
60# include <stdint.h>
61#endif
62
63/* malloc(), realloc(), free(). */
64#include <stdlib.h>
65
66/* errno. */
67#include <errno.h>
68
69/* Checked size_t computations. */
70#include "xsize.h"
71
72#if CHAR_T_ONLY_ASCII
73/* c_isascii(). */
74# include "c-ctype.h"
75#endif
76
77#ifdef STATIC
78STATIC
79#endif
80 int
81PRINTF_PARSE (const CHAR_T * format, DIRECTIVES * d, arguments * a)
82{
83 const CHAR_T *cp = format; /* pointer into format */
84 size_t arg_posn = 0; /* number of regular arguments consumed */
85 size_t d_allocated; /* allocated elements of d->dir */
86 size_t a_allocated; /* allocated elements of a->arg */
87 size_t max_width_length = 0;
88 size_t max_precision_length = 0;
89
90 d->count = 0;
91 d_allocated = 1;
92 d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE));
93 if (d->dir == NULL)
94 /* Out of memory. */
95 goto out_of_memory_1;
96
97 a->count = 0;
98 a_allocated = 0;
99 a->arg = NULL;
100
101#define REGISTER_ARG(_index_,_type_) \
102 { \
103 size_t n = (_index_); \
104 if (n >= a_allocated) \
105 { \
106 size_t memory_size; \
107 argument *memory; \
108 \
109 a_allocated = xtimes (a_allocated, 2); \
110 if (a_allocated <= n) \
111 a_allocated = xsum (n, 1); \
112 memory_size = xtimes (a_allocated, sizeof (argument)); \
113 if (size_overflow_p (memory_size)) \
114 /* Overflow, would lead to out of memory. */ \
115 goto out_of_memory; \
116 memory = (argument *) (a->arg \
117 ? realloc (a->arg, memory_size) \
118 : malloc (memory_size)); \
119 if (memory == NULL) \
120 /* Out of memory. */ \
121 goto out_of_memory; \
122 a->arg = memory; \
123 } \
124 while (a->count <= n) \
125 a->arg[a->count++].type = TYPE_NONE; \
126 if (a->arg[n].type == TYPE_NONE) \
127 a->arg[n].type = (_type_); \
128 else if (a->arg[n].type != (_type_)) \
129 /* Ambiguous type for positional argument. */ \
130 goto error; \
131 }
132
133 while (*cp != '\0')
134 {
135 CHAR_T c = *cp++;
136 if (c == '%')
137 {
138 size_t arg_index = ARG_NONE;
139 DIRECTIVE *dp = &d->dir[d->count]; /* pointer to next directive */
140
141 /* Initialize the next directive. */
142 dp->dir_start = cp - 1;
143 dp->flags = 0;
144 dp->width_start = NULL;
145 dp->width_end = NULL;
146 dp->width_arg_index = ARG_NONE;
147 dp->precision_start = NULL;
148 dp->precision_end = NULL;
149 dp->precision_arg_index = ARG_NONE;
150 dp->arg_index = ARG_NONE;
151
152 /* Test for positional argument. */
153 if (*cp >= '0' && *cp <= '9')
154 {
155 const CHAR_T *np;
156
157 for (np = cp; *np >= '0' && *np <= '9'; np++)
158 ;
159 if (*np == '$')
160 {
161 size_t n = 0;
162
163 for (np = cp; *np >= '0' && *np <= '9'; np++)
164 n = xsum (xtimes (n, 10), *np - '0');
165 if (n == 0)
166 /* Positional argument 0. */
167 goto error;
168 if (size_overflow_p (n))
169 /* n too large, would lead to out of memory later. */
170 goto error;
171 arg_index = n - 1;
172 cp = np + 1;
173 }
174 }
175
176 /* Read the flags. */
177 for (;;)
178 {
179 if (*cp == '\'')
180 {
181 dp->flags |= FLAG_GROUP;
182 cp++;
183 }
184 else if (*cp == '-')
185 {
186 dp->flags |= FLAG_LEFT;
187 cp++;
188 }
189 else if (*cp == '+')
190 {
191 dp->flags |= FLAG_SHOWSIGN;
192 cp++;
193 }
194 else if (*cp == ' ')
195 {
196 dp->flags |= FLAG_SPACE;
197 cp++;
198 }
199 else if (*cp == '#')
200 {
201 dp->flags |= FLAG_ALT;
202 cp++;
203 }
204 else if (*cp == '0')
205 {
206 dp->flags |= FLAG_ZERO;
207 cp++;
208 }
209 else
210 break;
211 }
212
213 /* Parse the field width. */
214 if (*cp == '*')
215 {
216 dp->width_start = cp;
217 cp++;
218 dp->width_end = cp;
219 if (max_width_length < 1)
220 max_width_length = 1;
221
222 /* Test for positional argument. */
223 if (*cp >= '0' && *cp <= '9')
224 {
225 const CHAR_T *np;
226
227 for (np = cp; *np >= '0' && *np <= '9'; np++)
228 ;
229 if (*np == '$')
230 {
231 size_t n = 0;
232
233 for (np = cp; *np >= '0' && *np <= '9'; np++)
234 n = xsum (xtimes (n, 10), *np - '0');
235 if (n == 0)
236 /* Positional argument 0. */
237 goto error;
238 if (size_overflow_p (n))
239 /* n too large, would lead to out of memory later. */
240 goto error;
241 dp->width_arg_index = n - 1;
242 cp = np + 1;
243 }
244 }
245 if (dp->width_arg_index == ARG_NONE)
246 {
247 dp->width_arg_index = arg_posn++;
248 if (dp->width_arg_index == ARG_NONE)
249 /* arg_posn wrapped around. */
250 goto error;
251 }
252 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
253 }
254 else if (*cp >= '0' && *cp <= '9')
255 {
256 size_t width_length;
257
258 dp->width_start = cp;
259 for (; *cp >= '0' && *cp <= '9'; cp++)
260 ;
261 dp->width_end = cp;
262 width_length = dp->width_end - dp->width_start;
263 if (max_width_length < width_length)
264 max_width_length = width_length;
265 }
266
267 /* Parse the precision. */
268 if (*cp == '.')
269 {
270 cp++;
271 if (*cp == '*')
272 {
273 dp->precision_start = cp - 1;
274 cp++;
275 dp->precision_end = cp;
276 if (max_precision_length < 2)
277 max_precision_length = 2;
278
279 /* Test for positional argument. */
280 if (*cp >= '0' && *cp <= '9')
281 {
282 const CHAR_T *np;
283
284 for (np = cp; *np >= '0' && *np <= '9'; np++)
285 ;
286 if (*np == '$')
287 {
288 size_t n = 0;
289
290 for (np = cp; *np >= '0' && *np <= '9'; np++)
291 n = xsum (xtimes (n, 10), *np - '0');
292 if (n == 0)
293 /* Positional argument 0. */
294 goto error;
295 if (size_overflow_p (n))
296 /* n too large, would lead to out of memory
297 later. */
298 goto error;
299 dp->precision_arg_index = n - 1;
300 cp = np + 1;
301 }
302 }
303 if (dp->precision_arg_index == ARG_NONE)
304 {
305 dp->precision_arg_index = arg_posn++;
306 if (dp->precision_arg_index == ARG_NONE)
307 /* arg_posn wrapped around. */
308 goto error;
309 }
310 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
311 }
312 else
313 {
314 size_t precision_length;
315
316 dp->precision_start = cp - 1;
317 for (; *cp >= '0' && *cp <= '9'; cp++)
318 ;
319 dp->precision_end = cp;
320 precision_length = dp->precision_end - dp->precision_start;
321 if (max_precision_length < precision_length)
322 max_precision_length = precision_length;
323 }
324 }
325
326 {
327 arg_type type;
328
329 /* Parse argument type/size specifiers. */
330 {
331 int flags = 0;
332
333 for (;;)
334 {
335 if (*cp == 'h')
336 {
337 flags |= (1 << (flags & 1));
338 cp++;
339 }
340 else if (*cp == 'L')
341 {
342 flags |= 4;
343 cp++;
344 }
345 else if (*cp == 'l')
346 {
347 flags += 8;
348 cp++;
349 }
350 else if (*cp == 'j')
351 {
352 if (sizeof (intmax_t) > sizeof (long))
353 {
354 /* intmax_t = long long */
355 flags += 16;
356 }
357 else if (sizeof (intmax_t) > sizeof (int))
358 {
359 /* intmax_t = long */
360 flags += 8;
361 }
362 cp++;
363 }
364 else if (*cp == 'z' || *cp == 'Z')
365 {
366 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
367 because the warning facility in gcc-2.95.2 understands
368 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
369 if (sizeof (size_t) > sizeof (long))
370 {
371 /* size_t = long long */
372 flags += 16;
373 }
374 else if (sizeof (size_t) > sizeof (int))
375 {
376 /* size_t = long */
377 flags += 8;
378 }
379 cp++;
380 }
381 else if (*cp == 't')
382 {
383 if (sizeof (ptrdiff_t) > sizeof (long))
384 {
385 /* ptrdiff_t = long long */
386 flags += 16;
387 }
388 else if (sizeof (ptrdiff_t) > sizeof (int))
389 {
390 /* ptrdiff_t = long */
391 flags += 8;
392 }
393 cp++;
394 }
395 else
396 break;
397 }
398
399 /* Read the conversion character. */
400 c = *cp++;
401 switch (c)
402 {
403 case 'd':
404 case 'i':
405#if HAVE_LONG_LONG_INT
406 /* If 'long long' exists and is larger than 'long': */
407 if (flags >= 16 || (flags & 4))
408 type = TYPE_LONGLONGINT;
409 else
410#endif
411 /* If 'long long' exists and is the same as 'long', we parse
412 "lld" into TYPE_LONGINT. */
413 if (flags >= 8)
414 type = TYPE_LONGINT;
415 else if (flags & 2)
416 type = TYPE_SCHAR;
417 else if (flags & 1)
418 type = TYPE_SHORT;
419 else
420 type = TYPE_INT;
421 break;
422 case 'o':
423 case 'u':
424 case 'x':
425 case 'X':
426#if HAVE_LONG_LONG_INT
427 /* If 'long long' exists and is larger than 'long': */
428 if (flags >= 16 || (flags & 4))
429 type = TYPE_ULONGLONGINT;
430 else
431#endif
432 /* If 'unsigned long long' exists and is the same as
433 'unsigned long', we parse "llu" into TYPE_ULONGINT. */
434 if (flags >= 8)
435 type = TYPE_ULONGINT;
436 else if (flags & 2)
437 type = TYPE_UCHAR;
438 else if (flags & 1)
439 type = TYPE_USHORT;
440 else
441 type = TYPE_UINT;
442 break;
443 case 'f':
444 case 'F':
445 case 'e':
446 case 'E':
447 case 'g':
448 case 'G':
449 case 'a':
450 case 'A':
451 if (flags >= 16 || (flags & 4))
452 type = TYPE_LONGDOUBLE;
453 else
454 type = TYPE_DOUBLE;
455 break;
456 case 'c':
457 if (flags >= 8)
458#if HAVE_WINT_T
459 type = TYPE_WIDE_CHAR;
460#else
461 goto error;
462#endif
463 else
464 type = TYPE_CHAR;
465 break;
466#if HAVE_WINT_T
467 case 'C':
468 type = TYPE_WIDE_CHAR;
469 c = 'c';
470 break;
471#endif
472 case 's':
473 if (flags >= 8)
474#if HAVE_WCHAR_T
475 type = TYPE_WIDE_STRING;
476#else
477 goto error;
478#endif
479 else
480 type = TYPE_STRING;
481 break;
482#if HAVE_WCHAR_T
483 case 'S':
484 type = TYPE_WIDE_STRING;
485 c = 's';
486 break;
487#endif
488 case 'p':
489 type = TYPE_POINTER;
490 break;
491 case 'n':
492#if HAVE_LONG_LONG_INT
493 /* If 'long long' exists and is larger than 'long': */
494 if (flags >= 16 || (flags & 4))
495 type = TYPE_COUNT_LONGLONGINT_POINTER;
496 else
497#endif
498 /* If 'long long' exists and is the same as 'long', we parse
499 "lln" into TYPE_COUNT_LONGINT_POINTER. */
500 if (flags >= 8)
501 type = TYPE_COUNT_LONGINT_POINTER;
502 else if (flags & 2)
503 type = TYPE_COUNT_SCHAR_POINTER;
504 else if (flags & 1)
505 type = TYPE_COUNT_SHORT_POINTER;
506 else
507 type = TYPE_COUNT_INT_POINTER;
508 break;
509#if ENABLE_UNISTDIO
510 /* The unistdio extensions. */
511 case 'U':
512 if (flags >= 16)
513 type = TYPE_U32_STRING;
514 else if (flags >= 8)
515 type = TYPE_U16_STRING;
516 else
517 type = TYPE_U8_STRING;
518 break;
519#endif
520 case '%':
521 type = TYPE_NONE;
522 break;
523 default:
524 /* Unknown conversion character. */
525 goto error;
526 }
527 }
528
529 if (type != TYPE_NONE)
530 {
531 dp->arg_index = arg_index;
532 if (dp->arg_index == ARG_NONE)
533 {
534 dp->arg_index = arg_posn++;
535 if (dp->arg_index == ARG_NONE)
536 /* arg_posn wrapped around. */
537 goto error;
538 }
539 REGISTER_ARG (dp->arg_index, type);
540 }
541 dp->conversion = c;
542 dp->dir_end = cp;
543 }
544
545 d->count++;
546 if (d->count >= d_allocated)
547 {
548 size_t memory_size;
549 DIRECTIVE *memory;
550
551 d_allocated = xtimes (d_allocated, 2);
552 memory_size = xtimes (d_allocated, sizeof (DIRECTIVE));
553 if (size_overflow_p (memory_size))
554 /* Overflow, would lead to out of memory. */
555 goto out_of_memory;
556 memory = (DIRECTIVE *) realloc (d->dir, memory_size);
557 if (memory == NULL)
558 /* Out of memory. */
559 goto out_of_memory;
560 d->dir = memory;
561 }
562 }
563#if CHAR_T_ONLY_ASCII
564 else if (!c_isascii (c))
565 {
566 /* Non-ASCII character. Not supported. */
567 goto error;
568 }
569#endif
570 }
571 d->dir[d->count].dir_start = cp;
572
573 d->max_width_length = max_width_length;
574 d->max_precision_length = max_precision_length;
575 return 0;
576
577error:
578 if (a->arg)
579 free (a->arg);
580 if (d->dir)
581 free (d->dir);
582 errno = EINVAL;
583 return -1;
584
585out_of_memory:
586 if (a->arg)
587 free (a->arg);
588 if (d->dir)
589 free (d->dir);
590out_of_memory_1:
591 errno = ENOMEM;
592 return -1;
593}
594
595#undef PRINTF_PARSE
596#undef DIRECTIVES
597#undef DIRECTIVE
598#undef CHAR_T_ONLY_ASCII
599#undef CHAR_T
diff --git a/src/daemon/https/lgl/printf-parse.h b/src/daemon/https/lgl/printf-parse.h
new file mode 100644
index 00000000..f9013278
--- /dev/null
+++ b/src/daemon/https/lgl/printf-parse.h
@@ -0,0 +1,178 @@
1/* Parse printf format string.
2 Copyright (C) 1999, 2002-2003, 2005, 2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _PRINTF_PARSE_H
19#define _PRINTF_PARSE_H
20
21/* This file can be parametrized with the following macros:
22 ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
23 STATIC Set to 'static' to declare the function static. */
24
25#include "printf-args.h"
26
27/* Flags */
28#define FLAG_GROUP 1 /* ' flag */
29#define FLAG_LEFT 2 /* - flag */
30#define FLAG_SHOWSIGN 4 /* + flag */
31#define FLAG_SPACE 8 /* space flag */
32#define FLAG_ALT 16 /* # flag */
33#define FLAG_ZERO 32
34
35/* arg_index value indicating that no argument is consumed. */
36#define ARG_NONE (~(size_t)0)
37
38/* xxx_directive: A parsed directive.
39 xxx_directives: A parsed format string. */
40
41/* A parsed directive. */
42typedef struct
43{
44 const char* dir_start;
45 const char* dir_end;
46 int flags;
47 const char* width_start;
48 const char* width_end;
49 size_t width_arg_index;
50 const char* precision_start;
51 const char* precision_end;
52 size_t precision_arg_index;
53 char conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
54 size_t arg_index;
55}
56char_directive;
57
58/* A parsed format string. */
59typedef struct
60{
61 size_t count;
62 char_directive *dir;
63 size_t max_width_length;
64 size_t max_precision_length;
65}
66char_directives;
67
68#if ENABLE_UNISTDIO
69
70/* A parsed directive. */
71typedef struct
72{
73 const uint8_t* dir_start;
74 const uint8_t* dir_end;
75 int flags;
76 const uint8_t* width_start;
77 const uint8_t* width_end;
78 size_t width_arg_index;
79 const uint8_t* precision_start;
80 const uint8_t* precision_end;
81 size_t precision_arg_index;
82 uint8_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
83 size_t arg_index;
84}
85u8_directive;
86
87/* A parsed format string. */
88typedef struct
89{
90 size_t count;
91 u8_directive *dir;
92 size_t max_width_length;
93 size_t max_precision_length;
94}
95u8_directives;
96
97/* A parsed directive. */
98typedef struct
99{
100 const uint16_t* dir_start;
101 const uint16_t* dir_end;
102 int flags;
103 const uint16_t* width_start;
104 const uint16_t* width_end;
105 size_t width_arg_index;
106 const uint16_t* precision_start;
107 const uint16_t* precision_end;
108 size_t precision_arg_index;
109 uint16_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
110 size_t arg_index;
111}
112u16_directive;
113
114/* A parsed format string. */
115typedef struct
116{
117 size_t count;
118 u16_directive *dir;
119 size_t max_width_length;
120 size_t max_precision_length;
121}
122u16_directives;
123
124/* A parsed directive. */
125typedef struct
126{
127 const uint32_t* dir_start;
128 const uint32_t* dir_end;
129 int flags;
130 const uint32_t* width_start;
131 const uint32_t* width_end;
132 size_t width_arg_index;
133 const uint32_t* precision_start;
134 const uint32_t* precision_end;
135 size_t precision_arg_index;
136 uint32_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
137 size_t arg_index;
138}
139u32_directive;
140
141/* A parsed format string. */
142typedef struct
143{
144 size_t count;
145 u32_directive *dir;
146 size_t max_width_length;
147 size_t max_precision_length;
148}
149u32_directives;
150
151#endif
152
153
154/* Parses the format string. Fills in the number N of directives, and fills
155 in directives[0], ..., directives[N-1], and sets directives[N].dir_start
156 to the end of the format string. Also fills in the arg_type fields of the
157 arguments and the needed count of arguments. */
158#if ENABLE_UNISTDIO
159extern int
160 ulc_printf_parse (const char *format, char_directives *d, arguments *a);
161extern int
162 u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a);
163extern int
164 u16_printf_parse (const uint16_t *format, u16_directives *d,
165 arguments *a);
166extern int
167 u32_printf_parse (const uint32_t *format, u32_directives *d,
168 arguments *a);
169#else
170# ifdef STATIC
171STATIC
172# else
173extern
174# endif
175int printf_parse (const char *format, char_directives *d, arguments *a);
176#endif
177
178#endif /* _PRINTF_PARSE_H */
diff --git a/src/daemon/https/lgl/read-file.c b/src/daemon/https/lgl/read-file.c
new file mode 100644
index 00000000..9172a6ef
--- /dev/null
+++ b/src/daemon/https/lgl/read-file.c
@@ -0,0 +1,136 @@
1/* read-file.c -- read file contents into a string
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by Simon Josefsson and Bruno Haible.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#include <config.h>
20
21#include "read-file.h"
22
23/* Get realloc, free. */
24#include <stdlib.h>
25
26/* Get errno. */
27#include <errno.h>
28
29/* Read a STREAM and return a newly allocated string with the content,
30 and set *LENGTH to the length of the string. The string is
31 zero-terminated, but the terminating zero byte is not counted in
32 *LENGTH. On errors, *LENGTH is undefined, errno preserves the
33 values set by system functions (if any), and NULL is returned. */
34char *
35fread_file (FILE * stream, size_t * length)
36{
37 char *buf = NULL;
38 size_t alloc = 0;
39 size_t size = 0;
40 int save_errno;
41
42 for (;;)
43 {
44 size_t count;
45 size_t requested;
46
47 if (size + BUFSIZ + 1 > alloc)
48 {
49 char *new_buf;
50
51 alloc += alloc / 2;
52 if (alloc < size + BUFSIZ + 1)
53 alloc = size + BUFSIZ + 1;
54
55 new_buf = realloc (buf, alloc);
56 if (!new_buf)
57 {
58 save_errno = errno;
59 break;
60 }
61
62 buf = new_buf;
63 }
64
65 requested = alloc - size - 1;
66 count = fread (buf + size, 1, requested, stream);
67 size += count;
68
69 if (count != requested)
70 {
71 save_errno = errno;
72 if (ferror (stream))
73 break;
74 buf[size] = '\0';
75 *length = size;
76 return buf;
77 }
78 }
79
80 free (buf);
81 errno = save_errno;
82 return NULL;
83}
84
85static char *
86internal_read_file (const char *filename, size_t * length, const char *mode)
87{
88 FILE *stream = fopen (filename, mode);
89 char *out;
90 int save_errno;
91
92 if (!stream)
93 return NULL;
94
95 out = fread_file (stream, length);
96
97 save_errno = errno;
98
99 if (fclose (stream) != 0)
100 {
101 if (out)
102 {
103 save_errno = errno;
104 free (out);
105 }
106 errno = save_errno;
107 return NULL;
108 }
109
110 return out;
111}
112
113/* Open and read the contents of FILENAME, and return a newly
114 allocated string with the content, and set *LENGTH to the length of
115 the string. The string is zero-terminated, but the terminating
116 zero byte is not counted in *LENGTH. On errors, *LENGTH is
117 undefined, errno preserves the values set by system functions (if
118 any), and NULL is returned. */
119char *
120read_file (const char *filename, size_t * length)
121{
122 return internal_read_file (filename, length, "r");
123}
124
125/* Open (on non-POSIX systems, in binary mode) and read the contents
126 of FILENAME, and return a newly allocated string with the content,
127 and set LENGTH to the length of the string. The string is
128 zero-terminated, but the terminating zero byte is not counted in
129 the LENGTH variable. On errors, *LENGTH is undefined, errno
130 preserves the values set by system functions (if any), and NULL is
131 returned. */
132char *
133read_binary_file (const char *filename, size_t * length)
134{
135 return internal_read_file (filename, length, "rb");
136}
diff --git a/src/daemon/https/lgl/read-file.h b/src/daemon/https/lgl/read-file.h
new file mode 100644
index 00000000..798d841f
--- /dev/null
+++ b/src/daemon/https/lgl/read-file.h
@@ -0,0 +1,34 @@
1/* read-file.h -- read file contents into a string
2 Copyright (C) 2006 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef READ_FILE_H
20#define READ_FILE_H
21
22/* Get size_t. */
23#include <stddef.h>
24
25/* Get FILE. */
26#include <stdio.h>
27
28extern char *fread_file (FILE * stream, size_t * length);
29
30extern char *read_file (const char *filename, size_t * length);
31
32extern char *read_binary_file (const char *filename, size_t * length);
33
34#endif /* READ_FILE_H */
diff --git a/src/daemon/https/lgl/realloc.c b/src/daemon/https/lgl/realloc.c
new file mode 100644
index 00000000..4661f913
--- /dev/null
+++ b/src/daemon/https/lgl/realloc.c
@@ -0,0 +1,87 @@
1/* realloc() function that is glibc compatible.
2
3 Copyright (C) 1997, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18/* written by Jim Meyering and Bruno Haible */
19
20#include <config.h>
21
22/* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h. */
23#ifdef realloc
24# define NEED_REALLOC_GNU 1
25#endif
26
27/* Infer the properties of the system's malloc function.
28 Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h. */
29#if GNULIB_MALLOC_GNU && !defined malloc
30# define SYSTEM_MALLOC_GLIBC_COMPATIBLE 1
31#endif
32
33/* Below we want to call the system's malloc and realloc.
34 Undefine the symbols here so that including <stdlib.h> provides a
35 declaration of malloc(), not of rpl_malloc(), and likewise for realloc. */
36#undef malloc
37#undef realloc
38
39/* Specification. */
40#include <stdlib.h>
41
42#include <errno.h>
43
44/* Below we want to call the system's malloc and realloc.
45 Undefine the symbols, if they were defined by gnulib's <stdlib.h>
46 replacement. */
47#undef malloc
48#undef realloc
49
50/* Change the size of an allocated block of memory P to N bytes,
51 with error checking. If N is zero, change it to 1. If P is NULL,
52 use malloc. */
53
54void *
55rpl_realloc (void *p, size_t n)
56{
57 void *result;
58
59#if NEED_REALLOC_GNU
60 if (n == 0)
61 {
62 n = 1;
63
64 /* In theory realloc might fail, so don't rely on it to free. */
65 free (p);
66 p = NULL;
67 }
68#endif
69
70 if (p == NULL)
71 {
72#if GNULIB_REALLOC_GNU && !NEED_REALLOC_GNU && !SYSTEM_MALLOC_GLIBC_COMPATIBLE
73 if (n == 0)
74 n = 1;
75#endif
76 result = malloc (n);
77 }
78 else
79 result = realloc (p, n);
80
81#if !HAVE_REALLOC_POSIX
82 if (result == NULL)
83 errno = ENOMEM;
84#endif
85
86 return result;
87}
diff --git a/src/daemon/https/lgl/rijndael-alg-fst.c b/src/daemon/https/lgl/rijndael-alg-fst.c
new file mode 100644
index 00000000..5baa0e95
--- /dev/null
+++ b/src/daemon/https/lgl/rijndael-alg-fst.c
@@ -0,0 +1,1083 @@
1/* rijndael-alg-fst.c --- Rijndael cipher implementation.
2 * Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Adapted for gnulib by Simon Josefsson.
22 *
23 * Based on public domain "Optimised C code" retrieved from (SHA1
24 * 7c8e4b00d06685d1dbc6724a9e0d502353de339e):
25 * http://www.iaik.tu-graz.ac.at/research/krypto/AES/old/~rijmen/rijndael/rijndael-fst-3.0.zip
26 */
27
28#include <config.h>
29
30/**
31 * rijndael-alg-fst.c
32 *
33 * @version 3.0 (December 2000)
34 *
35 * Optimised ANSI C code for the Rijndael cipher (now AES)
36 *
37 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
38 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
39 * @author Paulo Barreto <paulo.barreto@terra.com.br>
40 *
41 * This code is hereby placed in the public domain.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
50 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
52 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
53 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 */
55
56#include "rijndael-alg-fst.h"
57
58/*
59Te0[x] = S [x].[02, 01, 01, 03];
60Te1[x] = S [x].[03, 02, 01, 01];
61Te2[x] = S [x].[01, 03, 02, 01];
62Te3[x] = S [x].[01, 01, 03, 02];
63Te4[x] = S [x].[01, 01, 01, 01];
64
65Td0[x] = Si[x].[0e, 09, 0d, 0b];
66Td1[x] = Si[x].[0b, 0e, 09, 0d];
67Td2[x] = Si[x].[0d, 0b, 0e, 09];
68Td3[x] = Si[x].[09, 0d, 0b, 0e];
69Td4[x] = Si[x].[01, 01, 01, 01];
70*/
71
72static const uint32_t Te0[256] = {
73 0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d,
74 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
75 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d,
76 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
77 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87,
78 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b,
79 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea,
80 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b,
81 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a,
82 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f,
83 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108,
84 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f,
85 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e,
86 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5,
87 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d,
88 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f,
89 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e,
90 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb,
91 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce,
92 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497,
93 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c,
94 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed,
95 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b,
96 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a,
97 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16,
98 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594,
99 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81,
100 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3,
101 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a,
102 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504,
103 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163,
104 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d,
105 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f,
106 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739,
107 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47,
108 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395,
109 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f,
110 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883,
111 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c,
112 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76,
113 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e,
114 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4,
115 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6,
116 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b,
117 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7,
118 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0,
119 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25,
120 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818,
121 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72,
122 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651,
123 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21,
124 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85,
125 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa,
126 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12,
127 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0,
128 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9,
129 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133,
130 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7,
131 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920,
132 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a,
133 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17,
134 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8,
135 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11,
136 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a,
137};
138static const uint32_t Te1[256] = {
139 0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b,
140 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5,
141 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b,
142 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676,
143 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d,
144 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0,
145 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf,
146 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0,
147 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626,
148 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc,
149 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1,
150 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515,
151 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3,
152 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a,
153 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2,
154 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575,
155 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a,
156 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0,
157 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3,
158 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484,
159 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded,
160 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b,
161 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939,
162 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf,
163 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb,
164 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585,
165 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f,
166 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8,
167 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f,
168 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5,
169 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121,
170 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2,
171 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec,
172 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717,
173 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d,
174 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373,
175 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc,
176 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888,
177 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414,
178 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb,
179 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a,
180 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c,
181 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262,
182 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979,
183 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d,
184 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9,
185 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea,
186 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808,
187 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e,
188 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6,
189 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f,
190 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a,
191 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666,
192 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e,
193 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9,
194 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e,
195 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111,
196 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494,
197 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9,
198 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf,
199 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d,
200 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868,
201 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f,
202 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616,
203};
204static const uint32_t Te2[256] = {
205 0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b,
206 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5,
207 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b,
208 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76,
209 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d,
210 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0,
211 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af,
212 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0,
213 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26,
214 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc,
215 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1,
216 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15,
217 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3,
218 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a,
219 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2,
220 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75,
221 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a,
222 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0,
223 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3,
224 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384,
225 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed,
226 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b,
227 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239,
228 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf,
229 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb,
230 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185,
231 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f,
232 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8,
233 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f,
234 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5,
235 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221,
236 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2,
237 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec,
238 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17,
239 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d,
240 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673,
241 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc,
242 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88,
243 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814,
244 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb,
245 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a,
246 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c,
247 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462,
248 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279,
249 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d,
250 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9,
251 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea,
252 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008,
253 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e,
254 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6,
255 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f,
256 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a,
257 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66,
258 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e,
259 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9,
260 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e,
261 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211,
262 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394,
263 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9,
264 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df,
265 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d,
266 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068,
267 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f,
268 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16,
269};
270static const uint32_t Te3[256] = {
271 0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6,
272 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491,
273 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56,
274 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec,
275 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa,
276 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb,
277 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45,
278 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b,
279 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c,
280 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83,
281 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9,
282 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a,
283 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d,
284 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f,
285 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf,
286 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea,
287 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34,
288 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b,
289 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d,
290 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713,
291 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1,
292 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6,
293 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72,
294 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85,
295 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed,
296 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411,
297 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe,
298 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b,
299 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05,
300 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1,
301 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342,
302 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf,
303 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3,
304 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e,
305 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a,
306 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6,
307 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3,
308 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b,
309 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28,
310 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad,
311 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14,
312 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8,
313 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4,
314 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2,
315 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da,
316 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049,
317 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf,
318 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810,
319 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c,
320 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197,
321 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e,
322 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f,
323 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc,
324 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c,
325 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069,
326 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927,
327 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322,
328 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733,
329 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9,
330 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5,
331 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a,
332 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0,
333 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e,
334 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c,
335};
336static const uint32_t Te4[256] = {
337 0x63636363, 0x7c7c7c7c, 0x77777777, 0x7b7b7b7b,
338 0xf2f2f2f2, 0x6b6b6b6b, 0x6f6f6f6f, 0xc5c5c5c5,
339 0x30303030, 0x01010101, 0x67676767, 0x2b2b2b2b,
340 0xfefefefe, 0xd7d7d7d7, 0xabababab, 0x76767676,
341 0xcacacaca, 0x82828282, 0xc9c9c9c9, 0x7d7d7d7d,
342 0xfafafafa, 0x59595959, 0x47474747, 0xf0f0f0f0,
343 0xadadadad, 0xd4d4d4d4, 0xa2a2a2a2, 0xafafafaf,
344 0x9c9c9c9c, 0xa4a4a4a4, 0x72727272, 0xc0c0c0c0,
345 0xb7b7b7b7, 0xfdfdfdfd, 0x93939393, 0x26262626,
346 0x36363636, 0x3f3f3f3f, 0xf7f7f7f7, 0xcccccccc,
347 0x34343434, 0xa5a5a5a5, 0xe5e5e5e5, 0xf1f1f1f1,
348 0x71717171, 0xd8d8d8d8, 0x31313131, 0x15151515,
349 0x04040404, 0xc7c7c7c7, 0x23232323, 0xc3c3c3c3,
350 0x18181818, 0x96969696, 0x05050505, 0x9a9a9a9a,
351 0x07070707, 0x12121212, 0x80808080, 0xe2e2e2e2,
352 0xebebebeb, 0x27272727, 0xb2b2b2b2, 0x75757575,
353 0x09090909, 0x83838383, 0x2c2c2c2c, 0x1a1a1a1a,
354 0x1b1b1b1b, 0x6e6e6e6e, 0x5a5a5a5a, 0xa0a0a0a0,
355 0x52525252, 0x3b3b3b3b, 0xd6d6d6d6, 0xb3b3b3b3,
356 0x29292929, 0xe3e3e3e3, 0x2f2f2f2f, 0x84848484,
357 0x53535353, 0xd1d1d1d1, 0x00000000, 0xedededed,
358 0x20202020, 0xfcfcfcfc, 0xb1b1b1b1, 0x5b5b5b5b,
359 0x6a6a6a6a, 0xcbcbcbcb, 0xbebebebe, 0x39393939,
360 0x4a4a4a4a, 0x4c4c4c4c, 0x58585858, 0xcfcfcfcf,
361 0xd0d0d0d0, 0xefefefef, 0xaaaaaaaa, 0xfbfbfbfb,
362 0x43434343, 0x4d4d4d4d, 0x33333333, 0x85858585,
363 0x45454545, 0xf9f9f9f9, 0x02020202, 0x7f7f7f7f,
364 0x50505050, 0x3c3c3c3c, 0x9f9f9f9f, 0xa8a8a8a8,
365 0x51515151, 0xa3a3a3a3, 0x40404040, 0x8f8f8f8f,
366 0x92929292, 0x9d9d9d9d, 0x38383838, 0xf5f5f5f5,
367 0xbcbcbcbc, 0xb6b6b6b6, 0xdadadada, 0x21212121,
368 0x10101010, 0xffffffff, 0xf3f3f3f3, 0xd2d2d2d2,
369 0xcdcdcdcd, 0x0c0c0c0c, 0x13131313, 0xecececec,
370 0x5f5f5f5f, 0x97979797, 0x44444444, 0x17171717,
371 0xc4c4c4c4, 0xa7a7a7a7, 0x7e7e7e7e, 0x3d3d3d3d,
372 0x64646464, 0x5d5d5d5d, 0x19191919, 0x73737373,
373 0x60606060, 0x81818181, 0x4f4f4f4f, 0xdcdcdcdc,
374 0x22222222, 0x2a2a2a2a, 0x90909090, 0x88888888,
375 0x46464646, 0xeeeeeeee, 0xb8b8b8b8, 0x14141414,
376 0xdededede, 0x5e5e5e5e, 0x0b0b0b0b, 0xdbdbdbdb,
377 0xe0e0e0e0, 0x32323232, 0x3a3a3a3a, 0x0a0a0a0a,
378 0x49494949, 0x06060606, 0x24242424, 0x5c5c5c5c,
379 0xc2c2c2c2, 0xd3d3d3d3, 0xacacacac, 0x62626262,
380 0x91919191, 0x95959595, 0xe4e4e4e4, 0x79797979,
381 0xe7e7e7e7, 0xc8c8c8c8, 0x37373737, 0x6d6d6d6d,
382 0x8d8d8d8d, 0xd5d5d5d5, 0x4e4e4e4e, 0xa9a9a9a9,
383 0x6c6c6c6c, 0x56565656, 0xf4f4f4f4, 0xeaeaeaea,
384 0x65656565, 0x7a7a7a7a, 0xaeaeaeae, 0x08080808,
385 0xbabababa, 0x78787878, 0x25252525, 0x2e2e2e2e,
386 0x1c1c1c1c, 0xa6a6a6a6, 0xb4b4b4b4, 0xc6c6c6c6,
387 0xe8e8e8e8, 0xdddddddd, 0x74747474, 0x1f1f1f1f,
388 0x4b4b4b4b, 0xbdbdbdbd, 0x8b8b8b8b, 0x8a8a8a8a,
389 0x70707070, 0x3e3e3e3e, 0xb5b5b5b5, 0x66666666,
390 0x48484848, 0x03030303, 0xf6f6f6f6, 0x0e0e0e0e,
391 0x61616161, 0x35353535, 0x57575757, 0xb9b9b9b9,
392 0x86868686, 0xc1c1c1c1, 0x1d1d1d1d, 0x9e9e9e9e,
393 0xe1e1e1e1, 0xf8f8f8f8, 0x98989898, 0x11111111,
394 0x69696969, 0xd9d9d9d9, 0x8e8e8e8e, 0x94949494,
395 0x9b9b9b9b, 0x1e1e1e1e, 0x87878787, 0xe9e9e9e9,
396 0xcececece, 0x55555555, 0x28282828, 0xdfdfdfdf,
397 0x8c8c8c8c, 0xa1a1a1a1, 0x89898989, 0x0d0d0d0d,
398 0xbfbfbfbf, 0xe6e6e6e6, 0x42424242, 0x68686868,
399 0x41414141, 0x99999999, 0x2d2d2d2d, 0x0f0f0f0f,
400 0xb0b0b0b0, 0x54545454, 0xbbbbbbbb, 0x16161616,
401};
402static const uint32_t Td0[256] = {
403 0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96,
404 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393,
405 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25,
406 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f,
407 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1,
408 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6,
409 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da,
410 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844,
411 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd,
412 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4,
413 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45,
414 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94,
415 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7,
416 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a,
417 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5,
418 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c,
419 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1,
420 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a,
421 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75,
422 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051,
423 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46,
424 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff,
425 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77,
426 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb,
427 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000,
428 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e,
429 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927,
430 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a,
431 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e,
432 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16,
433 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d,
434 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8,
435 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd,
436 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34,
437 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163,
438 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120,
439 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d,
440 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0,
441 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422,
442 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef,
443 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36,
444 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4,
445 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662,
446 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5,
447 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3,
448 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b,
449 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8,
450 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6,
451 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6,
452 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0,
453 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815,
454 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f,
455 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df,
456 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f,
457 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e,
458 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713,
459 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89,
460 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c,
461 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf,
462 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86,
463 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f,
464 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541,
465 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190,
466 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742,
467};
468static const uint32_t Td1[256] = {
469 0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e,
470 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303,
471 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c,
472 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3,
473 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0,
474 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9,
475 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259,
476 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8,
477 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971,
478 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a,
479 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f,
480 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b,
481 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8,
482 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab,
483 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708,
484 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682,
485 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2,
486 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe,
487 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb,
488 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10,
489 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd,
490 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015,
491 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e,
492 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee,
493 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000,
494 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72,
495 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39,
496 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e,
497 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91,
498 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a,
499 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17,
500 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9,
501 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60,
502 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e,
503 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1,
504 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611,
505 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1,
506 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3,
507 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964,
508 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390,
509 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b,
510 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf,
511 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46,
512 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af,
513 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512,
514 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb,
515 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a,
516 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8,
517 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c,
518 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266,
519 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8,
520 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6,
521 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604,
522 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551,
523 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41,
524 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647,
525 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c,
526 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1,
527 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737,
528 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db,
529 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340,
530 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95,
531 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1,
532 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857,
533};
534static const uint32_t Td2[256] = {
535 0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27,
536 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3,
537 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502,
538 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562,
539 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe,
540 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3,
541 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552,
542 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9,
543 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9,
544 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce,
545 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253,
546 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908,
547 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b,
548 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655,
549 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337,
550 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16,
551 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69,
552 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6,
553 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6,
554 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e,
555 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6,
556 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050,
557 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9,
558 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8,
559 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000,
560 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a,
561 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d,
562 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436,
563 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b,
564 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12,
565 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b,
566 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e,
567 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f,
568 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb,
569 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4,
570 0xdccad731, 0x85104263, 0x22401397, 0x112084c6,
571 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729,
572 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1,
573 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9,
574 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233,
575 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4,
576 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad,
577 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e,
578 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3,
579 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25,
580 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b,
581 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f,
582 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15,
583 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0,
584 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2,
585 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7,
586 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791,
587 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496,
588 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665,
589 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b,
590 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6,
591 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13,
592 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47,
593 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7,
594 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844,
595 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3,
596 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d,
597 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456,
598 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8,
599};
600static const uint32_t Td3[256] = {
601 0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a,
602 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b,
603 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5,
604 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5,
605 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d,
606 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b,
607 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95,
608 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e,
609 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27,
610 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d,
611 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562,
612 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9,
613 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752,
614 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66,
615 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3,
616 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced,
617 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e,
618 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4,
619 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4,
620 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd,
621 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d,
622 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60,
623 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767,
624 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79,
625 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000,
626 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c,
627 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736,
628 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24,
629 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b,
630 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c,
631 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12,
632 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814,
633 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3,
634 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b,
635 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8,
636 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084,
637 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7,
638 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077,
639 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247,
640 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22,
641 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698,
642 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f,
643 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254,
644 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582,
645 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf,
646 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb,
647 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883,
648 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef,
649 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629,
650 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035,
651 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533,
652 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17,
653 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4,
654 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46,
655 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb,
656 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d,
657 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb,
658 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a,
659 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73,
660 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678,
661 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2,
662 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff,
663 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064,
664 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0,
665};
666static const uint32_t Td4[256] = {
667 0x52525252, 0x09090909, 0x6a6a6a6a, 0xd5d5d5d5,
668 0x30303030, 0x36363636, 0xa5a5a5a5, 0x38383838,
669 0xbfbfbfbf, 0x40404040, 0xa3a3a3a3, 0x9e9e9e9e,
670 0x81818181, 0xf3f3f3f3, 0xd7d7d7d7, 0xfbfbfbfb,
671 0x7c7c7c7c, 0xe3e3e3e3, 0x39393939, 0x82828282,
672 0x9b9b9b9b, 0x2f2f2f2f, 0xffffffff, 0x87878787,
673 0x34343434, 0x8e8e8e8e, 0x43434343, 0x44444444,
674 0xc4c4c4c4, 0xdededede, 0xe9e9e9e9, 0xcbcbcbcb,
675 0x54545454, 0x7b7b7b7b, 0x94949494, 0x32323232,
676 0xa6a6a6a6, 0xc2c2c2c2, 0x23232323, 0x3d3d3d3d,
677 0xeeeeeeee, 0x4c4c4c4c, 0x95959595, 0x0b0b0b0b,
678 0x42424242, 0xfafafafa, 0xc3c3c3c3, 0x4e4e4e4e,
679 0x08080808, 0x2e2e2e2e, 0xa1a1a1a1, 0x66666666,
680 0x28282828, 0xd9d9d9d9, 0x24242424, 0xb2b2b2b2,
681 0x76767676, 0x5b5b5b5b, 0xa2a2a2a2, 0x49494949,
682 0x6d6d6d6d, 0x8b8b8b8b, 0xd1d1d1d1, 0x25252525,
683 0x72727272, 0xf8f8f8f8, 0xf6f6f6f6, 0x64646464,
684 0x86868686, 0x68686868, 0x98989898, 0x16161616,
685 0xd4d4d4d4, 0xa4a4a4a4, 0x5c5c5c5c, 0xcccccccc,
686 0x5d5d5d5d, 0x65656565, 0xb6b6b6b6, 0x92929292,
687 0x6c6c6c6c, 0x70707070, 0x48484848, 0x50505050,
688 0xfdfdfdfd, 0xedededed, 0xb9b9b9b9, 0xdadadada,
689 0x5e5e5e5e, 0x15151515, 0x46464646, 0x57575757,
690 0xa7a7a7a7, 0x8d8d8d8d, 0x9d9d9d9d, 0x84848484,
691 0x90909090, 0xd8d8d8d8, 0xabababab, 0x00000000,
692 0x8c8c8c8c, 0xbcbcbcbc, 0xd3d3d3d3, 0x0a0a0a0a,
693 0xf7f7f7f7, 0xe4e4e4e4, 0x58585858, 0x05050505,
694 0xb8b8b8b8, 0xb3b3b3b3, 0x45454545, 0x06060606,
695 0xd0d0d0d0, 0x2c2c2c2c, 0x1e1e1e1e, 0x8f8f8f8f,
696 0xcacacaca, 0x3f3f3f3f, 0x0f0f0f0f, 0x02020202,
697 0xc1c1c1c1, 0xafafafaf, 0xbdbdbdbd, 0x03030303,
698 0x01010101, 0x13131313, 0x8a8a8a8a, 0x6b6b6b6b,
699 0x3a3a3a3a, 0x91919191, 0x11111111, 0x41414141,
700 0x4f4f4f4f, 0x67676767, 0xdcdcdcdc, 0xeaeaeaea,
701 0x97979797, 0xf2f2f2f2, 0xcfcfcfcf, 0xcececece,
702 0xf0f0f0f0, 0xb4b4b4b4, 0xe6e6e6e6, 0x73737373,
703 0x96969696, 0xacacacac, 0x74747474, 0x22222222,
704 0xe7e7e7e7, 0xadadadad, 0x35353535, 0x85858585,
705 0xe2e2e2e2, 0xf9f9f9f9, 0x37373737, 0xe8e8e8e8,
706 0x1c1c1c1c, 0x75757575, 0xdfdfdfdf, 0x6e6e6e6e,
707 0x47474747, 0xf1f1f1f1, 0x1a1a1a1a, 0x71717171,
708 0x1d1d1d1d, 0x29292929, 0xc5c5c5c5, 0x89898989,
709 0x6f6f6f6f, 0xb7b7b7b7, 0x62626262, 0x0e0e0e0e,
710 0xaaaaaaaa, 0x18181818, 0xbebebebe, 0x1b1b1b1b,
711 0xfcfcfcfc, 0x56565656, 0x3e3e3e3e, 0x4b4b4b4b,
712 0xc6c6c6c6, 0xd2d2d2d2, 0x79797979, 0x20202020,
713 0x9a9a9a9a, 0xdbdbdbdb, 0xc0c0c0c0, 0xfefefefe,
714 0x78787878, 0xcdcdcdcd, 0x5a5a5a5a, 0xf4f4f4f4,
715 0x1f1f1f1f, 0xdddddddd, 0xa8a8a8a8, 0x33333333,
716 0x88888888, 0x07070707, 0xc7c7c7c7, 0x31313131,
717 0xb1b1b1b1, 0x12121212, 0x10101010, 0x59595959,
718 0x27272727, 0x80808080, 0xecececec, 0x5f5f5f5f,
719 0x60606060, 0x51515151, 0x7f7f7f7f, 0xa9a9a9a9,
720 0x19191919, 0xb5b5b5b5, 0x4a4a4a4a, 0x0d0d0d0d,
721 0x2d2d2d2d, 0xe5e5e5e5, 0x7a7a7a7a, 0x9f9f9f9f,
722 0x93939393, 0xc9c9c9c9, 0x9c9c9c9c, 0xefefefef,
723 0xa0a0a0a0, 0xe0e0e0e0, 0x3b3b3b3b, 0x4d4d4d4d,
724 0xaeaeaeae, 0x2a2a2a2a, 0xf5f5f5f5, 0xb0b0b0b0,
725 0xc8c8c8c8, 0xebebebeb, 0xbbbbbbbb, 0x3c3c3c3c,
726 0x83838383, 0x53535353, 0x99999999, 0x61616161,
727 0x17171717, 0x2b2b2b2b, 0x04040404, 0x7e7e7e7e,
728 0xbabababa, 0x77777777, 0xd6d6d6d6, 0x26262626,
729 0xe1e1e1e1, 0x69696969, 0x14141414, 0x63636363,
730 0x55555555, 0x21212121, 0x0c0c0c0c, 0x7d7d7d7d,
731};
732static const uint32_t rcon[] = {
733 0x01000000, 0x02000000, 0x04000000, 0x08000000,
734 0x10000000, 0x20000000, 0x40000000, 0x80000000,
735 0x1B000000, 0x36000000
736 /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
737};
738
739#define GETU32(pt) (((uint32_t)((pt)[0] & 0xFF) << 24) ^ \
740 ((uint32_t)((pt)[1] & 0xFF) << 16) ^ \
741 ((uint32_t)((pt)[2] & 0xFF) << 8) ^ \
742 ((uint32_t)((pt)[3] & 0xFF)))
743#define PUTU32(ct, st) { \
744 (ct)[0] = (char)((st) >> 24); \
745 (ct)[1] = (char)((st) >> 16); \
746 (ct)[2] = (char)((st) >> 8); \
747 (ct)[3] = (char)(st); }
748
749/**
750 * Expand the cipher key into the encryption key schedule.
751 *
752 * @return the number of rounds for the given cipher key size.
753 */
754int
755rijndaelKeySetupEnc (uint32_t rk[ /*4*(Nr + 1) */ ],
756 const char cipherKey[], size_t keyBits)
757{
758 size_t i = 0;
759 uint32_t temp;
760
761 rk[0] = GETU32 (cipherKey);
762 rk[1] = GETU32 (cipherKey + 4);
763 rk[2] = GETU32 (cipherKey + 8);
764 rk[3] = GETU32 (cipherKey + 12);
765 if (keyBits == 128)
766 {
767 for (;;)
768 {
769 temp = rk[3];
770 rk[4] = rk[0] ^
771 (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
772 (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
773 (Te4[(temp) & 0xff] & 0x0000ff00) ^
774 (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
775 rk[5] = rk[1] ^ rk[4];
776 rk[6] = rk[2] ^ rk[5];
777 rk[7] = rk[3] ^ rk[6];
778 if (++i == 10)
779 {
780 return 10;
781 }
782 rk += 4;
783 }
784 }
785 rk[4] = GETU32 (cipherKey + 16);
786 rk[5] = GETU32 (cipherKey + 20);
787 if (keyBits == 192)
788 {
789 for (;;)
790 {
791 temp = rk[5];
792 rk[6] = rk[0] ^
793 (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
794 (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
795 (Te4[(temp) & 0xff] & 0x0000ff00) ^
796 (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
797 rk[7] = rk[1] ^ rk[6];
798 rk[8] = rk[2] ^ rk[7];
799 rk[9] = rk[3] ^ rk[8];
800 if (++i == 8)
801 {
802 return 12;
803 }
804 rk[10] = rk[4] ^ rk[9];
805 rk[11] = rk[5] ^ rk[10];
806 rk += 6;
807 }
808 }
809 rk[6] = GETU32 (cipherKey + 24);
810 rk[7] = GETU32 (cipherKey + 28);
811 if (keyBits == 256)
812 {
813 for (;;)
814 {
815 temp = rk[7];
816 rk[8] = rk[0] ^
817 (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
818 (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
819 (Te4[(temp) & 0xff] & 0x0000ff00) ^
820 (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
821 rk[9] = rk[1] ^ rk[8];
822 rk[10] = rk[2] ^ rk[9];
823 rk[11] = rk[3] ^ rk[10];
824 if (++i == 7)
825 {
826 return 14;
827 }
828 temp = rk[11];
829 rk[12] = rk[4] ^
830 (Te4[(temp >> 24)] & 0xff000000) ^
831 (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
832 (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^
833 (Te4[(temp) & 0xff] & 0x000000ff);
834 rk[13] = rk[5] ^ rk[12];
835 rk[14] = rk[6] ^ rk[13];
836 rk[15] = rk[7] ^ rk[14];
837
838 rk += 8;
839 }
840 }
841 return 0;
842}
843
844/**
845 * Expand the cipher key into the decryption key schedule.
846 *
847 * @return the number of rounds for the given cipher key size.
848 */
849int
850rijndaelKeySetupDec (uint32_t rk[ /*4*(Nr + 1) */ ],
851 const char cipherKey[], size_t keyBits)
852{
853 size_t Nr, i, j;
854 uint32_t temp;
855
856 /* expand the cipher key: */
857 Nr = rijndaelKeySetupEnc (rk, cipherKey, keyBits);
858 /* invert the order of the round keys: */
859 for (i = 0, j = 4 * Nr; i < j; i += 4, j -= 4)
860 {
861 temp = rk[i];
862 rk[i] = rk[j];
863 rk[j] = temp;
864 temp = rk[i + 1];
865 rk[i + 1] = rk[j + 1];
866 rk[j + 1] = temp;
867 temp = rk[i + 2];
868 rk[i + 2] = rk[j + 2];
869 rk[j + 2] = temp;
870 temp = rk[i + 3];
871 rk[i + 3] = rk[j + 3];
872 rk[j + 3] = temp;
873 }
874 /* apply the inverse MixColumn transform to all round keys but the
875 first and the last: */
876 for (i = 1; i < Nr; i++)
877 {
878 rk += 4;
879 rk[0] =
880 Td0[Te4[(rk[0] >> 24)] & 0xff] ^
881 Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^
882 Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^
883 Td3[Te4[(rk[0]) & 0xff] & 0xff];
884 rk[1] =
885 Td0[Te4[(rk[1] >> 24)] & 0xff] ^
886 Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^
887 Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^
888 Td3[Te4[(rk[1]) & 0xff] & 0xff];
889 rk[2] =
890 Td0[Te4[(rk[2] >> 24)] & 0xff] ^
891 Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^
892 Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^
893 Td3[Te4[(rk[2]) & 0xff] & 0xff];
894 rk[3] =
895 Td0[Te4[(rk[3] >> 24)] & 0xff] ^
896 Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^
897 Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^
898 Td3[Te4[(rk[3]) & 0xff] & 0xff];
899 }
900 return Nr;
901}
902
903void
904rijndaelEncrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
905 const char pt[16], char ct[16])
906{
907 uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
908 size_t r;
909
910 /*
911 * map byte array block to cipher state
912 * and add initial round key:
913 */
914 s0 = GETU32 (pt) ^ rk[0];
915 s1 = GETU32 (pt + 4) ^ rk[1];
916 s2 = GETU32 (pt + 8) ^ rk[2];
917 s3 = GETU32 (pt + 12) ^ rk[3];
918 /*
919 * Nr - 1 full rounds:
920 */
921 r = Nr >> 1;
922 for (;;)
923 {
924 t0 =
925 Te0[(s0 >> 24)] ^
926 Te1[(s1 >> 16) & 0xff] ^
927 Te2[(s2 >> 8) & 0xff] ^ Te3[(s3) & 0xff] ^ rk[4];
928 t1 =
929 Te0[(s1 >> 24)] ^
930 Te1[(s2 >> 16) & 0xff] ^
931 Te2[(s3 >> 8) & 0xff] ^ Te3[(s0) & 0xff] ^ rk[5];
932 t2 =
933 Te0[(s2 >> 24)] ^
934 Te1[(s3 >> 16) & 0xff] ^
935 Te2[(s0 >> 8) & 0xff] ^ Te3[(s1) & 0xff] ^ rk[6];
936 t3 =
937 Te0[(s3 >> 24)] ^
938 Te1[(s0 >> 16) & 0xff] ^
939 Te2[(s1 >> 8) & 0xff] ^ Te3[(s2) & 0xff] ^ rk[7];
940
941 rk += 8;
942 if (--r == 0)
943 {
944 break;
945 }
946
947 s0 =
948 Te0[(t0 >> 24)] ^
949 Te1[(t1 >> 16) & 0xff] ^
950 Te2[(t2 >> 8) & 0xff] ^ Te3[(t3) & 0xff] ^ rk[0];
951 s1 =
952 Te0[(t1 >> 24)] ^
953 Te1[(t2 >> 16) & 0xff] ^
954 Te2[(t3 >> 8) & 0xff] ^ Te3[(t0) & 0xff] ^ rk[1];
955 s2 =
956 Te0[(t2 >> 24)] ^
957 Te1[(t3 >> 16) & 0xff] ^
958 Te2[(t0 >> 8) & 0xff] ^ Te3[(t1) & 0xff] ^ rk[2];
959 s3 =
960 Te0[(t3 >> 24)] ^
961 Te1[(t0 >> 16) & 0xff] ^
962 Te2[(t1 >> 8) & 0xff] ^ Te3[(t2) & 0xff] ^ rk[3];
963 }
964 /*
965 * apply last round and
966 * map cipher state to byte array block:
967 */
968 s0 =
969 (Te4[(t0 >> 24)] & 0xff000000) ^
970 (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^
971 (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^
972 (Te4[(t3) & 0xff] & 0x000000ff) ^ rk[0];
973 PUTU32 (ct, s0);
974 s1 =
975 (Te4[(t1 >> 24)] & 0xff000000) ^
976 (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^
977 (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^
978 (Te4[(t0) & 0xff] & 0x000000ff) ^ rk[1];
979 PUTU32 (ct + 4, s1);
980 s2 =
981 (Te4[(t2 >> 24)] & 0xff000000) ^
982 (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^
983 (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^
984 (Te4[(t1) & 0xff] & 0x000000ff) ^ rk[2];
985 PUTU32 (ct + 8, s2);
986 s3 =
987 (Te4[(t3 >> 24)] & 0xff000000) ^
988 (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^
989 (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^
990 (Te4[(t2) & 0xff] & 0x000000ff) ^ rk[3];
991 PUTU32 (ct + 12, s3);
992}
993
994void
995rijndaelDecrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
996 const char ct[16], char pt[16])
997{
998 uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
999 size_t r;
1000
1001 /*
1002 * map byte array block to cipher state
1003 * and add initial round key:
1004 */
1005 s0 = GETU32 (ct) ^ rk[0];
1006 s1 = GETU32 (ct + 4) ^ rk[1];
1007 s2 = GETU32 (ct + 8) ^ rk[2];
1008 s3 = GETU32 (ct + 12) ^ rk[3];
1009 /*
1010 * Nr - 1 full rounds:
1011 */
1012 r = Nr >> 1;
1013 for (;;)
1014 {
1015 t0 =
1016 Td0[(s0 >> 24)] ^
1017 Td1[(s3 >> 16) & 0xff] ^
1018 Td2[(s2 >> 8) & 0xff] ^ Td3[(s1) & 0xff] ^ rk[4];
1019 t1 =
1020 Td0[(s1 >> 24)] ^
1021 Td1[(s0 >> 16) & 0xff] ^
1022 Td2[(s3 >> 8) & 0xff] ^ Td3[(s2) & 0xff] ^ rk[5];
1023 t2 =
1024 Td0[(s2 >> 24)] ^
1025 Td1[(s1 >> 16) & 0xff] ^
1026 Td2[(s0 >> 8) & 0xff] ^ Td3[(s3) & 0xff] ^ rk[6];
1027 t3 =
1028 Td0[(s3 >> 24)] ^
1029 Td1[(s2 >> 16) & 0xff] ^
1030 Td2[(s1 >> 8) & 0xff] ^ Td3[(s0) & 0xff] ^ rk[7];
1031
1032 rk += 8;
1033 if (--r == 0)
1034 {
1035 break;
1036 }
1037
1038 s0 =
1039 Td0[(t0 >> 24)] ^
1040 Td1[(t3 >> 16) & 0xff] ^
1041 Td2[(t2 >> 8) & 0xff] ^ Td3[(t1) & 0xff] ^ rk[0];
1042 s1 =
1043 Td0[(t1 >> 24)] ^
1044 Td1[(t0 >> 16) & 0xff] ^
1045 Td2[(t3 >> 8) & 0xff] ^ Td3[(t2) & 0xff] ^ rk[1];
1046 s2 =
1047 Td0[(t2 >> 24)] ^
1048 Td1[(t1 >> 16) & 0xff] ^
1049 Td2[(t0 >> 8) & 0xff] ^ Td3[(t3) & 0xff] ^ rk[2];
1050 s3 =
1051 Td0[(t3 >> 24)] ^
1052 Td1[(t2 >> 16) & 0xff] ^
1053 Td2[(t1 >> 8) & 0xff] ^ Td3[(t0) & 0xff] ^ rk[3];
1054 }
1055 /*
1056 * apply last round and
1057 * map cipher state to byte array block:
1058 */
1059 s0 =
1060 (Td4[(t0 >> 24)] & 0xff000000) ^
1061 (Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^
1062 (Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^
1063 (Td4[(t1) & 0xff] & 0x000000ff) ^ rk[0];
1064 PUTU32 (pt, s0);
1065 s1 =
1066 (Td4[(t1 >> 24)] & 0xff000000) ^
1067 (Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^
1068 (Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^
1069 (Td4[(t2) & 0xff] & 0x000000ff) ^ rk[1];
1070 PUTU32 (pt + 4, s1);
1071 s2 =
1072 (Td4[(t2 >> 24)] & 0xff000000) ^
1073 (Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^
1074 (Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^
1075 (Td4[(t3) & 0xff] & 0x000000ff) ^ rk[2];
1076 PUTU32 (pt + 8, s2);
1077 s3 =
1078 (Td4[(t3 >> 24)] & 0xff000000) ^
1079 (Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^
1080 (Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^
1081 (Td4[(t0) & 0xff] & 0x000000ff) ^ rk[3];
1082 PUTU32 (pt + 12, s3);
1083}
diff --git a/src/daemon/https/lgl/rijndael-alg-fst.h b/src/daemon/https/lgl/rijndael-alg-fst.h
new file mode 100644
index 00000000..88391023
--- /dev/null
+++ b/src/daemon/https/lgl/rijndael-alg-fst.h
@@ -0,0 +1,67 @@
1/* rijndael-alg-fst.h --- Rijndael cipher implementation.
2 * Copyright (C) 2005 Free Software Foundation, Inc.
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Adapted for gnulib by Simon Josefsson. */
22
23/**
24 * rijndael-alg-fst.h
25 *
26 * @version 3.0 (December 2000)
27 *
28 * Optimised ANSI C code for the Rijndael cipher (now AES)
29 *
30 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
31 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
32 * @author Paulo Barreto <paulo.barreto@terra.com.br>
33 *
34 * This code is hereby placed in the public domain.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
37 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
40 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 */
48#ifndef __RIJNDAEL_ALG_FST_H
49#define __RIJNDAEL_ALG_FST_H
50
51#include <stdint.h>
52#include <stddef.h>
53
54#define RIJNDAEL_MAXKC (256/32)
55#define RIJNDAEL_MAXKB (256/8)
56#define RIJNDAEL_MAXNR 14
57
58int rijndaelKeySetupEnc (uint32_t rk[ /*4*(Nr + 1) */ ],
59 const char cipherKey[], size_t keyBits);
60int rijndaelKeySetupDec (uint32_t rk[ /*4*(Nr + 1) */ ],
61 const char cipherKey[], size_t keyBits);
62void rijndaelEncrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
63 const char pt[16], char ct[16]);
64void rijndaelDecrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
65 const char ct[16], char pt[16]);
66
67#endif /* __RIJNDAEL_ALG_FST_H */
diff --git a/src/daemon/https/lgl/rijndael-api-fst.c b/src/daemon/https/lgl/rijndael-api-fst.c
new file mode 100644
index 00000000..35d920a9
--- /dev/null
+++ b/src/daemon/https/lgl/rijndael-api-fst.c
@@ -0,0 +1,517 @@
1/* rijndael-api-fst.c --- Rijndael cipher implementation.
2 * Copyright (C) 2005, 2006 Free Software Foundation, Inc.
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Adapted for gnulib by Simon Josefsson.
22 *
23 * Based on public domain "Optimised C code" retrieved from (SHA1
24 * 7c8e4b00d06685d1dbc6724a9e0d502353de339e):
25 * http://www.iaik.tu-graz.ac.at/research/krypto/AES/old/~rijmen/rijndael/rijndael-fst-3.0.zip
26 */
27
28#include <config.h>
29
30/**
31 * rijndael-api-fst.c
32 *
33 * @version 2.9 (December 2000)
34 *
35 * Optimised ANSI C code for the Rijndael cipher (now AES)
36 *
37 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
38 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
39 * @author Paulo Barreto <paulo.barreto@terra.com.br>
40 *
41 * This code is hereby placed in the public domain.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
47 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
50 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
52 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
53 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * Acknowledgements:
56 *
57 * We are deeply indebted to the following people for their bug reports,
58 * fixes, and improvement suggestions to this implementation. Though we
59 * tried to list all contributions, we apologise in advance for any
60 * missing reference.
61 *
62 * Andrew Bales <Andrew.Bales@Honeywell.com>
63 * Markus Friedl <markus.friedl@informatik.uni-erlangen.de>
64 * John Skodon <skodonj@webquill.com>
65 */
66
67#include "rijndael-alg-fst.h"
68#include "rijndael-api-fst.h"
69
70#include <assert.h>
71#include <stdlib.h>
72#include <string.h>
73
74rijndael_rc
75rijndaelMakeKey (rijndaelKeyInstance * key, rijndael_direction direction,
76 size_t keyLen, const char *keyMaterial)
77{
78 size_t i;
79 char *keyMat;
80 char cipherKey[RIJNDAEL_MAXKB];
81
82 if (key == NULL)
83 {
84 return RIJNDAEL_BAD_KEY_INSTANCE;
85 }
86
87 if ((direction == RIJNDAEL_DIR_ENCRYPT)
88 || (direction == RIJNDAEL_DIR_DECRYPT))
89 {
90 key->direction = direction;
91 }
92 else
93 {
94 return RIJNDAEL_BAD_KEY_DIR;
95 }
96
97 if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256))
98 {
99 key->keyLen = keyLen;
100 }
101 else
102 {
103 return RIJNDAEL_BAD_KEY_MAT;
104 }
105
106 if (keyMaterial != NULL)
107 {
108 strncpy (key->keyMaterial, keyMaterial, keyLen / 4);
109 }
110
111 /* initialize key schedule: */
112 keyMat = key->keyMaterial;
113 for (i = 0; i < key->keyLen / 8; i++)
114 {
115 char t, v;
116
117 t = *keyMat++;
118 if ((t >= '0') && (t <= '9'))
119 v = (t - '0') << 4;
120 else if ((t >= 'a') && (t <= 'f'))
121 v = (t - 'a' + 10) << 4;
122 else if ((t >= 'A') && (t <= 'F'))
123 v = (t - 'A' + 10) << 4;
124 else
125 return RIJNDAEL_BAD_KEY_MAT;
126
127 t = *keyMat++;
128 if ((t >= '0') && (t <= '9'))
129 v ^= (t - '0');
130 else if ((t >= 'a') && (t <= 'f'))
131 v ^= (t - 'a' + 10);
132 else if ((t >= 'A') && (t <= 'F'))
133 v ^= (t - 'A' + 10);
134 else
135 return RIJNDAEL_BAD_KEY_MAT;
136
137 cipherKey[i] = v;
138 }
139 if (direction == RIJNDAEL_DIR_ENCRYPT)
140 {
141 key->Nr = rijndaelKeySetupEnc (key->rk, cipherKey, keyLen);
142 }
143 else
144 {
145 key->Nr = rijndaelKeySetupDec (key->rk, cipherKey, keyLen);
146 }
147 rijndaelKeySetupEnc (key->ek, cipherKey, keyLen);
148 return 0;
149}
150
151rijndael_rc
152rijndaelCipherInit (rijndaelCipherInstance * cipher, rijndael_mode mode,
153 const char *IV)
154{
155 if ((mode == RIJNDAEL_MODE_ECB) || (mode == RIJNDAEL_MODE_CBC)
156 || (mode == RIJNDAEL_MODE_CFB1))
157 {
158 cipher->mode = mode;
159 }
160 else
161 {
162 return RIJNDAEL_BAD_CIPHER_MODE;
163 }
164 if (IV != NULL)
165 {
166 int i;
167 for (i = 0; i < RIJNDAEL_MAX_IV_SIZE; i++)
168 {
169 int t, j;
170
171 t = IV[2 * i];
172 if ((t >= '0') && (t <= '9'))
173 j = (t - '0') << 4;
174 else if ((t >= 'a') && (t <= 'f'))
175 j = (t - 'a' + 10) << 4;
176 else if ((t >= 'A') && (t <= 'F'))
177 j = (t - 'A' + 10) << 4;
178 else
179 return RIJNDAEL_BAD_CIPHER_INSTANCE;
180
181 t = IV[2 * i + 1];
182 if ((t >= '0') && (t <= '9'))
183 j ^= (t - '0');
184 else if ((t >= 'a') && (t <= 'f'))
185 j ^= (t - 'a' + 10);
186 else if ((t >= 'A') && (t <= 'F'))
187 j ^= (t - 'A' + 10);
188 else
189 return RIJNDAEL_BAD_CIPHER_INSTANCE;
190
191 cipher->IV[i] = (uint8_t) j;
192 }
193 }
194 else
195 {
196 memset (cipher->IV, 0, RIJNDAEL_MAX_IV_SIZE);
197 }
198 return 0;
199}
200
201int
202rijndaelBlockEncrypt (rijndaelCipherInstance * cipher,
203 const rijndaelKeyInstance * key,
204 const char *input, size_t inputLen, char *outBuffer)
205{
206 size_t i, k, t, numBlocks;
207 char block[16], *iv;
208
209 if (cipher == NULL || key == NULL || key->direction == RIJNDAEL_DIR_DECRYPT)
210 {
211 return RIJNDAEL_BAD_CIPHER_STATE;
212 }
213 if (input == NULL || inputLen <= 0)
214 {
215 return 0; /* nothing to do */
216 }
217
218 numBlocks = inputLen / 128;
219
220 switch (cipher->mode)
221 {
222 case RIJNDAEL_MODE_ECB:
223 for (i = numBlocks; i > 0; i--)
224 {
225 rijndaelEncrypt (key->rk, key->Nr, input, outBuffer);
226 input += 16;
227 outBuffer += 16;
228 }
229 break;
230
231 case RIJNDAEL_MODE_CBC:
232 iv = cipher->IV;
233 for (i = numBlocks; i > 0; i--)
234 {
235 ((uint32_t *) block)[0] = ((uint32_t *) input)[0] ^
236 ((uint32_t *) iv)[0];
237 ((uint32_t *) block)[1] = ((uint32_t *) input)[1] ^
238 ((uint32_t *) iv)[1];
239 ((uint32_t *) block)[2] = ((uint32_t *) input)[2] ^
240 ((uint32_t *) iv)[2];
241 ((uint32_t *) block)[3] = ((uint32_t *) input)[3] ^
242 ((uint32_t *) iv)[3];
243 rijndaelEncrypt (key->rk, key->Nr, block, outBuffer);
244 memcpy (cipher->IV, outBuffer, 16);
245 input += 16;
246 outBuffer += 16;
247 }
248 break;
249
250 case RIJNDAEL_MODE_CFB1:
251 iv = cipher->IV;
252 for (i = numBlocks; i > 0; i--)
253 {
254 memcpy (outBuffer, input, 16);
255 for (k = 0; k < 128; k++)
256 {
257 rijndaelEncrypt (key->ek, key->Nr, iv, block);
258 outBuffer[k >> 3] ^= (block[0] & 0x80U) >> (k & 7);
259 for (t = 0; t < 15; t++)
260 {
261 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
262 }
263 iv[15] = (iv[15] << 1) |
264 ((outBuffer[k >> 3] >> (7 - (k & 7))) & 1);
265 }
266 outBuffer += 16;
267 input += 16;
268 }
269 break;
270
271 default:
272 return RIJNDAEL_BAD_CIPHER_STATE;
273 }
274
275 return 128 * numBlocks;
276}
277
278int
279rijndaelPadEncrypt (rijndaelCipherInstance * cipher,
280 const rijndaelKeyInstance * key,
281 const char *input, size_t inputOctets, char *outBuffer)
282{
283 size_t i, numBlocks, padLen;
284 char block[16], *iv;
285
286 if (cipher == NULL || key == NULL || key->direction == RIJNDAEL_DIR_DECRYPT)
287 {
288 return RIJNDAEL_BAD_CIPHER_STATE;
289 }
290 if (input == NULL || inputOctets <= 0)
291 {
292 return 0; /* nothing to do */
293 }
294
295 numBlocks = inputOctets / 16;
296
297 switch (cipher->mode)
298 {
299 case RIJNDAEL_MODE_ECB:
300 for (i = numBlocks; i > 0; i--)
301 {
302 rijndaelEncrypt (key->rk, key->Nr, input, outBuffer);
303 input += 16;
304 outBuffer += 16;
305 }
306 padLen = 16 - (inputOctets - 16 * numBlocks);
307 assert (padLen > 0 && padLen <= 16);
308 memcpy (block, input, 16 - padLen);
309 memset (block + 16 - padLen, padLen, padLen);
310 rijndaelEncrypt (key->rk, key->Nr, block, outBuffer);
311 break;
312
313 case RIJNDAEL_MODE_CBC:
314 iv = cipher->IV;
315 for (i = numBlocks; i > 0; i--)
316 {
317 ((uint32_t *) block)[0] = ((uint32_t *) input)[0] ^
318 ((uint32_t *) iv)[0];
319 ((uint32_t *) block)[1] = ((uint32_t *) input)[1] ^
320 ((uint32_t *) iv)[1];
321 ((uint32_t *) block)[2] = ((uint32_t *) input)[2] ^
322 ((uint32_t *) iv)[2];
323 ((uint32_t *) block)[3] = ((uint32_t *) input)[3] ^
324 ((uint32_t *) iv)[3];
325 rijndaelEncrypt (key->rk, key->Nr, block, outBuffer);
326 memcpy (cipher->IV, outBuffer, 16);
327 input += 16;
328 outBuffer += 16;
329 }
330 padLen = 16 - (inputOctets - 16 * numBlocks);
331 assert (padLen > 0 && padLen <= 16);
332 for (i = 0; i < 16 - padLen; i++)
333 {
334 block[i] = input[i] ^ iv[i];
335 }
336 for (i = 16 - padLen; i < 16; i++)
337 {
338 block[i] = (char) padLen ^ iv[i];
339 }
340 rijndaelEncrypt (key->rk, key->Nr, block, outBuffer);
341 memcpy (cipher->IV, outBuffer, 16);
342 break;
343
344 default:
345 return RIJNDAEL_BAD_CIPHER_STATE;
346 }
347
348 return 16 * (numBlocks + 1);
349}
350
351int
352rijndaelBlockDecrypt (rijndaelCipherInstance * cipher,
353 const rijndaelKeyInstance * key,
354 const char *input, size_t inputLen, char *outBuffer)
355{
356 size_t i, k, t, numBlocks;
357 char block[16], *iv;
358
359 if (cipher == NULL
360 || key == NULL
361 || (cipher->mode != RIJNDAEL_MODE_CFB1
362 && key->direction == RIJNDAEL_DIR_ENCRYPT))
363 {
364 return RIJNDAEL_BAD_CIPHER_STATE;
365 }
366 if (input == NULL || inputLen <= 0)
367 {
368 return 0; /* nothing to do */
369 }
370
371 numBlocks = inputLen / 128;
372
373 switch (cipher->mode)
374 {
375 case RIJNDAEL_MODE_ECB:
376 for (i = numBlocks; i > 0; i--)
377 {
378 rijndaelDecrypt (key->rk, key->Nr, input, outBuffer);
379 input += 16;
380 outBuffer += 16;
381 }
382 break;
383
384 case RIJNDAEL_MODE_CBC:
385 iv = cipher->IV;
386 for (i = numBlocks; i > 0; i--)
387 {
388 rijndaelDecrypt (key->rk, key->Nr, input, block);
389 ((uint32_t *) block)[0] ^= ((uint32_t *) iv)[0];
390 ((uint32_t *) block)[1] ^= ((uint32_t *) iv)[1];
391 ((uint32_t *) block)[2] ^= ((uint32_t *) iv)[2];
392 ((uint32_t *) block)[3] ^= ((uint32_t *) iv)[3];
393 memcpy (cipher->IV, input, 16);
394 memcpy (outBuffer, block, 16);
395 input += 16;
396 outBuffer += 16;
397 }
398 break;
399
400 case RIJNDAEL_MODE_CFB1:
401 iv = cipher->IV;
402 for (i = numBlocks; i > 0; i--)
403 {
404 memcpy (outBuffer, input, 16);
405 for (k = 0; k < 128; k++)
406 {
407 rijndaelEncrypt (key->ek, key->Nr, iv, block);
408 for (t = 0; t < 15; t++)
409 {
410 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
411 }
412 iv[15] = (iv[15] << 1) | ((input[k >> 3] >> (7 - (k & 7))) & 1);
413 outBuffer[k >> 3] ^= (block[0] & 0x80U) >> (k & 7);
414 }
415 outBuffer += 16;
416 input += 16;
417 }
418 break;
419
420 default:
421 return RIJNDAEL_BAD_CIPHER_STATE;
422 }
423
424 return 128 * numBlocks;
425}
426
427int
428rijndaelPadDecrypt (rijndaelCipherInstance * cipher,
429 const rijndaelKeyInstance * key,
430 const char *input, size_t inputOctets, char *outBuffer)
431{
432 size_t i, numBlocks, padLen;
433 char block[16];
434
435 if (cipher == NULL || key == NULL || key->direction == RIJNDAEL_DIR_ENCRYPT)
436 {
437 return RIJNDAEL_BAD_CIPHER_STATE;
438 }
439 if (input == NULL || inputOctets <= 0)
440 {
441 return 0; /* nothing to do */
442 }
443 if (inputOctets % 16 != 0)
444 {
445 return RIJNDAEL_BAD_DATA;
446 }
447
448 numBlocks = inputOctets / 16;
449
450 switch (cipher->mode)
451 {
452 case RIJNDAEL_MODE_ECB:
453 /* all blocks but last */
454 for (i = numBlocks - 1; i > 0; i--)
455 {
456 rijndaelDecrypt (key->rk, key->Nr, input, outBuffer);
457 input += 16;
458 outBuffer += 16;
459 }
460 /* last block */
461 rijndaelDecrypt (key->rk, key->Nr, input, block);
462 padLen = block[15];
463 if (padLen >= 16)
464 {
465 return RIJNDAEL_BAD_DATA;
466 }
467 for (i = 16 - padLen; i < 16; i++)
468 {
469 if (block[i] != padLen)
470 {
471 return RIJNDAEL_BAD_DATA;
472 }
473 }
474 memcpy (outBuffer, block, 16 - padLen);
475 break;
476
477 case RIJNDAEL_MODE_CBC:
478 /* all blocks but last */
479 for (i = numBlocks - 1; i > 0; i--)
480 {
481 rijndaelDecrypt (key->rk, key->Nr, input, block);
482 ((uint32_t *) block)[0] ^= ((uint32_t *) cipher->IV)[0];
483 ((uint32_t *) block)[1] ^= ((uint32_t *) cipher->IV)[1];
484 ((uint32_t *) block)[2] ^= ((uint32_t *) cipher->IV)[2];
485 ((uint32_t *) block)[3] ^= ((uint32_t *) cipher->IV)[3];
486 memcpy (cipher->IV, input, 16);
487 memcpy (outBuffer, block, 16);
488 input += 16;
489 outBuffer += 16;
490 }
491 /* last block */
492 rijndaelDecrypt (key->rk, key->Nr, input, block);
493 ((uint32_t *) block)[0] ^= ((uint32_t *) cipher->IV)[0];
494 ((uint32_t *) block)[1] ^= ((uint32_t *) cipher->IV)[1];
495 ((uint32_t *) block)[2] ^= ((uint32_t *) cipher->IV)[2];
496 ((uint32_t *) block)[3] ^= ((uint32_t *) cipher->IV)[3];
497 padLen = block[15];
498 if (padLen <= 0 || padLen > 16)
499 {
500 return RIJNDAEL_BAD_DATA;
501 }
502 for (i = 16 - padLen; i < 16; i++)
503 {
504 if (block[i] != padLen)
505 {
506 return RIJNDAEL_BAD_DATA;
507 }
508 }
509 memcpy (outBuffer, block, 16 - padLen);
510 break;
511
512 default:
513 return RIJNDAEL_BAD_CIPHER_STATE;
514 }
515
516 return 16 * numBlocks - padLen;
517}
diff --git a/src/daemon/https/lgl/rijndael-api-fst.h b/src/daemon/https/lgl/rijndael-api-fst.h
new file mode 100644
index 00000000..d0ff60ac
--- /dev/null
+++ b/src/daemon/https/lgl/rijndael-api-fst.h
@@ -0,0 +1,207 @@
1/* rijndael-api-fst.h --- Rijndael cipher implementation.
2 * Copyright (C) 2005 Free Software Foundation, Inc.
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Adapted for gnulib by Simon Josefsson. */
22
23/**
24 * rijndael-api-fst.h
25 *
26 * @version 2.9 (December 2000)
27 *
28 * Optimised ANSI C code for the Rijndael cipher (now AES)
29 *
30 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
31 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
32 * @author Paulo Barreto <paulo.barreto@terra.com.br>
33 *
34 * This code is hereby placed in the public domain.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
37 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
40 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 * Acknowledgements:
49 *
50 * We are deeply indebted to the following people for their bug reports,
51 * fixes, and improvement suggestions to this implementation. Though we
52 * tried to list all contributions, we apologise in advance for any
53 * missing reference.
54 *
55 * Andrew Bales <Andrew.Bales@Honeywell.com>
56 * Markus Friedl <markus.friedl@informatik.uni-erlangen.de>
57 * John Skodon <skodonj@webquill.com>
58 */
59
60#ifndef __RIJNDAEL_API_FST_H
61#define __RIJNDAEL_API_FST_H
62
63#include "rijndael-alg-fst.h"
64
65#include <stdio.h>
66
67/* Default number of bits in a cipher block */
68#define RIJNDAEL_BITSPERBLOCK 128
69
70/* Number of ASCII char's needed to represent a key */
71#define RIJNDAEL_MAX_KEY_SIZE 64
72
73/* Number bytes needed to represent an IV */
74#define RIJNDAEL_MAX_IV_SIZE 16
75
76typedef enum
77{
78 /* Key direction is invalid, e.g., unknown value */
79 RIJNDAEL_BAD_KEY_DIR = -1,
80 /* Key material not of correct length */
81 RIJNDAEL_BAD_KEY_MAT = -2,
82 /* Key passed is not valid */
83 RIJNDAEL_BAD_KEY_INSTANCE = -3,
84 /* Params struct passed to cipherInit invalid */
85 RIJNDAEL_BAD_CIPHER_MODE = -4,
86 /* Cipher in wrong state (e.g., not initialized) */
87 RIJNDAEL_BAD_CIPHER_STATE = -5,
88 RIJNDAEL_BAD_BLOCK_LENGTH = -6,
89 RIJNDAEL_BAD_CIPHER_INSTANCE = -7,
90 /* Data contents are invalid, e.g., invalid padding */
91 RIJNDAEL_BAD_DATA = -8,
92 /* Unknown error */
93 RIJNDAEL_BAD_OTHER = -9
94} rijndael_rc;
95
96typedef enum
97{
98 RIJNDAEL_DIR_ENCRYPT = 0, /* Are we encrypting? */
99 RIJNDAEL_DIR_DECRYPT = 1 /* Are we decrypting? */
100} rijndael_direction;
101
102typedef enum
103{
104 RIJNDAEL_MODE_ECB = 1, /* Are we ciphering in ECB mode? */
105 RIJNDAEL_MODE_CBC = 2, /* Are we ciphering in CBC mode? */
106 RIJNDAEL_MODE_CFB1 = 3 /* Are we ciphering in 1-bit CFB mode? */
107} rijndael_mode;
108
109/* The structure for key information */
110typedef struct
111{
112 /* Key used for encrypting or decrypting? */
113 rijndael_direction direction;
114 /* Length of the key */
115 size_t keyLen;
116 /* Raw key data in ASCII, e.g., user input or KAT values */
117 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
118 /* key-length-dependent number of rounds */
119 int Nr;
120 /* key schedule */
121 uint32_t rk[4 * (RIJNDAEL_MAXNR + 1)];
122 /* CFB1 key schedule (encryption only) */
123 uint32_t ek[4 * (RIJNDAEL_MAXNR + 1)];
124} rijndaelKeyInstance;
125
126/* The structure for cipher information */
127typedef struct
128{ /* changed order of the components */
129 rijndael_mode mode; /* MODE_ECB, MODE_CBC, or MODE_CFB1 */
130 /* A possible Initialization Vector for ciphering */
131 char IV[RIJNDAEL_MAX_IV_SIZE];
132} rijndaelCipherInstance;
133
134/* Function prototypes */
135
136/* Create KEY, for encryption or decryption depending on DIRECTION,
137 from KEYMATERIAL, a hex string, of KEYLEN size. KEYLEN should be
138 128, 192 or 256. Returns 0 on success, or an error code. */
139extern rijndael_rc
140rijndaelMakeKey (rijndaelKeyInstance *key, rijndael_direction direction,
141 size_t keyLen, const char *keyMaterial);
142
143/* Initialize cipher state CIPHER for encryption MODE (e.g.,
144 RIJNDAEL_MODE_CBC) with initialization vector IV, a hex string of
145 2*RIJNDAEL_MAX_IV_SIZE length. IV may be NULL for modes that do
146 not need an IV (i.e., RIJNDAEL_MODE_ECB). */
147extern rijndael_rc
148rijndaelCipherInit (rijndaelCipherInstance *cipher,
149 rijndael_mode mode, const char *IV);
150
151/* Encrypt data in INPUT, of INPUTLEN/8 bytes length, placing the
152 output in the pre-allocated OUTBUFFER which must hold at least
153 INPUTLEN/8 bytes of data. The CIPHER is used as state, and must be
154 initialized with rijndaelCipherInit before calling this function.
155 The encryption KEY must be initialized with rijndaelMakeKey before
156 calling this function. Return the number of bits written, or a
157 negative rijndael_rc error code. */
158extern int
159rijndaelBlockEncrypt (rijndaelCipherInstance *cipher,
160 const rijndaelKeyInstance *key,
161 const char *input, size_t inputLen,
162 char *outBuffer);
163
164/* Encrypt data in INPUT, of INPUTOCTETS bytes length, placing the
165 output in the pre-allocated OUTBUFFER which must hold at least
166 INPUTOCTETS aligned to the next block size boundary.
167 Ciphertext-Stealing as described in RFC 2040 is used to encrypt
168 partial blocks. The CIPHER is used as state, and must be
169 initialized with rijndaelCipherInit before calling this function.
170 The encryption KEY must be initialized with rijndaelMakeKey before
171 calling this function. Return the number of bits written, or a
172 negative rijndael_rc error code. */
173extern int
174rijndaelPadEncrypt (rijndaelCipherInstance *cipher,
175 const rijndaelKeyInstance *key,
176 const char *input, size_t inputOctets,
177 char *outBuffer);
178
179/* Decrypt data in INPUT, of INPUTLEN/8 bytes length, placing the
180 output in the pre-allocated OUTBUFFER which must hold at least
181 INPUTLEN/8 bytes of data. The CIPHER is used as state, and must be
182 initialized with rijndaelCipherInit before calling this function.
183 The encryption KEY must be initialized with rijndaelMakeKey before
184 calling this function. Return the number of bits written, or a
185 negative rijndael_rc error code. */
186extern int
187rijndaelBlockDecrypt (rijndaelCipherInstance *cipher,
188 const rijndaelKeyInstance *key,
189 const char *input, size_t inputLen,
190 char *outBuffer);
191
192/* Decrypt data in INPUT, of INPUTOCTETS bytes length, placing the
193 output in the pre-allocated OUTBUFFER which must hold at least
194 INPUTOCTETS aligned to the next block size boundary.
195 Ciphertext-Stealing as described in RFC 2040 is used to encrypt
196 partial blocks. The CIPHER is used as state, and must be
197 initialized with rijndaelCipherInit before calling this function.
198 The encryption KEY must be initialized with rijndaelMakeKey before
199 calling this function. Return the number of bits written, or a
200 negative rijndael_rc error code. */
201extern int
202rijndaelPadDecrypt (rijndaelCipherInstance *cipher,
203 const rijndaelKeyInstance *key,
204 const char *input, size_t inputOctets,
205 char *outBuffer);
206
207#endif /* __RIJNDAEL_API_FST_H */
diff --git a/src/daemon/https/lgl/sha1.c b/src/daemon/https/lgl/sha1.c
new file mode 100644
index 00000000..f5573da5
--- /dev/null
+++ b/src/daemon/https/lgl/sha1.c
@@ -0,0 +1,416 @@
1/* sha1.c - Functions to compute SHA1 message digest of files or
2 memory blocks according to the NIST specification FIPS-180-1.
3
4 Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 Free Software
5 Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 2.1, or (at your option) any
10 later version.
11
12 This program 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
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21/* Written by Scott G. Miller
22 Credits:
23 Robert Klep <robert@ilse.nl> -- Expansion function fix
24*/
25
26#include <config.h>
27
28#include "sha1.h"
29
30#include <stddef.h>
31#include <string.h>
32
33#if USE_UNLOCKED_IO
34# include "unlocked-io.h"
35#endif
36
37#ifdef WORDS_BIGENDIAN
38# define SWAP(n) (n)
39#else
40# define SWAP(n) \
41 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
42#endif
43
44#define BLOCKSIZE 4096
45#if BLOCKSIZE % 64 != 0
46# error "invalid BLOCKSIZE"
47#endif
48
49/* This array contains the bytes used to pad the buffer to the next
50 64-byte boundary. (RFC 1321, 3.1: Step 1) */
51static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
52
53
54/* Take a pointer to a 160 bit block of data (five 32 bit ints) and
55 initialize it to the start constants of the SHA1 algorithm. This
56 must be called before using hash in the call to sha1_hash. */
57void
58sha1_init_ctx (struct sha1_ctx *ctx)
59{
60 ctx->A = 0x67452301;
61 ctx->B = 0xefcdab89;
62 ctx->C = 0x98badcfe;
63 ctx->D = 0x10325476;
64 ctx->E = 0xc3d2e1f0;
65
66 ctx->total[0] = ctx->total[1] = 0;
67 ctx->buflen = 0;
68}
69
70/* Put result from CTX in first 20 bytes following RESBUF. The result
71 must be in little endian byte order.
72
73 IMPORTANT: On some systems it is required that RESBUF is correctly
74 aligned for a 32-bit value. */
75void *
76sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
77{
78 ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
79 ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
80 ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
81 ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
82 ((uint32_t *) resbuf)[4] = SWAP (ctx->E);
83
84 return resbuf;
85}
86
87/* Process the remaining bytes in the internal buffer and the usual
88 prolog according to the standard and write the result to RESBUF.
89
90 IMPORTANT: On some systems it is required that RESBUF is correctly
91 aligned for a 32-bit value. */
92void *
93sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
94{
95 /* Take yet unprocessed bytes into account. */
96 uint32_t bytes = ctx->buflen;
97 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
98
99 /* Now count remaining bytes. */
100 ctx->total[0] += bytes;
101 if (ctx->total[0] < bytes)
102 ++ctx->total[1];
103
104 /* Put the 64-bit file length in *bits* at the end of the buffer. */
105 ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
106 ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
107
108 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
109
110 /* Process last bytes. */
111 sha1_process_block (ctx->buffer, size * 4, ctx);
112
113 return sha1_read_ctx (ctx, resbuf);
114}
115
116/* Compute SHA1 message digest for bytes read from STREAM. The
117 resulting message digest number will be written into the 16 bytes
118 beginning at RESBLOCK. */
119int
120sha1_stream (FILE * stream, void *resblock)
121{
122 struct sha1_ctx ctx;
123 char buffer[BLOCKSIZE + 72];
124 size_t sum;
125
126 /* Initialize the computation context. */
127 sha1_init_ctx (&ctx);
128
129 /* Iterate over full file contents. */
130 while (1)
131 {
132 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
133 computation function processes the whole buffer so that with the
134 next round of the loop another block can be read. */
135 size_t n;
136 sum = 0;
137
138 /* Read block. Take care for partial reads. */
139 while (1)
140 {
141 n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
142
143 sum += n;
144
145 if (sum == BLOCKSIZE)
146 break;
147
148 if (n == 0)
149 {
150 /* Check for the error flag IFF N == 0, so that we don't
151 exit the loop after a partial read due to e.g., EAGAIN
152 or EWOULDBLOCK. */
153 if (ferror (stream))
154 return 1;
155 goto process_partial_block;
156 }
157
158 /* We've read at least one byte, so ignore errors. But always
159 check for EOF, since feof may be true even though N > 0.
160 Otherwise, we could end up calling fread after EOF. */
161 if (feof (stream))
162 goto process_partial_block;
163 }
164
165 /* Process buffer with BLOCKSIZE bytes. Note that
166 BLOCKSIZE % 64 == 0
167 */
168 sha1_process_block (buffer, BLOCKSIZE, &ctx);
169 }
170
171process_partial_block:;
172
173 /* Process any remaining bytes. */
174 if (sum > 0)
175 sha1_process_bytes (buffer, sum, &ctx);
176
177 /* Construct result in desired memory. */
178 sha1_finish_ctx (&ctx, resblock);
179 return 0;
180}
181
182/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
183 result is always in little endian byte order, so that a byte-wise
184 output yields to the wanted ASCII representation of the message
185 digest. */
186void *
187sha1_buffer (const char *buffer, size_t len, void *resblock)
188{
189 struct sha1_ctx ctx;
190
191 /* Initialize the computation context. */
192 sha1_init_ctx (&ctx);
193
194 /* Process whole buffer but last len % 64 bytes. */
195 sha1_process_bytes (buffer, len, &ctx);
196
197 /* Put result in desired memory area. */
198 return sha1_finish_ctx (&ctx, resblock);
199}
200
201void
202sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
203{
204 /* When we already have some bits in our internal buffer concatenate
205 both inputs first. */
206 if (ctx->buflen != 0)
207 {
208 size_t left_over = ctx->buflen;
209 size_t add = 128 - left_over > len ? len : 128 - left_over;
210
211 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
212 ctx->buflen += add;
213
214 if (ctx->buflen > 64)
215 {
216 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
217
218 ctx->buflen &= 63;
219 /* The regions in the following copy operation cannot overlap. */
220 memcpy (ctx->buffer,
221 &((char *) ctx->buffer)[(left_over + add) & ~63],
222 ctx->buflen);
223 }
224
225 buffer = (const char *) buffer + add;
226 len -= add;
227 }
228
229 /* Process available complete blocks. */
230 if (len >= 64)
231 {
232#if !_STRING_ARCH_unaligned
233# define alignof(type) offsetof (struct { char c; type x; }, x)
234# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
235 if (UNALIGNED_P (buffer))
236 while (len > 64)
237 {
238 sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
239 buffer = (const char *) buffer + 64;
240 len -= 64;
241 }
242 else
243#endif
244 {
245 sha1_process_block (buffer, len & ~63, ctx);
246 buffer = (const char *) buffer + (len & ~63);
247 len &= 63;
248 }
249 }
250
251 /* Move remaining bytes in internal buffer. */
252 if (len > 0)
253 {
254 size_t left_over = ctx->buflen;
255
256 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
257 left_over += len;
258 if (left_over >= 64)
259 {
260 sha1_process_block (ctx->buffer, 64, ctx);
261 left_over -= 64;
262 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
263 }
264 ctx->buflen = left_over;
265 }
266}
267
268/* --- Code below is the primary difference between md5.c and sha1.c --- */
269
270/* SHA1 round constants */
271#define K1 0x5a827999
272#define K2 0x6ed9eba1
273#define K3 0x8f1bbcdc
274#define K4 0xca62c1d6
275
276/* Round functions. Note that F2 is the same as F4. */
277#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
278#define F2(B,C,D) (B ^ C ^ D)
279#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
280#define F4(B,C,D) (B ^ C ^ D)
281
282/* Process LEN bytes of BUFFER, accumulating context into CTX.
283 It is assumed that LEN % 64 == 0.
284 Most of this code comes from GnuPG's cipher/sha1.c. */
285
286void
287sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
288{
289 const uint32_t *words = buffer;
290 size_t nwords = len / sizeof (uint32_t);
291 const uint32_t *endp = words + nwords;
292 uint32_t x[16];
293 uint32_t a = ctx->A;
294 uint32_t b = ctx->B;
295 uint32_t c = ctx->C;
296 uint32_t d = ctx->D;
297 uint32_t e = ctx->E;
298
299 /* First increment the byte count. RFC 1321 specifies the possible
300 length of the file up to 2^64 bits. Here we only compute the
301 number of bytes. Do a double word increment. */
302 ctx->total[0] += len;
303 if (ctx->total[0] < len)
304 ++ctx->total[1];
305
306#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
307
308#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
309 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
310 , (x[I&0x0f] = rol(tm, 1)) )
311
312#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
313 + F( B, C, D ) \
314 + K \
315 + M; \
316 B = rol( B, 30 ); \
317 } while(0)
318
319 while (words < endp)
320 {
321 uint32_t tm;
322 int t;
323 for (t = 0; t < 16; t++)
324 {
325 x[t] = SWAP (*words);
326 words++;
327 }
328
329 R (a, b, c, d, e, F1, K1, x[0]);
330 R (e, a, b, c, d, F1, K1, x[1]);
331 R (d, e, a, b, c, F1, K1, x[2]);
332 R (c, d, e, a, b, F1, K1, x[3]);
333 R (b, c, d, e, a, F1, K1, x[4]);
334 R (a, b, c, d, e, F1, K1, x[5]);
335 R (e, a, b, c, d, F1, K1, x[6]);
336 R (d, e, a, b, c, F1, K1, x[7]);
337 R (c, d, e, a, b, F1, K1, x[8]);
338 R (b, c, d, e, a, F1, K1, x[9]);
339 R (a, b, c, d, e, F1, K1, x[10]);
340 R (e, a, b, c, d, F1, K1, x[11]);
341 R (d, e, a, b, c, F1, K1, x[12]);
342 R (c, d, e, a, b, F1, K1, x[13]);
343 R (b, c, d, e, a, F1, K1, x[14]);
344 R (a, b, c, d, e, F1, K1, x[15]);
345 R (e, a, b, c, d, F1, K1, M (16));
346 R (d, e, a, b, c, F1, K1, M (17));
347 R (c, d, e, a, b, F1, K1, M (18));
348 R (b, c, d, e, a, F1, K1, M (19));
349 R (a, b, c, d, e, F2, K2, M (20));
350 R (e, a, b, c, d, F2, K2, M (21));
351 R (d, e, a, b, c, F2, K2, M (22));
352 R (c, d, e, a, b, F2, K2, M (23));
353 R (b, c, d, e, a, F2, K2, M (24));
354 R (a, b, c, d, e, F2, K2, M (25));
355 R (e, a, b, c, d, F2, K2, M (26));
356 R (d, e, a, b, c, F2, K2, M (27));
357 R (c, d, e, a, b, F2, K2, M (28));
358 R (b, c, d, e, a, F2, K2, M (29));
359 R (a, b, c, d, e, F2, K2, M (30));
360 R (e, a, b, c, d, F2, K2, M (31));
361 R (d, e, a, b, c, F2, K2, M (32));
362 R (c, d, e, a, b, F2, K2, M (33));
363 R (b, c, d, e, a, F2, K2, M (34));
364 R (a, b, c, d, e, F2, K2, M (35));
365 R (e, a, b, c, d, F2, K2, M (36));
366 R (d, e, a, b, c, F2, K2, M (37));
367 R (c, d, e, a, b, F2, K2, M (38));
368 R (b, c, d, e, a, F2, K2, M (39));
369 R (a, b, c, d, e, F3, K3, M (40));
370 R (e, a, b, c, d, F3, K3, M (41));
371 R (d, e, a, b, c, F3, K3, M (42));
372 R (c, d, e, a, b, F3, K3, M (43));
373 R (b, c, d, e, a, F3, K3, M (44));
374 R (a, b, c, d, e, F3, K3, M (45));
375 R (e, a, b, c, d, F3, K3, M (46));
376 R (d, e, a, b, c, F3, K3, M (47));
377 R (c, d, e, a, b, F3, K3, M (48));
378 R (b, c, d, e, a, F3, K3, M (49));
379 R (a, b, c, d, e, F3, K3, M (50));
380 R (e, a, b, c, d, F3, K3, M (51));
381 R (d, e, a, b, c, F3, K3, M (52));
382 R (c, d, e, a, b, F3, K3, M (53));
383 R (b, c, d, e, a, F3, K3, M (54));
384 R (a, b, c, d, e, F3, K3, M (55));
385 R (e, a, b, c, d, F3, K3, M (56));
386 R (d, e, a, b, c, F3, K3, M (57));
387 R (c, d, e, a, b, F3, K3, M (58));
388 R (b, c, d, e, a, F3, K3, M (59));
389 R (a, b, c, d, e, F4, K4, M (60));
390 R (e, a, b, c, d, F4, K4, M (61));
391 R (d, e, a, b, c, F4, K4, M (62));
392 R (c, d, e, a, b, F4, K4, M (63));
393 R (b, c, d, e, a, F4, K4, M (64));
394 R (a, b, c, d, e, F4, K4, M (65));
395 R (e, a, b, c, d, F4, K4, M (66));
396 R (d, e, a, b, c, F4, K4, M (67));
397 R (c, d, e, a, b, F4, K4, M (68));
398 R (b, c, d, e, a, F4, K4, M (69));
399 R (a, b, c, d, e, F4, K4, M (70));
400 R (e, a, b, c, d, F4, K4, M (71));
401 R (d, e, a, b, c, F4, K4, M (72));
402 R (c, d, e, a, b, F4, K4, M (73));
403 R (b, c, d, e, a, F4, K4, M (74));
404 R (a, b, c, d, e, F4, K4, M (75));
405 R (e, a, b, c, d, F4, K4, M (76));
406 R (d, e, a, b, c, F4, K4, M (77));
407 R (c, d, e, a, b, F4, K4, M (78));
408 R (b, c, d, e, a, F4, K4, M (79));
409
410 a = ctx->A += a;
411 b = ctx->B += b;
412 c = ctx->C += c;
413 d = ctx->D += d;
414 e = ctx->E += e;
415 }
416}
diff --git a/src/daemon/https/lgl/sha1.h b/src/daemon/https/lgl/sha1.h
new file mode 100644
index 00000000..ed0de2b4
--- /dev/null
+++ b/src/daemon/https/lgl/sha1.h
@@ -0,0 +1,87 @@
1/* Declarations of functions and data types used for SHA1 sum
2 library functions.
3 Copyright (C) 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published by the
7 Free Software Foundation; either version 2.1, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef SHA1_H
20# define SHA1_H 1
21
22# include <stdio.h>
23# include <stdint.h>
24
25/* Structure to save state of computation between the single steps. */
26struct sha1_ctx
27{
28 uint32_t A;
29 uint32_t B;
30 uint32_t C;
31 uint32_t D;
32 uint32_t E;
33
34 uint32_t total[2];
35 uint32_t buflen;
36 uint32_t buffer[32];
37};
38
39
40/* Initialize structure containing state of computation. */
41extern void sha1_init_ctx (struct sha1_ctx *ctx);
42
43/* Starting with the result of former calls of this function (or the
44 initialization function update the context for the next LEN bytes
45 starting at BUFFER.
46 It is necessary that LEN is a multiple of 64!!! */
47extern void sha1_process_block (const void *buffer, size_t len,
48 struct sha1_ctx *ctx);
49
50/* Starting with the result of former calls of this function (or the
51 initialization function update the context for the next LEN bytes
52 starting at BUFFER.
53 It is NOT required that LEN is a multiple of 64. */
54extern void sha1_process_bytes (const void *buffer, size_t len,
55 struct sha1_ctx *ctx);
56
57/* Process the remaining bytes in the buffer and put result from CTX
58 in first 20 bytes following RESBUF. The result is always in little
59 endian byte order, so that a byte-wise output yields to the wanted
60 ASCII representation of the message digest.
61
62 IMPORTANT: On some systems it is required that RESBUF be correctly
63 aligned for a 32 bits value. */
64extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
65
66
67/* Put result from CTX in first 20 bytes following RESBUF. The result is
68 always in little endian byte order, so that a byte-wise output yields
69 to the wanted ASCII representation of the message digest.
70
71 IMPORTANT: On some systems it is required that RESBUF is correctly
72 aligned for a 32 bits value. */
73extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
74
75
76/* Compute SHA1 message digest for bytes read from STREAM. The
77 resulting message digest number will be written into the 20 bytes
78 beginning at RESBLOCK. */
79extern int sha1_stream (FILE *stream, void *resblock);
80
81/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The
82 result is always in little endian byte order, so that a byte-wise
83 output yields to the wanted ASCII representation of the message
84 digest. */
85extern void *sha1_buffer (const char *buffer, size_t len, void *resblock);
86
87#endif
diff --git a/src/daemon/https/lgl/snprintf.c b/src/daemon/https/lgl/snprintf.c
new file mode 100644
index 00000000..538ee08e
--- /dev/null
+++ b/src/daemon/https/lgl/snprintf.c
@@ -0,0 +1,77 @@
1/* Formatted output to strings.
2 Copyright (C) 2004, 2006-2007 Free Software Foundation, Inc.
3 Written by Simon Josefsson and Paul Eggert.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#include <config.h>
20
21/* Specification. */
22#include <stdio.h>
23
24#include <errno.h>
25#include <limits.h>
26#include <stdarg.h>
27#include <stdlib.h>
28#include <string.h>
29
30#include "vasnprintf.h"
31
32/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
33#ifndef EOVERFLOW
34# define EOVERFLOW E2BIG
35#endif
36
37/* Print formatted output to string STR. Similar to sprintf, but
38 additional length SIZE limit how much is written into STR. Returns
39 string length of formatted string (which may be larger than SIZE).
40 STR may be NULL, in which case nothing will be written. On error,
41 return a negative value. */
42int
43snprintf (char *str, size_t size, const char *format, ...)
44{
45 char *output;
46 size_t len;
47 size_t lenbuf = size;
48 va_list args;
49
50 va_start (args, format);
51 output = vasnprintf (str, &lenbuf, format, args);
52 len = lenbuf;
53 va_end (args);
54
55 if (!output)
56 return -1;
57
58 if (output != str)
59 {
60 if (size)
61 {
62 size_t pruned_len = (len < size ? len : size - 1);
63 memcpy (str, output, pruned_len);
64 str[pruned_len] = '\0';
65 }
66
67 free (output);
68 }
69
70 if (INT_MAX < len)
71 {
72 errno = EOVERFLOW;
73 return -1;
74 }
75
76 return len;
77}
diff --git a/src/daemon/https/lgl/strverscmp.c b/src/daemon/https/lgl/strverscmp.c
new file mode 100644
index 00000000..3b6bd20e
--- /dev/null
+++ b/src/daemon/https/lgl/strverscmp.c
@@ -0,0 +1,130 @@
1/* Compare strings while treating digits characters numerically.
2 Copyright (C) 1997, 2000, 2002, 2004, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20#if !_LIBC
21# include <config.h>
22#endif
23
24#include <string.h>
25#include <ctype.h>
26
27/* states: S_N: normal, S_I: comparing integral part, S_F: comparing
28 fractional parts, S_Z: idem but with leading Zeroes only */
29#define S_N 0x0
30#define S_I 0x4
31#define S_F 0x8
32#define S_Z 0xC
33
34/* result_type: CMP: return diff; LEN: compare using len_diff/diff */
35#define CMP 2
36#define LEN 3
37
38
39/* ISDIGIT differs from isdigit, as follows:
40 - Its arg may be any int or unsigned int; it need not be an unsigned char
41 or EOF.
42 - It's typically faster.
43 POSIX says that only '0' through '9' are digits. Prefer ISDIGIT to
44 isdigit unless it's important to use the locale's definition
45 of `digit' even when the host does not conform to POSIX. */
46#define ISDIGIT(c) ((unsigned int) (c) - '0' <= 9)
47
48#undef __strverscmp
49#undef strverscmp
50
51#ifndef weak_alias
52# define __strverscmp strverscmp
53#endif
54
55/* Compare S1 and S2 as strings holding indices/version numbers,
56 returning less than, equal to or greater than zero if S1 is less than,
57 equal to or greater than S2 (for more info, see the texinfo doc).
58*/
59
60int
61__strverscmp (const char *s1, const char *s2)
62{
63 const unsigned char *p1 = (const unsigned char *) s1;
64 const unsigned char *p2 = (const unsigned char *) s2;
65 unsigned char c1, c2;
66 int state;
67 int diff;
68
69 /* Symbol(s) 0 [1-9] others (padding)
70 Transition (10) 0 (01) d (00) x (11) - */
71 static const unsigned int next_state[] = {
72 /* state x d 0 - */
73 /* S_N */ S_N, S_I, S_Z, S_N,
74 /* S_I */ S_N, S_I, S_I, S_I,
75 /* S_F */ S_N, S_F, S_F, S_F,
76 /* S_Z */ S_N, S_F, S_Z, S_Z
77 };
78
79 static const int result_type[] = {
80 /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
81 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
82
83 /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
84 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
85 /* S_I */ CMP, -1, -1, CMP, 1, LEN, LEN, CMP,
86 1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
87 /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
88 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
89 /* S_Z */ CMP, 1, 1, CMP, -1, CMP, CMP, CMP,
90 -1, CMP, CMP, CMP
91 };
92
93 if (p1 == p2)
94 return 0;
95
96 c1 = *p1++;
97 c2 = *p2++;
98 /* Hint: '0' is a digit too. */
99 state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
100
101 while ((diff = c1 - c2) == 0 && c1 != '\0')
102 {
103 state = next_state[state];
104 c1 = *p1++;
105 c2 = *p2++;
106 state |= (c1 == '0') + (ISDIGIT (c1) != 0);
107 }
108
109 state = result_type[state << 2 | ((c2 == '0') + (ISDIGIT (c2) != 0))];
110
111 switch (state)
112 {
113 case CMP:
114 return diff;
115
116 case LEN:
117 while (ISDIGIT (*p1++))
118 if (!ISDIGIT (*p2++))
119 return 1;
120
121 return ISDIGIT (*p2) ? -1 : diff;
122
123 default:
124 return state;
125 }
126}
127
128#ifdef weak_alias
129weak_alias (__strverscmp, strverscmp)
130#endif
diff --git a/src/daemon/https/lgl/strverscmp.h b/src/daemon/https/lgl/strverscmp.h
new file mode 100644
index 00000000..d95d7b31
--- /dev/null
+++ b/src/daemon/https/lgl/strverscmp.h
@@ -0,0 +1,24 @@
1/* Compare strings while treating digits characters numerically.
2
3 Copyright (C) 1997, 2003 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef STRVERSCMP_H_
20# define STRVERSCMP_H_
21
22int strverscmp (const char *, const char *);
23
24#endif /* not STRVERSCMP_H_ */
diff --git a/src/daemon/https/lgl/time_r.c b/src/daemon/https/lgl/time_r.c
new file mode 100644
index 00000000..691525df
--- /dev/null
+++ b/src/daemon/https/lgl/time_r.c
@@ -0,0 +1,46 @@
1/* Reentrant time functions like localtime_r.
2
3 Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19/* Written by Paul Eggert. */
20
21#include <config.h>
22
23#include <time.h>
24
25#include <string.h>
26
27static struct tm *
28copy_tm_result (struct tm *dest, struct tm const *src)
29{
30 if (!src)
31 return 0;
32 *dest = *src;
33 return dest;
34}
35
36struct tm *
37gmtime_r (time_t const *restrict t, struct tm *restrict tp)
38{
39 return copy_tm_result (tp, gmtime (t));
40}
41
42struct tm *
43localtime_r (time_t const *restrict t, struct tm *restrict tp)
44{
45 return copy_tm_result (tp, localtime (t));
46}
diff --git a/src/daemon/https/lgl/vasnprintf.c b/src/daemon/https/lgl/vasnprintf.c
new file mode 100644
index 00000000..d247b592
--- /dev/null
+++ b/src/daemon/https/lgl/vasnprintf.c
@@ -0,0 +1,4825 @@
1/* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18/* This file can be parametrized with the following macros:
19 VASNPRINTF The name of the function being defined.
20 FCHAR_T The element type of the format string.
21 DCHAR_T The element type of the destination (result) string.
22 FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
23 in the format string are ASCII. MUST be set if
24 FCHAR_T and DCHAR_T are not the same type.
25 DIRECTIVE Structure denoting a format directive.
26 Depends on FCHAR_T.
27 DIRECTIVES Structure denoting the set of format directives of a
28 format string. Depends on FCHAR_T.
29 PRINTF_PARSE Function that parses a format string.
30 Depends on FCHAR_T.
31 DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
32 DCHAR_SET memset like function for DCHAR_T[] arrays.
33 DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
34 SNPRINTF The system's snprintf (or similar) function.
35 This may be either snprintf or swprintf.
36 TCHAR_T The element type of the argument and result string
37 of the said SNPRINTF function. This may be either
38 char or wchar_t. The code exploits that
39 sizeof (TCHAR_T) | sizeof (DCHAR_T) and
40 alignof (TCHAR_T) <= alignof (DCHAR_T).
41 DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
42 DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
43 DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
44 DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
45 DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */
46
47/* Tell glibc's <stdio.h> to provide a prototype for snprintf().
48 This must come before <config.h> because <config.h> may include
49 <features.h>, and once <features.h> has been included, it's too late. */
50#ifndef _GNU_SOURCE
51# define _GNU_SOURCE 1
52#endif
53
54#ifndef VASNPRINTF
55# include <config.h>
56#endif
57#ifndef IN_LIBINTL
58# include <alloca.h>
59#endif
60
61/* Specification. */
62#ifndef VASNPRINTF
63# if WIDE_CHAR_VERSION
64# include "vasnwprintf.h"
65# else
66# include "vasnprintf.h"
67# endif
68#endif
69
70#include <locale.h> /* localeconv() */
71#include <stdio.h> /* snprintf(), sprintf() */
72#include <stdlib.h> /* abort(), malloc(), realloc(), free() */
73#include <string.h> /* memcpy(), strlen() */
74#include <errno.h> /* errno */
75#include <limits.h> /* CHAR_BIT */
76#include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
77#if HAVE_NL_LANGINFO
78# include <langinfo.h>
79#endif
80#ifndef VASNPRINTF
81# if WIDE_CHAR_VERSION
82# include "wprintf-parse.h"
83# else
84# include "printf-parse.h"
85# endif
86#endif
87
88/* Checked size_t computations. */
89#include "xsize.h"
90
91#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
92# include <math.h>
93# include "float+.h"
94#endif
95
96#if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
97# include <math.h>
98# include "isnan.h"
99#endif
100
101#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
102# include <math.h>
103# include "isnanl-nolibm.h"
104# include "fpucw.h"
105#endif
106
107#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
108# include <math.h>
109# include "isnan.h"
110# include "printf-frexp.h"
111#endif
112
113#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
114# include <math.h>
115# include "isnanl-nolibm.h"
116# include "printf-frexpl.h"
117# include "fpucw.h"
118#endif
119
120/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
121#ifndef EOVERFLOW
122# define EOVERFLOW E2BIG
123#endif
124
125#if HAVE_WCHAR_T
126# if HAVE_WCSLEN
127# define local_wcslen wcslen
128# else
129 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
130 a dependency towards this library, here is a local substitute.
131 Define this substitute only once, even if this file is included
132 twice in the same compilation unit. */
133# ifndef local_wcslen_defined
134# define local_wcslen_defined 1
135static size_t
136local_wcslen (const wchar_t * s)
137{
138 const wchar_t *ptr;
139
140 for (ptr = s; *ptr != (wchar_t) 0; ptr++)
141 ;
142 return ptr - s;
143}
144# endif
145# endif
146#endif
147
148/* Default parameters. */
149#ifndef VASNPRINTF
150# if WIDE_CHAR_VERSION
151# define VASNPRINTF vasnwprintf
152# define FCHAR_T wchar_t
153# define DCHAR_T wchar_t
154# define TCHAR_T wchar_t
155# define DCHAR_IS_TCHAR 1
156# define DIRECTIVE wchar_t_directive
157# define DIRECTIVES wchar_t_directives
158# define PRINTF_PARSE wprintf_parse
159# define DCHAR_CPY wmemcpy
160# else
161# define VASNPRINTF vasnprintf
162# define FCHAR_T char
163# define DCHAR_T char
164# define TCHAR_T char
165# define DCHAR_IS_TCHAR 1
166# define DIRECTIVE char_directive
167# define DIRECTIVES char_directives
168# define PRINTF_PARSE printf_parse
169# define DCHAR_CPY memcpy
170# endif
171#endif
172#if WIDE_CHAR_VERSION
173 /* TCHAR_T is wchar_t. */
174# define USE_SNPRINTF 1
175# if HAVE_DECL__SNWPRINTF
176 /* On Windows, the function swprintf() has a different signature than
177 on Unix; we use the _snwprintf() function instead. */
178# define SNPRINTF _snwprintf
179# else
180 /* Unix. */
181# define SNPRINTF swprintf
182# endif
183#else
184 /* TCHAR_T is char. */
185# /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
186 But don't use it on BeOS, since BeOS snprintf produces no output if the
187 size argument is >= 0x3000000. */
188# if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
189# define USE_SNPRINTF 1
190# else
191# define USE_SNPRINTF 0
192# endif
193# if HAVE_DECL__SNPRINTF
194 /* Windows. */
195# define SNPRINTF _snprintf
196# else
197 /* Unix. */
198# define SNPRINTF snprintf
199 /* Here we need to call the native snprintf, not rpl_snprintf. */
200# undef snprintf
201# endif
202#endif
203/* Here we need to call the native sprintf, not rpl_sprintf. */
204#undef sprintf
205
206#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
207/* Determine the decimal-point character according to the current locale. */
208# ifndef decimal_point_char_defined
209# define decimal_point_char_defined 1
210static char
211decimal_point_char ()
212{
213 const char *point;
214 /* Determine it in a multithread-safe way. We know nl_langinfo is
215 multithread-safe on glibc systems, but is not required to be multithread-
216 safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
217 is rarely multithread-safe. */
218# if HAVE_NL_LANGINFO && __GLIBC__
219 point = nl_langinfo (RADIXCHAR);
220# elif 1
221 char pointbuf[5];
222 sprintf (pointbuf, "%#.0f", 1.0);
223 point = &pointbuf[1];
224# else
225 point = localeconv ()->decimal_point;
226# endif
227 /* The decimal point is always a single byte: either '.' or ','. */
228 return (point[0] != '\0' ? point[0] : '.');
229}
230# endif
231#endif
232
233#if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
234
235/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
236static int
237is_infinite_or_zero (double x)
238{
239 return isnan (x) || x + x == x;
240}
241
242#endif
243
244#if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
245
246/* Equivalent to !isfinite(x), but does not require libm. */
247static int
248is_infinitel (long double x)
249{
250 return isnanl (x) || (x + x == x && x != 0.0L);
251}
252
253#endif
254
255#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
256
257/* Converting 'long double' to decimal without rare rounding bugs requires
258 real bignums. We use the naming conventions of GNU gmp, but vastly simpler
259 (and slower) algorithms. */
260
261typedef unsigned int mp_limb_t;
262# define GMP_LIMB_BITS 32
263typedef int mp_limb_verify[2 *
264 (sizeof (mp_limb_t) * CHAR_BIT ==
265 GMP_LIMB_BITS) - 1];
266
267typedef unsigned long long mp_twolimb_t;
268# define GMP_TWOLIMB_BITS 64
269typedef int mp_twolimb_verify[2 *
270 (sizeof (mp_twolimb_t) * CHAR_BIT ==
271 GMP_TWOLIMB_BITS) - 1];
272
273/* Representation of a bignum >= 0. */
274typedef struct
275{
276 size_t nlimbs;
277 mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
278} mpn_t;
279
280/* Compute the product of two bignums >= 0.
281 Return the allocated memory in case of success, NULL in case of memory
282 allocation failure. */
283static void *
284multiply (mpn_t src1, mpn_t src2, mpn_t * dest)
285{
286 const mp_limb_t *p1;
287 const mp_limb_t *p2;
288 size_t len1;
289 size_t len2;
290
291 if (src1.nlimbs <= src2.nlimbs)
292 {
293 len1 = src1.nlimbs;
294 p1 = src1.limbs;
295 len2 = src2.nlimbs;
296 p2 = src2.limbs;
297 }
298 else
299 {
300 len1 = src2.nlimbs;
301 p1 = src2.limbs;
302 len2 = src1.nlimbs;
303 p2 = src1.limbs;
304 }
305 /* Now 0 <= len1 <= len2. */
306 if (len1 == 0)
307 {
308 /* src1 or src2 is zero. */
309 dest->nlimbs = 0;
310 dest->limbs = (mp_limb_t *) malloc (1);
311 }
312 else
313 {
314 /* Here 1 <= len1 <= len2. */
315 size_t dlen;
316 mp_limb_t *dp;
317 size_t k, i, j;
318
319 dlen = len1 + len2;
320 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
321 if (dp == NULL)
322 return NULL;
323 for (k = len2; k > 0;)
324 dp[--k] = 0;
325 for (i = 0; i < len1; i++)
326 {
327 mp_limb_t digit1 = p1[i];
328 mp_twolimb_t carry = 0;
329 for (j = 0; j < len2; j++)
330 {
331 mp_limb_t digit2 = p2[j];
332 carry += (mp_twolimb_t) digit1 *(mp_twolimb_t) digit2;
333 carry += dp[i + j];
334 dp[i + j] = (mp_limb_t) carry;
335 carry = carry >> GMP_LIMB_BITS;
336 }
337 dp[i + len2] = (mp_limb_t) carry;
338 }
339 /* Normalise. */
340 while (dlen > 0 && dp[dlen - 1] == 0)
341 dlen--;
342 dest->nlimbs = dlen;
343 dest->limbs = dp;
344 }
345 return dest->limbs;
346}
347
348/* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
349 a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
350 the remainder.
351 Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
352 q is incremented.
353 Return the allocated memory in case of success, NULL in case of memory
354 allocation failure. */
355static void *
356divide (mpn_t a, mpn_t b, mpn_t * q)
357{
358 /* Algorithm:
359 First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
360 with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
361 If m<n, then q:=0 and r:=a.
362 If m>=n=1, perform a single-precision division:
363 r:=0, j:=m,
364 while j>0 do
365 {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
366 = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
367 j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
368 Normalise [q[m-1],...,q[0]], yields q.
369 If m>=n>1, perform a multiple-precision division:
370 We have a/b < beta^(m-n+1).
371 s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
372 Shift a and b left by s bits, copying them. r:=a.
373 r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
374 For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
375 Compute q* :
376 q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
377 In case of overflow (q* >= beta) set q* := beta-1.
378 Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
379 and c3 := b[n-2] * q*.
380 {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
381 occurred. Furthermore 0 <= c3 < beta^2.
382 If there was overflow and
383 r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
384 the next test can be skipped.}
385 While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
386 Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
387 If q* > 0:
388 Put r := r - b * q* * beta^j. In detail:
389 [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
390 hence: u:=0, for i:=0 to n-1 do
391 u := u + q* * b[i],
392 r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
393 u:=u div beta (+ 1, if carry in subtraction)
394 r[n+j]:=r[n+j]-u.
395 {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
396 < q* + 1 <= beta,
397 the carry u does not overflow.}
398 If a negative carry occurs, put q* := q* - 1
399 and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
400 Set q[j] := q*.
401 Normalise [q[m-n],..,q[0]]; this yields the quotient q.
402 Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
403 rest r.
404 The room for q[j] can be allocated at the memory location of r[n+j].
405 Finally, round-to-even:
406 Shift r left by 1 bit.
407 If r > b or if r = b and q[0] is odd, q := q+1.
408 */
409 const mp_limb_t *a_ptr = a.limbs;
410 size_t a_len = a.nlimbs;
411 const mp_limb_t *b_ptr = b.limbs;
412 size_t b_len = b.nlimbs;
413 mp_limb_t *roomptr;
414 mp_limb_t *tmp_roomptr = NULL;
415 mp_limb_t *q_ptr;
416 size_t q_len;
417 mp_limb_t *r_ptr;
418 size_t r_len;
419
420 /* Allocate room for a_len+2 digits.
421 (Need a_len+1 digits for the real division and 1 more digit for the
422 final rounding of q.) */
423 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
424 if (roomptr == NULL)
425 return NULL;
426
427 /* Normalise a. */
428 while (a_len > 0 && a_ptr[a_len - 1] == 0)
429 a_len--;
430
431 /* Normalise b. */
432 for (;;)
433 {
434 if (b_len == 0)
435 /* Division by zero. */
436 abort ();
437 if (b_ptr[b_len - 1] == 0)
438 b_len--;
439 else
440 break;
441 }
442
443 /* Here m = a_len >= 0 and n = b_len > 0. */
444
445 if (a_len < b_len)
446 {
447 /* m<n: trivial case. q=0, r := copy of a. */
448 r_ptr = roomptr;
449 r_len = a_len;
450 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
451 q_ptr = roomptr + a_len;
452 q_len = 0;
453 }
454 else if (b_len == 1)
455 {
456 /* n=1: single precision division.
457 beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
458 r_ptr = roomptr;
459 q_ptr = roomptr + 1;
460 {
461 mp_limb_t den = b_ptr[0];
462 mp_limb_t remainder = 0;
463 const mp_limb_t *sourceptr = a_ptr + a_len;
464 mp_limb_t *destptr = q_ptr + a_len;
465 size_t count;
466 for (count = a_len; count > 0; count--)
467 {
468 mp_twolimb_t num =
469 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
470 *--destptr = num / den;
471 remainder = num % den;
472 }
473 /* Normalise and store r. */
474 if (remainder > 0)
475 {
476 r_ptr[0] = remainder;
477 r_len = 1;
478 }
479 else
480 r_len = 0;
481 /* Normalise q. */
482 q_len = a_len;
483 if (q_ptr[q_len - 1] == 0)
484 q_len--;
485 }
486 }
487 else
488 {
489 /* n>1: multiple precision division.
490 beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
491 beta^(m-n-1) <= a/b < beta^(m-n+1). */
492 /* Determine s. */
493 size_t s;
494 {
495 mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
496 s = 31;
497 if (msd >= 0x10000)
498 {
499 msd = msd >> 16;
500 s -= 16;
501 }
502 if (msd >= 0x100)
503 {
504 msd = msd >> 8;
505 s -= 8;
506 }
507 if (msd >= 0x10)
508 {
509 msd = msd >> 4;
510 s -= 4;
511 }
512 if (msd >= 0x4)
513 {
514 msd = msd >> 2;
515 s -= 2;
516 }
517 if (msd >= 0x2)
518 {
519 msd = msd >> 1;
520 s -= 1;
521 }
522 }
523 /* 0 <= s < GMP_LIMB_BITS.
524 Copy b, shifting it left by s bits. */
525 if (s > 0)
526 {
527 tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
528 if (tmp_roomptr == NULL)
529 {
530 free (roomptr);
531 return NULL;
532 }
533 {
534 const mp_limb_t *sourceptr = b_ptr;
535 mp_limb_t *destptr = tmp_roomptr;
536 mp_twolimb_t accu = 0;
537 size_t count;
538 for (count = b_len; count > 0; count--)
539 {
540 accu += (mp_twolimb_t) * sourceptr++ << s;
541 *destptr++ = (mp_limb_t) accu;
542 accu = accu >> GMP_LIMB_BITS;
543 }
544 /* accu must be zero, since that was how s was determined. */
545 if (accu != 0)
546 abort ();
547 }
548 b_ptr = tmp_roomptr;
549 }
550 /* Copy a, shifting it left by s bits, yields r.
551 Memory layout:
552 At the beginning: r = roomptr[0..a_len],
553 at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
554 r_ptr = roomptr;
555 if (s == 0)
556 {
557 memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
558 r_ptr[a_len] = 0;
559 }
560 else
561 {
562 const mp_limb_t *sourceptr = a_ptr;
563 mp_limb_t *destptr = r_ptr;
564 mp_twolimb_t accu = 0;
565 size_t count;
566 for (count = a_len; count > 0; count--)
567 {
568 accu += (mp_twolimb_t) * sourceptr++ << s;
569 *destptr++ = (mp_limb_t) accu;
570 accu = accu >> GMP_LIMB_BITS;
571 }
572 *destptr++ = (mp_limb_t) accu;
573 }
574 q_ptr = roomptr + b_len;
575 q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
576 {
577 size_t j = a_len - b_len; /* m-n */
578 mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
579 mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
580 mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
581 ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
582 /* Division loop, traversed m-n+1 times.
583 j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
584 for (;;)
585 {
586 mp_limb_t q_star;
587 mp_limb_t c1;
588 if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
589 {
590 /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
591 mp_twolimb_t num =
592 ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
593 | r_ptr[j + b_len - 1];
594 q_star = num / b_msd;
595 c1 = num % b_msd;
596 }
597 else
598 {
599 /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
600 q_star = (mp_limb_t) ~ (mp_limb_t) 0; /* q* = beta-1 */
601 /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
602 <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
603 <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
604 {<= beta !}.
605 If yes, jump directly to the subtraction loop.
606 (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
607 <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
608 if (r_ptr[j + b_len] > b_msd
609 || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
610 /* r[j+n] >= b[n-1]+1 or
611 r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
612 carry. */
613 goto subtract;
614 }
615 /* q_star = q*,
616 c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
617 {
618 mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
619 ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
620 mp_twolimb_t c3 = /* b[n-2] * q* */
621 (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
622 /* While c2 < c3, increase c2 and decrease c3.
623 Consider c3-c2. While it is > 0, decrease it by
624 b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
625 this can happen only twice. */
626 if (c3 > c2)
627 {
628 q_star = q_star - 1; /* q* := q* - 1 */
629 if (c3 - c2 > b_msdd)
630 q_star = q_star - 1; /* q* := q* - 1 */
631 }
632 }
633 if (q_star > 0)
634 subtract:
635 {
636 /* Subtract r := r - b * q* * beta^j. */
637 mp_limb_t cr;
638 {
639 const mp_limb_t *sourceptr = b_ptr;
640 mp_limb_t *destptr = r_ptr + j;
641 mp_twolimb_t carry = 0;
642 size_t count;
643 for (count = b_len; count > 0; count--)
644 {
645 /* Here 0 <= carry <= q*. */
646 carry =
647 carry
648 + (mp_twolimb_t) q_star *(mp_twolimb_t) * sourceptr++
649 + (mp_limb_t) ~ (*destptr);
650 /* Here 0 <= carry <= beta*q* + beta-1. */
651 *destptr++ = ~(mp_limb_t) carry;
652 carry = carry >> GMP_LIMB_BITS; /* <= q* */
653 }
654 cr = (mp_limb_t) carry;
655 }
656 /* Subtract cr from r_ptr[j + b_len], then forget about
657 r_ptr[j + b_len]. */
658 if (cr > r_ptr[j + b_len])
659 {
660 /* Subtraction gave a carry. */
661 q_star = q_star - 1; /* q* := q* - 1 */
662 /* Add b back. */
663 {
664 const mp_limb_t *sourceptr = b_ptr;
665 mp_limb_t *destptr = r_ptr + j;
666 mp_limb_t carry = 0;
667 size_t count;
668 for (count = b_len; count > 0; count--)
669 {
670 mp_limb_t source1 = *sourceptr++;
671 mp_limb_t source2 = *destptr;
672 *destptr++ = source1 + source2 + carry;
673 carry =
674 (carry
675 ? source1 >= (mp_limb_t) ~ source2
676 : source1 > (mp_limb_t) ~ source2);
677 }
678 }
679 /* Forget about the carry and about r[j+n]. */
680 }
681 }
682 /* q* is determined. Store it as q[j]. */
683 q_ptr[j] = q_star;
684 if (j == 0)
685 break;
686 j--;
687 }
688 }
689 r_len = b_len;
690 /* Normalise q. */
691 if (q_ptr[q_len - 1] == 0)
692 q_len--;
693# if 0 /* Not needed here, since we need r only to compare it with b/2, and
694 b is shifted left by s bits. */
695 /* Shift r right by s bits. */
696 if (s > 0)
697 {
698 mp_limb_t ptr = r_ptr + r_len;
699 mp_twolimb_t accu = 0;
700 size_t count;
701 for (count = r_len; count > 0; count--)
702 {
703 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
704 accu += (mp_twolimb_t) * --ptr << (GMP_LIMB_BITS - s);
705 *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
706 }
707 }
708# endif
709 /* Normalise r. */
710 while (r_len > 0 && r_ptr[r_len - 1] == 0)
711 r_len--;
712 }
713 /* Compare r << 1 with b. */
714 if (r_len > b_len)
715 goto increment_q;
716 {
717 size_t i;
718 for (i = b_len;;)
719 {
720 mp_limb_t r_i =
721 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
722 | (i < r_len ? r_ptr[i] << 1 : 0);
723 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
724 if (r_i > b_i)
725 goto increment_q;
726 if (r_i < b_i)
727 goto keep_q;
728 if (i == 0)
729 break;
730 i--;
731 }
732 }
733 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
734 /* q is odd. */
735 increment_q:
736 {
737 size_t i;
738 for (i = 0; i < q_len; i++)
739 if (++(q_ptr[i]) != 0)
740 goto keep_q;
741 q_ptr[q_len++] = 1;
742 }
743keep_q:
744 if (tmp_roomptr != NULL)
745 free (tmp_roomptr);
746 q->limbs = q_ptr;
747 q->nlimbs = q_len;
748 return roomptr;
749}
750
751/* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
752 representation.
753 Destroys the contents of a.
754 Return the allocated memory - containing the decimal digits in low-to-high
755 order, terminated with a NUL character - in case of success, NULL in case
756 of memory allocation failure. */
757static char *
758convert_to_decimal (mpn_t a, size_t extra_zeroes)
759{
760 mp_limb_t *a_ptr = a.limbs;
761 size_t a_len = a.nlimbs;
762 /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
763 size_t c_len = 9 * ((size_t) (a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
764 char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
765 if (c_ptr != NULL)
766 {
767 char *d_ptr = c_ptr;
768 for (; extra_zeroes > 0; extra_zeroes--)
769 *d_ptr++ = '0';
770 while (a_len > 0)
771 {
772 /* Divide a by 10^9, in-place. */
773 mp_limb_t remainder = 0;
774 mp_limb_t *ptr = a_ptr + a_len;
775 size_t count;
776 for (count = a_len; count > 0; count--)
777 {
778 mp_twolimb_t num =
779 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
780 *ptr = num / 1000000000;
781 remainder = num % 1000000000;
782 }
783 /* Store the remainder as 9 decimal digits. */
784 for (count = 9; count > 0; count--)
785 {
786 *d_ptr++ = '0' + (remainder % 10);
787 remainder = remainder / 10;
788 }
789 /* Normalize a. */
790 if (a_ptr[a_len - 1] == 0)
791 a_len--;
792 }
793 /* Remove leading zeroes. */
794 while (d_ptr > c_ptr && d_ptr[-1] == '0')
795 d_ptr--;
796 /* But keep at least one zero. */
797 if (d_ptr == c_ptr)
798 *d_ptr++ = '0';
799 /* Terminate the string. */
800 *d_ptr = '\0';
801 }
802 return c_ptr;
803}
804
805# if NEED_PRINTF_LONG_DOUBLE
806
807/* Assuming x is finite and >= 0:
808 write x as x = 2^e * m, where m is a bignum.
809 Return the allocated memory in case of success, NULL in case of memory
810 allocation failure. */
811static void *
812decode_long_double (long double x, int *ep, mpn_t * mp)
813{
814 mpn_t m;
815 int exp;
816 long double y;
817 size_t i;
818
819 /* Allocate memory for result. */
820 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
821 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
822 if (m.limbs == NULL)
823 return NULL;
824 /* Split into exponential part and mantissa. */
825 y = frexpl (x, &exp);
826 if (!(y >= 0.0L && y < 1.0L))
827 abort ();
828 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
829 latter is an integer. */
830 /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
831 I'm not sure whether it's safe to cast a 'long double' value between
832 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
833 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
834 doesn't matter). */
835# if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
836# if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
837 {
838 mp_limb_t hi, lo;
839 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
840 hi = (int) y;
841 y -= hi;
842 if (!(y >= 0.0L && y < 1.0L))
843 abort ();
844 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
845 lo = (int) y;
846 y -= lo;
847 if (!(y >= 0.0L && y < 1.0L))
848 abort ();
849 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
850 }
851# else
852 {
853 mp_limb_t d;
854 y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
855 d = (int) y;
856 y -= d;
857 if (!(y >= 0.0L && y < 1.0L))
858 abort ();
859 m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
860 }
861# endif
862# endif
863 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0;)
864 {
865 mp_limb_t hi, lo;
866 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
867 hi = (int) y;
868 y -= hi;
869 if (!(y >= 0.0L && y < 1.0L))
870 abort ();
871 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
872 lo = (int) y;
873 y -= lo;
874 if (!(y >= 0.0L && y < 1.0L))
875 abort ();
876 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
877 }
878#if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
879 precision. */
880 if (!(y == 0.0L))
881 abort ();
882#endif
883 /* Normalise. */
884 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
885 m.nlimbs--;
886 *mp = m;
887 *ep = exp - LDBL_MANT_BIT;
888 return m.limbs;
889}
890
891# endif
892
893# if NEED_PRINTF_DOUBLE
894
895/* Assuming x is finite and >= 0:
896 write x as x = 2^e * m, where m is a bignum.
897 Return the allocated memory in case of success, NULL in case of memory
898 allocation failure. */
899static void *
900decode_double (double x, int *ep, mpn_t * mp)
901{
902 mpn_t m;
903 int exp;
904 double y;
905 size_t i;
906
907 /* Allocate memory for result. */
908 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
909 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
910 if (m.limbs == NULL)
911 return NULL;
912 /* Split into exponential part and mantissa. */
913 y = frexp (x, &exp);
914 if (!(y >= 0.0 && y < 1.0))
915 abort ();
916 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
917 latter is an integer. */
918 /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
919 I'm not sure whether it's safe to cast a 'double' value between
920 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
921 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
922 doesn't matter). */
923# if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
924# if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
925 {
926 mp_limb_t hi, lo;
927 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
928 hi = (int) y;
929 y -= hi;
930 if (!(y >= 0.0 && y < 1.0))
931 abort ();
932 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
933 lo = (int) y;
934 y -= lo;
935 if (!(y >= 0.0 && y < 1.0))
936 abort ();
937 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
938 }
939# else
940 {
941 mp_limb_t d;
942 y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
943 d = (int) y;
944 y -= d;
945 if (!(y >= 0.0 && y < 1.0))
946 abort ();
947 m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
948 }
949# endif
950# endif
951 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0;)
952 {
953 mp_limb_t hi, lo;
954 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
955 hi = (int) y;
956 y -= hi;
957 if (!(y >= 0.0 && y < 1.0))
958 abort ();
959 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
960 lo = (int) y;
961 y -= lo;
962 if (!(y >= 0.0 && y < 1.0))
963 abort ();
964 m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
965 }
966 if (!(y == 0.0))
967 abort ();
968 /* Normalise. */
969 while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
970 m.nlimbs--;
971 *mp = m;
972 *ep = exp - DBL_MANT_BIT;
973 return m.limbs;
974}
975
976# endif
977
978/* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
979 Returns the decimal representation of round (x * 10^n).
980 Return the allocated memory - containing the decimal digits in low-to-high
981 order, terminated with a NUL character - in case of success, NULL in case
982 of memory allocation failure. */
983static char *
984scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
985{
986 int s;
987 size_t extra_zeroes;
988 unsigned int abs_n;
989 unsigned int abs_s;
990 mp_limb_t *pow5_ptr;
991 size_t pow5_len;
992 unsigned int s_limbs;
993 unsigned int s_bits;
994 mpn_t pow5;
995 mpn_t z;
996 void *z_memory;
997 char *digits;
998
999 if (memory == NULL)
1000 return NULL;
1001 /* x = 2^e * m, hence
1002 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1003 = round (2^s * 5^n * m). */
1004 s = e + n;
1005 extra_zeroes = 0;
1006 /* Factor out a common power of 10 if possible. */
1007 if (s > 0 && n > 0)
1008 {
1009 extra_zeroes = (s < n ? s : n);
1010 s -= extra_zeroes;
1011 n -= extra_zeroes;
1012 }
1013 /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1014 Before converting to decimal, we need to compute
1015 z = round (2^s * 5^n * m). */
1016 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1017 sign. 2.322 is slightly larger than log(5)/log(2). */
1018 abs_n = (n >= 0 ? n : -n);
1019 abs_s = (s >= 0 ? s : -s);
1020 pow5_ptr =
1021 (mp_limb_t *)
1022 malloc (((int) (abs_n * (2.322f / GMP_LIMB_BITS)) + 1 +
1023 abs_s / GMP_LIMB_BITS + 1) * sizeof (mp_limb_t));
1024 if (pow5_ptr == NULL)
1025 {
1026 free (memory);
1027 return NULL;
1028 }
1029 /* Initialize with 1. */
1030 pow5_ptr[0] = 1;
1031 pow5_len = 1;
1032 /* Multiply with 5^|n|. */
1033 if (abs_n > 0)
1034 {
1035 static mp_limb_t const small_pow5[13 + 1] = {
1036 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1037 48828125, 244140625, 1220703125
1038 };
1039 unsigned int n13;
1040 for (n13 = 0; n13 <= abs_n; n13 += 13)
1041 {
1042 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1043 size_t j;
1044 mp_twolimb_t carry = 0;
1045 for (j = 0; j < pow5_len; j++)
1046 {
1047 mp_limb_t digit2 = pow5_ptr[j];
1048 carry += (mp_twolimb_t) digit1 *(mp_twolimb_t) digit2;
1049 pow5_ptr[j] = (mp_limb_t) carry;
1050 carry = carry >> GMP_LIMB_BITS;
1051 }
1052 if (carry > 0)
1053 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1054 }
1055 }
1056 s_limbs = abs_s / GMP_LIMB_BITS;
1057 s_bits = abs_s % GMP_LIMB_BITS;
1058 if (n >= 0 ? s >= 0 : s <= 0)
1059 {
1060 /* Multiply with 2^|s|. */
1061 if (s_bits > 0)
1062 {
1063 mp_limb_t *ptr = pow5_ptr;
1064 mp_twolimb_t accu = 0;
1065 size_t count;
1066 for (count = pow5_len; count > 0; count--)
1067 {
1068 accu += (mp_twolimb_t) * ptr << s_bits;
1069 *ptr++ = (mp_limb_t) accu;
1070 accu = accu >> GMP_LIMB_BITS;
1071 }
1072 if (accu > 0)
1073 {
1074 *ptr = (mp_limb_t) accu;
1075 pow5_len++;
1076 }
1077 }
1078 if (s_limbs > 0)
1079 {
1080 size_t count;
1081 for (count = pow5_len; count > 0;)
1082 {
1083 count--;
1084 pow5_ptr[s_limbs + count] = pow5_ptr[count];
1085 }
1086 for (count = s_limbs; count > 0;)
1087 {
1088 count--;
1089 pow5_ptr[count] = 0;
1090 }
1091 pow5_len += s_limbs;
1092 }
1093 pow5.limbs = pow5_ptr;
1094 pow5.nlimbs = pow5_len;
1095 if (n >= 0)
1096 {
1097 /* Multiply m with pow5. No division needed. */
1098 z_memory = multiply (m, pow5, &z);
1099 }
1100 else
1101 {
1102 /* Divide m by pow5 and round. */
1103 z_memory = divide (m, pow5, &z);
1104 }
1105 }
1106 else
1107 {
1108 pow5.limbs = pow5_ptr;
1109 pow5.nlimbs = pow5_len;
1110 if (n >= 0)
1111 {
1112 /* n >= 0, s < 0.
1113 Multiply m with pow5, then divide by 2^|s|. */
1114 mpn_t numerator;
1115 mpn_t denominator;
1116 void *tmp_memory;
1117 tmp_memory = multiply (m, pow5, &numerator);
1118 if (tmp_memory == NULL)
1119 {
1120 free (pow5_ptr);
1121 free (memory);
1122 return NULL;
1123 }
1124 /* Construct 2^|s|. */
1125 {
1126 mp_limb_t *ptr = pow5_ptr + pow5_len;
1127 size_t i;
1128 for (i = 0; i < s_limbs; i++)
1129 ptr[i] = 0;
1130 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1131 denominator.limbs = ptr;
1132 denominator.nlimbs = s_limbs + 1;
1133 }
1134 z_memory = divide (numerator, denominator, &z);
1135 free (tmp_memory);
1136 }
1137 else
1138 {
1139 /* n < 0, s > 0.
1140 Multiply m with 2^s, then divide by pow5. */
1141 mpn_t numerator;
1142 mp_limb_t *num_ptr;
1143 num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1144 * sizeof (mp_limb_t));
1145 if (num_ptr == NULL)
1146 {
1147 free (pow5_ptr);
1148 free (memory);
1149 return NULL;
1150 }
1151 {
1152 mp_limb_t *destptr = num_ptr;
1153 {
1154 size_t i;
1155 for (i = 0; i < s_limbs; i++)
1156 *destptr++ = 0;
1157 }
1158 if (s_bits > 0)
1159 {
1160 const mp_limb_t *sourceptr = m.limbs;
1161 mp_twolimb_t accu = 0;
1162 size_t count;
1163 for (count = m.nlimbs; count > 0; count--)
1164 {
1165 accu += (mp_twolimb_t) * sourceptr++ << s_bits;
1166 *destptr++ = (mp_limb_t) accu;
1167 accu = accu >> GMP_LIMB_BITS;
1168 }
1169 if (accu > 0)
1170 *destptr++ = (mp_limb_t) accu;
1171 }
1172 else
1173 {
1174 const mp_limb_t *sourceptr = m.limbs;
1175 size_t count;
1176 for (count = m.nlimbs; count > 0; count--)
1177 *destptr++ = *sourceptr++;
1178 }
1179 numerator.limbs = num_ptr;
1180 numerator.nlimbs = destptr - num_ptr;
1181 }
1182 z_memory = divide (numerator, pow5, &z);
1183 free (num_ptr);
1184 }
1185 }
1186 free (pow5_ptr);
1187 free (memory);
1188
1189 /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1190
1191 if (z_memory == NULL)
1192 return NULL;
1193 digits = convert_to_decimal (z, extra_zeroes);
1194 free (z_memory);
1195 return digits;
1196}
1197
1198# if NEED_PRINTF_LONG_DOUBLE
1199
1200/* Assuming x is finite and >= 0, and n is an integer:
1201 Returns the decimal representation of round (x * 10^n).
1202 Return the allocated memory - containing the decimal digits in low-to-high
1203 order, terminated with a NUL character - in case of success, NULL in case
1204 of memory allocation failure. */
1205static char *
1206scale10_round_decimal_long_double (long double x, int n)
1207{
1208 int e;
1209 mpn_t m;
1210 void *memory = decode_long_double (x, &e, &m);
1211 return scale10_round_decimal_decoded (e, m, memory, n);
1212}
1213
1214# endif
1215
1216# if NEED_PRINTF_DOUBLE
1217
1218/* Assuming x is finite and >= 0, and n is an integer:
1219 Returns the decimal representation of round (x * 10^n).
1220 Return the allocated memory - containing the decimal digits in low-to-high
1221 order, terminated with a NUL character - in case of success, NULL in case
1222 of memory allocation failure. */
1223static char *
1224scale10_round_decimal_double (double x, int n)
1225{
1226 int e;
1227 mpn_t m;
1228 void *memory = decode_double (x, &e, &m);
1229 return scale10_round_decimal_decoded (e, m, memory, n);
1230}
1231
1232# endif
1233
1234# if NEED_PRINTF_LONG_DOUBLE
1235
1236/* Assuming x is finite and > 0:
1237 Return an approximation for n with 10^n <= x < 10^(n+1).
1238 The approximation is usually the right n, but may be off by 1 sometimes. */
1239static int
1240floorlog10l (long double x)
1241{
1242 int exp;
1243 long double y;
1244 double z;
1245 double l;
1246
1247 /* Split into exponential part and mantissa. */
1248 y = frexpl (x, &exp);
1249 if (!(y >= 0.0L && y < 1.0L))
1250 abort ();
1251 if (y == 0.0L)
1252 return INT_MIN;
1253 if (y < 0.5L)
1254 {
1255 while (y <
1256 (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1257 {
1258 y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1259 exp -= GMP_LIMB_BITS;
1260 }
1261 if (y < (1.0L / (1 << 16)))
1262 {
1263 y *= 1.0L * (1 << 16);
1264 exp -= 16;
1265 }
1266 if (y < (1.0L / (1 << 8)))
1267 {
1268 y *= 1.0L * (1 << 8);
1269 exp -= 8;
1270 }
1271 if (y < (1.0L / (1 << 4)))
1272 {
1273 y *= 1.0L * (1 << 4);
1274 exp -= 4;
1275 }
1276 if (y < (1.0L / (1 << 2)))
1277 {
1278 y *= 1.0L * (1 << 2);
1279 exp -= 2;
1280 }
1281 if (y < (1.0L / (1 << 1)))
1282 {
1283 y *= 1.0L * (1 << 1);
1284 exp -= 1;
1285 }
1286 }
1287 if (!(y >= 0.5L && y < 1.0L))
1288 abort ();
1289 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1290 l = exp;
1291 z = y;
1292 if (z < 0.70710678118654752444)
1293 {
1294 z *= 1.4142135623730950488;
1295 l -= 0.5;
1296 }
1297 if (z < 0.8408964152537145431)
1298 {
1299 z *= 1.1892071150027210667;
1300 l -= 0.25;
1301 }
1302 if (z < 0.91700404320467123175)
1303 {
1304 z *= 1.0905077326652576592;
1305 l -= 0.125;
1306 }
1307 if (z < 0.9576032806985736469)
1308 {
1309 z *= 1.0442737824274138403;
1310 l -= 0.0625;
1311 }
1312 /* Now 0.95 <= z <= 1.01. */
1313 z = 1 - z;
1314 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1315 Four terms are enough to get an approximation with error < 10^-7. */
1316 l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1317 /* Finally multiply with log(2)/log(10), yields an approximation for
1318 log10(x). */
1319 l *= 0.30102999566398119523;
1320 /* Round down to the next integer. */
1321 return (int) l + (l < 0 ? -1 : 0);
1322}
1323
1324# endif
1325
1326# if NEED_PRINTF_DOUBLE
1327
1328/* Assuming x is finite and > 0:
1329 Return an approximation for n with 10^n <= x < 10^(n+1).
1330 The approximation is usually the right n, but may be off by 1 sometimes. */
1331static int
1332floorlog10 (double x)
1333{
1334 int exp;
1335 double y;
1336 double z;
1337 double l;
1338
1339 /* Split into exponential part and mantissa. */
1340 y = frexp (x, &exp);
1341 if (!(y >= 0.0 && y < 1.0))
1342 abort ();
1343 if (y == 0.0)
1344 return INT_MIN;
1345 if (y < 0.5)
1346 {
1347 while (y <
1348 (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1349 {
1350 y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1351 exp -= GMP_LIMB_BITS;
1352 }
1353 if (y < (1.0 / (1 << 16)))
1354 {
1355 y *= 1.0 * (1 << 16);
1356 exp -= 16;
1357 }
1358 if (y < (1.0 / (1 << 8)))
1359 {
1360 y *= 1.0 * (1 << 8);
1361 exp -= 8;
1362 }
1363 if (y < (1.0 / (1 << 4)))
1364 {
1365 y *= 1.0 * (1 << 4);
1366 exp -= 4;
1367 }
1368 if (y < (1.0 / (1 << 2)))
1369 {
1370 y *= 1.0 * (1 << 2);
1371 exp -= 2;
1372 }
1373 if (y < (1.0 / (1 << 1)))
1374 {
1375 y *= 1.0 * (1 << 1);
1376 exp -= 1;
1377 }
1378 }
1379 if (!(y >= 0.5 && y < 1.0))
1380 abort ();
1381 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1382 l = exp;
1383 z = y;
1384 if (z < 0.70710678118654752444)
1385 {
1386 z *= 1.4142135623730950488;
1387 l -= 0.5;
1388 }
1389 if (z < 0.8408964152537145431)
1390 {
1391 z *= 1.1892071150027210667;
1392 l -= 0.25;
1393 }
1394 if (z < 0.91700404320467123175)
1395 {
1396 z *= 1.0905077326652576592;
1397 l -= 0.125;
1398 }
1399 if (z < 0.9576032806985736469)
1400 {
1401 z *= 1.0442737824274138403;
1402 l -= 0.0625;
1403 }
1404 /* Now 0.95 <= z <= 1.01. */
1405 z = 1 - z;
1406 /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1407 Four terms are enough to get an approximation with error < 10^-7. */
1408 l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1409 /* Finally multiply with log(2)/log(10), yields an approximation for
1410 log10(x). */
1411 l *= 0.30102999566398119523;
1412 /* Round down to the next integer. */
1413 return (int) l + (l < 0 ? -1 : 0);
1414}
1415
1416# endif
1417
1418#endif
1419
1420DCHAR_T *
1421VASNPRINTF (DCHAR_T * resultbuf, size_t * lengthp,
1422 const FCHAR_T * format, va_list args)
1423{
1424 DIRECTIVES d;
1425 arguments a;
1426
1427 if (PRINTF_PARSE (format, &d, &a) < 0)
1428 /* errno is already set. */
1429 return NULL;
1430
1431#define CLEANUP() \
1432 free (d.dir); \
1433 if (a.arg) \
1434 free (a.arg);
1435
1436 if (PRINTF_FETCHARGS (args, &a) < 0)
1437 {
1438 CLEANUP ();
1439 errno = EINVAL;
1440 return NULL;
1441 }
1442
1443 {
1444 size_t buf_neededlength;
1445 TCHAR_T *buf;
1446 TCHAR_T *buf_malloced;
1447 const FCHAR_T *cp;
1448 size_t i;
1449 DIRECTIVE *dp;
1450 /* Output string accumulator. */
1451 DCHAR_T *result;
1452 size_t allocated;
1453 size_t length;
1454
1455 /* Allocate a small buffer that will hold a directive passed to
1456 sprintf or snprintf. */
1457 buf_neededlength =
1458 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
1459#if HAVE_ALLOCA
1460 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
1461 {
1462 buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
1463 buf_malloced = NULL;
1464 }
1465 else
1466#endif
1467 {
1468 size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
1469 if (size_overflow_p (buf_memsize))
1470 goto out_of_memory_1;
1471 buf = (TCHAR_T *) malloc (buf_memsize);
1472 if (buf == NULL)
1473 goto out_of_memory_1;
1474 buf_malloced = buf;
1475 }
1476
1477 if (resultbuf != NULL)
1478 {
1479 result = resultbuf;
1480 allocated = *lengthp;
1481 }
1482 else
1483 {
1484 result = NULL;
1485 allocated = 0;
1486 }
1487 length = 0;
1488 /* Invariants:
1489 result is either == resultbuf or == NULL or malloc-allocated.
1490 If length > 0, then result != NULL. */
1491
1492 /* Ensures that allocated >= needed. Aborts through a jump to
1493 out_of_memory if needed is SIZE_MAX or otherwise too big. */
1494#define ENSURE_ALLOCATION(needed) \
1495 if ((needed) > allocated) \
1496 { \
1497 size_t memory_size; \
1498 DCHAR_T *memory; \
1499 \
1500 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
1501 if ((needed) > allocated) \
1502 allocated = (needed); \
1503 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1504 if (size_overflow_p (memory_size)) \
1505 goto out_of_memory; \
1506 if (result == resultbuf || result == NULL) \
1507 memory = (DCHAR_T *) malloc (memory_size); \
1508 else \
1509 memory = (DCHAR_T *) realloc (result, memory_size); \
1510 if (memory == NULL) \
1511 goto out_of_memory; \
1512 if (result == resultbuf && length > 0) \
1513 DCHAR_CPY (memory, result, length); \
1514 result = memory; \
1515 }
1516
1517 for (cp = format, i = 0, dp = &d.dir[0];; cp = dp->dir_end, i++, dp++)
1518 {
1519 if (cp != dp->dir_start)
1520 {
1521 size_t n = dp->dir_start - cp;
1522 size_t augmented_length = xsum (length, n);
1523
1524 ENSURE_ALLOCATION (augmented_length);
1525 /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
1526 need that the format string contains only ASCII characters
1527 if FCHAR_T and DCHAR_T are not the same type. */
1528 if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
1529 {
1530 DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
1531 length = augmented_length;
1532 }
1533 else
1534 {
1535 do
1536 result[length++] = (unsigned char) *cp++;
1537 while (--n > 0);
1538 }
1539 }
1540 if (i == d.count)
1541 break;
1542
1543 /* Execute a single directive. */
1544 if (dp->conversion == '%')
1545 {
1546 size_t augmented_length;
1547
1548 if (!(dp->arg_index == ARG_NONE))
1549 abort ();
1550 augmented_length = xsum (length, 1);
1551 ENSURE_ALLOCATION (augmented_length);
1552 result[length] = '%';
1553 length = augmented_length;
1554 }
1555 else
1556 {
1557 if (!(dp->arg_index != ARG_NONE))
1558 abort ();
1559
1560 if (dp->conversion == 'n')
1561 {
1562 switch (a.arg[dp->arg_index].type)
1563 {
1564 case TYPE_COUNT_SCHAR_POINTER:
1565 *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
1566 break;
1567 case TYPE_COUNT_SHORT_POINTER:
1568 *a.arg[dp->arg_index].a.a_count_short_pointer = length;
1569 break;
1570 case TYPE_COUNT_INT_POINTER:
1571 *a.arg[dp->arg_index].a.a_count_int_pointer = length;
1572 break;
1573 case TYPE_COUNT_LONGINT_POINTER:
1574 *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
1575 break;
1576#if HAVE_LONG_LONG_INT
1577 case TYPE_COUNT_LONGLONGINT_POINTER:
1578 *a.arg[dp->arg_index].a.a_count_longlongint_pointer =
1579 length;
1580 break;
1581#endif
1582 default:
1583 abort ();
1584 }
1585 }
1586#if ENABLE_UNISTDIO
1587 /* The unistdio extensions. */
1588 else if (dp->conversion == 'U')
1589 {
1590 arg_type type = a.arg[dp->arg_index].type;
1591 int flags = dp->flags;
1592 int has_width;
1593 size_t width;
1594 int has_precision;
1595 size_t precision;
1596
1597 has_width = 0;
1598 width = 0;
1599 if (dp->width_start != dp->width_end)
1600 {
1601 if (dp->width_arg_index != ARG_NONE)
1602 {
1603 int arg;
1604
1605 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1606 abort ();
1607 arg = a.arg[dp->width_arg_index].a.a_int;
1608 if (arg < 0)
1609 {
1610 /* "A negative field width is taken as a '-' flag
1611 followed by a positive field width." */
1612 flags |= FLAG_LEFT;
1613 width = (unsigned int) (-arg);
1614 }
1615 else
1616 width = arg;
1617 }
1618 else
1619 {
1620 const FCHAR_T *digitp = dp->width_start;
1621
1622 do
1623 width = xsum (xtimes (width, 10), *digitp++ - '0');
1624 while (digitp != dp->width_end);
1625 }
1626 has_width = 1;
1627 }
1628
1629 has_precision = 0;
1630 precision = 0;
1631 if (dp->precision_start != dp->precision_end)
1632 {
1633 if (dp->precision_arg_index != ARG_NONE)
1634 {
1635 int arg;
1636
1637 if (!
1638 (a.arg[dp->precision_arg_index].type == TYPE_INT))
1639 abort ();
1640 arg = a.arg[dp->precision_arg_index].a.a_int;
1641 /* "A negative precision is taken as if the precision
1642 were omitted." */
1643 if (arg >= 0)
1644 {
1645 precision = arg;
1646 has_precision = 1;
1647 }
1648 }
1649 else
1650 {
1651 const FCHAR_T *digitp = dp->precision_start + 1;
1652
1653 precision = 0;
1654 while (digitp != dp->precision_end)
1655 precision =
1656 xsum (xtimes (precision, 10), *digitp++ - '0');
1657 has_precision = 1;
1658 }
1659 }
1660
1661 switch (type)
1662 {
1663 case TYPE_U8_STRING:
1664 {
1665 const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1666 const uint8_t *arg_end;
1667 size_t characters;
1668
1669 if (has_precision)
1670 {
1671 /* Use only PRECISION characters, from the left. */
1672 arg_end = arg;
1673 characters = 0;
1674 for (; precision > 0; precision--)
1675 {
1676 int count = u8_strmblen (arg_end);
1677 if (count == 0)
1678 break;
1679 if (count < 0)
1680 {
1681 if (!
1682 (result == resultbuf || result == NULL))
1683 free (result);
1684 if (buf_malloced != NULL)
1685 free (buf_malloced);
1686 CLEANUP ();
1687 errno = EILSEQ;
1688 return NULL;
1689 }
1690 arg_end += count;
1691 characters++;
1692 }
1693 }
1694 else if (has_width)
1695 {
1696 /* Use the entire string, and count the number of
1697 characters. */
1698 arg_end = arg;
1699 characters = 0;
1700 for (;;)
1701 {
1702 int count = u8_strmblen (arg_end);
1703 if (count == 0)
1704 break;
1705 if (count < 0)
1706 {
1707 if (!
1708 (result == resultbuf || result == NULL))
1709 free (result);
1710 if (buf_malloced != NULL)
1711 free (buf_malloced);
1712 CLEANUP ();
1713 errno = EILSEQ;
1714 return NULL;
1715 }
1716 arg_end += count;
1717 characters++;
1718 }
1719 }
1720 else
1721 {
1722 /* Use the entire string. */
1723 arg_end = arg + u8_strlen (arg);
1724 /* The number of characters doesn't matter. */
1725 characters = 0;
1726 }
1727
1728 if (has_width && width > characters
1729 && !(dp->flags & FLAG_LEFT))
1730 {
1731 size_t n = width - characters;
1732 ENSURE_ALLOCATION (xsum (length, n));
1733 DCHAR_SET (result + length, ' ', n);
1734 length += n;
1735 }
1736
1737# if DCHAR_IS_UINT8_T
1738 {
1739 size_t n = arg_end - arg;
1740 ENSURE_ALLOCATION (xsum (length, n));
1741 DCHAR_CPY (result + length, arg, n);
1742 length += n;
1743 }
1744# else
1745 { /* Convert. */
1746 DCHAR_T *converted = result + length;
1747 size_t converted_len = allocated - length;
1748# if DCHAR_IS_TCHAR
1749 /* Convert from UTF-8 to locale encoding. */
1750 if (u8_conv_to_encoding (locale_charset (),
1751 iconveh_question_mark,
1752 arg, arg_end - arg, NULL,
1753 &converted, &converted_len)
1754 < 0)
1755# else
1756 /* Convert from UTF-8 to UTF-16/UTF-32. */
1757 converted =
1758 U8_TO_DCHAR (arg, arg_end - arg,
1759 converted, &converted_len);
1760 if (converted == NULL)
1761# endif
1762 {
1763 int saved_errno = errno;
1764 if (!(result == resultbuf || result == NULL))
1765 free (result);
1766 if (buf_malloced != NULL)
1767 free (buf_malloced);
1768 CLEANUP ();
1769 errno = saved_errno;
1770 return NULL;
1771 }
1772 if (converted != result + length)
1773 {
1774 ENSURE_ALLOCATION (xsum (length, converted_len));
1775 DCHAR_CPY (result + length, converted,
1776 converted_len);
1777 free (converted);
1778 }
1779 length += converted_len;
1780 }
1781# endif
1782
1783 if (has_width && width > characters
1784 && (dp->flags & FLAG_LEFT))
1785 {
1786 size_t n = width - characters;
1787 ENSURE_ALLOCATION (xsum (length, n));
1788 DCHAR_SET (result + length, ' ', n);
1789 length += n;
1790 }
1791 }
1792 break;
1793
1794 case TYPE_U16_STRING:
1795 {
1796 const uint16_t *arg =
1797 a.arg[dp->arg_index].a.a_u16_string;
1798 const uint16_t *arg_end;
1799 size_t characters;
1800
1801 if (has_precision)
1802 {
1803 /* Use only PRECISION characters, from the left. */
1804 arg_end = arg;
1805 characters = 0;
1806 for (; precision > 0; precision--)
1807 {
1808 int count = u16_strmblen (arg_end);
1809 if (count == 0)
1810 break;
1811 if (count < 0)
1812 {
1813 if (!
1814 (result == resultbuf || result == NULL))
1815 free (result);
1816 if (buf_malloced != NULL)
1817 free (buf_malloced);
1818 CLEANUP ();
1819 errno = EILSEQ;
1820 return NULL;
1821 }
1822 arg_end += count;
1823 characters++;
1824 }
1825 }
1826 else if (has_width)
1827 {
1828 /* Use the entire string, and count the number of
1829 characters. */
1830 arg_end = arg;
1831 characters = 0;
1832 for (;;)
1833 {
1834 int count = u16_strmblen (arg_end);
1835 if (count == 0)
1836 break;
1837 if (count < 0)
1838 {
1839 if (!
1840 (result == resultbuf || result == NULL))
1841 free (result);
1842 if (buf_malloced != NULL)
1843 free (buf_malloced);
1844 CLEANUP ();
1845 errno = EILSEQ;
1846 return NULL;
1847 }
1848 arg_end += count;
1849 characters++;
1850 }
1851 }
1852 else
1853 {
1854 /* Use the entire string. */
1855 arg_end = arg + u16_strlen (arg);
1856 /* The number of characters doesn't matter. */
1857 characters = 0;
1858 }
1859
1860 if (has_width && width > characters
1861 && !(dp->flags & FLAG_LEFT))
1862 {
1863 size_t n = width - characters;
1864 ENSURE_ALLOCATION (xsum (length, n));
1865 DCHAR_SET (result + length, ' ', n);
1866 length += n;
1867 }
1868
1869# if DCHAR_IS_UINT16_T
1870 {
1871 size_t n = arg_end - arg;
1872 ENSURE_ALLOCATION (xsum (length, n));
1873 DCHAR_CPY (result + length, arg, n);
1874 length += n;
1875 }
1876# else
1877 { /* Convert. */
1878 DCHAR_T *converted = result + length;
1879 size_t converted_len = allocated - length;
1880# if DCHAR_IS_TCHAR
1881 /* Convert from UTF-16 to locale encoding. */
1882 if (u16_conv_to_encoding (locale_charset (),
1883 iconveh_question_mark,
1884 arg, arg_end - arg, NULL,
1885 &converted, &converted_len)
1886 < 0)
1887# else
1888 /* Convert from UTF-16 to UTF-8/UTF-32. */
1889 converted =
1890 U16_TO_DCHAR (arg, arg_end - arg,
1891 converted, &converted_len);
1892 if (converted == NULL)
1893# endif
1894 {
1895 int saved_errno = errno;
1896 if (!(result == resultbuf || result == NULL))
1897 free (result);
1898 if (buf_malloced != NULL)
1899 free (buf_malloced);
1900 CLEANUP ();
1901 errno = saved_errno;
1902 return NULL;
1903 }
1904 if (converted != result + length)
1905 {
1906 ENSURE_ALLOCATION (xsum (length, converted_len));
1907 DCHAR_CPY (result + length, converted,
1908 converted_len);
1909 free (converted);
1910 }
1911 length += converted_len;
1912 }
1913# endif
1914
1915 if (has_width && width > characters
1916 && (dp->flags & FLAG_LEFT))
1917 {
1918 size_t n = width - characters;
1919 ENSURE_ALLOCATION (xsum (length, n));
1920 DCHAR_SET (result + length, ' ', n);
1921 length += n;
1922 }
1923 }
1924 break;
1925
1926 case TYPE_U32_STRING:
1927 {
1928 const uint32_t *arg =
1929 a.arg[dp->arg_index].a.a_u32_string;
1930 const uint32_t *arg_end;
1931 size_t characters;
1932
1933 if (has_precision)
1934 {
1935 /* Use only PRECISION characters, from the left. */
1936 arg_end = arg;
1937 characters = 0;
1938 for (; precision > 0; precision--)
1939 {
1940 int count = u32_strmblen (arg_end);
1941 if (count == 0)
1942 break;
1943 if (count < 0)
1944 {
1945 if (!
1946 (result == resultbuf || result == NULL))
1947 free (result);
1948 if (buf_malloced != NULL)
1949 free (buf_malloced);
1950 CLEANUP ();
1951 errno = EILSEQ;
1952 return NULL;
1953 }
1954 arg_end += count;
1955 characters++;
1956 }
1957 }
1958 else if (has_width)
1959 {
1960 /* Use the entire string, and count the number of
1961 characters. */
1962 arg_end = arg;
1963 characters = 0;
1964 for (;;)
1965 {
1966 int count = u32_strmblen (arg_end);
1967 if (count == 0)
1968 break;
1969 if (count < 0)
1970 {
1971 if (!
1972 (result == resultbuf || result == NULL))
1973 free (result);
1974 if (buf_malloced != NULL)
1975 free (buf_malloced);
1976 CLEANUP ();
1977 errno = EILSEQ;
1978 return NULL;
1979 }
1980 arg_end += count;
1981 characters++;
1982 }
1983 }
1984 else
1985 {
1986 /* Use the entire string. */
1987 arg_end = arg + u32_strlen (arg);
1988 /* The number of characters doesn't matter. */
1989 characters = 0;
1990 }
1991
1992 if (has_width && width > characters
1993 && !(dp->flags & FLAG_LEFT))
1994 {
1995 size_t n = width - characters;
1996 ENSURE_ALLOCATION (xsum (length, n));
1997 DCHAR_SET (result + length, ' ', n);
1998 length += n;
1999 }
2000
2001# if DCHAR_IS_UINT32_T
2002 {
2003 size_t n = arg_end - arg;
2004 ENSURE_ALLOCATION (xsum (length, n));
2005 DCHAR_CPY (result + length, arg, n);
2006 length += n;
2007 }
2008# else
2009 { /* Convert. */
2010 DCHAR_T *converted = result + length;
2011 size_t converted_len = allocated - length;
2012# if DCHAR_IS_TCHAR
2013 /* Convert from UTF-32 to locale encoding. */
2014 if (u32_conv_to_encoding (locale_charset (),
2015 iconveh_question_mark,
2016 arg, arg_end - arg, NULL,
2017 &converted, &converted_len)
2018 < 0)
2019# else
2020 /* Convert from UTF-32 to UTF-8/UTF-16. */
2021 converted =
2022 U32_TO_DCHAR (arg, arg_end - arg,
2023 converted, &converted_len);
2024 if (converted == NULL)
2025# endif
2026 {
2027 int saved_errno = errno;
2028 if (!(result == resultbuf || result == NULL))
2029 free (result);
2030 if (buf_malloced != NULL)
2031 free (buf_malloced);
2032 CLEANUP ();
2033 errno = saved_errno;
2034 return NULL;
2035 }
2036 if (converted != result + length)
2037 {
2038 ENSURE_ALLOCATION (xsum (length, converted_len));
2039 DCHAR_CPY (result + length, converted,
2040 converted_len);
2041 free (converted);
2042 }
2043 length += converted_len;
2044 }
2045# endif
2046
2047 if (has_width && width > characters
2048 && (dp->flags & FLAG_LEFT))
2049 {
2050 size_t n = width - characters;
2051 ENSURE_ALLOCATION (xsum (length, n));
2052 DCHAR_SET (result + length, ' ', n);
2053 length += n;
2054 }
2055 }
2056 break;
2057
2058 default:
2059 abort ();
2060 }
2061 }
2062#endif
2063#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2064 else if ((dp->conversion == 'a' || dp->conversion == 'A')
2065# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2066 && (0
2067# if NEED_PRINTF_DOUBLE
2068 || a.arg[dp->arg_index].type == TYPE_DOUBLE
2069# endif
2070# if NEED_PRINTF_LONG_DOUBLE
2071 || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2072# endif
2073 )
2074# endif
2075 )
2076 {
2077 arg_type type = a.arg[dp->arg_index].type;
2078 int flags = dp->flags;
2079 int has_width;
2080 size_t width;
2081 int has_precision;
2082 size_t precision;
2083 size_t tmp_length;
2084 DCHAR_T tmpbuf[700];
2085 DCHAR_T *tmp;
2086 DCHAR_T *pad_ptr;
2087 DCHAR_T *p;
2088
2089 has_width = 0;
2090 width = 0;
2091 if (dp->width_start != dp->width_end)
2092 {
2093 if (dp->width_arg_index != ARG_NONE)
2094 {
2095 int arg;
2096
2097 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2098 abort ();
2099 arg = a.arg[dp->width_arg_index].a.a_int;
2100 if (arg < 0)
2101 {
2102 /* "A negative field width is taken as a '-' flag
2103 followed by a positive field width." */
2104 flags |= FLAG_LEFT;
2105 width = (unsigned int) (-arg);
2106 }
2107 else
2108 width = arg;
2109 }
2110 else
2111 {
2112 const FCHAR_T *digitp = dp->width_start;
2113
2114 do
2115 width = xsum (xtimes (width, 10), *digitp++ - '0');
2116 while (digitp != dp->width_end);
2117 }
2118 has_width = 1;
2119 }
2120
2121 has_precision = 0;
2122 precision = 0;
2123 if (dp->precision_start != dp->precision_end)
2124 {
2125 if (dp->precision_arg_index != ARG_NONE)
2126 {
2127 int arg;
2128
2129 if (!
2130 (a.arg[dp->precision_arg_index].type == TYPE_INT))
2131 abort ();
2132 arg = a.arg[dp->precision_arg_index].a.a_int;
2133 /* "A negative precision is taken as if the precision
2134 were omitted." */
2135 if (arg >= 0)
2136 {
2137 precision = arg;
2138 has_precision = 1;
2139 }
2140 }
2141 else
2142 {
2143 const FCHAR_T *digitp = dp->precision_start + 1;
2144
2145 precision = 0;
2146 while (digitp != dp->precision_end)
2147 precision =
2148 xsum (xtimes (precision, 10), *digitp++ - '0');
2149 has_precision = 1;
2150 }
2151 }
2152
2153 /* Allocate a temporary buffer of sufficient size. */
2154 if (type == TYPE_LONGDOUBLE)
2155 tmp_length = (unsigned int) ((LDBL_DIG + 1) * 0.831 /* decimal -> hexadecimal */
2156 ) + 1; /* turn floor into ceil */
2157 else
2158 tmp_length = (unsigned int) ((DBL_DIG + 1) * 0.831 /* decimal -> hexadecimal */
2159 ) + 1; /* turn floor into ceil */
2160 if (tmp_length < precision)
2161 tmp_length = precision;
2162 /* Account for sign, decimal point etc. */
2163 tmp_length = xsum (tmp_length, 12);
2164
2165 if (tmp_length < width)
2166 tmp_length = width;
2167
2168 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2169
2170 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2171 tmp = tmpbuf;
2172 else
2173 {
2174 size_t tmp_memsize =
2175 xtimes (tmp_length, sizeof (DCHAR_T));
2176
2177 if (size_overflow_p (tmp_memsize))
2178 /* Overflow, would lead to out of memory. */
2179 goto out_of_memory;
2180 tmp = (DCHAR_T *) malloc (tmp_memsize);
2181 if (tmp == NULL)
2182 /* Out of memory. */
2183 goto out_of_memory;
2184 }
2185
2186 pad_ptr = NULL;
2187 p = tmp;
2188 if (type == TYPE_LONGDOUBLE)
2189 {
2190# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2191 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2192
2193 if (isnanl (arg))
2194 {
2195 if (dp->conversion == 'A')
2196 {
2197 *p++ = 'N';
2198 *p++ = 'A';
2199 *p++ = 'N';
2200 }
2201 else
2202 {
2203 *p++ = 'n';
2204 *p++ = 'a';
2205 *p++ = 'n';
2206 }
2207 }
2208 else
2209 {
2210 int sign = 0;
2211 DECL_LONG_DOUBLE_ROUNDING
2212 BEGIN_LONG_DOUBLE_ROUNDING ();
2213
2214 if (signbit (arg)) /* arg < 0.0L or negative zero */
2215 {
2216 sign = -1;
2217 arg = -arg;
2218 }
2219
2220 if (sign < 0)
2221 *p++ = '-';
2222 else if (flags & FLAG_SHOWSIGN)
2223 *p++ = '+';
2224 else if (flags & FLAG_SPACE)
2225 *p++ = ' ';
2226
2227 if (arg > 0.0L && arg + arg == arg)
2228 {
2229 if (dp->conversion == 'A')
2230 {
2231 *p++ = 'I';
2232 *p++ = 'N';
2233 *p++ = 'F';
2234 }
2235 else
2236 {
2237 *p++ = 'i';
2238 *p++ = 'n';
2239 *p++ = 'f';
2240 }
2241 }
2242 else
2243 {
2244 int exponent;
2245 long double mantissa;
2246
2247 if (arg > 0.0L)
2248 mantissa = printf_frexpl (arg, &exponent);
2249 else
2250 {
2251 exponent = 0;
2252 mantissa = 0.0L;
2253 }
2254
2255 if (has_precision
2256 && precision <
2257 (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2258 {
2259 /* Round the mantissa. */
2260 long double tail = mantissa;
2261 size_t q;
2262
2263 for (q = precision;; q--)
2264 {
2265 int digit = (int) tail;
2266 tail -= digit;
2267 if (q == 0)
2268 {
2269 if (digit & 1 ? tail >= 0.5L : tail >
2270 0.5L)
2271 tail = 1 - tail;
2272 else
2273 tail = -tail;
2274 break;
2275 }
2276 tail *= 16.0L;
2277 }
2278 if (tail != 0.0L)
2279 for (q = precision; q > 0; q--)
2280 tail *= 0.0625L;
2281 mantissa += tail;
2282 }
2283
2284 *p++ = '0';
2285 *p++ = dp->conversion - 'A' + 'X';
2286 pad_ptr = p;
2287 {
2288 int digit;
2289
2290 digit = (int) mantissa;
2291 mantissa -= digit;
2292 *p++ = '0' + digit;
2293 if ((flags & FLAG_ALT)
2294 || mantissa > 0.0L || precision > 0)
2295 {
2296 *p++ = decimal_point_char ();
2297 /* This loop terminates because we assume
2298 that FLT_RADIX is a power of 2. */
2299 while (mantissa > 0.0L)
2300 {
2301 mantissa *= 16.0L;
2302 digit = (int) mantissa;
2303 mantissa -= digit;
2304 *p++ = digit
2305 + (digit < 10
2306 ? '0' : dp->conversion - 10);
2307 if (precision > 0)
2308 precision--;
2309 }
2310 while (precision > 0)
2311 {
2312 *p++ = '0';
2313 precision--;
2314 }
2315 }
2316 }
2317 *p++ = dp->conversion - 'A' + 'P';
2318# if WIDE_CHAR_VERSION
2319 {
2320 static const wchar_t decimal_format[] =
2321 { '%', '+', 'd', '\0' };
2322 SNPRINTF (p, 6 + 1, decimal_format, exponent);
2323 }
2324 while (*p != '\0')
2325 p++;
2326# else
2327 if (sizeof (DCHAR_T) == 1)
2328 {
2329 sprintf ((char *) p, "%+d", exponent);
2330 while (*p != '\0')
2331 p++;
2332 }
2333 else
2334 {
2335 char expbuf[6 + 1];
2336 const char *ep;
2337 sprintf (expbuf, "%+d", exponent);
2338 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2339 p++;
2340 }
2341# endif
2342 }
2343
2344 END_LONG_DOUBLE_ROUNDING ();
2345 }
2346# else
2347 abort ();
2348# endif
2349 }
2350 else
2351 {
2352# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2353 double arg = a.arg[dp->arg_index].a.a_double;
2354
2355 if (isnan (arg))
2356 {
2357 if (dp->conversion == 'A')
2358 {
2359 *p++ = 'N';
2360 *p++ = 'A';
2361 *p++ = 'N';
2362 }
2363 else
2364 {
2365 *p++ = 'n';
2366 *p++ = 'a';
2367 *p++ = 'n';
2368 }
2369 }
2370 else
2371 {
2372 int sign = 0;
2373
2374 if (signbit (arg)) /* arg < 0.0 or negative zero */
2375 {
2376 sign = -1;
2377 arg = -arg;
2378 }
2379
2380 if (sign < 0)
2381 *p++ = '-';
2382 else if (flags & FLAG_SHOWSIGN)
2383 *p++ = '+';
2384 else if (flags & FLAG_SPACE)
2385 *p++ = ' ';
2386
2387 if (arg > 0.0 && arg + arg == arg)
2388 {
2389 if (dp->conversion == 'A')
2390 {
2391 *p++ = 'I';
2392 *p++ = 'N';
2393 *p++ = 'F';
2394 }
2395 else
2396 {
2397 *p++ = 'i';
2398 *p++ = 'n';
2399 *p++ = 'f';
2400 }
2401 }
2402 else
2403 {
2404 int exponent;
2405 double mantissa;
2406
2407 if (arg > 0.0)
2408 mantissa = printf_frexp (arg, &exponent);
2409 else
2410 {
2411 exponent = 0;
2412 mantissa = 0.0;
2413 }
2414
2415 if (has_precision
2416 && precision <
2417 (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2418 {
2419 /* Round the mantissa. */
2420 double tail = mantissa;
2421 size_t q;
2422
2423 for (q = precision;; q--)
2424 {
2425 int digit = (int) tail;
2426 tail -= digit;
2427 if (q == 0)
2428 {
2429 if (digit & 1 ? tail >= 0.5 : tail >
2430 0.5)
2431 tail = 1 - tail;
2432 else
2433 tail = -tail;
2434 break;
2435 }
2436 tail *= 16.0;
2437 }
2438 if (tail != 0.0)
2439 for (q = precision; q > 0; q--)
2440 tail *= 0.0625;
2441 mantissa += tail;
2442 }
2443
2444 *p++ = '0';
2445 *p++ = dp->conversion - 'A' + 'X';
2446 pad_ptr = p;
2447 {
2448 int digit;
2449
2450 digit = (int) mantissa;
2451 mantissa -= digit;
2452 *p++ = '0' + digit;
2453 if ((flags & FLAG_ALT)
2454 || mantissa > 0.0 || precision > 0)
2455 {
2456 *p++ = decimal_point_char ();
2457 /* This loop terminates because we assume
2458 that FLT_RADIX is a power of 2. */
2459 while (mantissa > 0.0)
2460 {
2461 mantissa *= 16.0;
2462 digit = (int) mantissa;
2463 mantissa -= digit;
2464 *p++ = digit
2465 + (digit < 10
2466 ? '0' : dp->conversion - 10);
2467 if (precision > 0)
2468 precision--;
2469 }
2470 while (precision > 0)
2471 {
2472 *p++ = '0';
2473 precision--;
2474 }
2475 }
2476 }
2477 *p++ = dp->conversion - 'A' + 'P';
2478# if WIDE_CHAR_VERSION
2479 {
2480 static const wchar_t decimal_format[] =
2481 { '%', '+', 'd', '\0' };
2482 SNPRINTF (p, 6 + 1, decimal_format, exponent);
2483 }
2484 while (*p != '\0')
2485 p++;
2486# else
2487 if (sizeof (DCHAR_T) == 1)
2488 {
2489 sprintf ((char *) p, "%+d", exponent);
2490 while (*p != '\0')
2491 p++;
2492 }
2493 else
2494 {
2495 char expbuf[6 + 1];
2496 const char *ep;
2497 sprintf (expbuf, "%+d", exponent);
2498 for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2499 p++;
2500 }
2501# endif
2502 }
2503 }
2504# else
2505 abort ();
2506# endif
2507 }
2508 /* The generated string now extends from tmp to p, with the
2509 zero padding insertion point being at pad_ptr. */
2510 if (has_width && p - tmp < width)
2511 {
2512 size_t pad = width - (p - tmp);
2513 DCHAR_T *end = p + pad;
2514
2515 if (flags & FLAG_LEFT)
2516 {
2517 /* Pad with spaces on the right. */
2518 for (; pad > 0; pad--)
2519 *p++ = ' ';
2520 }
2521 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
2522 {
2523 /* Pad with zeroes. */
2524 DCHAR_T *q = end;
2525
2526 while (p > pad_ptr)
2527 *--q = *--p;
2528 for (; pad > 0; pad--)
2529 *p++ = '0';
2530 }
2531 else
2532 {
2533 /* Pad with spaces on the left. */
2534 DCHAR_T *q = end;
2535
2536 while (p > tmp)
2537 *--q = *--p;
2538 for (; pad > 0; pad--)
2539 *p++ = ' ';
2540 }
2541
2542 p = end;
2543 }
2544
2545 {
2546 size_t count = p - tmp;
2547
2548 if (count >= tmp_length)
2549 /* tmp_length was incorrectly calculated - fix the
2550 code above! */
2551 abort ();
2552
2553 /* Make room for the result. */
2554 if (count >= allocated - length)
2555 {
2556 size_t n = xsum (length, count);
2557
2558 ENSURE_ALLOCATION (n);
2559 }
2560
2561 /* Append the result. */
2562 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
2563 if (tmp != tmpbuf)
2564 free (tmp);
2565 length += count;
2566 }
2567 }
2568#endif
2569#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2570 else if ((dp->conversion == 'f' || dp->conversion == 'F'
2571 || dp->conversion == 'e' || dp->conversion == 'E'
2572 || dp->conversion == 'g' || dp->conversion == 'G'
2573 || dp->conversion == 'a' || dp->conversion == 'A') && (0
2574# if NEED_PRINTF_DOUBLE
2575 ||
2576 a.
2577 arg
2578 [dp->
2579 arg_index].
2580 type
2581 ==
2582 TYPE_DOUBLE
2583# elif NEED_PRINTF_INFINITE_DOUBLE
2584 ||
2585 (a.
2586 arg
2587 [dp->
2588 arg_index].
2589 type
2590 ==
2591 TYPE_DOUBLE
2592 /* The systems (mingw) which produce wrong output
2593 for Inf, -Inf, and NaN also do so for -0.0.
2594 Therefore we treat this case here as well. */
2595 &&
2596 is_infinite_or_zero
2597 (a.
2598 arg
2599 [dp->
2600 arg_index].
2601 a.
2602 a_double))
2603# endif
2604# if NEED_PRINTF_LONG_DOUBLE
2605 ||
2606 a.
2607 arg
2608 [dp->
2609 arg_index].
2610 type
2611 ==
2612 TYPE_LONGDOUBLE
2613# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2614 ||
2615 (a.
2616 arg
2617 [dp->
2618 arg_index].
2619 type
2620 ==
2621 TYPE_LONGDOUBLE
2622 /* Some systems produce wrong output for Inf,
2623 -Inf, and NaN. */
2624 &&
2625 is_infinitel
2626 (a.
2627 arg
2628 [dp->
2629 arg_index].
2630 a.
2631 a_longdouble))
2632# endif
2633 ))
2634 {
2635# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2636 arg_type type = a.arg[dp->arg_index].type;
2637# endif
2638 int flags = dp->flags;
2639 int has_width;
2640 size_t width;
2641 int has_precision;
2642 size_t precision;
2643 size_t tmp_length;
2644 DCHAR_T tmpbuf[700];
2645 DCHAR_T *tmp;
2646 DCHAR_T *pad_ptr;
2647 DCHAR_T *p;
2648
2649 has_width = 0;
2650 width = 0;
2651 if (dp->width_start != dp->width_end)
2652 {
2653 if (dp->width_arg_index != ARG_NONE)
2654 {
2655 int arg;
2656
2657 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2658 abort ();
2659 arg = a.arg[dp->width_arg_index].a.a_int;
2660 if (arg < 0)
2661 {
2662 /* "A negative field width is taken as a '-' flag
2663 followed by a positive field width." */
2664 flags |= FLAG_LEFT;
2665 width = (unsigned int) (-arg);
2666 }
2667 else
2668 width = arg;
2669 }
2670 else
2671 {
2672 const FCHAR_T *digitp = dp->width_start;
2673
2674 do
2675 width = xsum (xtimes (width, 10), *digitp++ - '0');
2676 while (digitp != dp->width_end);
2677 }
2678 has_width = 1;
2679 }
2680
2681 has_precision = 0;
2682 precision = 0;
2683 if (dp->precision_start != dp->precision_end)
2684 {
2685 if (dp->precision_arg_index != ARG_NONE)
2686 {
2687 int arg;
2688
2689 if (!
2690 (a.arg[dp->precision_arg_index].type == TYPE_INT))
2691 abort ();
2692 arg = a.arg[dp->precision_arg_index].a.a_int;
2693 /* "A negative precision is taken as if the precision
2694 were omitted." */
2695 if (arg >= 0)
2696 {
2697 precision = arg;
2698 has_precision = 1;
2699 }
2700 }
2701 else
2702 {
2703 const FCHAR_T *digitp = dp->precision_start + 1;
2704
2705 precision = 0;
2706 while (digitp != dp->precision_end)
2707 precision =
2708 xsum (xtimes (precision, 10), *digitp++ - '0');
2709 has_precision = 1;
2710 }
2711 }
2712
2713 /* POSIX specifies the default precision to be 6 for %f, %F,
2714 %e, %E, but not for %g, %G. Implementations appear to use
2715 the same default precision also for %g, %G. */
2716 if (!has_precision)
2717 precision = 6;
2718
2719 /* Allocate a temporary buffer of sufficient size. */
2720# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2721 tmp_length =
2722 (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
2723# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2724 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
2725# elif NEED_PRINTF_LONG_DOUBLE
2726 tmp_length = LDBL_DIG + 1;
2727# elif NEED_PRINTF_DOUBLE
2728 tmp_length = DBL_DIG + 1;
2729# else
2730 tmp_length = 0;
2731# endif
2732 if (tmp_length < precision)
2733 tmp_length = precision;
2734# if NEED_PRINTF_LONG_DOUBLE
2735# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2736 if (type == TYPE_LONGDOUBLE)
2737# endif
2738 if (dp->conversion == 'f' || dp->conversion == 'F')
2739 {
2740 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2741 if (!(isnanl (arg) || arg + arg == arg))
2742 {
2743 /* arg is finite and nonzero. */
2744 int exponent = floorlog10l (arg < 0 ? -arg : arg);
2745 if (exponent >= 0
2746 && tmp_length < exponent + precision)
2747 tmp_length = exponent + precision;
2748 }
2749 }
2750# endif
2751# if NEED_PRINTF_DOUBLE
2752# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2753 if (type == TYPE_DOUBLE)
2754# endif
2755 if (dp->conversion == 'f' || dp->conversion == 'F')
2756 {
2757 double arg = a.arg[dp->arg_index].a.a_double;
2758 if (!(isnan (arg) || arg + arg == arg))
2759 {
2760 /* arg is finite and nonzero. */
2761 int exponent = floorlog10 (arg < 0 ? -arg : arg);
2762 if (exponent >= 0
2763 && tmp_length < exponent + precision)
2764 tmp_length = exponent + precision;
2765 }
2766 }
2767# endif
2768 /* Account for sign, decimal point etc. */
2769 tmp_length = xsum (tmp_length, 12);
2770
2771 if (tmp_length < width)
2772 tmp_length = width;
2773
2774 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2775
2776 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2777 tmp = tmpbuf;
2778 else
2779 {
2780 size_t tmp_memsize =
2781 xtimes (tmp_length, sizeof (DCHAR_T));
2782
2783 if (size_overflow_p (tmp_memsize))
2784 /* Overflow, would lead to out of memory. */
2785 goto out_of_memory;
2786 tmp = (DCHAR_T *) malloc (tmp_memsize);
2787 if (tmp == NULL)
2788 /* Out of memory. */
2789 goto out_of_memory;
2790 }
2791
2792 pad_ptr = NULL;
2793 p = tmp;
2794
2795# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2796# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2797 if (type == TYPE_LONGDOUBLE)
2798# endif
2799 {
2800 long double arg = a.arg[dp->arg_index].a.a_longdouble;
2801
2802 if (isnanl (arg))
2803 {
2804 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2805 {
2806 *p++ = 'N';
2807 *p++ = 'A';
2808 *p++ = 'N';
2809 }
2810 else
2811 {
2812 *p++ = 'n';
2813 *p++ = 'a';
2814 *p++ = 'n';
2815 }
2816 }
2817 else
2818 {
2819 int sign = 0;
2820 DECL_LONG_DOUBLE_ROUNDING
2821 BEGIN_LONG_DOUBLE_ROUNDING ();
2822
2823 if (signbit (arg)) /* arg < 0.0L or negative zero */
2824 {
2825 sign = -1;
2826 arg = -arg;
2827 }
2828
2829 if (sign < 0)
2830 *p++ = '-';
2831 else if (flags & FLAG_SHOWSIGN)
2832 *p++ = '+';
2833 else if (flags & FLAG_SPACE)
2834 *p++ = ' ';
2835
2836 if (arg > 0.0L && arg + arg == arg)
2837 {
2838 if (dp->conversion >= 'A'
2839 && dp->conversion <= 'Z')
2840 {
2841 *p++ = 'I';
2842 *p++ = 'N';
2843 *p++ = 'F';
2844 }
2845 else
2846 {
2847 *p++ = 'i';
2848 *p++ = 'n';
2849 *p++ = 'f';
2850 }
2851 }
2852 else
2853 {
2854# if NEED_PRINTF_LONG_DOUBLE
2855 pad_ptr = p;
2856
2857 if (dp->conversion == 'f'
2858 || dp->conversion == 'F')
2859 {
2860 char *digits;
2861 size_t ndigits;
2862
2863 digits =
2864 scale10_round_decimal_long_double (arg,
2865 precision);
2866 if (digits == NULL)
2867 {
2868 END_LONG_DOUBLE_ROUNDING ();
2869 goto out_of_memory;
2870 }
2871 ndigits = strlen (digits);
2872
2873 if (ndigits > precision)
2874 do
2875 {
2876 --ndigits;
2877 *p++ = digits[ndigits];
2878 }
2879 while (ndigits > precision);
2880 else
2881 *p++ = '0';
2882 /* Here ndigits <= precision. */
2883 if ((flags & FLAG_ALT) || precision > 0)
2884 {
2885 *p++ = decimal_point_char ();
2886 for (; precision > ndigits; precision--)
2887 *p++ = '0';
2888 while (ndigits > 0)
2889 {
2890 --ndigits;
2891 *p++ = digits[ndigits];
2892 }
2893 }
2894
2895 free (digits);
2896 }
2897 else if (dp->conversion == 'e'
2898 || dp->conversion == 'E')
2899 {
2900 int exponent;
2901
2902 if (arg == 0.0L)
2903 {
2904 exponent = 0;
2905 *p++ = '0';
2906 if ((flags & FLAG_ALT) || precision > 0)
2907 {
2908 *p++ = decimal_point_char ();
2909 for (; precision > 0; precision--)
2910 *p++ = '0';
2911 }
2912 }
2913 else
2914 {
2915 /* arg > 0.0L. */
2916 int adjusted;
2917 char *digits;
2918 size_t ndigits;
2919
2920 exponent = floorlog10l (arg);
2921 adjusted = 0;
2922 for (;;)
2923 {
2924 digits =
2925 scale10_round_decimal_long_double
2926 (arg, (int) precision - exponent);
2927 if (digits == NULL)
2928 {
2929 END_LONG_DOUBLE_ROUNDING ();
2930 goto out_of_memory;
2931 }
2932 ndigits = strlen (digits);
2933
2934 if (ndigits == precision + 1)
2935 break;
2936 if (ndigits < precision
2937 || ndigits > precision + 2)
2938 /* The exponent was not guessed
2939 precisely enough. */
2940 abort ();
2941 if (adjusted)
2942 /* None of two values of exponent is
2943 the right one. Prevent an endless
2944 loop. */
2945 abort ();
2946 free (digits);
2947 if (ndigits == precision)
2948 exponent -= 1;
2949 else
2950 exponent += 1;
2951 adjusted = 1;
2952 }
2953
2954 /* Here ndigits = precision+1. */
2955 *p++ = digits[--ndigits];
2956 if ((flags & FLAG_ALT) || precision > 0)
2957 {
2958 *p++ = decimal_point_char ();
2959 while (ndigits > 0)
2960 {
2961 --ndigits;
2962 *p++ = digits[ndigits];
2963 }
2964 }
2965
2966 free (digits);
2967 }
2968
2969 *p++ = dp->conversion; /* 'e' or 'E' */
2970# if WIDE_CHAR_VERSION
2971 {
2972 static const wchar_t decimal_format[] =
2973 { '%', '+', '.', '2', 'd', '\0' };
2974 SNPRINTF (p, 6 + 1, decimal_format,
2975 exponent);
2976 }
2977 while (*p != '\0')
2978 p++;
2979# else
2980 if (sizeof (DCHAR_T) == 1)
2981 {
2982 sprintf ((char *) p, "%+.2d", exponent);
2983 while (*p != '\0')
2984 p++;
2985 }
2986 else
2987 {
2988 char expbuf[6 + 1];
2989 const char *ep;
2990 sprintf (expbuf, "%+.2d", exponent);
2991 for (ep = expbuf; (*p = *ep) != '\0';
2992 ep++)
2993 p++;
2994 }
2995# endif
2996 }
2997 else if (dp->conversion == 'g'
2998 || dp->conversion == 'G')
2999 {
3000 if (precision == 0)
3001 precision = 1;
3002 /* precision >= 1. */
3003
3004 if (arg == 0.0L)
3005 /* The exponent is 0, >= -4, < precision.
3006 Use fixed-point notation. */
3007 {
3008 size_t ndigits = precision;
3009 /* Number of trailing zeroes that have to be
3010 dropped. */
3011 size_t nzeroes =
3012 (flags & FLAG_ALT ? 0 : precision - 1);
3013
3014 --ndigits;
3015 *p++ = '0';
3016 if ((flags & FLAG_ALT)
3017 || ndigits > nzeroes)
3018 {
3019 *p++ = decimal_point_char ();
3020 while (ndigits > nzeroes)
3021 {
3022 --ndigits;
3023 *p++ = '0';
3024 }
3025 }
3026 }
3027 else
3028 {
3029 /* arg > 0.0L. */
3030 int exponent;
3031 int adjusted;
3032 char *digits;
3033 size_t ndigits;
3034 size_t nzeroes;
3035
3036 exponent = floorlog10l (arg);
3037 adjusted = 0;
3038 for (;;)
3039 {
3040 digits =
3041 scale10_round_decimal_long_double
3042 (arg,
3043 (int) (precision - 1) - exponent);
3044 if (digits == NULL)
3045 {
3046 END_LONG_DOUBLE_ROUNDING ();
3047 goto out_of_memory;
3048 }
3049 ndigits = strlen (digits);
3050
3051 if (ndigits == precision)
3052 break;
3053 if (ndigits < precision - 1
3054 || ndigits > precision + 1)
3055 /* The exponent was not guessed
3056 precisely enough. */
3057 abort ();
3058 if (adjusted)
3059 /* None of two values of exponent is
3060 the right one. Prevent an endless
3061 loop. */
3062 abort ();
3063 free (digits);
3064 if (ndigits < precision)
3065 exponent -= 1;
3066 else
3067 exponent += 1;
3068 adjusted = 1;
3069 }
3070 /* Here ndigits = precision. */
3071
3072 /* Determine the number of trailing zeroes
3073 that have to be dropped. */
3074 nzeroes = 0;
3075 if ((flags & FLAG_ALT) == 0)
3076 while (nzeroes < ndigits
3077 && digits[nzeroes] == '0')
3078 nzeroes++;
3079
3080 /* The exponent is now determined. */
3081 if (exponent >= -4
3082 && exponent < (long) precision)
3083 {
3084 /* Fixed-point notation:
3085 max(exponent,0)+1 digits, then the
3086 decimal point, then the remaining
3087 digits without trailing zeroes. */
3088 if (exponent >= 0)
3089 {
3090 size_t count = exponent + 1;
3091 /* Note: count <= precision = ndigits. */
3092 for (; count > 0; count--)
3093 *p++ = digits[--ndigits];
3094 if ((flags & FLAG_ALT)
3095 || ndigits > nzeroes)
3096 {
3097 *p++ = decimal_point_char ();
3098 while (ndigits > nzeroes)
3099 {
3100 --ndigits;
3101 *p++ = digits[ndigits];
3102 }
3103 }
3104 }
3105 else
3106 {
3107 size_t count = -exponent - 1;
3108 *p++ = '0';
3109 *p++ = decimal_point_char ();
3110 for (; count > 0; count--)
3111 *p++ = '0';
3112 while (ndigits > nzeroes)
3113 {
3114 --ndigits;
3115 *p++ = digits[ndigits];
3116 }
3117 }
3118 }
3119 else
3120 {
3121 /* Exponential notation. */
3122 *p++ = digits[--ndigits];
3123 if ((flags & FLAG_ALT)
3124 || ndigits > nzeroes)
3125 {
3126 *p++ = decimal_point_char ();
3127 while (ndigits > nzeroes)
3128 {
3129 --ndigits;
3130 *p++ = digits[ndigits];
3131 }
3132 }
3133 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3134# if WIDE_CHAR_VERSION
3135 {
3136 static const wchar_t
3137 decimal_format[] =
3138 { '%', '+', '.', '2', 'd', '\0' };
3139 SNPRINTF (p, 6 + 1, decimal_format,
3140 exponent);
3141 }
3142 while (*p != '\0')
3143 p++;
3144# else
3145 if (sizeof (DCHAR_T) == 1)
3146 {
3147 sprintf ((char *) p, "%+.2d",
3148 exponent);
3149 while (*p != '\0')
3150 p++;
3151 }
3152 else
3153 {
3154 char expbuf[6 + 1];
3155 const char *ep;
3156 sprintf (expbuf, "%+.2d",
3157 exponent);
3158 for (ep = expbuf;
3159 (*p = *ep) != '\0'; ep++)
3160 p++;
3161 }
3162# endif
3163 }
3164
3165 free (digits);
3166 }
3167 }
3168 else
3169 abort ();
3170# else
3171 /* arg is finite. */
3172 abort ();
3173# endif
3174 }
3175
3176 END_LONG_DOUBLE_ROUNDING ();
3177 }
3178 }
3179# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3180 else
3181# endif
3182# endif
3183# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3184 {
3185 double arg = a.arg[dp->arg_index].a.a_double;
3186
3187 if (isnan (arg))
3188 {
3189 if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3190 {
3191 *p++ = 'N';
3192 *p++ = 'A';
3193 *p++ = 'N';
3194 }
3195 else
3196 {
3197 *p++ = 'n';
3198 *p++ = 'a';
3199 *p++ = 'n';
3200 }
3201 }
3202 else
3203 {
3204 int sign = 0;
3205
3206 if (signbit (arg)) /* arg < 0.0 or negative zero */
3207 {
3208 sign = -1;
3209 arg = -arg;
3210 }
3211
3212 if (sign < 0)
3213 *p++ = '-';
3214 else if (flags & FLAG_SHOWSIGN)
3215 *p++ = '+';
3216 else if (flags & FLAG_SPACE)
3217 *p++ = ' ';
3218
3219 if (arg > 0.0 && arg + arg == arg)
3220 {
3221 if (dp->conversion >= 'A'
3222 && dp->conversion <= 'Z')
3223 {
3224 *p++ = 'I';
3225 *p++ = 'N';
3226 *p++ = 'F';
3227 }
3228 else
3229 {
3230 *p++ = 'i';
3231 *p++ = 'n';
3232 *p++ = 'f';
3233 }
3234 }
3235 else
3236 {
3237# if NEED_PRINTF_DOUBLE
3238 pad_ptr = p;
3239
3240 if (dp->conversion == 'f'
3241 || dp->conversion == 'F')
3242 {
3243 char *digits;
3244 size_t ndigits;
3245
3246 digits =
3247 scale10_round_decimal_double (arg,
3248 precision);
3249 if (digits == NULL)
3250 goto out_of_memory;
3251 ndigits = strlen (digits);
3252
3253 if (ndigits > precision)
3254 do
3255 {
3256 --ndigits;
3257 *p++ = digits[ndigits];
3258 }
3259 while (ndigits > precision);
3260 else
3261 *p++ = '0';
3262 /* Here ndigits <= precision. */
3263 if ((flags & FLAG_ALT) || precision > 0)
3264 {
3265 *p++ = decimal_point_char ();
3266 for (; precision > ndigits; precision--)
3267 *p++ = '0';
3268 while (ndigits > 0)
3269 {
3270 --ndigits;
3271 *p++ = digits[ndigits];
3272 }
3273 }
3274
3275 free (digits);
3276 }
3277 else if (dp->conversion == 'e'
3278 || dp->conversion == 'E')
3279 {
3280 int exponent;
3281
3282 if (arg == 0.0)
3283 {
3284 exponent = 0;
3285 *p++ = '0';
3286 if ((flags & FLAG_ALT) || precision > 0)
3287 {
3288 *p++ = decimal_point_char ();
3289 for (; precision > 0; precision--)
3290 *p++ = '0';
3291 }
3292 }
3293 else
3294 {
3295 /* arg > 0.0. */
3296 int adjusted;
3297 char *digits;
3298 size_t ndigits;
3299
3300 exponent = floorlog10 (arg);
3301 adjusted = 0;
3302 for (;;)
3303 {
3304 digits =
3305 scale10_round_decimal_double (arg,
3306 (int)
3307 precision
3308 -
3309 exponent);
3310 if (digits == NULL)
3311 goto out_of_memory;
3312 ndigits = strlen (digits);
3313
3314 if (ndigits == precision + 1)
3315 break;
3316 if (ndigits < precision
3317 || ndigits > precision + 2)
3318 /* The exponent was not guessed
3319 precisely enough. */
3320 abort ();
3321 if (adjusted)
3322 /* None of two values of exponent is
3323 the right one. Prevent an endless
3324 loop. */
3325 abort ();
3326 free (digits);
3327 if (ndigits == precision)
3328 exponent -= 1;
3329 else
3330 exponent += 1;
3331 adjusted = 1;
3332 }
3333
3334 /* Here ndigits = precision+1. */
3335 *p++ = digits[--ndigits];
3336 if ((flags & FLAG_ALT) || precision > 0)
3337 {
3338 *p++ = decimal_point_char ();
3339 while (ndigits > 0)
3340 {
3341 --ndigits;
3342 *p++ = digits[ndigits];
3343 }
3344 }
3345
3346 free (digits);
3347 }
3348
3349 *p++ = dp->conversion; /* 'e' or 'E' */
3350# if WIDE_CHAR_VERSION
3351 {
3352 static const wchar_t decimal_format[] =
3353 /* Produce the same number of exponent digits
3354 as the native printf implementation. */
3355# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3356 { '%', '+', '.', '3', 'd', '\0' };
3357# else
3358 { '%', '+', '.', '2', 'd', '\0' };
3359# endif
3360 SNPRINTF (p, 6 + 1, decimal_format,
3361 exponent);
3362 }
3363 while (*p != '\0')
3364 p++;
3365# else
3366 {
3367 static const char decimal_format[] =
3368 /* Produce the same number of exponent digits
3369 as the native printf implementation. */
3370# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3371 "%+.3d";
3372# else
3373 "%+.2d";
3374# endif
3375 if (sizeof (DCHAR_T) == 1)
3376 {
3377 sprintf ((char *) p, decimal_format,
3378 exponent);
3379 while (*p != '\0')
3380 p++;
3381 }
3382 else
3383 {
3384 char expbuf[6 + 1];
3385 const char *ep;
3386 sprintf (expbuf, decimal_format,
3387 exponent);
3388 for (ep = expbuf; (*p = *ep) != '\0';
3389 ep++)
3390 p++;
3391 }
3392 }
3393# endif
3394 }
3395 else if (dp->conversion == 'g'
3396 || dp->conversion == 'G')
3397 {
3398 if (precision == 0)
3399 precision = 1;
3400 /* precision >= 1. */
3401
3402 if (arg == 0.0)
3403 /* The exponent is 0, >= -4, < precision.
3404 Use fixed-point notation. */
3405 {
3406 size_t ndigits = precision;
3407 /* Number of trailing zeroes that have to be
3408 dropped. */
3409 size_t nzeroes =
3410 (flags & FLAG_ALT ? 0 : precision - 1);
3411
3412 --ndigits;
3413 *p++ = '0';
3414 if ((flags & FLAG_ALT)
3415 || ndigits > nzeroes)
3416 {
3417 *p++ = decimal_point_char ();
3418 while (ndigits > nzeroes)
3419 {
3420 --ndigits;
3421 *p++ = '0';
3422 }
3423 }
3424 }
3425 else
3426 {
3427 /* arg > 0.0. */
3428 int exponent;
3429 int adjusted;
3430 char *digits;
3431 size_t ndigits;
3432 size_t nzeroes;
3433
3434 exponent = floorlog10 (arg);
3435 adjusted = 0;
3436 for (;;)
3437 {
3438 digits =
3439 scale10_round_decimal_double (arg,
3440 (int)
3441 (precision
3442 -
3443 1) -
3444 exponent);
3445 if (digits == NULL)
3446 goto out_of_memory;
3447 ndigits = strlen (digits);
3448
3449 if (ndigits == precision)
3450 break;
3451 if (ndigits < precision - 1
3452 || ndigits > precision + 1)
3453 /* The exponent was not guessed
3454 precisely enough. */
3455 abort ();
3456 if (adjusted)
3457 /* None of two values of exponent is
3458 the right one. Prevent an endless
3459 loop. */
3460 abort ();
3461 free (digits);
3462 if (ndigits < precision)
3463 exponent -= 1;
3464 else
3465 exponent += 1;
3466 adjusted = 1;
3467 }
3468 /* Here ndigits = precision. */
3469
3470 /* Determine the number of trailing zeroes
3471 that have to be dropped. */
3472 nzeroes = 0;
3473 if ((flags & FLAG_ALT) == 0)
3474 while (nzeroes < ndigits
3475 && digits[nzeroes] == '0')
3476 nzeroes++;
3477
3478 /* The exponent is now determined. */
3479 if (exponent >= -4
3480 && exponent < (long) precision)
3481 {
3482 /* Fixed-point notation:
3483 max(exponent,0)+1 digits, then the
3484 decimal point, then the remaining
3485 digits without trailing zeroes. */
3486 if (exponent >= 0)
3487 {
3488 size_t count = exponent + 1;
3489 /* Note: count <= precision = ndigits. */
3490 for (; count > 0; count--)
3491 *p++ = digits[--ndigits];
3492 if ((flags & FLAG_ALT)
3493 || ndigits > nzeroes)
3494 {
3495 *p++ = decimal_point_char ();
3496 while (ndigits > nzeroes)
3497 {
3498 --ndigits;
3499 *p++ = digits[ndigits];
3500 }
3501 }
3502 }
3503 else
3504 {
3505 size_t count = -exponent - 1;
3506 *p++ = '0';
3507 *p++ = decimal_point_char ();
3508 for (; count > 0; count--)
3509 *p++ = '0';
3510 while (ndigits > nzeroes)
3511 {
3512 --ndigits;
3513 *p++ = digits[ndigits];
3514 }
3515 }
3516 }
3517 else
3518 {
3519 /* Exponential notation. */
3520 *p++ = digits[--ndigits];
3521 if ((flags & FLAG_ALT)
3522 || ndigits > nzeroes)
3523 {
3524 *p++ = decimal_point_char ();
3525 while (ndigits > nzeroes)
3526 {
3527 --ndigits;
3528 *p++ = digits[ndigits];
3529 }
3530 }
3531 *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3532# if WIDE_CHAR_VERSION
3533 {
3534 static const wchar_t
3535 decimal_format[] =
3536 /* Produce the same number of exponent digits
3537 as the native printf implementation. */
3538# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3539 { '%', '+', '.', '3', 'd', '\0' };
3540# else
3541 { '%', '+', '.', '2', 'd', '\0' };
3542# endif
3543 SNPRINTF (p, 6 + 1, decimal_format,
3544 exponent);
3545 }
3546 while (*p != '\0')
3547 p++;
3548# else
3549 {
3550 static const char decimal_format[] =
3551 /* Produce the same number of exponent digits
3552 as the native printf implementation. */
3553# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3554 "%+.3d";
3555# else
3556 "%+.2d";
3557# endif
3558 if (sizeof (DCHAR_T) == 1)
3559 {
3560 sprintf ((char *) p,
3561 decimal_format,
3562 exponent);
3563 while (*p != '\0')
3564 p++;
3565 }
3566 else
3567 {
3568 char expbuf[6 + 1];
3569 const char *ep;
3570 sprintf (expbuf, decimal_format,
3571 exponent);
3572 for (ep = expbuf;
3573 (*p = *ep) != '\0'; ep++)
3574 p++;
3575 }
3576 }
3577# endif
3578 }
3579
3580 free (digits);
3581 }
3582 }
3583 else
3584 abort ();
3585# else
3586 /* arg is finite. */
3587 if (!(arg == 0.0))
3588 abort ();
3589
3590 pad_ptr = p;
3591
3592 if (dp->conversion == 'f'
3593 || dp->conversion == 'F')
3594 {
3595 *p++ = '0';
3596 if ((flags & FLAG_ALT) || precision > 0)
3597 {
3598 *p++ = decimal_point_char ();
3599 for (; precision > 0; precision--)
3600 *p++ = '0';
3601 }
3602 }
3603 else if (dp->conversion == 'e'
3604 || dp->conversion == 'E')
3605 {
3606 *p++ = '0';
3607 if ((flags & FLAG_ALT) || precision > 0)
3608 {
3609 *p++ = decimal_point_char ();
3610 for (; precision > 0; precision--)
3611 *p++ = '0';
3612 }
3613 *p++ = dp->conversion; /* 'e' or 'E' */
3614 *p++ = '+';
3615 /* Produce the same number of exponent digits as
3616 the native printf implementation. */
3617# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3618 *p++ = '0';
3619# endif
3620 *p++ = '0';
3621 *p++ = '0';
3622 }
3623 else if (dp->conversion == 'g'
3624 || dp->conversion == 'G')
3625 {
3626 *p++ = '0';
3627 if (flags & FLAG_ALT)
3628 {
3629 size_t ndigits =
3630 (precision > 0 ? precision - 1 : 0);
3631 *p++ = decimal_point_char ();
3632 for (; ndigits > 0; --ndigits)
3633 *p++ = '0';
3634 }
3635 }
3636 else
3637 abort ();
3638# endif
3639 }
3640 }
3641 }
3642# endif
3643
3644 /* The generated string now extends from tmp to p, with the
3645 zero padding insertion point being at pad_ptr. */
3646 if (has_width && p - tmp < width)
3647 {
3648 size_t pad = width - (p - tmp);
3649 DCHAR_T *end = p + pad;
3650
3651 if (flags & FLAG_LEFT)
3652 {
3653 /* Pad with spaces on the right. */
3654 for (; pad > 0; pad--)
3655 *p++ = ' ';
3656 }
3657 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3658 {
3659 /* Pad with zeroes. */
3660 DCHAR_T *q = end;
3661
3662 while (p > pad_ptr)
3663 *--q = *--p;
3664 for (; pad > 0; pad--)
3665 *p++ = '0';
3666 }
3667 else
3668 {
3669 /* Pad with spaces on the left. */
3670 DCHAR_T *q = end;
3671
3672 while (p > tmp)
3673 *--q = *--p;
3674 for (; pad > 0; pad--)
3675 *p++ = ' ';
3676 }
3677
3678 p = end;
3679 }
3680
3681 {
3682 size_t count = p - tmp;
3683
3684 if (count >= tmp_length)
3685 /* tmp_length was incorrectly calculated - fix the
3686 code above! */
3687 abort ();
3688
3689 /* Make room for the result. */
3690 if (count >= allocated - length)
3691 {
3692 size_t n = xsum (length, count);
3693
3694 ENSURE_ALLOCATION (n);
3695 }
3696
3697 /* Append the result. */
3698 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
3699 if (tmp != tmpbuf)
3700 free (tmp);
3701 length += count;
3702 }
3703 }
3704#endif
3705 else
3706 {
3707 arg_type type = a.arg[dp->arg_index].type;
3708 int flags = dp->flags;
3709#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3710 int has_width;
3711 size_t width;
3712#endif
3713#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3714 int has_precision;
3715 size_t precision;
3716#endif
3717#if NEED_PRINTF_UNBOUNDED_PRECISION
3718 int prec_ourselves;
3719#else
3720# define prec_ourselves 0
3721#endif
3722#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3723 int pad_ourselves;
3724#else
3725# define pad_ourselves 0
3726#endif
3727 TCHAR_T *fbp;
3728 unsigned int prefix_count;
3729 int prefixes[2];
3730#if !USE_SNPRINTF
3731 size_t tmp_length;
3732 TCHAR_T tmpbuf[700];
3733 TCHAR_T *tmp;
3734#endif
3735
3736#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3737 has_width = 0;
3738 width = 0;
3739 if (dp->width_start != dp->width_end)
3740 {
3741 if (dp->width_arg_index != ARG_NONE)
3742 {
3743 int arg;
3744
3745 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3746 abort ();
3747 arg = a.arg[dp->width_arg_index].a.a_int;
3748 if (arg < 0)
3749 {
3750 /* "A negative field width is taken as a '-' flag
3751 followed by a positive field width." */
3752 flags |= FLAG_LEFT;
3753 width = (unsigned int) (-arg);
3754 }
3755 else
3756 width = arg;
3757 }
3758 else
3759 {
3760 const FCHAR_T *digitp = dp->width_start;
3761
3762 do
3763 width = xsum (xtimes (width, 10), *digitp++ - '0');
3764 while (digitp != dp->width_end);
3765 }
3766 has_width = 1;
3767 }
3768#endif
3769
3770#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3771 has_precision = 0;
3772 precision = 6;
3773 if (dp->precision_start != dp->precision_end)
3774 {
3775 if (dp->precision_arg_index != ARG_NONE)
3776 {
3777 int arg;
3778
3779 if (!
3780 (a.arg[dp->precision_arg_index].type == TYPE_INT))
3781 abort ();
3782 arg = a.arg[dp->precision_arg_index].a.a_int;
3783 /* "A negative precision is taken as if the precision
3784 were omitted." */
3785 if (arg >= 0)
3786 {
3787 precision = arg;
3788 has_precision = 1;
3789 }
3790 }
3791 else
3792 {
3793 const FCHAR_T *digitp = dp->precision_start + 1;
3794
3795 precision = 0;
3796 while (digitp != dp->precision_end)
3797 precision =
3798 xsum (xtimes (precision, 10), *digitp++ - '0');
3799 has_precision = 1;
3800 }
3801 }
3802#endif
3803
3804#if !USE_SNPRINTF
3805 /* Allocate a temporary buffer of sufficient size for calling
3806 sprintf. */
3807 {
3808 switch (dp->conversion)
3809 {
3810
3811 case 'd':
3812 case 'i':
3813 case 'u':
3814# if HAVE_LONG_LONG_INT
3815 if (type == TYPE_LONGLONGINT
3816 || type == TYPE_ULONGLONGINT)
3817 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.30103 /* binary -> decimal */
3818 ) + 1; /* turn floor into ceil */
3819 else
3820# endif
3821 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3822 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.30103 /* binary -> decimal */
3823 ) + 1; /* turn floor into ceil */
3824 else
3825 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.30103 /* binary -> decimal */
3826 ) + 1; /* turn floor into ceil */
3827 if (tmp_length < precision)
3828 tmp_length = precision;
3829 /* Multiply by 2, as an estimate for FLAG_GROUP. */
3830 tmp_length = xsum (tmp_length, tmp_length);
3831 /* Add 1, to account for a leading sign. */
3832 tmp_length = xsum (tmp_length, 1);
3833 break;
3834
3835 case 'o':
3836# if HAVE_LONG_LONG_INT
3837 if (type == TYPE_LONGLONGINT
3838 || type == TYPE_ULONGLONGINT)
3839 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.333334 /* binary -> octal */
3840 ) + 1; /* turn floor into ceil */
3841 else
3842# endif
3843 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3844 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.333334 /* binary -> octal */
3845 ) + 1; /* turn floor into ceil */
3846 else
3847 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.333334 /* binary -> octal */
3848 ) + 1; /* turn floor into ceil */
3849 if (tmp_length < precision)
3850 tmp_length = precision;
3851 /* Add 1, to account for a leading sign. */
3852 tmp_length = xsum (tmp_length, 1);
3853 break;
3854
3855 case 'x':
3856 case 'X':
3857# if HAVE_LONG_LONG_INT
3858 if (type == TYPE_LONGLONGINT
3859 || type == TYPE_ULONGLONGINT)
3860 tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
3861 ) + 1; /* turn floor into ceil */
3862 else
3863# endif
3864 if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
3865 tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
3866 ) + 1; /* turn floor into ceil */
3867 else
3868 tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
3869 ) + 1; /* turn floor into ceil */
3870 if (tmp_length < precision)
3871 tmp_length = precision;
3872 /* Add 2, to account for a leading sign or alternate form. */
3873 tmp_length = xsum (tmp_length, 2);
3874 break;
3875
3876 case 'f':
3877 case 'F':
3878 if (type == TYPE_LONGDOUBLE)
3879 tmp_length = (unsigned int) (LDBL_MAX_EXP * 0.30103 /* binary -> decimal */
3880 * 2 /* estimate for FLAG_GROUP */
3881 ) + 1 /* turn floor into ceil */
3882 + 10; /* sign, decimal point etc. */
3883 else
3884 tmp_length = (unsigned int) (DBL_MAX_EXP * 0.30103 /* binary -> decimal */
3885 * 2 /* estimate for FLAG_GROUP */
3886 ) + 1 /* turn floor into ceil */
3887 + 10; /* sign, decimal point etc. */
3888 tmp_length = xsum (tmp_length, precision);
3889 break;
3890
3891 case 'e':
3892 case 'E':
3893 case 'g':
3894 case 'G':
3895 tmp_length = 12; /* sign, decimal point, exponent etc. */
3896 tmp_length = xsum (tmp_length, precision);
3897 break;
3898
3899 case 'a':
3900 case 'A':
3901 if (type == TYPE_LONGDOUBLE)
3902 tmp_length = (unsigned int) (LDBL_DIG * 0.831 /* decimal -> hexadecimal */
3903 ) + 1; /* turn floor into ceil */
3904 else
3905 tmp_length = (unsigned int) (DBL_DIG * 0.831 /* decimal -> hexadecimal */
3906 ) + 1; /* turn floor into ceil */
3907 if (tmp_length < precision)
3908 tmp_length = precision;
3909 /* Account for sign, decimal point etc. */
3910 tmp_length = xsum (tmp_length, 12);
3911 break;
3912
3913 case 'c':
3914# if HAVE_WINT_T && !WIDE_CHAR_VERSION
3915 if (type == TYPE_WIDE_CHAR)
3916 tmp_length = MB_CUR_MAX;
3917 else
3918# endif
3919 tmp_length = 1;
3920 break;
3921
3922 case 's':
3923# if HAVE_WCHAR_T
3924 if (type == TYPE_WIDE_STRING)
3925 {
3926 tmp_length =
3927 local_wcslen (a.arg[dp->arg_index].a.
3928 a_wide_string);
3929
3930# if !WIDE_CHAR_VERSION
3931 tmp_length = xtimes (tmp_length, MB_CUR_MAX);
3932# endif
3933 }
3934 else
3935# endif
3936 tmp_length = strlen (a.arg[dp->arg_index].a.a_string);
3937 break;
3938
3939 case 'p':
3940 tmp_length = (unsigned int) (sizeof (void *) * CHAR_BIT * 0.25 /* binary -> hexadecimal */
3941 ) + 1 /* turn floor into ceil */
3942 + 2; /* account for leading 0x */
3943 break;
3944
3945 default:
3946 abort ();
3947 }
3948
3949# if ENABLE_UNISTDIO
3950 /* Padding considers the number of characters, therefore the
3951 number of elements after padding may be
3952 > max (tmp_length, width)
3953 but is certainly
3954 <= tmp_length + width. */
3955 tmp_length = xsum (tmp_length, width);
3956# else
3957 /* Padding considers the number of elements, says POSIX. */
3958 if (tmp_length < width)
3959 tmp_length = width;
3960# endif
3961
3962 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
3963 }
3964
3965 if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
3966 tmp = tmpbuf;
3967 else
3968 {
3969 size_t tmp_memsize =
3970 xtimes (tmp_length, sizeof (TCHAR_T));
3971
3972 if (size_overflow_p (tmp_memsize))
3973 /* Overflow, would lead to out of memory. */
3974 goto out_of_memory;
3975 tmp = (TCHAR_T *) malloc (tmp_memsize);
3976 if (tmp == NULL)
3977 /* Out of memory. */
3978 goto out_of_memory;
3979 }
3980#endif
3981
3982 /* Decide whether to handle the precision ourselves. */
3983#if NEED_PRINTF_UNBOUNDED_PRECISION
3984 switch (dp->conversion)
3985 {
3986 case 'd':
3987 case 'i':
3988 case 'u':
3989 case 'o':
3990 case 'x':
3991 case 'X':
3992 case 'p':
3993 prec_ourselves = has_precision && (precision > 0);
3994 break;
3995 default:
3996 prec_ourselves = 0;
3997 break;
3998 }
3999#endif
4000
4001 /* Decide whether to perform the padding ourselves. */
4002#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4003 switch (dp->conversion)
4004 {
4005# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
4006 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
4007 to perform the padding after this conversion. Functions
4008 with unistdio extensions perform the padding based on
4009 character count rather than element count. */
4010 case 'c':
4011 case 's':
4012# endif
4013# if NEED_PRINTF_FLAG_ZERO
4014 case 'f':
4015 case 'F':
4016 case 'e':
4017 case 'E':
4018 case 'g':
4019 case 'G':
4020 case 'a':
4021 case 'A':
4022# endif
4023 pad_ourselves = 1;
4024 break;
4025 default:
4026 pad_ourselves = prec_ourselves;
4027 break;
4028 }
4029#endif
4030
4031 /* Construct the format string for calling snprintf or
4032 sprintf. */
4033 fbp = buf;
4034 *fbp++ = '%';
4035#if NEED_PRINTF_FLAG_GROUPING
4036 /* The underlying implementation doesn't support the ' flag.
4037 Produce no grouping characters in this case; this is
4038 acceptable because the grouping is locale dependent. */
4039#else
4040 if (flags & FLAG_GROUP)
4041 *fbp++ = '\'';
4042#endif
4043 if (flags & FLAG_LEFT)
4044 *fbp++ = '-';
4045 if (flags & FLAG_SHOWSIGN)
4046 *fbp++ = '+';
4047 if (flags & FLAG_SPACE)
4048 *fbp++ = ' ';
4049 if (flags & FLAG_ALT)
4050 *fbp++ = '#';
4051 if (!pad_ourselves)
4052 {
4053 if (flags & FLAG_ZERO)
4054 *fbp++ = '0';
4055 if (dp->width_start != dp->width_end)
4056 {
4057 size_t n = dp->width_end - dp->width_start;
4058 /* The width specification is known to consist only
4059 of standard ASCII characters. */
4060 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4061 {
4062 memcpy (fbp, dp->width_start,
4063 n * sizeof (TCHAR_T));
4064 fbp += n;
4065 }
4066 else
4067 {
4068 const FCHAR_T *mp = dp->width_start;
4069 do
4070 *fbp++ = (unsigned char) *mp++;
4071 while (--n > 0);
4072 }
4073 }
4074 }
4075 if (!prec_ourselves)
4076 {
4077 if (dp->precision_start != dp->precision_end)
4078 {
4079 size_t n = dp->precision_end - dp->precision_start;
4080 /* The precision specification is known to consist only
4081 of standard ASCII characters. */
4082 if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
4083 {
4084 memcpy (fbp, dp->precision_start,
4085 n * sizeof (TCHAR_T));
4086 fbp += n;
4087 }
4088 else
4089 {
4090 const FCHAR_T *mp = dp->precision_start;
4091 do
4092 *fbp++ = (unsigned char) *mp++;
4093 while (--n > 0);
4094 }
4095 }
4096 }
4097
4098 switch (type)
4099 {
4100#if HAVE_LONG_LONG_INT
4101 case TYPE_LONGLONGINT:
4102 case TYPE_ULONGLONGINT:
4103# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
4104 *fbp++ = 'I';
4105 *fbp++ = '6';
4106 *fbp++ = '4';
4107 break;
4108# else
4109 *fbp++ = 'l';
4110 /*FALLTHROUGH*/
4111# endif
4112#endif
4113 case TYPE_LONGINT:
4114 case TYPE_ULONGINT:
4115#if HAVE_WINT_T
4116 case TYPE_WIDE_CHAR:
4117#endif
4118#if HAVE_WCHAR_T
4119 case TYPE_WIDE_STRING:
4120#endif
4121 *fbp++ = 'l';
4122 break;
4123 case TYPE_LONGDOUBLE:
4124 *fbp++ = 'L';
4125 break;
4126 default:
4127 break;
4128 }
4129#if NEED_PRINTF_DIRECTIVE_F
4130 if (dp->conversion == 'F')
4131 *fbp = 'f';
4132 else
4133#endif
4134 *fbp = dp->conversion;
4135#if USE_SNPRINTF
4136# if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
4137 fbp[1] = '%';
4138 fbp[2] = 'n';
4139 fbp[3] = '\0';
4140# else
4141 /* On glibc2 systems from glibc >= 2.3 - probably also older
4142 ones - we know that snprintf's returns value conforms to
4143 ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes.
4144 Therefore we can avoid using %n in this situation.
4145 On glibc2 systems from 2004-10-18 or newer, the use of %n
4146 in format strings in writable memory may crash the program
4147 (if compiled with _FORTIFY_SOURCE=2), so we should avoid it
4148 in this situation. */
4149 fbp[1] = '\0';
4150# endif
4151#else
4152 fbp[1] = '\0';
4153#endif
4154
4155 /* Construct the arguments for calling snprintf or sprintf. */
4156 prefix_count = 0;
4157 if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
4158 {
4159 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4160 abort ();
4161 prefixes[prefix_count++] =
4162 a.arg[dp->width_arg_index].a.a_int;
4163 }
4164 if (dp->precision_arg_index != ARG_NONE)
4165 {
4166 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4167 abort ();
4168 prefixes[prefix_count++] =
4169 a.arg[dp->precision_arg_index].a.a_int;
4170 }
4171
4172#if USE_SNPRINTF
4173 /* The SNPRINTF result is appended after result[0..length].
4174 The latter is an array of DCHAR_T; SNPRINTF appends an
4175 array of TCHAR_T to it. This is possible because
4176 sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
4177 alignof (TCHAR_T) <= alignof (DCHAR_T). */
4178# define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
4179 /* Ensure that maxlen below will be >= 2. Needed on BeOS,
4180 where an snprintf() with maxlen==1 acts like sprintf(). */
4181 ENSURE_ALLOCATION (xsum (length,
4182 (2 + TCHARS_PER_DCHAR - 1)
4183 / TCHARS_PER_DCHAR));
4184 /* Prepare checking whether snprintf returns the count
4185 via %n. */
4186 *(TCHAR_T *) (result + length) = '\0';
4187#endif
4188
4189 for (;;)
4190 {
4191 int count = -1;
4192
4193#if USE_SNPRINTF
4194 int retcount = 0;
4195 size_t maxlen = allocated - length;
4196 /* SNPRINTF can fail if its second argument is
4197 > INT_MAX. */
4198 if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
4199 maxlen = INT_MAX / TCHARS_PER_DCHAR;
4200 maxlen = maxlen * TCHARS_PER_DCHAR;
4201# define SNPRINTF_BUF(arg) \
4202 switch (prefix_count) \
4203 { \
4204 case 0: \
4205 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4206 maxlen, buf, \
4207 arg, &count); \
4208 break; \
4209 case 1: \
4210 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4211 maxlen, buf, \
4212 prefixes[0], arg, &count); \
4213 break; \
4214 case 2: \
4215 retcount = SNPRINTF ((TCHAR_T *) (result + length), \
4216 maxlen, buf, \
4217 prefixes[0], prefixes[1], arg, \
4218 &count); \
4219 break; \
4220 default: \
4221 abort (); \
4222 }
4223#else
4224# define SNPRINTF_BUF(arg) \
4225 switch (prefix_count) \
4226 { \
4227 case 0: \
4228 count = sprintf (tmp, buf, arg); \
4229 break; \
4230 case 1: \
4231 count = sprintf (tmp, buf, prefixes[0], arg); \
4232 break; \
4233 case 2: \
4234 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
4235 arg); \
4236 break; \
4237 default: \
4238 abort (); \
4239 }
4240#endif
4241
4242 switch (type)
4243 {
4244 case TYPE_SCHAR:
4245 {
4246 int arg = a.arg[dp->arg_index].a.a_schar;
4247 SNPRINTF_BUF (arg);
4248 }
4249 break;
4250 case TYPE_UCHAR:
4251 {
4252 unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
4253 SNPRINTF_BUF (arg);
4254 }
4255 break;
4256 case TYPE_SHORT:
4257 {
4258 int arg = a.arg[dp->arg_index].a.a_short;
4259 SNPRINTF_BUF (arg);
4260 }
4261 break;
4262 case TYPE_USHORT:
4263 {
4264 unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
4265 SNPRINTF_BUF (arg);
4266 }
4267 break;
4268 case TYPE_INT:
4269 {
4270 int arg = a.arg[dp->arg_index].a.a_int;
4271 SNPRINTF_BUF (arg);
4272 }
4273 break;
4274 case TYPE_UINT:
4275 {
4276 unsigned int arg = a.arg[dp->arg_index].a.a_uint;
4277 SNPRINTF_BUF (arg);
4278 }
4279 break;
4280 case TYPE_LONGINT:
4281 {
4282 long int arg = a.arg[dp->arg_index].a.a_longint;
4283 SNPRINTF_BUF (arg);
4284 }
4285 break;
4286 case TYPE_ULONGINT:
4287 {
4288 unsigned long int arg =
4289 a.arg[dp->arg_index].a.a_ulongint;
4290 SNPRINTF_BUF (arg);
4291 }
4292 break;
4293#if HAVE_LONG_LONG_INT
4294 case TYPE_LONGLONGINT:
4295 {
4296 long long int arg =
4297 a.arg[dp->arg_index].a.a_longlongint;
4298 SNPRINTF_BUF (arg);
4299 }
4300 break;
4301 case TYPE_ULONGLONGINT:
4302 {
4303 unsigned long long int arg =
4304 a.arg[dp->arg_index].a.a_ulonglongint;
4305 SNPRINTF_BUF (arg);
4306 }
4307 break;
4308#endif
4309 case TYPE_DOUBLE:
4310 {
4311 double arg = a.arg[dp->arg_index].a.a_double;
4312 SNPRINTF_BUF (arg);
4313 }
4314 break;
4315 case TYPE_LONGDOUBLE:
4316 {
4317 long double arg =
4318 a.arg[dp->arg_index].a.a_longdouble;
4319 SNPRINTF_BUF (arg);
4320 }
4321 break;
4322 case TYPE_CHAR:
4323 {
4324 int arg = a.arg[dp->arg_index].a.a_char;
4325 SNPRINTF_BUF (arg);
4326 }
4327 break;
4328#if HAVE_WINT_T
4329 case TYPE_WIDE_CHAR:
4330 {
4331 wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
4332 SNPRINTF_BUF (arg);
4333 }
4334 break;
4335#endif
4336 case TYPE_STRING:
4337 {
4338 const char *arg = a.arg[dp->arg_index].a.a_string;
4339 SNPRINTF_BUF (arg);
4340 }
4341 break;
4342#if HAVE_WCHAR_T
4343 case TYPE_WIDE_STRING:
4344 {
4345 const wchar_t *arg =
4346 a.arg[dp->arg_index].a.a_wide_string;
4347 SNPRINTF_BUF (arg);
4348 }
4349 break;
4350#endif
4351 case TYPE_POINTER:
4352 {
4353 void *arg = a.arg[dp->arg_index].a.a_pointer;
4354 SNPRINTF_BUF (arg);
4355 }
4356 break;
4357 default:
4358 abort ();
4359 }
4360
4361#if USE_SNPRINTF
4362 /* Portability: Not all implementations of snprintf()
4363 are ISO C 99 compliant. Determine the number of
4364 bytes that snprintf() has produced or would have
4365 produced. */
4366 if (count >= 0)
4367 {
4368 /* Verify that snprintf() has NUL-terminated its
4369 result. */
4370 if (count < maxlen
4371 && ((TCHAR_T *) (result + length))[count] != '\0')
4372 abort ();
4373 /* Portability hack. */
4374 if (retcount > count)
4375 count = retcount;
4376 }
4377 else
4378 {
4379 /* snprintf() doesn't understand the '%n'
4380 directive. */
4381 if (fbp[1] != '\0')
4382 {
4383 /* Don't use the '%n' directive; instead, look
4384 at the snprintf() return value. */
4385 fbp[1] = '\0';
4386 continue;
4387 }
4388 else
4389 {
4390 /* Look at the snprintf() return value. */
4391 if (retcount < 0)
4392 {
4393 /* HP-UX 10.20 snprintf() is doubly deficient:
4394 It doesn't understand the '%n' directive,
4395 *and* it returns -1 (rather than the length
4396 that would have been required) when the
4397 buffer is too small. */
4398 size_t bigger_need =
4399 xsum (xtimes (allocated, 2), 12);
4400 ENSURE_ALLOCATION (bigger_need);
4401 continue;
4402 }
4403 else
4404 count = retcount;
4405 }
4406 }
4407#endif
4408
4409 /* Attempt to handle failure. */
4410 if (count < 0)
4411 {
4412 if (!(result == resultbuf || result == NULL))
4413 free (result);
4414 if (buf_malloced != NULL)
4415 free (buf_malloced);
4416 CLEANUP ();
4417 errno = EINVAL;
4418 return NULL;
4419 }
4420
4421#if USE_SNPRINTF
4422 /* Handle overflow of the allocated buffer.
4423 If such an overflow occurs, a C99 compliant snprintf()
4424 returns a count >= maxlen. However, a non-compliant
4425 snprintf() function returns only count = maxlen - 1. To
4426 cover both cases, test whether count >= maxlen - 1. */
4427 if ((unsigned int) count + 1 >= maxlen)
4428 {
4429 /* If maxlen already has attained its allowed maximum,
4430 allocating more memory will not increase maxlen.
4431 Instead of looping, bail out. */
4432 if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
4433 goto overflow;
4434 else
4435 {
4436 /* Need at least (count + 1) * sizeof (TCHAR_T)
4437 bytes. (The +1 is for the trailing NUL.)
4438 But ask for (count + 2) * sizeof (TCHAR_T)
4439 bytes, so that in the next round, we likely get
4440 maxlen > (unsigned int) count + 1
4441 and so we don't get here again.
4442 And allocate proportionally, to avoid looping
4443 eternally if snprintf() reports a too small
4444 count. */
4445 size_t n = xmax (xsum (length,
4446 ((unsigned int) count + 2
4447 + TCHARS_PER_DCHAR - 1)
4448 / TCHARS_PER_DCHAR),
4449 xtimes (allocated, 2));
4450
4451 ENSURE_ALLOCATION (n);
4452 continue;
4453 }
4454 }
4455#endif
4456
4457#if NEED_PRINTF_UNBOUNDED_PRECISION
4458 if (prec_ourselves)
4459 {
4460 /* Handle the precision. */
4461 TCHAR_T *prec_ptr =
4462# if USE_SNPRINTF
4463 (TCHAR_T *) (result + length);
4464# else
4465 tmp;
4466# endif
4467 size_t prefix_count;
4468 size_t move;
4469
4470 prefix_count = 0;
4471 /* Put the additional zeroes after the sign. */
4472 if (count >= 1
4473 && (*prec_ptr == '-' || *prec_ptr == '+'
4474 || *prec_ptr == ' '))
4475 prefix_count = 1;
4476 /* Put the additional zeroes after the 0x prefix if
4477 (flags & FLAG_ALT) || (dp->conversion == 'p'). */
4478 else if (count >= 2
4479 && prec_ptr[0] == '0'
4480 && (prec_ptr[1] == 'x'
4481 || prec_ptr[1] == 'X'))
4482 prefix_count = 2;
4483
4484 move = count - prefix_count;
4485 if (precision > move)
4486 {
4487 /* Insert zeroes. */
4488 size_t insert = precision - move;
4489 TCHAR_T *prec_end;
4490
4491# if USE_SNPRINTF
4492 size_t n = xsum (length,
4493 (count + insert +
4494 TCHARS_PER_DCHAR -
4495 1) / TCHARS_PER_DCHAR);
4496 length +=
4497 (count + TCHARS_PER_DCHAR -
4498 1) / TCHARS_PER_DCHAR;
4499 ENSURE_ALLOCATION (n);
4500 length -=
4501 (count + TCHARS_PER_DCHAR -
4502 1) / TCHARS_PER_DCHAR;
4503 prec_ptr = (TCHAR_T *) (result + length);
4504# endif
4505
4506 prec_end = prec_ptr + count;
4507 prec_ptr += prefix_count;
4508
4509 while (prec_end > prec_ptr)
4510 {
4511 prec_end--;
4512 prec_end[insert] = prec_end[0];
4513 }
4514
4515 prec_end += insert;
4516 do
4517 *--prec_end = '0';
4518 while (prec_end > prec_ptr);
4519
4520 count += insert;
4521 }
4522 }
4523#endif
4524
4525#if !DCHAR_IS_TCHAR
4526# if !USE_SNPRINTF
4527 if (count >= tmp_length)
4528 /* tmp_length was incorrectly calculated - fix the
4529 code above! */
4530 abort ();
4531# endif
4532
4533 /* Convert from TCHAR_T[] to DCHAR_T[]. */
4534 if (dp->conversion == 'c' || dp->conversion == 's')
4535 {
4536 /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
4537 TYPE_WIDE_STRING.
4538 The result string is not certainly ASCII. */
4539 const TCHAR_T *tmpsrc;
4540 DCHAR_T *tmpdst;
4541 size_t tmpdst_len;
4542 /* This code assumes that TCHAR_T is 'char'. */
4543 typedef int TCHAR_T_verify
4544 [2 * (sizeof (TCHAR_T) == 1) - 1];
4545# if USE_SNPRINTF
4546 tmpsrc = (TCHAR_T *) (result + length);
4547# else
4548 tmpsrc = tmp;
4549# endif
4550 tmpdst = NULL;
4551 tmpdst_len = 0;
4552 if (DCHAR_CONV_FROM_ENCODING (locale_charset (),
4553 iconveh_question_mark,
4554 tmpsrc, count,
4555 NULL,
4556 &tmpdst, &tmpdst_len)
4557 < 0)
4558 {
4559 int saved_errno = errno;
4560 if (!(result == resultbuf || result == NULL))
4561 free (result);
4562 if (buf_malloced != NULL)
4563 free (buf_malloced);
4564 CLEANUP ();
4565 errno = saved_errno;
4566 return NULL;
4567 }
4568 ENSURE_ALLOCATION (xsum (length, tmpdst_len));
4569 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
4570 free (tmpdst);
4571 count = tmpdst_len;
4572 }
4573 else
4574 {
4575 /* The result string is ASCII.
4576 Simple 1:1 conversion. */
4577# if USE_SNPRINTF
4578 /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
4579 no-op conversion, in-place on the array starting
4580 at (result + length). */
4581 if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
4582# endif
4583 {
4584 const TCHAR_T *tmpsrc;
4585 DCHAR_T *tmpdst;
4586 size_t n;
4587
4588# if USE_SNPRINTF
4589 if (result == resultbuf)
4590 {
4591 tmpsrc = (TCHAR_T *) (result + length);
4592 /* ENSURE_ALLOCATION will not move tmpsrc
4593 (because it's part of resultbuf). */
4594 ENSURE_ALLOCATION (xsum (length, count));
4595 }
4596 else
4597 {
4598 /* ENSURE_ALLOCATION will move the array
4599 (because it uses realloc(). */
4600 ENSURE_ALLOCATION (xsum (length, count));
4601 tmpsrc = (TCHAR_T *) (result + length);
4602 }
4603# else
4604 tmpsrc = tmp;
4605 ENSURE_ALLOCATION (xsum (length, count));
4606# endif
4607 tmpdst = result + length;
4608 /* Copy backwards, because of overlapping. */
4609 tmpsrc += count;
4610 tmpdst += count;
4611 for (n = count; n > 0; n--)
4612 *--tmpdst = (unsigned char) *--tmpsrc;
4613 }
4614 }
4615#endif
4616
4617#if DCHAR_IS_TCHAR && !USE_SNPRINTF
4618 /* Make room for the result. */
4619 if (count > allocated - length)
4620 {
4621 /* Need at least count elements. But allocate
4622 proportionally. */
4623 size_t n =
4624 xmax (xsum (length, count), xtimes (allocated, 2));
4625
4626 ENSURE_ALLOCATION (n);
4627 }
4628#endif
4629
4630 /* Here count <= allocated - length. */
4631
4632 /* Perform padding. */
4633#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
4634 if (pad_ourselves && has_width)
4635 {
4636 size_t w;
4637# if ENABLE_UNISTDIO
4638 /* Outside POSIX, it's preferrable to compare the width
4639 against the number of _characters_ of the converted
4640 value. */
4641 w = DCHAR_MBSNLEN (result + length, count);
4642# else
4643 /* The width is compared against the number of _bytes_
4644 of the converted value, says POSIX. */
4645 w = count;
4646# endif
4647 if (w < width)
4648 {
4649 size_t pad = width - w;
4650# if USE_SNPRINTF
4651 /* Make room for the result. */
4652 if (xsum (count, pad) > allocated - length)
4653 {
4654 /* Need at least count + pad elements. But
4655 allocate proportionally. */
4656 size_t n = xmax (xsum3 (length, count, pad),
4657 xtimes (allocated, 2));
4658
4659 length += count;
4660 ENSURE_ALLOCATION (n);
4661 length -= count;
4662 }
4663 /* Here count + pad <= allocated - length. */
4664# endif
4665 {
4666# if !DCHAR_IS_TCHAR || USE_SNPRINTF
4667 DCHAR_T *const rp = result + length;
4668# else
4669 DCHAR_T *const rp = tmp;
4670# endif
4671 DCHAR_T *p = rp + count;
4672 DCHAR_T *end = p + pad;
4673# if NEED_PRINTF_FLAG_ZERO
4674 DCHAR_T *pad_ptr;
4675# if !DCHAR_IS_TCHAR
4676 if (dp->conversion == 'c'
4677 || dp->conversion == 's')
4678 /* No zero-padding for string directives. */
4679 pad_ptr = NULL;
4680 else
4681# endif
4682 {
4683 pad_ptr = (*rp == '-' ? rp + 1 : rp);
4684 /* No zero-padding of "inf" and "nan". */
4685 if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
4686 || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
4687 pad_ptr = NULL;
4688 }
4689# endif
4690 /* The generated string now extends from rp to p,
4691 with the zero padding insertion point being at
4692 pad_ptr. */
4693
4694 count = count + pad; /* = end - rp */
4695
4696 if (flags & FLAG_LEFT)
4697 {
4698 /* Pad with spaces on the right. */
4699 for (; pad > 0; pad--)
4700 *p++ = ' ';
4701 }
4702# if NEED_PRINTF_FLAG_ZERO
4703 else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
4704 {
4705 /* Pad with zeroes. */
4706 DCHAR_T *q = end;
4707
4708 while (p > pad_ptr)
4709 *--q = *--p;
4710 for (; pad > 0; pad--)
4711 *p++ = '0';
4712 }
4713# endif
4714 else
4715 {
4716 /* Pad with spaces on the left. */
4717 DCHAR_T *q = end;
4718
4719 while (p > rp)
4720 *--q = *--p;
4721 for (; pad > 0; pad--)
4722 *p++ = ' ';
4723 }
4724 }
4725 }
4726 }
4727#endif
4728
4729#if DCHAR_IS_TCHAR && !USE_SNPRINTF
4730 if (count >= tmp_length)
4731 /* tmp_length was incorrectly calculated - fix the
4732 code above! */
4733 abort ();
4734#endif
4735
4736 /* Here still count <= allocated - length. */
4737
4738#if !DCHAR_IS_TCHAR || USE_SNPRINTF
4739 /* The snprintf() result did fit. */
4740#else
4741 /* Append the sprintf() result. */
4742 memcpy (result + length, tmp, count * sizeof (DCHAR_T));
4743#endif
4744#if !USE_SNPRINTF
4745 if (tmp != tmpbuf)
4746 free (tmp);
4747#endif
4748
4749#if NEED_PRINTF_DIRECTIVE_F
4750 if (dp->conversion == 'F')
4751 {
4752 /* Convert the %f result to upper case for %F. */
4753 DCHAR_T *rp = result + length;
4754 size_t rc;
4755 for (rc = count; rc > 0; rc--, rp++)
4756 if (*rp >= 'a' && *rp <= 'z')
4757 *rp = *rp - 'a' + 'A';
4758 }
4759#endif
4760
4761 length += count;
4762 break;
4763 }
4764 }
4765 }
4766 }
4767
4768 /* Add the final NUL. */
4769 ENSURE_ALLOCATION (xsum (length, 1));
4770 result[length] = '\0';
4771
4772 if (result != resultbuf && length + 1 < allocated)
4773 {
4774 /* Shrink the allocated memory if possible. */
4775 DCHAR_T *memory;
4776
4777 memory =
4778 (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
4779 if (memory != NULL)
4780 result = memory;
4781 }
4782
4783 if (buf_malloced != NULL)
4784 free (buf_malloced);
4785 CLEANUP ();
4786 *lengthp = length;
4787 /* Note that we can produce a big string of a length > INT_MAX. POSIX
4788 says that snprintf() fails with errno = EOVERFLOW in this case, but
4789 that's only because snprintf() returns an 'int'. This function does
4790 not have this limitation. */
4791 return result;
4792
4793 overflow:
4794 if (!(result == resultbuf || result == NULL))
4795 free (result);
4796 if (buf_malloced != NULL)
4797 free (buf_malloced);
4798 CLEANUP ();
4799 errno = EOVERFLOW;
4800 return NULL;
4801
4802 out_of_memory:
4803 if (!(result == resultbuf || result == NULL))
4804 free (result);
4805 if (buf_malloced != NULL)
4806 free (buf_malloced);
4807 out_of_memory_1:
4808 CLEANUP ();
4809 errno = ENOMEM;
4810 return NULL;
4811 }
4812}
4813
4814#undef TCHARS_PER_DCHAR
4815#undef SNPRINTF
4816#undef USE_SNPRINTF
4817#undef DCHAR_CPY
4818#undef PRINTF_PARSE
4819#undef DIRECTIVES
4820#undef DIRECTIVE
4821#undef DCHAR_IS_TCHAR
4822#undef TCHAR_T
4823#undef DCHAR_T
4824#undef FCHAR_T
4825#undef VASNPRINTF
diff --git a/src/daemon/https/lgl/vasnprintf.h b/src/daemon/https/lgl/vasnprintf.h
new file mode 100644
index 00000000..4524ce77
--- /dev/null
+++ b/src/daemon/https/lgl/vasnprintf.h
@@ -0,0 +1,81 @@
1/* vsprintf with automatic memory allocation.
2 Copyright (C) 2002-2004, 2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#ifndef _VASNPRINTF_H
19#define _VASNPRINTF_H
20
21/* Get va_list. */
22#include <stdarg.h>
23
24/* Get size_t. */
25#include <stddef.h>
26
27#ifndef __attribute__
28/* This feature is available in gcc versions 2.5 and later. */
29# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
30# define __attribute__(Spec) /* empty */
31# endif
32/* The __-protected variants of `format' and `printf' attributes
33 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
34# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
35# define __format__ format
36# define __printf__ printf
37# endif
38#endif
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* Write formatted output to a string dynamically allocated with malloc().
45 You can pass a preallocated buffer for the result in RESULTBUF and its
46 size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
47 If successful, return the address of the string (this may be = RESULTBUF
48 if no dynamic memory allocation was necessary) and set *LENGTHP to the
49 number of resulting bytes, excluding the trailing NUL. Upon error, set
50 errno and return NULL.
51
52 When dynamic memory allocation occurs, the preallocated buffer is left
53 alone (with possibly modified contents). This makes it possible to use
54 a statically allocated or stack-allocated buffer, like this:
55
56 char buf[100];
57 size_t len = sizeof (buf);
58 char *output = vasnprintf (buf, &len, format, args);
59 if (output == NULL)
60 ... error handling ...;
61 else
62 {
63 ... use the output string ...;
64 if (output != buf)
65 free (output);
66 }
67 */
68#if REPLACE_VASNPRINTF
69# define asnprintf rpl_asnprintf
70# define vasnprintf rpl_vasnprintf
71#endif
72extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
73 __attribute__ ((__format__ (__printf__, 3, 4)));
74extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
75 __attribute__ ((__format__ (__printf__, 3, 0)));
76
77#ifdef __cplusplus
78}
79#endif
80
81#endif /* _VASNPRINTF_H */
diff --git a/src/daemon/https/lgl/vasprintf.c b/src/daemon/https/lgl/vasprintf.c
new file mode 100644
index 00000000..7b645460
--- /dev/null
+++ b/src/daemon/https/lgl/vasprintf.c
@@ -0,0 +1,56 @@
1/* Formatted output to strings.
2 Copyright (C) 1999, 2002, 2006-2007 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18#include <config.h>
19
20/* Specification. */
21#ifdef IN_LIBASPRINTF
22# include "vasprintf.h"
23#else
24# include <stdio.h>
25#endif
26
27#include <errno.h>
28#include <limits.h>
29#include <stdlib.h>
30
31#include "vasnprintf.h"
32
33/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
34#ifndef EOVERFLOW
35# define EOVERFLOW E2BIG
36#endif
37
38int
39vasprintf (char **resultp, const char *format, va_list args)
40{
41 size_t length;
42 char *result = vasnprintf (NULL, &length, format, args);
43 if (result == NULL)
44 return -1;
45
46 if (length > INT_MAX)
47 {
48 free (result);
49 errno = EOVERFLOW;
50 return -1;
51 }
52
53 *resultp = result;
54 /* Return the number of resulting bytes, excluding the trailing NUL. */
55 return length;
56}
diff --git a/src/daemon/https/lgl/xsize.h b/src/daemon/https/lgl/xsize.h
new file mode 100644
index 00000000..d37de38a
--- /dev/null
+++ b/src/daemon/https/lgl/xsize.h
@@ -0,0 +1,109 @@
1/* xsize.h -- Checked size_t computations.
2
3 Copyright (C) 2003 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19#ifndef _XSIZE_H
20#define _XSIZE_H
21
22/* Get size_t. */
23#include <stddef.h>
24
25/* Get SIZE_MAX. */
26#include <limits.h>
27#if HAVE_STDINT_H
28# include <stdint.h>
29#endif
30
31/* The size of memory objects is often computed through expressions of
32 type size_t. Example:
33 void* p = malloc (header_size + n * element_size).
34 These computations can lead to overflow. When this happens, malloc()
35 returns a piece of memory that is way too small, and the program then
36 crashes while attempting to fill the memory.
37 To avoid this, the functions and macros in this file check for overflow.
38 The convention is that SIZE_MAX represents overflow.
39 malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
40 implementation that uses mmap --, it's recommended to use size_overflow_p()
41 or size_in_bounds_p() before invoking malloc().
42 The example thus becomes:
43 size_t size = xsum (header_size, xtimes (n, element_size));
44 void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
45*/
46
47/* Convert an arbitrary value >= 0 to type size_t. */
48#define xcast_size_t(N) \
49 ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
50
51/* Sum of two sizes, with overflow check. */
52static inline size_t
53#if __GNUC__ >= 3
54__attribute__ ((__pure__))
55#endif
56xsum (size_t size1, size_t size2)
57{
58 size_t sum = size1 + size2;
59 return (sum >= size1 ? sum : SIZE_MAX);
60}
61
62/* Sum of three sizes, with overflow check. */
63static inline size_t
64#if __GNUC__ >= 3
65__attribute__ ((__pure__))
66#endif
67xsum3 (size_t size1, size_t size2, size_t size3)
68{
69 return xsum (xsum (size1, size2), size3);
70}
71
72/* Sum of four sizes, with overflow check. */
73static inline size_t
74#if __GNUC__ >= 3
75__attribute__ ((__pure__))
76#endif
77xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
78{
79 return xsum (xsum (xsum (size1, size2), size3), size4);
80}
81
82/* Maximum of two sizes, with overflow check. */
83static inline size_t
84#if __GNUC__ >= 3
85__attribute__ ((__pure__))
86#endif
87xmax (size_t size1, size_t size2)
88{
89 /* No explicit check is needed here, because for any n:
90 max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX. */
91 return (size1 >= size2 ? size1 : size2);
92}
93
94/* Multiplication of a count with an element size, with overflow check.
95 The count must be >= 0 and the element size must be > 0.
96 This is a macro, not an inline function, so that it works correctly even
97 when N is of a wider tupe and N > SIZE_MAX. */
98#define xtimes(N, ELSIZE) \
99 ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
100
101/* Check for overflow. */
102#define size_overflow_p(SIZE) \
103 ((SIZE) == SIZE_MAX)
104/* Check against overflow. */
105#define size_in_bounds_p(SIZE) \
106 ((SIZE) != SIZE_MAX)
107
108#endif /* _XSIZE_H */
109