bare-buffer 2.5.8 → 2.5.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CMakeLists.txt CHANGED
@@ -26,6 +26,9 @@ target_sources(
26
26
 
27
27
  target_link_libraries(
28
28
  ${bare_buffer}
29
+ PRIVATE
30
+ $<TARGET_OBJECTS:base64>
31
+ $<TARGET_OBJECTS:hex>
29
32
  PUBLIC
30
33
  base64
31
34
  hex
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "2.5.8",
3
+ "version": "2.5.10",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -1,6 +1,7 @@
1
1
  #ifndef BASE64_H
2
2
  #define BASE64_H
3
3
 
4
+ #include <stdbool.h>
4
5
  #include <stddef.h>
5
6
  #include <stdint.h>
6
7
  #include <utf.h>
@@ -9,11 +10,116 @@
9
10
  extern "C" {
10
11
  #endif
11
12
 
12
- int
13
- base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
13
+ static const char base64__alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14
14
 
15
- int
16
- base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
15
+ static const char base64__inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
16
+
17
+ static inline int
18
+ base64__encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
19
+ size_t len = 4 * ((buffer_len + 2) / 3);
20
+
21
+ if (string == NULL) {
22
+ *string_len = len;
23
+ return 0;
24
+ }
25
+
26
+ if (*string_len < len) return -1;
27
+
28
+ bool terminate = *string_len > len;
29
+
30
+ *string_len = len;
31
+
32
+ size_t k = 0, i = 0, n = buffer_len;
33
+
34
+ uint8_t a, b, c;
35
+
36
+ for (; i + 2 < n; i += 3) {
37
+ a = buffer[i];
38
+ b = buffer[i + 1];
39
+ c = buffer[i + 2];
40
+
41
+ string[k++] = base64__alphabet[a >> 2];
42
+ string[k++] = base64__alphabet[((a & 0x03) << 4) | (b >> 4)];
43
+ string[k++] = base64__alphabet[((b & 0x0f) << 2) | (c >> 6)];
44
+ string[k++] = base64__alphabet[c & 0x3f];
45
+ }
46
+
47
+ if (i < n) {
48
+ a = buffer[i];
49
+
50
+ string[k++] = base64__alphabet[a >> 2];
51
+
52
+ if (i + 1 < n) {
53
+ b = buffer[i + 1];
54
+
55
+ string[k++] = base64__alphabet[((a & 0x03) << 4) | (b >> 4)];
56
+ string[k++] = base64__alphabet[(b & 0x0f) << 2];
57
+ } else {
58
+ string[k++] = base64__alphabet[(a & 0x03) << 4];
59
+ string[k++] = '=';
60
+ }
61
+
62
+ string[k++] = '=';
63
+ }
64
+
65
+ if (terminate) string[k] = '\0';
66
+
67
+ return 0;
68
+ }
69
+
70
+ static inline int
71
+ base64__decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
72
+ if (string_len % 4 != 0) return -1;
73
+
74
+ if (string_len == 0) {
75
+ *buffer_len = 0;
76
+ return 0;
77
+ }
78
+
79
+ size_t len = string_len / 4 * 3;
80
+
81
+ if (string[string_len - 1] == '=') len--;
82
+ if (string[string_len - 2] == '=') len--;
83
+
84
+ if (buffer == NULL) {
85
+ *buffer_len = len;
86
+ return 0;
87
+ }
88
+
89
+ if (*buffer_len < len) return -1;
90
+
91
+ *buffer_len = len;
92
+
93
+ size_t k = 0;
94
+
95
+ for (size_t i = 0, n = string_len; i < n; i += 4) {
96
+ char chunk[4];
97
+
98
+ for (size_t j = 0; j < 4; j++) {
99
+ chunk[j] = string[i + j] == '=' ? 0 : base64__inverse_alphabet[string[i + j]];
100
+
101
+ if (chunk[j] == (char) -1) return -1;
102
+ }
103
+
104
+ uint32_t triple = (chunk[0] << 3 * 6) + (chunk[1] << 2 * 6) + (chunk[2] << 1 * 6) + (chunk[3] << 0 * 6);
105
+
106
+ if (k < len) buffer[k++] = (triple >> 2 * 8) & 0xFF;
107
+ if (k < len) buffer[k++] = (triple >> 1 * 8) & 0xFF;
108
+ if (k < len) buffer[k++] = (triple >> 0 * 8) & 0xFF;
109
+ }
110
+
111
+ return 0;
112
+ }
113
+
114
+ inline int
115
+ base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
116
+ return base64__encode(buffer, buffer_len, string, string_len);
117
+ }
118
+
119
+ inline int
120
+ base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
121
+ return base64__decode(string, string_len, buffer, buffer_len);
122
+ }
17
123
 
18
124
  #ifdef __cplusplus
19
125
  }
@@ -1,107 +1,10 @@
1
- #include <stdbool.h>
2
- #include <stddef.h>
3
1
  #include <stdint.h>
4
2
  #include <utf.h>
5
3
 
6
4
  #include "../include/base64.h"
7
5
 
8
- static const char base64_alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
6
+ extern int
7
+ base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
9
8
 
10
- static const char base64_inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
11
-
12
- int
13
- base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
14
- size_t len = 4 * ((buffer_len + 2) / 3);
15
-
16
- if (string == NULL) {
17
- *string_len = len;
18
- return 0;
19
- }
20
-
21
- if (*string_len < len) return -1;
22
-
23
- bool terminate = *string_len > len;
24
-
25
- *string_len = len;
26
-
27
- size_t k = 0, i = 0, n = buffer_len;
28
-
29
- uint8_t a, b, c;
30
-
31
- for (; i + 2 < n; i += 3) {
32
- a = buffer[i];
33
- b = buffer[i + 1];
34
- c = buffer[i + 2];
35
-
36
- string[k++] = base64_alphabet[a >> 2];
37
- string[k++] = base64_alphabet[((a & 0x03) << 4) | (b >> 4)];
38
- string[k++] = base64_alphabet[((b & 0x0f) << 2) | (c >> 6)];
39
- string[k++] = base64_alphabet[c & 0x3f];
40
- }
41
-
42
- if (i < n) {
43
- a = buffer[i];
44
-
45
- string[k++] = base64_alphabet[a >> 2];
46
-
47
- if (i + 1 < n) {
48
- b = buffer[i + 1];
49
-
50
- string[k++] = base64_alphabet[((a & 0x03) << 4) | (b >> 4)];
51
- string[k++] = base64_alphabet[(b & 0x0f) << 2];
52
- } else {
53
- string[k++] = base64_alphabet[(a & 0x03) << 4];
54
- string[k++] = '=';
55
- }
56
-
57
- string[k++] = '=';
58
- }
59
-
60
- if (terminate) string[k] = '\0';
61
-
62
- return 0;
63
- }
64
-
65
- int
66
- base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
67
- if (string_len % 4 != 0) return -1;
68
-
69
- if (string_len == 0) {
70
- *buffer_len = 0;
71
- return 0;
72
- }
73
-
74
- size_t len = string_len / 4 * 3;
75
-
76
- if (string[string_len - 1] == '=') len--;
77
- if (string[string_len - 2] == '=') len--;
78
-
79
- if (buffer == NULL) {
80
- *buffer_len = len;
81
- return 0;
82
- }
83
-
84
- if (*buffer_len < len) return -1;
85
-
86
- *buffer_len = len;
87
-
88
- size_t k = 0;
89
-
90
- for (size_t i = 0, n = string_len; i < n; i += 4) {
91
- char chunk[4];
92
-
93
- for (size_t j = 0; j < 4; j++) {
94
- chunk[j] = string[i + j] == '=' ? 0 : base64_inverse_alphabet[string[i + j]];
95
-
96
- if (chunk[j] == (char) -1) return -1;
97
- }
98
-
99
- uint32_t triple = (chunk[0] << 3 * 6) + (chunk[1] << 2 * 6) + (chunk[2] << 1 * 6) + (chunk[3] << 0 * 6);
100
-
101
- if (k < len) buffer[k++] = (triple >> 2 * 8) & 0xFF;
102
- if (k < len) buffer[k++] = (triple >> 1 * 8) & 0xFF;
103
- if (k < len) buffer[k++] = (triple >> 0 * 8) & 0xFF;
104
- }
105
-
106
- return 0;
107
- }
9
+ extern int
10
+ base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
@@ -1,6 +1,7 @@
1
1
  #ifndef HEX_H
2
2
  #define HEX_H
3
3
 
4
+ #include <stdbool.h>
4
5
  #include <stddef.h>
5
6
  #include <stdint.h>
6
7
  #include <utf.h>
@@ -9,11 +10,76 @@
9
10
  extern "C" {
10
11
  #endif
11
12
 
12
- int
13
- hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
13
+ static const char hex__alphabet[16] = "0123456789abcdef";
14
14
 
15
- int
16
- hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
15
+ static const char hex__inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
16
+
17
+ static inline int
18
+ hex__encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
19
+ size_t len = buffer_len * 2;
20
+
21
+ if (string == NULL) {
22
+ *string_len = len;
23
+ return 0;
24
+ }
25
+
26
+ if (*string_len < len) return -1;
27
+
28
+ bool terminate = *string_len > len;
29
+
30
+ *string_len = len;
31
+
32
+ size_t k = 0;
33
+
34
+ for (size_t i = 0, n = buffer_len; i < n; i++) {
35
+ string[k++] = hex__alphabet[buffer[i] >> 4];
36
+ string[k++] = hex__alphabet[buffer[i] & 0x0f];
37
+ }
38
+
39
+ if (terminate) string[k] = '\0';
40
+
41
+ return 0;
42
+ }
43
+
44
+ static inline int
45
+ hex__decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
46
+ size_t len = string_len >> 1;
47
+
48
+ if (buffer == NULL) {
49
+ *buffer_len = len;
50
+ return 0;
51
+ }
52
+
53
+ if (*buffer_len < len) return -1;
54
+
55
+ *buffer_len = len;
56
+
57
+ size_t k = 0;
58
+
59
+ for (size_t i = 0, n = string_len; i < n; i += 2) {
60
+ char chunk[2];
61
+
62
+ for (size_t j = 0; j < 2; j++) {
63
+ chunk[j] = i + j < n ? hex__inverse_alphabet[string[i + j]] : 0;
64
+
65
+ if (chunk[j] == (char) -1) return -1;
66
+ }
67
+
68
+ buffer[k++] = (chunk[0] << 4) | chunk[1];
69
+ }
70
+
71
+ return 0;
72
+ }
73
+
74
+ inline int
75
+ hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
76
+ return hex__encode(buffer, buffer_len, string, string_len);
77
+ }
78
+
79
+ inline int
80
+ hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
81
+ return hex__decode(string, string_len, buffer, buffer_len);
82
+ }
17
83
 
18
84
  #ifdef __cplusplus
19
85
  }
@@ -1,67 +1,10 @@
1
- #include <stdbool.h>
2
- #include <stddef.h>
3
1
  #include <stdint.h>
4
2
  #include <utf.h>
5
3
 
6
4
  #include "../include/hex.h"
7
5
 
8
- static const char hex_alphabet[16] = "0123456789abcdef";
6
+ extern int
7
+ hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
9
8
 
10
- static const char hex_inverse_alphabet[256] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
11
-
12
- int
13
- hex_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len) {
14
- size_t len = buffer_len * 2;
15
-
16
- if (string == NULL) {
17
- *string_len = len;
18
- return 0;
19
- }
20
-
21
- if (*string_len < len) return -1;
22
-
23
- bool terminate = *string_len > len;
24
-
25
- *string_len = len;
26
-
27
- size_t k = 0;
28
-
29
- for (size_t i = 0, n = buffer_len; i < n; i++) {
30
- string[k++] = hex_alphabet[buffer[i] >> 4];
31
- string[k++] = hex_alphabet[buffer[i] & 0x0f];
32
- }
33
-
34
- if (terminate) string[k] = '\0';
35
-
36
- return 0;
37
- }
38
-
39
- int
40
- hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len) {
41
- size_t len = string_len >> 1;
42
-
43
- if (buffer == NULL) {
44
- *buffer_len = len;
45
- return 0;
46
- }
47
-
48
- if (*buffer_len < len) return -1;
49
-
50
- *buffer_len = len;
51
-
52
- size_t k = 0;
53
-
54
- for (size_t i = 0, n = string_len; i < n; i += 2) {
55
- char chunk[2];
56
-
57
- for (size_t j = 0; j < 2; j++) {
58
- chunk[j] = i + j < n ? hex_inverse_alphabet[string[i + j]] : 0;
59
-
60
- if (chunk[j] == (char) -1) return -1;
61
- }
62
-
63
- buffer[k++] = (chunk[0] << 4) | chunk[1];
64
- }
65
-
66
- return 0;
67
- }
9
+ extern int
10
+ hex_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);