ray 0.0.0.pre1 → 0.0.0.pre2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/.gitignore +15 -0
  2. data/.gitmodules +3 -0
  3. data/Rakefile +148 -5
  4. data/VERSION +1 -1
  5. data/ext/event.c +535 -0
  6. data/ext/extconf.rb +7 -1
  7. data/ext/image.c +110 -18
  8. data/ext/joystick.c +145 -0
  9. data/ext/ray.c +288 -35
  10. data/ext/ray.h +54 -2
  11. data/lib/ray/config.rb +84 -0
  12. data/lib/ray/dsl.rb +19 -0
  13. data/lib/ray/dsl/converter.rb +65 -0
  14. data/lib/ray/dsl/event.rb +52 -0
  15. data/lib/ray/dsl/event_raiser.rb +21 -0
  16. data/lib/ray/dsl/event_runner.rb +39 -0
  17. data/lib/ray/dsl/event_translator.rb +38 -0
  18. data/lib/ray/dsl/handler.rb +71 -0
  19. data/lib/ray/dsl/listener.rb +30 -0
  20. data/lib/ray/dsl/matcher.rb +60 -0
  21. data/lib/ray/dsl/type.rb +58 -0
  22. data/lib/ray/game.rb +107 -0
  23. data/lib/ray/helper.rb +17 -0
  24. data/lib/ray/image.rb +11 -0
  25. data/lib/ray/ray.rb +8 -0
  26. data/lib/ray/scene.rb +102 -0
  27. data/psp/SDL_psp_main.c +84 -0
  28. data/psp/bigdecimal/README +60 -0
  29. data/psp/bigdecimal/bigdecimal.c +4697 -0
  30. data/psp/bigdecimal/bigdecimal.h +216 -0
  31. data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +85 -0
  32. data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +84 -0
  33. data/psp/bigdecimal/lib/bigdecimal/math.rb +235 -0
  34. data/psp/bigdecimal/lib/bigdecimal/newton.rb +77 -0
  35. data/psp/bigdecimal/lib/bigdecimal/util.rb +65 -0
  36. data/psp/digest/bubblebabble/bubblebabble.c +142 -0
  37. data/psp/digest/defs.h +20 -0
  38. data/psp/digest/digest.c +643 -0
  39. data/psp/digest/digest.h +32 -0
  40. data/psp/digest/lib/digest.rb +50 -0
  41. data/psp/digest/lib/md5.rb +27 -0
  42. data/psp/digest/lib/sha1.rb +27 -0
  43. data/psp/digest/md5/md5.c +420 -0
  44. data/psp/digest/md5/md5.h +80 -0
  45. data/psp/digest/md5/md5init.c +40 -0
  46. data/psp/digest/rmd160/rmd160.c +457 -0
  47. data/psp/digest/rmd160/rmd160.h +56 -0
  48. data/psp/digest/rmd160/rmd160init.c +40 -0
  49. data/psp/digest/sha1/sha1.c +269 -0
  50. data/psp/digest/sha1/sha1.h +39 -0
  51. data/psp/digest/sha1/sha1init.c +40 -0
  52. data/psp/digest/sha2/lib/sha2.rb +73 -0
  53. data/psp/digest/sha2/sha2.c +919 -0
  54. data/psp/digest/sha2/sha2.h +109 -0
  55. data/psp/digest/sha2/sha2init.c +52 -0
  56. data/psp/enumerator/enumerator.c +298 -0
  57. data/psp/etc/etc.c +559 -0
  58. data/psp/ext.c +285 -0
  59. data/psp/fcntl/fcntl.c +187 -0
  60. data/psp/lib/rbconfig.rb +178 -0
  61. data/psp/nkf/lib/kconv.rb +367 -0
  62. data/psp/nkf/nkf-utf8/config.h +88 -0
  63. data/psp/nkf/nkf-utf8/nkf.c +6040 -0
  64. data/psp/nkf/nkf-utf8/utf8tbl.c +8500 -0
  65. data/psp/nkf/nkf-utf8/utf8tbl.h +34 -0
  66. data/psp/nkf/nkf.c +654 -0
  67. data/psp/socket/addrinfo.h +173 -0
  68. data/psp/socket/getaddrinfo.c +676 -0
  69. data/psp/socket/getnameinfo.c +270 -0
  70. data/psp/socket/pspsocket.c +71 -0
  71. data/psp/socket/pspsocket.h +28 -0
  72. data/psp/socket/socket.c +4662 -0
  73. data/psp/socket/sockport.h +76 -0
  74. data/psp/stringio/stringio.c +1306 -0
  75. data/psp/strscan/strscan.c +1320 -0
  76. data/psp/syck/bytecode.c +1166 -0
  77. data/psp/syck/emitter.c +1242 -0
  78. data/psp/syck/gram.c +1894 -0
  79. data/psp/syck/gram.h +79 -0
  80. data/psp/syck/handler.c +174 -0
  81. data/psp/syck/implicit.c +2990 -0
  82. data/psp/syck/node.c +408 -0
  83. data/psp/syck/rubyext.c +2367 -0
  84. data/psp/syck/syck.c +504 -0
  85. data/psp/syck/syck.h +456 -0
  86. data/psp/syck/token.c +2725 -0
  87. data/psp/syck/yaml2byte.c +257 -0
  88. data/psp/syck/yamlbyte.h +170 -0
  89. data/psp/thread/thread.c +1175 -0
  90. data/psp/zlib/zlib.c +3547 -0
  91. data/script.rb +10 -0
  92. data/spec/ray/config_spec.rb +90 -0
  93. data/spec/ray/conversion_spec.rb +43 -0
  94. data/spec/ray/event_spec.rb +191 -0
  95. data/spec/ray/image_spec.rb +43 -1
  96. data/spec/ray/joystick_spec.rb +17 -0
  97. data/spec/ray/matcher_spec.rb +73 -0
  98. data/spec/ray/ray_spec.rb +72 -1
  99. data/spec/ray/type_spec.rb +17 -0
  100. data/spec/res/aqua.bmp +0 -0
  101. data/spec/res/aqua.png +0 -0
  102. data/spec/res/not_a_jpeg.jpeg +0 -0
  103. data/spec_runner.rb +4 -0
  104. metadata +101 -9
  105. data/ext/Makefile +0 -189
  106. data/ext/ray +0 -0
  107. data/ext/ray.bundle +0 -0
  108. data/ext/ray_ext.bundle +0 -0
  109. data/ext/ray_ext.so +0 -0
  110. data/ext/test.rb +0 -21
@@ -0,0 +1,32 @@
1
+ /************************************************
2
+
3
+ digest.h - header file for ruby digest modules
4
+
5
+ $Author: shyouhei $
6
+ created at: Fri May 25 08:54:56 JST 2001
7
+
8
+
9
+ Copyright (C) 2001-2006 Akinori MUSHA
10
+
11
+ $RoughId: digest.h,v 1.3 2001/07/13 15:38:27 knu Exp $
12
+ $Id: digest.h 11708 2007-02-12 23:01:19Z shyouhei $
13
+
14
+ ************************************************/
15
+
16
+ #include <ruby/ruby.h>
17
+
18
+ #define RUBY_DIGEST_API_VERSION 2
19
+
20
+ typedef void (*rb_digest_hash_init_func_t)(void *);
21
+ typedef void (*rb_digest_hash_update_func_t)(void *, unsigned char *, size_t);
22
+ typedef void (*rb_digest_hash_finish_func_t)(void *, unsigned char *);
23
+
24
+ typedef struct {
25
+ int api_version;
26
+ size_t digest_len;
27
+ size_t block_len;
28
+ size_t ctx_size;
29
+ rb_digest_hash_init_func_t init_func;
30
+ rb_digest_hash_update_func_t update_func;
31
+ rb_digest_hash_finish_func_t finish_func;
32
+ } rb_digest_metadata_t;
@@ -0,0 +1,50 @@
1
+ if defined? Ray and not defined? Digest
2
+ Ray.init_internal("digest")
3
+ end
4
+
5
+ module Digest
6
+ def self.const_missing(name)
7
+ case name
8
+ when :SHA256, :SHA384, :SHA512
9
+ lib = 'digest/sha2.rb'
10
+ else
11
+ lib = File.join('digest', name.to_s.downcase)
12
+ end
13
+
14
+ begin
15
+ require lib
16
+ rescue LoadError => e
17
+ raise LoadError, "library not found for class Digest::#{name} -- #{lib}"
18
+ end
19
+
20
+ Digest.const_get(name)
21
+ end
22
+
23
+ class ::Digest::Class
24
+ # creates a digest object and reads a given file, _name_.
25
+ #
26
+ # p Digest::SHA256.file("X11R6.8.2-src.tar.bz2").hexdigest
27
+ # # => "f02e3c85572dc9ad7cb77c2a638e3be24cc1b5bea9fdbb0b0299c9668475c534"
28
+ def self.file(name)
29
+ new.file(name)
30
+ end
31
+ end
32
+
33
+ module Instance
34
+ # updates the digest with the contents of a given file _name_ and
35
+ # returns self.
36
+ def file(name)
37
+ File.open(name, "rb") {|f|
38
+ buf = ""
39
+ while f.read(16384, buf)
40
+ update buf
41
+ end
42
+ }
43
+ self
44
+ end
45
+ end
46
+ end
47
+
48
+ def Digest(name)
49
+ Digest.const_get(name)
50
+ end
@@ -0,0 +1,27 @@
1
+ # just for compatibility; requiring "md5" is obsoleted
2
+ #
3
+ # $RoughId: md5.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
4
+ # $Id: md5.rb 12008 2007-03-06 10:12:12Z knu $
5
+
6
+ require 'digest'
7
+
8
+ if defined? Ray and not defined? Digest::MD5
9
+ Ray.init_internal("md5")
10
+ end
11
+
12
+ class MD5 < Digest::MD5
13
+ class << self
14
+ alias orig_new new
15
+ def new(str = nil)
16
+ if str
17
+ orig_new.update(str)
18
+ else
19
+ orig_new
20
+ end
21
+ end
22
+
23
+ def md5(*args)
24
+ new(*args)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # just for compatibility; requiring "sha1" is obsoleted
2
+ #
3
+ # $RoughId: sha1.rb,v 1.4 2001/07/13 15:38:27 knu Exp $
4
+ # $Id: sha1.rb 12008 2007-03-06 10:12:12Z knu $
5
+
6
+ require 'digest'
7
+
8
+ if defined? Ray and not defined? Digest::SHA1
9
+ Ray.init_internal("sha1")
10
+ end
11
+
12
+ class SHA1 < Digest::SHA1
13
+ class << self
14
+ alias orig_new new
15
+ def new(str = nil)
16
+ if str
17
+ orig_new.update(str)
18
+ else
19
+ orig_new
20
+ end
21
+ end
22
+
23
+ def sha1(*args)
24
+ new(*args)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,420 @@
1
+ /*
2
+ Copyright (C) 1999, 2000 Aladdin Enterprises. All rights reserved.
3
+
4
+ This software is provided 'as-is', without any express or implied
5
+ warranty. In no event will the authors be held liable for any damages
6
+ arising from the use of this software.
7
+
8
+ Permission is granted to anyone to use this software for any purpose,
9
+ including commercial applications, and to alter it and redistribute it
10
+ freely, subject to the following restrictions:
11
+
12
+ 1. The origin of this software must not be misrepresented; you must not
13
+ claim that you wrote the original software. If you use this software
14
+ in a product, an acknowledgment in the product documentation would be
15
+ appreciated but is not required.
16
+ 2. Altered source versions must be plainly marked as such, and must not be
17
+ misrepresented as being the original software.
18
+ 3. This notice may not be removed or altered from any source distribution.
19
+
20
+ L. Peter Deutsch
21
+ ghost@aladdin.com
22
+
23
+ */
24
+
25
+ /*
26
+ Independent implementation of MD5 (RFC 1321).
27
+
28
+ This code implements the MD5 Algorithm defined in RFC 1321.
29
+ It is derived directly from the text of the RFC and not from the
30
+ reference implementation.
31
+
32
+ The original and principal author of md5.c is L. Peter Deutsch
33
+ <ghost@aladdin.com>. Other authors are noted in the change history
34
+ that follows (in reverse chronological order):
35
+
36
+ 2000-07-03 lpd Patched to eliminate warnings about "constant is
37
+ unsigned in ANSI C, signed in traditional";
38
+ made test program self-checking.
39
+ 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
40
+ 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
41
+ 1999-05-03 lpd Original version.
42
+ */
43
+
44
+ /*
45
+ This code was modified for use in Ruby.
46
+
47
+ - Akinori MUSHA <knu@idaemons.org>
48
+ */
49
+
50
+ /*$OrigId: md5c.c,v 1.2 2001/03/26 08:57:14 matz Exp $ */
51
+ /*$RoughId: md5.c,v 1.2 2001/07/13 19:48:41 knu Exp $ */
52
+ /*$Id: md5.c 11708 2007-02-12 23:01:19Z shyouhei $ */
53
+
54
+ #include "md5.h"
55
+
56
+ #ifdef TEST
57
+ /*
58
+ * Compile with -DTEST to create a self-contained executable test program.
59
+ * The test program should print out the same values as given in section
60
+ * A.5 of RFC 1321, reproduced below.
61
+ */
62
+ #include <string.h>
63
+ main()
64
+ {
65
+ static const char *const test[7*2] = {
66
+ "", "d41d8cd98f00b204e9800998ecf8427e",
67
+ "a", "0cc175b9c0f1b6a831c399e269772661",
68
+ "abc", "900150983cd24fb0d6963f7d28e17f72",
69
+ "message digest", "f96b697d7cb7938d525a2f31aaf161d0",
70
+ "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b",
71
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
72
+ "d174ab98d277d9f5a5611c2c9f419d9f",
73
+ "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a"
74
+ };
75
+ int i;
76
+
77
+ for (i = 0; i < 7*2; i += 2) {
78
+ MD5_CTX state;
79
+ uint8_t digest[16];
80
+ char hex_output[16*2 + 1];
81
+ int di;
82
+
83
+ MD5_Init(&state);
84
+ MD5_Update(&state, (const uint8_t *)test[i], strlen(test[i]));
85
+ MD5_Final(digest, &state);
86
+ printf("MD5 (\"%s\") = ", test[i]);
87
+ for (di = 0; di < 16; ++di)
88
+ sprintf(hex_output + di * 2, "%02x", digest[di]);
89
+ puts(hex_output);
90
+ if (strcmp(hex_output, test[i + 1]))
91
+ printf("**** ERROR, should be: %s\n", test[i + 1]);
92
+ }
93
+ return 0;
94
+ }
95
+ #endif /* TEST */
96
+
97
+
98
+ /*
99
+ * For reference, here is the program that computed the T values.
100
+ */
101
+ #ifdef COMPUTE_T_VALUES
102
+ #include <math.h>
103
+ main()
104
+ {
105
+ int i;
106
+ for (i = 1; i <= 64; ++i) {
107
+ unsigned long v = (unsigned long)(4294967296.0 * fabs(sin((double)i)));
108
+
109
+ /*
110
+ * The following nonsense is only to avoid compiler warnings about
111
+ * "integer constant is unsigned in ANSI C, signed with -traditional".
112
+ */
113
+ if (v >> 31) {
114
+ printf("#define T%d /* 0x%08lx */ (T_MASK ^ 0x%08lx)\n", i,
115
+ v, (unsigned long)(unsigned int)(~v));
116
+ } else {
117
+ printf("#define T%d 0x%08lx\n", i, v);
118
+ }
119
+ }
120
+ return 0;
121
+ }
122
+ #endif /* COMPUTE_T_VALUES */
123
+ /*
124
+ * End of T computation program.
125
+ */
126
+ #ifdef T_MASK
127
+ #undef T_MASK
128
+ #endif
129
+ #define T_MASK ((uint32_t)~0)
130
+ #define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
131
+ #define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
132
+ #define T3 0x242070db
133
+ #define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
134
+ #define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
135
+ #define T6 0x4787c62a
136
+ #define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
137
+ #define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
138
+ #define T9 0x698098d8
139
+ #define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
140
+ #define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
141
+ #define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
142
+ #define T13 0x6b901122
143
+ #define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
144
+ #define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
145
+ #define T16 0x49b40821
146
+ #define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
147
+ #define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
148
+ #define T19 0x265e5a51
149
+ #define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
150
+ #define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
151
+ #define T22 0x02441453
152
+ #define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
153
+ #define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
154
+ #define T25 0x21e1cde6
155
+ #define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
156
+ #define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
157
+ #define T28 0x455a14ed
158
+ #define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
159
+ #define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
160
+ #define T31 0x676f02d9
161
+ #define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
162
+ #define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
163
+ #define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
164
+ #define T35 0x6d9d6122
165
+ #define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
166
+ #define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
167
+ #define T38 0x4bdecfa9
168
+ #define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
169
+ #define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
170
+ #define T41 0x289b7ec6
171
+ #define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
172
+ #define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
173
+ #define T44 0x04881d05
174
+ #define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
175
+ #define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
176
+ #define T47 0x1fa27cf8
177
+ #define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
178
+ #define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
179
+ #define T50 0x432aff97
180
+ #define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
181
+ #define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
182
+ #define T53 0x655b59c3
183
+ #define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
184
+ #define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
185
+ #define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
186
+ #define T57 0x6fa87e4f
187
+ #define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
188
+ #define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
189
+ #define T60 0x4e0811a1
190
+ #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
191
+ #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
192
+ #define T63 0x2ad7d2bb
193
+ #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
194
+
195
+
196
+ static void
197
+ md5_process(MD5_CTX *pms, const uint8_t *data /*[64]*/)
198
+ {
199
+ uint32_t
200
+ a = pms->state[0], b = pms->state[1],
201
+ c = pms->state[2], d = pms->state[3];
202
+ uint32_t t;
203
+
204
+ #ifdef WORDS_BIGENDIAN
205
+
206
+ /*
207
+ * On big-endian machines, we must arrange the bytes in the right
208
+ * order. (This also works on machines of unknown byte order.)
209
+ */
210
+ uint32_t X[16];
211
+ const uint8_t *xp = data;
212
+ int i;
213
+
214
+ for (i = 0; i < 16; ++i, xp += 4)
215
+ X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
216
+
217
+ #else
218
+
219
+ /*
220
+ * On little-endian machines, we can process properly aligned data
221
+ * without copying it.
222
+ */
223
+ uint32_t xbuf[16];
224
+ const uint32_t *X;
225
+
226
+ if (!((data - (const uint8_t *)0) & 3)) {
227
+ /* data are properly aligned */
228
+ X = (const uint32_t *)data;
229
+ } else {
230
+ /* not aligned */
231
+ memcpy(xbuf, data, 64);
232
+ X = xbuf;
233
+ }
234
+ #endif
235
+
236
+ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
237
+
238
+ /* Round 1. */
239
+ /* Let [abcd k s i] denote the operation
240
+ a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
241
+ #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
242
+ #define SET(a, b, c, d, k, s, Ti)\
243
+ t = a + F(b,c,d) + X[k] + Ti;\
244
+ a = ROTATE_LEFT(t, s) + b
245
+ /* Do the following 16 operations. */
246
+ SET(a, b, c, d, 0, 7, T1);
247
+ SET(d, a, b, c, 1, 12, T2);
248
+ SET(c, d, a, b, 2, 17, T3);
249
+ SET(b, c, d, a, 3, 22, T4);
250
+ SET(a, b, c, d, 4, 7, T5);
251
+ SET(d, a, b, c, 5, 12, T6);
252
+ SET(c, d, a, b, 6, 17, T7);
253
+ SET(b, c, d, a, 7, 22, T8);
254
+ SET(a, b, c, d, 8, 7, T9);
255
+ SET(d, a, b, c, 9, 12, T10);
256
+ SET(c, d, a, b, 10, 17, T11);
257
+ SET(b, c, d, a, 11, 22, T12);
258
+ SET(a, b, c, d, 12, 7, T13);
259
+ SET(d, a, b, c, 13, 12, T14);
260
+ SET(c, d, a, b, 14, 17, T15);
261
+ SET(b, c, d, a, 15, 22, T16);
262
+ #undef SET
263
+
264
+ /* Round 2. */
265
+ /* Let [abcd k s i] denote the operation
266
+ a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
267
+ #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
268
+ #define SET(a, b, c, d, k, s, Ti)\
269
+ t = a + G(b,c,d) + X[k] + Ti;\
270
+ a = ROTATE_LEFT(t, s) + b
271
+ /* Do the following 16 operations. */
272
+ SET(a, b, c, d, 1, 5, T17);
273
+ SET(d, a, b, c, 6, 9, T18);
274
+ SET(c, d, a, b, 11, 14, T19);
275
+ SET(b, c, d, a, 0, 20, T20);
276
+ SET(a, b, c, d, 5, 5, T21);
277
+ SET(d, a, b, c, 10, 9, T22);
278
+ SET(c, d, a, b, 15, 14, T23);
279
+ SET(b, c, d, a, 4, 20, T24);
280
+ SET(a, b, c, d, 9, 5, T25);
281
+ SET(d, a, b, c, 14, 9, T26);
282
+ SET(c, d, a, b, 3, 14, T27);
283
+ SET(b, c, d, a, 8, 20, T28);
284
+ SET(a, b, c, d, 13, 5, T29);
285
+ SET(d, a, b, c, 2, 9, T30);
286
+ SET(c, d, a, b, 7, 14, T31);
287
+ SET(b, c, d, a, 12, 20, T32);
288
+ #undef SET
289
+
290
+ /* Round 3. */
291
+ /* Let [abcd k s t] denote the operation
292
+ a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
293
+ #define H(x, y, z) ((x) ^ (y) ^ (z))
294
+ #define SET(a, b, c, d, k, s, Ti)\
295
+ t = a + H(b,c,d) + X[k] + Ti;\
296
+ a = ROTATE_LEFT(t, s) + b
297
+ /* Do the following 16 operations. */
298
+ SET(a, b, c, d, 5, 4, T33);
299
+ SET(d, a, b, c, 8, 11, T34);
300
+ SET(c, d, a, b, 11, 16, T35);
301
+ SET(b, c, d, a, 14, 23, T36);
302
+ SET(a, b, c, d, 1, 4, T37);
303
+ SET(d, a, b, c, 4, 11, T38);
304
+ SET(c, d, a, b, 7, 16, T39);
305
+ SET(b, c, d, a, 10, 23, T40);
306
+ SET(a, b, c, d, 13, 4, T41);
307
+ SET(d, a, b, c, 0, 11, T42);
308
+ SET(c, d, a, b, 3, 16, T43);
309
+ SET(b, c, d, a, 6, 23, T44);
310
+ SET(a, b, c, d, 9, 4, T45);
311
+ SET(d, a, b, c, 12, 11, T46);
312
+ SET(c, d, a, b, 15, 16, T47);
313
+ SET(b, c, d, a, 2, 23, T48);
314
+ #undef SET
315
+
316
+ /* Round 4. */
317
+ /* Let [abcd k s t] denote the operation
318
+ a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
319
+ #define I(x, y, z) ((y) ^ ((x) | ~(z)))
320
+ #define SET(a, b, c, d, k, s, Ti)\
321
+ t = a + I(b,c,d) + X[k] + Ti;\
322
+ a = ROTATE_LEFT(t, s) + b
323
+ /* Do the following 16 operations. */
324
+ SET(a, b, c, d, 0, 6, T49);
325
+ SET(d, a, b, c, 7, 10, T50);
326
+ SET(c, d, a, b, 14, 15, T51);
327
+ SET(b, c, d, a, 5, 21, T52);
328
+ SET(a, b, c, d, 12, 6, T53);
329
+ SET(d, a, b, c, 3, 10, T54);
330
+ SET(c, d, a, b, 10, 15, T55);
331
+ SET(b, c, d, a, 1, 21, T56);
332
+ SET(a, b, c, d, 8, 6, T57);
333
+ SET(d, a, b, c, 15, 10, T58);
334
+ SET(c, d, a, b, 6, 15, T59);
335
+ SET(b, c, d, a, 13, 21, T60);
336
+ SET(a, b, c, d, 4, 6, T61);
337
+ SET(d, a, b, c, 11, 10, T62);
338
+ SET(c, d, a, b, 2, 15, T63);
339
+ SET(b, c, d, a, 9, 21, T64);
340
+ #undef SET
341
+
342
+ /* Then perform the following additions. (That is increment each
343
+ of the four registers by the value it had before this block
344
+ was started.) */
345
+ pms->state[0] += a;
346
+ pms->state[1] += b;
347
+ pms->state[2] += c;
348
+ pms->state[3] += d;
349
+ }
350
+
351
+ void
352
+ MD5_Init(MD5_CTX *pms)
353
+ {
354
+ pms->count[0] = pms->count[1] = 0;
355
+ pms->state[0] = 0x67452301;
356
+ pms->state[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
357
+ pms->state[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
358
+ pms->state[3] = 0x10325476;
359
+ }
360
+
361
+ void
362
+ MD5_Update(MD5_CTX *pms, const uint8_t *data, size_t nbytes)
363
+ {
364
+ const uint8_t *p = data;
365
+ size_t left = nbytes;
366
+ size_t offset = (pms->count[0] >> 3) & 63;
367
+ uint32_t nbits = (uint32_t)(nbytes << 3);
368
+
369
+ if (nbytes <= 0)
370
+ return;
371
+
372
+ /* Update the message length. */
373
+ pms->count[1] += nbytes >> 29;
374
+ pms->count[0] += nbits;
375
+ if (pms->count[0] < nbits)
376
+ pms->count[1]++;
377
+
378
+ /* Process an initial partial block. */
379
+ if (offset) {
380
+ size_t copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
381
+
382
+ memcpy(pms->buffer + offset, p, copy);
383
+ if (offset + copy < 64)
384
+ return;
385
+ p += copy;
386
+ left -= copy;
387
+ md5_process(pms, pms->buffer);
388
+ }
389
+
390
+ /* Process full blocks. */
391
+ for (; left >= 64; p += 64, left -= 64)
392
+ md5_process(pms, p);
393
+
394
+ /* Process a final partial block. */
395
+ if (left)
396
+ memcpy(pms->buffer, p, left);
397
+ }
398
+
399
+ void
400
+ MD5_Finish(MD5_CTX *pms, uint8_t *digest)
401
+ {
402
+ static const uint8_t pad[64] = {
403
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
404
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
405
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
406
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
407
+ };
408
+ uint8_t data[8];
409
+ size_t i;
410
+
411
+ /* Save the length before padding. */
412
+ for (i = 0; i < 8; ++i)
413
+ data[i] = (uint8_t)(pms->count[i >> 2] >> ((i & 3) << 3));
414
+ /* Pad to 56 bytes mod 64. */
415
+ MD5_Update(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
416
+ /* Append the length. */
417
+ MD5_Update(pms, data, 8);
418
+ for (i = 0; i < 16; ++i)
419
+ digest[i] = (uint8_t)(pms->state[i >> 2] >> ((i & 3) << 3));
420
+ }