iodine 0.4.19 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of iodine might be problematic. Click here for more details.

Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/CHANGELOG.md +22 -0
  4. data/LIMITS.md +19 -9
  5. data/README.md +92 -77
  6. data/SPEC-PubSub-Draft.md +113 -0
  7. data/SPEC-Websocket-Draft.md +127 -143
  8. data/bin/http-hello +0 -1
  9. data/bin/raw-rbhttp +1 -1
  10. data/bin/raw_broadcast +8 -10
  11. data/bin/updated api +2 -2
  12. data/bin/ws-broadcast +2 -4
  13. data/bin/ws-echo +2 -2
  14. data/examples/config.ru +13 -13
  15. data/examples/echo.ru +5 -6
  16. data/examples/hello.ru +2 -3
  17. data/examples/info.md +316 -0
  18. data/examples/pubsub_engine.ru +81 -0
  19. data/examples/redis.ru +9 -9
  20. data/examples/shootout.ru +45 -11
  21. data/ext/iodine/defer.c +194 -297
  22. data/ext/iodine/defer.h +61 -53
  23. data/ext/iodine/evio.c +0 -260
  24. data/ext/iodine/evio.h +50 -22
  25. data/ext/iodine/evio_callbacks.c +26 -0
  26. data/ext/iodine/evio_epoll.c +251 -0
  27. data/ext/iodine/evio_kqueue.c +193 -0
  28. data/ext/iodine/extconf.rb +1 -1
  29. data/ext/iodine/facil.c +1420 -542
  30. data/ext/iodine/facil.h +151 -64
  31. data/ext/iodine/fio_ary.h +418 -0
  32. data/ext/iodine/{base64.c → fio_base64.c} +33 -24
  33. data/ext/iodine/{base64.h → fio_base64.h} +6 -7
  34. data/ext/iodine/{fio_cli_helper.c → fio_cli.c} +77 -58
  35. data/ext/iodine/{fio_cli_helper.h → fio_cli.h} +9 -4
  36. data/ext/iodine/fio_hashmap.h +759 -0
  37. data/ext/iodine/fio_json_parser.h +651 -0
  38. data/ext/iodine/fio_llist.h +257 -0
  39. data/ext/iodine/fio_mem.c +672 -0
  40. data/ext/iodine/fio_mem.h +140 -0
  41. data/ext/iodine/fio_random.c +248 -0
  42. data/ext/iodine/{random.h → fio_random.h} +11 -14
  43. data/ext/iodine/{sha1.c → fio_sha1.c} +28 -24
  44. data/ext/iodine/{sha1.h → fio_sha1.h} +38 -16
  45. data/ext/iodine/{sha2.c → fio_sha2.c} +66 -49
  46. data/ext/iodine/{sha2.h → fio_sha2.h} +57 -26
  47. data/ext/iodine/{fiobj_internal.c → fio_siphash.c} +9 -90
  48. data/ext/iodine/fio_siphash.h +18 -0
  49. data/ext/iodine/fio_tmpfile.h +38 -0
  50. data/ext/iodine/fiobj.h +24 -7
  51. data/ext/iodine/fiobj4sock.h +23 -0
  52. data/ext/iodine/fiobj_ary.c +143 -226
  53. data/ext/iodine/fiobj_ary.h +17 -16
  54. data/ext/iodine/fiobj_data.c +1160 -0
  55. data/ext/iodine/fiobj_data.h +164 -0
  56. data/ext/iodine/fiobj_hash.c +298 -406
  57. data/ext/iodine/fiobj_hash.h +101 -54
  58. data/ext/iodine/fiobj_json.c +478 -601
  59. data/ext/iodine/fiobj_json.h +34 -9
  60. data/ext/iodine/fiobj_numbers.c +383 -51
  61. data/ext/iodine/fiobj_numbers.h +87 -11
  62. data/ext/iodine/fiobj_str.c +423 -184
  63. data/ext/iodine/fiobj_str.h +81 -32
  64. data/ext/iodine/fiobject.c +273 -522
  65. data/ext/iodine/fiobject.h +477 -112
  66. data/ext/iodine/http.c +2243 -83
  67. data/ext/iodine/http.h +842 -121
  68. data/ext/iodine/http1.c +810 -385
  69. data/ext/iodine/http1.h +16 -39
  70. data/ext/iodine/http1_parser.c +146 -74
  71. data/ext/iodine/http1_parser.h +15 -4
  72. data/ext/iodine/http_internal.c +1258 -0
  73. data/ext/iodine/http_internal.h +226 -0
  74. data/ext/iodine/http_mime_parser.h +341 -0
  75. data/ext/iodine/iodine.c +86 -68
  76. data/ext/iodine/iodine.h +26 -11
  77. data/ext/iodine/iodine_helpers.c +8 -7
  78. data/ext/iodine/iodine_http.c +487 -324
  79. data/ext/iodine/iodine_json.c +304 -0
  80. data/ext/iodine/iodine_json.h +6 -0
  81. data/ext/iodine/iodine_protocol.c +107 -45
  82. data/ext/iodine/iodine_pubsub.c +526 -225
  83. data/ext/iodine/iodine_pubsub.h +10 -0
  84. data/ext/iodine/iodine_websockets.c +268 -510
  85. data/ext/iodine/iodine_websockets.h +2 -4
  86. data/ext/iodine/pubsub.c +726 -432
  87. data/ext/iodine/pubsub.h +85 -103
  88. data/ext/iodine/rb-call.c +4 -4
  89. data/ext/iodine/rb-defer.c +46 -22
  90. data/ext/iodine/rb-fiobj2rb.h +117 -0
  91. data/ext/iodine/rb-rack-io.c +73 -238
  92. data/ext/iodine/rb-rack-io.h +2 -2
  93. data/ext/iodine/rb-registry.c +35 -93
  94. data/ext/iodine/rb-registry.h +1 -0
  95. data/ext/iodine/redis_engine.c +742 -304
  96. data/ext/iodine/redis_engine.h +42 -39
  97. data/ext/iodine/resp_parser.h +311 -0
  98. data/ext/iodine/sock.c +627 -490
  99. data/ext/iodine/sock.h +345 -297
  100. data/ext/iodine/spnlock.inc +15 -4
  101. data/ext/iodine/websocket_parser.h +16 -20
  102. data/ext/iodine/websockets.c +188 -257
  103. data/ext/iodine/websockets.h +24 -133
  104. data/lib/iodine.rb +52 -7
  105. data/lib/iodine/cli.rb +6 -24
  106. data/lib/iodine/json.rb +40 -0
  107. data/lib/iodine/version.rb +1 -1
  108. data/lib/iodine/websocket.rb +5 -3
  109. data/lib/rack/handler/iodine.rb +58 -13
  110. metadata +38 -48
  111. data/bin/ws-shootout +0 -107
  112. data/examples/broadcast.ru +0 -56
  113. data/ext/iodine/bscrypt-common.h +0 -116
  114. data/ext/iodine/bscrypt.h +0 -49
  115. data/ext/iodine/fio2resp.c +0 -60
  116. data/ext/iodine/fio2resp.h +0 -51
  117. data/ext/iodine/fio_dict.c +0 -446
  118. data/ext/iodine/fio_dict.h +0 -99
  119. data/ext/iodine/fio_hash_table.h +0 -370
  120. data/ext/iodine/fio_list.h +0 -111
  121. data/ext/iodine/fiobj_internal.h +0 -280
  122. data/ext/iodine/fiobj_primitives.c +0 -131
  123. data/ext/iodine/fiobj_primitives.h +0 -55
  124. data/ext/iodine/fiobj_sym.c +0 -135
  125. data/ext/iodine/fiobj_sym.h +0 -60
  126. data/ext/iodine/hex.c +0 -124
  127. data/ext/iodine/hex.h +0 -70
  128. data/ext/iodine/http1_request.c +0 -81
  129. data/ext/iodine/http1_request.h +0 -58
  130. data/ext/iodine/http1_response.c +0 -417
  131. data/ext/iodine/http1_response.h +0 -95
  132. data/ext/iodine/http_request.c +0 -111
  133. data/ext/iodine/http_request.h +0 -102
  134. data/ext/iodine/http_response.c +0 -1703
  135. data/ext/iodine/http_response.h +0 -250
  136. data/ext/iodine/misc.c +0 -182
  137. data/ext/iodine/misc.h +0 -74
  138. data/ext/iodine/random.c +0 -208
  139. data/ext/iodine/redis_connection.c +0 -278
  140. data/ext/iodine/redis_connection.h +0 -86
  141. data/ext/iodine/resp.c +0 -842
  142. data/ext/iodine/resp.h +0 -261
  143. data/ext/iodine/siphash.c +0 -154
  144. data/ext/iodine/siphash.h +0 -22
  145. data/ext/iodine/xor-crypt.c +0 -193
  146. data/ext/iodine/xor-crypt.h +0 -107
@@ -5,9 +5,32 @@ of), which might be subject to their own licenses.
5
5
 
6
6
  Feel free to copy, use and enjoy in accordance with to the license(s).
7
7
  */
8
- #ifndef bscrypt_SHA1_H
9
- #define bscrypt_SHA1_H
10
- #include "bscrypt-common.h"
8
+ #ifndef H_FIO_SHA1_H
9
+ #define H_FIO_SHA1_H
10
+
11
+ #include <stdint.h>
12
+ #include <stdlib.h>
13
+
14
+ // clang-format off
15
+ #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
16
+ # if defined(__has_include)
17
+ # if __has_include(<endian.h>)
18
+ # include <endian.h>
19
+ # elif __has_include(<sys/endian.h>)
20
+ # include <sys/endian.h>
21
+ # endif
22
+ # endif
23
+ # if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) && \
24
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
25
+ # define __BIG_ENDIAN__
26
+ # endif
27
+ #endif
28
+
29
+ #ifndef UNUSED_FUNC
30
+ # define UNUSED_FUNC __attribute__((unused))
31
+ #endif
32
+ // clang-format on
33
+
11
34
  /* *****************************************************************************
12
35
  C++ extern
13
36
  */
@@ -28,12 +51,11 @@ required.
28
51
 
29
52
  Use, for example:
30
53
 
31
- #include "mini-crypt.h"
32
54
  sha1_s sha1;
33
- bscrypt.sha1_init(&sha1);
34
- bscrypt.sha1_write(&sha1,
55
+ fio_sha1_init(&sha1);
56
+ fio_sha1_write(&sha1,
35
57
  "The quick brown fox jumps over the lazy dog", 43);
36
- char *hashed_result = bscrypt.sha1_result(&sha1);
58
+ char *hashed_result = fio_sha1_result(&sha1);
37
59
  */
38
60
  typedef struct {
39
61
  uint64_t length;
@@ -48,31 +70,31 @@ typedef struct {
48
70
  Initialize or reset the `sha1` object. This must be performed before hashing
49
71
  data using sha1.
50
72
  */
51
- sha1_s bscrypt_sha1_init(void);
73
+ sha1_s fio_sha1_init(void);
52
74
  /**
53
75
  Writes data to the sha1 buffer.
54
76
  */
55
- void bscrypt_sha1_write(sha1_s *s, const void *data, size_t len);
77
+ void fio_sha1_write(sha1_s *s, const void *data, size_t len);
56
78
  /**
57
79
  Finalizes the SHA1 hash, returning the Hashed data.
58
80
 
59
81
  `sha1_result` can be called for the same object multiple times, but the
60
82
  finalization will only be performed the first time this function is called.
61
83
  */
62
- char *bscrypt_sha1_result(sha1_s *s);
84
+ char *fio_sha1_result(sha1_s *s);
63
85
 
64
86
  /**
65
87
  An SHA1 helper function that performs initialiation, writing and finalizing.
66
88
  */
67
- static inline UNUSED_FUNC char *bscrypt_sha1(sha1_s *s, const void *data,
68
- size_t len) {
69
- *s = bscrypt_sha1_init();
70
- bscrypt_sha1_write(s, data, len);
71
- return bscrypt_sha1_result(s);
89
+ static inline UNUSED_FUNC char *fio_sha1(sha1_s *s, const void *data,
90
+ size_t len) {
91
+ *s = fio_sha1_init();
92
+ fio_sha1_write(s, data, len);
93
+ return fio_sha1_result(s);
72
94
  }
73
95
 
74
96
  #if defined(DEBUG) && DEBUG == 1
75
- void bscrypt_test_sha1(void);
97
+ void fio_sha1_test(void);
76
98
  #endif
77
99
 
78
100
  /* *****************************************************************************
@@ -8,7 +8,21 @@ Feel free to copy, use and enjoy in accordance with to the license(s).
8
8
  #ifndef _GNU_SOURCE
9
9
  #define _GNU_SOURCE
10
10
  #endif
11
- #include "sha2.h"
11
+ #include "fio_sha2.h"
12
+
13
+ #include <stdio.h>
14
+ #include <string.h>
15
+
16
+ #ifndef __has_include
17
+ #define __has_include(x) 0
18
+ #endif
19
+
20
+ /* include intrinsics if supported */
21
+ #if __has_include(<x86intrin.h>)
22
+ #include <x86intrin.h>
23
+ #define HAVE_X86Intrin
24
+ #endif
25
+
12
26
  /*****************************************************************************
13
27
  Useful Macros
14
28
  */
@@ -50,6 +64,7 @@ Useful Macros
50
64
  (((i)&0xFF000000000000ULL) >> 40) | \
51
65
  (((i)&0xFF00000000000000ULL) >> 56); \
52
66
  } while (0);
67
+ #ifdef __SIZEOF_INT128__
53
68
  /** get the byte swap value of a 128 bit ...??? */
54
69
  #define gbswap128(c) \
55
70
  (((*((__uint128_t *)(c))) & 0xFFULL) << 120) | \
@@ -67,6 +82,7 @@ Useful Macros
67
82
  (((*((__uint128_t *)(c))) & 0xFF000000000000000000000000ULL) >> 72) | \
68
83
  (((*((__uint128_t *)(c))) & 0xFF00000000000000000000000000ULL) >> 88) | \
69
84
  (((*((__uint128_t *)(c))) & 0xFF0000000000000000000000000000ULL) >> 104)
85
+ #endif
70
86
 
71
87
  #ifdef HAVE_X86Intrin
72
88
  #undef bswap64
@@ -373,7 +389,7 @@ apply. The following are valid options (see the sha2_variant enum):
373
389
  - SHA_224
374
390
 
375
391
  */
376
- sha2_s bscrypt_sha2_init(sha2_variant variant) {
392
+ sha2_s fio_sha2_init(sha2_variant variant) {
377
393
  if (variant == SHA_256) {
378
394
  return (sha2_s){
379
395
  .type = SHA_256,
@@ -447,14 +463,14 @@ sha2_s bscrypt_sha2_init(sha2_variant variant) {
447
463
  .digest.i64[7] = 0x0eb72ddc81c52ca2,
448
464
  };
449
465
  }
450
- fprintf(stderr, "bscrypt SHA2 ERROR - variant unknown\n");
466
+ fprintf(stderr, "SHA2 ERROR - variant unknown\n");
451
467
  exit(2);
452
468
  }
453
469
 
454
470
  /**
455
471
  Writes data to the SHA-2 buffer.
456
472
  */
457
- void bscrypt_sha2_write(sha2_s *s, const void *data, size_t len) {
473
+ void fio_sha2_write(sha2_s *s, const void *data, size_t len) {
458
474
  size_t in_buffer;
459
475
  size_t partial;
460
476
  if (s->type & 1) { /* 512 type derived */
@@ -525,7 +541,7 @@ Finalizes the SHA-2 hash, returning the Hashed data.
525
541
  `sha2_result` can be called for the same object multiple times, but the
526
542
  finalization will only be performed the first time this function is called.
527
543
  */
528
- char *bscrypt_sha2_result(sha2_s *s) {
544
+ char *fio_sha2_result(sha2_s *s) {
529
545
  if (s->type & 1) {
530
546
  /* 512 bits derived hashing */
531
547
 
@@ -648,6 +664,7 @@ char *bscrypt_sha2_result(sha2_s *s) {
648
664
  SHA-2 testing
649
665
  */
650
666
  #if defined(DEBUG) && DEBUG == 1
667
+ #include <time.h>
651
668
 
652
669
  // SHA_512 = 1, SHA_512_256 = 3, SHA_512_224 = 5, SHA_384 = 7, SHA_256 = 2,
653
670
  // SHA_224 = 4,
@@ -663,96 +680,96 @@ static char *sha2_variant_names[] = {
663
680
  #endif
664
681
  // clang-format on
665
682
 
666
- void bscrypt_test_sha2(void) {
683
+ void fio_sha2_test(void) {
667
684
  sha2_s s;
668
- char *expect = NULL;
669
- char *got = NULL;
685
+ char *expect;
686
+ char *got;
670
687
  char *str = "";
671
688
  fprintf(stderr, "===================================\n");
672
- fprintf(stderr, "bscrypt SHA-2 struct size: %lu\n", sizeof(sha2_s));
673
- fprintf(stderr, "+ bscrypt");
689
+ fprintf(stderr, "fio SHA-2 struct size: %zu\n", sizeof(sha2_s));
690
+ fprintf(stderr, "+ fio");
674
691
  // start tests
675
- s = bscrypt_sha2_init(SHA_224);
676
- bscrypt_sha2_write(&s, str, 0);
692
+ s = fio_sha2_init(SHA_224);
693
+ fio_sha2_write(&s, str, 0);
677
694
  expect = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34\xc4"
678
695
  "\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
679
- got = bscrypt_sha2_result(&s);
696
+ got = fio_sha2_result(&s);
680
697
  if (strcmp(expect, got))
681
698
  goto error;
682
699
 
683
- s = bscrypt_sha2_init(SHA_256);
684
- bscrypt_sha2_write(&s, str, 0);
700
+ s = fio_sha2_init(SHA_256);
701
+ fio_sha2_write(&s, str, 0);
685
702
  expect =
686
703
  "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24\x27"
687
704
  "\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55";
688
- got = bscrypt_sha2_result(&s);
705
+ got = fio_sha2_result(&s);
689
706
  if (strcmp(expect, got))
690
707
  goto error;
691
708
 
692
- s = bscrypt_sha2_init(SHA_384);
693
- bscrypt_sha2_write(&s, str, 0);
709
+ s = fio_sha2_init(SHA_384);
710
+ fio_sha2_write(&s, str, 0);
694
711
  expect = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e"
695
712
  "\xb1\xb1\xe3\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c"
696
713
  "\xc7\xbf\x63\xf6\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb"
697
714
  "\xd5\x1a\xd2\xf1\x48\x98\xb9\x5b";
698
- got = bscrypt_sha2_result(&s);
715
+ got = fio_sha2_result(&s);
699
716
  if (strcmp(expect, got))
700
717
  goto error;
701
718
 
702
- s = bscrypt_sha2_init(SHA_512);
703
- bscrypt_sha2_write(&s, str, 0);
719
+ s = fio_sha2_init(SHA_512);
720
+ fio_sha2_write(&s, str, 0);
704
721
  expect = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d"
705
722
  "\x80\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21"
706
723
  "\xd3\x6c\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83"
707
724
  "\x18\xd2\x87\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81"
708
725
  "\xa5\x38\x32\x7a\xf9\x27\xda\x3e";
709
- got = bscrypt_sha2_result(&s);
726
+ got = fio_sha2_result(&s);
710
727
  if (strcmp(expect, got))
711
728
  goto error;
712
729
 
713
- s = bscrypt_sha2_init(SHA_512_224);
714
- bscrypt_sha2_write(&s, str, 0);
730
+ s = fio_sha2_init(SHA_512_224);
731
+ fio_sha2_write(&s, str, 0);
715
732
  expect = "\x6e\xd0\xdd\x02\x80\x6f\xa8\x9e\x25\xde\x06\x0c\x19\xd3"
716
733
  "\xac\x86\xca\xbb\x87\xd6\xa0\xdd\xd0\x5c\x33\x3b\x84\xf4";
717
- got = bscrypt_sha2_result(&s);
734
+ got = fio_sha2_result(&s);
718
735
  if (strcmp(expect, got))
719
736
  goto error;
720
737
 
721
- s = bscrypt_sha2_init(SHA_512_256);
722
- bscrypt_sha2_write(&s, str, 0);
738
+ s = fio_sha2_init(SHA_512_256);
739
+ fio_sha2_write(&s, str, 0);
723
740
  expect = "\xc6\x72\xb8\xd1\xef\x56\xed\x28\xab\x87\xc3\x62\x2c\x51\x14\x06"
724
741
  "\x9b\xdd\x3a\xd7\xb8\xf9\x73\x74\x98\xd0\xc0\x1e\xce\xf0\x96\x7a";
725
- got = bscrypt_sha2_result(&s);
742
+ got = fio_sha2_result(&s);
726
743
  if (strcmp(expect, got))
727
744
  goto error;
728
745
 
729
- s = bscrypt_sha2_init(SHA_512);
746
+ s = fio_sha2_init(SHA_512);
730
747
  str = "god is a rotten tomato";
731
- bscrypt_sha2_write(&s, str, strlen(str));
748
+ fio_sha2_write(&s, str, strlen(str));
732
749
  expect = "\x61\x97\x4d\x41\x9f\x77\x45\x21\x09\x4e\x95\xa3\xcb\x4d\xe4\x79"
733
750
  "\x26\x32\x2f\x2b\xe2\x62\x64\x5a\xb4\x5d\x3f\x73\x69\xef\x46\x20"
734
751
  "\xb2\xd3\xce\xda\xa9\xc2\x2c\xac\xe3\xf9\x02\xb2\x20\x5d\x2e\xfd"
735
752
  "\x40\xca\xa0\xc1\x67\xe0\xdc\xdf\x60\x04\x3e\x4e\x76\x87\x82\x74";
736
- got = bscrypt_sha2_result(&s);
753
+ got = fio_sha2_result(&s);
737
754
  if (strcmp(expect, got))
738
755
  goto error;
739
756
 
740
- // s = bscrypt_sha2_init(SHA_256);
757
+ // s = fio_sha2_init(SHA_256);
741
758
  // str = "The quick brown fox jumps over the lazy dog";
742
- // bscrypt_sha2_write(&s, str, strlen(str));
759
+ // fio_sha2_write(&s, str, strlen(str));
743
760
  // expect =
744
761
  // "\xd7\xa8\xfb\xb3\x07\xd7\x80\x94\x69\xca\x9a\xbc\xb0\x08\x2e\x4f"
745
762
  // "\x8d\x56\x51\xe4\x6d\x3c\xdb\x76\x2d\x02\xd0\xbf\x37\xc9\xe5\x92";
746
- // got = bscrypt_sha2_result(&s);
763
+ // got = fio_sha2_result(&s);
747
764
  // if (strcmp(expect, got))
748
765
  // goto error;
749
766
 
750
- s = bscrypt_sha2_init(SHA_224);
767
+ s = fio_sha2_init(SHA_224);
751
768
  str = "The quick brown fox jumps over the lazy dog";
752
- bscrypt_sha2_write(&s, str, strlen(str));
769
+ fio_sha2_write(&s, str, strlen(str));
753
770
  expect = "\x73\x0e\x10\x9b\xd7\xa8\xa3\x2b\x1c\xb9\xd9\xa0\x9a\xa2"
754
771
  "\x32\x5d\x24\x30\x58\x7d\xdb\xc0\xc3\x8b\xad\x91\x15\x25";
755
- got = bscrypt_sha2_result(&s);
772
+ got = fio_sha2_result(&s);
756
773
  if (strcmp(expect, got))
757
774
  goto error;
758
775
 
@@ -760,9 +777,9 @@ void bscrypt_test_sha2(void) {
760
777
 
761
778
  #ifdef HAVE_OPENSSL
762
779
  fprintf(stderr, "===================================\n");
763
- fprintf(stderr, "bscrypt SHA-2 struct size: %lu\n", sizeof(sha2_s));
764
- fprintf(stderr, "OpenSSL SHA-2/256 struct size: %lu\n", sizeof(SHA256_CTX));
765
- fprintf(stderr, "OpenSSL SHA-2/512 struct size: %lu\n", sizeof(SHA512_CTX));
780
+ fprintf(stderr, "fio SHA-2 struct size: %zu\n", sizeof(sha2_s));
781
+ fprintf(stderr, "OpenSSL SHA-2/256 struct size: %zu\n", sizeof(SHA256_CTX));
782
+ fprintf(stderr, "OpenSSL SHA-2/512 struct size: %zu\n", sizeof(SHA512_CTX));
766
783
  fprintf(stderr, "===================================\n");
767
784
  SHA512_CTX s2;
768
785
  SHA256_CTX s3;
@@ -770,11 +787,11 @@ void bscrypt_test_sha2(void) {
770
787
  hash[SHA512_DIGEST_LENGTH] = 0;
771
788
  clock_t start = clock();
772
789
  for (size_t i = 0; i < 100000; i++) {
773
- s = bscrypt_sha2_init(SHA_512);
774
- bscrypt_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
775
- bscrypt_sha2_result(&s);
790
+ s = fio_sha2_init(SHA_512);
791
+ fio_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
792
+ fio_sha2_result(&s);
776
793
  }
777
- fprintf(stderr, "bscrypt 100K SHA-2/512: %lf\n",
794
+ fprintf(stderr, "fio 100K SHA-2/512: %lf\n",
778
795
  (double)(clock() - start) / CLOCKS_PER_SEC);
779
796
 
780
797
  start = clock();
@@ -788,11 +805,11 @@ void bscrypt_test_sha2(void) {
788
805
 
789
806
  start = clock();
790
807
  for (size_t i = 0; i < 100000; i++) {
791
- s = bscrypt_sha2_init(SHA_256);
792
- bscrypt_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
793
- bscrypt_sha2_result(&s);
808
+ s = fio_sha2_init(SHA_256);
809
+ fio_sha2_write(&s, "The quick brown fox jumps over the lazy dog", 43);
810
+ fio_sha2_result(&s);
794
811
  }
795
- fprintf(stderr, "bscrypt 100K SHA-2/256: %lf\n",
812
+ fprintf(stderr, "fio 100K SHA-2/256: %lf\n",
796
813
  (double)(clock() - start) / CLOCKS_PER_SEC);
797
814
 
798
815
  hash[SHA256_DIGEST_LENGTH] = 0;
@@ -812,7 +829,7 @@ void bscrypt_test_sha2(void) {
812
829
 
813
830
  error:
814
831
  fprintf(stderr,
815
- ":\n--- bscrypt SHA-2 Test FAILED!\ntype: "
832
+ ":\n--- fio SHA-2 Test FAILED!\ntype: "
816
833
  "%s (%d)\nstring %s\nexpected:\n",
817
834
  sha2_variant_names[s.type], s.type, str);
818
835
  while (*expect)
@@ -5,9 +5,32 @@ of), which might be subject to their own licenses.
5
5
 
6
6
  Feel free to copy, use and enjoy in accordance with to the license(s).
7
7
  */
8
- #ifndef bscrypt_SHA2_H
9
- #define bscrypt_SHA2_H
10
- #include "bscrypt-common.h"
8
+ #ifndef H_FIO_SHA2_H
9
+ #define H_FIO_SHA2_H
10
+
11
+ // clang-format off
12
+ #if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
13
+ # if defined(__has_include)
14
+ # if __has_include(<endian.h>)
15
+ # include <endian.h>
16
+ # elif __has_include(<sys/endian.h>)
17
+ # include <sys/endian.h>
18
+ # endif
19
+ # endif
20
+ # if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__) && \
21
+ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
22
+ # define __BIG_ENDIAN__
23
+ # endif
24
+ #endif
25
+
26
+ #ifndef UNUSED_FUNC
27
+ # define UNUSED_FUNC __attribute__((unused))
28
+ #endif
29
+ // clang-format on
30
+
31
+ #include <stdlib.h>
32
+ #include <stdint.h>
33
+
11
34
  /* *****************************************************************************
12
35
  C++ extern
13
36
  */
@@ -45,15 +68,23 @@ Use, for example:
45
68
 
46
69
  #include "mini-crypt.h"
47
70
  sha2_s sha2;
48
- bscrypt.sha2_init(&sha2, SHA_512);
49
- bscrypt.sha2_write(&sha2,
71
+ fio_sha2_init(&sha2, SHA_512);
72
+ fio_sha2_write(&sha2,
50
73
  "The quick brown fox jumps over the lazy dog", 43);
51
- char *hashed_result = bscrypt.sha2_result(&sha2);
74
+ char *hashed_result = fio_sha2_result(&sha2);
52
75
 
53
76
  */
54
77
  typedef struct {
55
78
  /* notice: we're counting bits, not bytes. max length: 2^128 bits */
56
- bits128_u length;
79
+ union {
80
+ uint8_t bytes[16];
81
+ uint8_t matrix[4][4];
82
+ uint32_t words_small[4];
83
+ uint64_t words[2];
84
+ #if defined(__SIZEOF_INT128__)
85
+ __uint128_t i;
86
+ #endif
87
+ } length;
57
88
  uint8_t buffer[128];
58
89
  union {
59
90
  uint32_t i32[16];
@@ -78,54 +109,54 @@ apply. The following are valid options (see the sha2_variant enum):
78
109
  - SHA_224
79
110
 
80
111
  */
81
- sha2_s bscrypt_sha2_init(sha2_variant variant);
112
+ sha2_s fio_sha2_init(sha2_variant variant);
82
113
  /**
83
114
  Writes data to the SHA-2 buffer.
84
115
  */
85
- void bscrypt_sha2_write(sha2_s *s, const void *data, size_t len);
116
+ void fio_sha2_write(sha2_s *s, const void *data, size_t len);
86
117
  /**
87
118
  Finalizes the SHA-2 hash, returning the Hashed data.
88
119
 
89
120
  `sha2_result` can be called for the same object multiple times, but the
90
121
  finalization will only be performed the first time this function is called.
91
122
  */
92
- char *bscrypt_sha2_result(sha2_s *s);
123
+ char *fio_sha2_result(sha2_s *s);
93
124
 
94
125
  /**
95
126
  An SHA2 helper function that performs initialiation, writing and finalizing.
96
127
  Uses the SHA2 512 variant.
97
128
  */
98
- static inline UNUSED_FUNC char *bscrypt_sha2_512(sha2_s *s, const void *data,
99
- size_t len) {
100
- *s = bscrypt_sha2_init(SHA_512);
101
- bscrypt_sha2_write(s, data, len);
102
- return bscrypt_sha2_result(s);
129
+ static inline UNUSED_FUNC char *fio_sha2_512(sha2_s *s, const void *data,
130
+ size_t len) {
131
+ *s = fio_sha2_init(SHA_512);
132
+ fio_sha2_write(s, data, len);
133
+ return fio_sha2_result(s);
103
134
  }
104
135
 
105
136
  /**
106
137
  An SHA2 helper function that performs initialiation, writing and finalizing.
107
138
  Uses the SHA2 256 variant.
108
139
  */
109
- static inline UNUSED_FUNC char *bscrypt_sha2_256(sha2_s *s, const void *data,
110
- size_t len) {
111
- *s = bscrypt_sha2_init(SHA_256);
112
- bscrypt_sha2_write(s, data, len);
113
- return bscrypt_sha2_result(s);
140
+ static inline UNUSED_FUNC char *fio_sha2_256(sha2_s *s, const void *data,
141
+ size_t len) {
142
+ *s = fio_sha2_init(SHA_256);
143
+ fio_sha2_write(s, data, len);
144
+ return fio_sha2_result(s);
114
145
  }
115
146
 
116
147
  /**
117
148
  An SHA2 helper function that performs initialiation, writing and finalizing.
118
149
  Uses the SHA2 384 variant.
119
150
  */
120
- static inline UNUSED_FUNC char *bscrypt_sha2_384(sha2_s *s, const void *data,
121
- size_t len) {
122
- *s = bscrypt_sha2_init(SHA_384);
123
- bscrypt_sha2_write(s, data, len);
124
- return bscrypt_sha2_result(s);
151
+ static inline UNUSED_FUNC char *fio_sha2_384(sha2_s *s, const void *data,
152
+ size_t len) {
153
+ *s = fio_sha2_init(SHA_384);
154
+ fio_sha2_write(s, data, len);
155
+ return fio_sha2_result(s);
125
156
  }
126
157
 
127
158
  #if defined(DEBUG) && DEBUG == 1
128
- void bscrypt_test_sha2(void);
159
+ void fio_sha2_test(void);
129
160
  #endif
130
161
 
131
162
  /* *****************************************************************************