bare-buffer 2.5.10 → 2.6.0

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.
Files changed (57) hide show
  1. package/CMakeLists.txt +6 -11
  2. package/binding.c +1 -11
  3. package/index.js +71 -136
  4. package/package.json +3 -8
  5. package/prebuilds/android-arm/bare-buffer.bare +0 -0
  6. package/prebuilds/android-arm64/bare-buffer.bare +0 -0
  7. package/prebuilds/android-ia32/bare-buffer.bare +0 -0
  8. package/prebuilds/android-x64/bare-buffer.bare +0 -0
  9. package/prebuilds/darwin-arm64/bare-buffer.bare +0 -0
  10. package/prebuilds/darwin-x64/bare-buffer.bare +0 -0
  11. package/prebuilds/ios-arm64/bare-buffer.bare +0 -0
  12. package/prebuilds/ios-arm64-simulator/bare-buffer.bare +0 -0
  13. package/prebuilds/ios-x64-simulator/bare-buffer.bare +0 -0
  14. package/prebuilds/linux-arm64/bare-buffer.bare +0 -0
  15. package/prebuilds/linux-x64/bare-buffer.bare +0 -0
  16. package/prebuilds/win32-arm64/bare-buffer.bare +0 -0
  17. package/prebuilds/win32-x64/bare-buffer.bare +0 -0
  18. package/vendor/libbase64/CMakeLists.txt +0 -70
  19. package/vendor/libbase64/include/base64.h +0 -128
  20. package/vendor/libbase64/src/base64.c +0 -10
  21. package/vendor/libhex/CMakeLists.txt +0 -71
  22. package/vendor/libhex/include/hex.h +0 -88
  23. package/vendor/libhex/src/hex.c +0 -10
  24. package/vendor/libutf/CMakeLists.txt +0 -93
  25. package/vendor/libutf/include/utf/string.h +0 -786
  26. package/vendor/libutf/include/utf.h +0 -132
  27. package/vendor/libutf/src/ascii/validate.c +0 -47
  28. package/vendor/libutf/src/endianness.c +0 -19
  29. package/vendor/libutf/src/endianness.h +0 -54
  30. package/vendor/libutf/src/latin1/convert-to-utf16.c +0 -37
  31. package/vendor/libutf/src/latin1/convert-to-utf32.c +0 -34
  32. package/vendor/libutf/src/latin1/convert-to-utf8.c +0 -58
  33. package/vendor/libutf/src/latin1/length-from-utf16.c +0 -26
  34. package/vendor/libutf/src/latin1/length-from-utf32.c +0 -26
  35. package/vendor/libutf/src/latin1/length-from-utf8.c +0 -34
  36. package/vendor/libutf/src/utf16/convert-to-latin1.c +0 -41
  37. package/vendor/libutf/src/utf16/convert-to-utf32.c +0 -56
  38. package/vendor/libutf/src/utf16/convert-to-utf8.c +0 -75
  39. package/vendor/libutf/src/utf16/length-from-latin1.c +0 -26
  40. package/vendor/libutf/src/utf16/length-from-utf32.c +0 -33
  41. package/vendor/libutf/src/utf16/length-from-utf8.c +0 -38
  42. package/vendor/libutf/src/utf16/validate.c +0 -53
  43. package/vendor/libutf/src/utf32/convert-to-latin1.c +0 -40
  44. package/vendor/libutf/src/utf32/convert-to-utf16.c +0 -56
  45. package/vendor/libutf/src/utf32/convert-to-utf8.c +0 -71
  46. package/vendor/libutf/src/utf32/length-from-latin1.c +0 -26
  47. package/vendor/libutf/src/utf32/length-from-utf16.c +0 -35
  48. package/vendor/libutf/src/utf32/length-from-utf8.c +0 -35
  49. package/vendor/libutf/src/utf32/validate.c +0 -39
  50. package/vendor/libutf/src/utf8/convert-to-latin1.c +0 -70
  51. package/vendor/libutf/src/utf8/convert-to-utf16.c +0 -95
  52. package/vendor/libutf/src/utf8/convert-to-utf32.c +0 -110
  53. package/vendor/libutf/src/utf8/length-from-latin1.c +0 -32
  54. package/vendor/libutf/src/utf8/length-from-utf16.c +0 -44
  55. package/vendor/libutf/src/utf8/length-from-utf32.c +0 -35
  56. package/vendor/libutf/src/utf8/string.c +0 -149
  57. package/vendor/libutf/src/utf8/validate.c +0 -107
package/CMakeLists.txt CHANGED
@@ -1,20 +1,13 @@
1
1
  cmake_minimum_required(VERSION 3.25)
2
2
 
3
3
  find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
+ find_package(cmake-fetch REQUIRED PATHS node_modules/cmake-fetch)
4
5
 
5
6
  project(bare_buffer C)
6
7
 
7
- if(NOT TARGET utf)
8
- add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
9
- endif()
10
-
11
- if(NOT TARGET base64)
12
- add_subdirectory(vendor/libbase64 EXCLUDE_FROM_ALL)
13
- endif()
14
-
15
- if(NOT TARGET hex)
16
- add_subdirectory(vendor/libhex EXCLUDE_FROM_ALL)
17
- endif()
8
+ fetch_package("github:holepunchto/libutf#65b6ba0")
9
+ fetch_package("github:holepunchto/libbase64#4cd8d8b")
10
+ fetch_package("github:holepunchto/libhex#939999b")
18
11
 
19
12
  add_bare_module(bare_buffer)
20
13
 
@@ -27,9 +20,11 @@ target_sources(
27
20
  target_link_libraries(
28
21
  ${bare_buffer}
29
22
  PRIVATE
23
+ $<TARGET_OBJECTS:utf>
30
24
  $<TARGET_OBJECTS:base64>
31
25
  $<TARGET_OBJECTS:hex>
32
26
  PUBLIC
27
+ utf
33
28
  base64
34
29
  hex
35
30
  )
package/binding.c CHANGED
@@ -38,17 +38,7 @@ bare_buffer_set_zero_fill_enabled (js_env_t *env, js_callback_info_t *info) {
38
38
 
39
39
  static uint32_t
40
40
  bare_buffer_byte_length_utf8_fast (js_ffi_receiver_t *receiver, js_ffi_string_t *str) {
41
- const uint8_t *data = (const uint8_t *) str->data;
42
-
43
- uint32_t len = 0;
44
-
45
- for (uint32_t i = 0, n = str->len; i < n; i++) {
46
- // The string data is latin1 so we add an additional byte count for all
47
- // non-ASCII characters.
48
- len += 1 + (data[i] >> 7);
49
- }
50
-
51
- return len;
41
+ return utf8_length_from_latin1((const latin1_t *) str->data, str->len);
52
42
  }
53
43
 
54
44
  static js_value_t *
package/index.js CHANGED
@@ -6,11 +6,21 @@ const utf8 = require('./lib/utf8')
6
6
  const utf16le = require('./lib/utf16le')
7
7
  const binding = require('./binding')
8
8
 
9
+ let poolSize = 0
10
+
9
11
  const Buffer = module.exports = exports = class Buffer extends Uint8Array {
10
12
  static {
11
13
  binding.tag(this)
12
14
  }
13
15
 
16
+ static get poolSize () {
17
+ return poolSize
18
+ }
19
+
20
+ static set poolSize (value) {
21
+ poolSize = Math.max(0, value)
22
+ }
23
+
14
24
  [Symbol.species] () {
15
25
  return Buffer
16
26
  }
@@ -225,169 +235,83 @@ const Buffer = module.exports = exports = class Buffer extends Uint8Array {
225
235
  return codecFor(encoding).write(buffer, string)
226
236
  }
227
237
 
228
- writeUInt8 (value, offset = 0) {
229
- const view = new DataView(this.buffer, this.byteoffset, this.bytelength)
230
- view.setUint8(offset, value, true)
238
+ readBigInt64BE (offset = 0) { return viewOf(this).getBigInt64(offset, false) }
239
+ readBigInt64LE (offset = 0) { return viewOf(this).getBigInt64(offset, true) }
231
240
 
232
- return offset + 1
233
- }
241
+ readBigUint64BE (offset = 0) { return viewOf(this).getBigUint64(offset, false) }
242
+ readBigUint64LE (offset = 0) { return viewOf(this).getBigUint64(offset, true) }
234
243
 
235
- writeUint8 (...args) {
236
- return this.writeUInt8(...args)
237
- }
244
+ readDoubleBE (offset = 0) { return viewOf(this).getFloat64(offset, false) }
245
+ readDoubleLE (offset = 0) { return viewOf(this).getFloat64(offset, true) }
238
246
 
239
- writeInt8 (value, offset = 0) {
240
- const view = new DataView(this.buffer, this.byteoffset, this.bytelength)
241
- view.setInt8(offset, value)
247
+ readFloatBE (offset = 0) { return viewOf(this).getFloat32(offset, false) }
248
+ readFloatLE (offset = 0) { return viewOf(this).getFloat32(offset, true) }
242
249
 
243
- return offset + 1
244
- }
250
+ readInt8 (offset = 0) { return viewOf(this).getInt8(offset) }
245
251
 
246
- writeDoubleLE (value, offset = 0) {
247
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
248
- view.setFloat64(offset, value, true)
252
+ readInt16BE (offset = 0) { return viewOf(this).getInt16(offset, false) }
253
+ readInt16LE (offset = 0) { return viewOf(this).getInt16(offset, true) }
249
254
 
250
- return offset + 8
251
- }
255
+ readInt32BE (offset = 0) { return viewOf(this).getInt32(offset, false) }
256
+ readInt32LE (offset = 0) { return viewOf(this).getInt32(offset, true) }
252
257
 
253
- writeFloatLE (value, offset = 0) {
254
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
255
- view.setFloat32(offset, value, true)
258
+ readUint8 (offset = 0) { return viewOf(this).getUint8(offset) }
256
259
 
257
- return offset + 4
258
- }
260
+ readUint16BE (offset = 0) { return viewOf(this).getUint16(offset, false) }
261
+ readUint16LE (offset = 0) { return viewOf(this).getUint16(offset, true) }
259
262
 
260
- writeUInt16LE (value, offset = 0) {
261
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
262
- view.setUint16(offset, value, true)
263
+ readUint32BE (offset = 0) { return viewOf(this).getUint32(offset, false) }
264
+ readUint32LE (offset = 0) { return viewOf(this).getUint32(offset, true) }
263
265
 
264
- return offset + 2
265
- }
266
+ readBigUInt64BE (...args) { return this.readBigUint64BE(...args) }
267
+ readBigUInt64LE (...args) { return this.readBigUint64LE(...args) }
266
268
 
267
- writeUInt32LE (value, offset = 0) {
268
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
269
- view.setUint32(offset, value, true)
269
+ readUInt8 (...args) { return this.readUint8(...args) }
270
270
 
271
- return offset + 4
272
- }
271
+ readUInt16BE (...args) { return this.readUint16BE(...args) }
272
+ readUInt16LE (...args) { return this.readUint16LE(...args) }
273
273
 
274
- writeInt32LE (value, offset = 0) {
275
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
276
- view.setInt32(offset, value, true)
274
+ readUInt32BE (...args) { return this.readUint32BE(...args) }
275
+ readUInt32LE (...args) { return this.readUint32LE(...args) }
277
276
 
278
- return offset + 4
279
- }
277
+ writeBigInt64BE (value, offset = 0) { viewOf(this).setBigInt64(offset, value, false); return offset + 8 }
278
+ writeBigInt64LE (value, offset = 0) { viewOf(this).setBigInt64(offset, value, true); return offset + 8 }
280
279
 
281
- readInt8 (offset = 0) {
282
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
280
+ writeBigUint64BE (value, offset = 0) { viewOf(this).setBigUint64(offset, value, false); return offset + 8 }
281
+ writeBigUint64LE (value, offset = 0) { viewOf(this).setBigUint64(offset, value, true); return offset + 8 }
283
282
 
284
- return view.getInt8(offset)
285
- }
283
+ writeDoubleBE (value, offset = 0) { viewOf(this).setFloat64(offset, value, false); return offset + 8 }
284
+ writeDoubleLE (value, offset = 0) { viewOf(this).setFloat64(offset, value, true); return offset + 8 }
286
285
 
287
- readUInt8 (offset = 0) {
288
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
286
+ writeFloatBE (value, offset = 0) { viewOf(this).setFloat32(offset, value, false); return offset + 4 }
287
+ writeFloatLE (value, offset = 0) { viewOf(this).setFloat32(offset, value, true); return offset + 4 }
289
288
 
290
- return view.getUint8(offset)
291
- }
289
+ writeInt8 (value, offset = 0) { viewOf(this).setInt8(offset, value); return offset + 1 }
292
290
 
293
- readUint8 (...args) {
294
- return this.readUInt8(...args)
295
- }
296
-
297
- readDoubleLE (offset = 0) {
298
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
299
-
300
- return view.getFloat64(offset, true)
301
- }
302
-
303
- readFloatLE (offset = 0) {
304
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
305
-
306
- return view.getFloat32(offset, true)
307
- }
291
+ writeInt16BE (value, offset = 0) { viewOf(this).setInt16(offset, value, false); return offset + 2 }
292
+ writeInt16LE (value, offset = 0) { viewOf(this).setInt16(offset, value, true); return offset + 2 }
308
293
 
309
- readUInt16LE (offset = 0) {
310
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
294
+ writeInt32BE (value, offset = 0) { viewOf(this).setInt32(offset, value, false); return offset + 4 }
295
+ writeInt32LE (value, offset = 0) { viewOf(this).setInt32(offset, value, true); return offset + 4 }
311
296
 
312
- return view.getUint16(offset, true)
313
- }
297
+ writeUint8 (value, offset = 0) { viewOf(this).setUint8(offset, value, true); return offset + 1 }
314
298
 
315
- readUInt32LE (offset = 0) {
316
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
299
+ writeUint16BE (value, offset = 0) { viewOf(this).setUint16(offset, value, false); return offset + 2 }
300
+ writeUint16LE (value, offset = 0) { viewOf(this).setUint16(offset, value, true); return offset + 2 }
317
301
 
318
- return view.getUint32(offset, true)
319
- }
302
+ writeUint32LE (value, offset = 0) { viewOf(this).setUint32(offset, value, true); return offset + 4 }
303
+ writeUint32BE (value, offset = 0) { viewOf(this).setUint32(offset, value, false); return offset + 4 }
320
304
 
321
- readInt32LE (offset = 0) {
322
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
305
+ writeBigUInt64BE (...args) { return this.writeBigUint64BE(...args) }
306
+ writeBigUInt64LE (...args) { return this.writeBigUint64LE(...args) }
323
307
 
324
- return view.getInt32(offset, true)
325
- }
308
+ writeUInt8 (...args) { return this.writeUint8(...args) }
326
309
 
327
- writeDoubleBE (value, offset = 0) {
328
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
329
- view.setFloat64(offset, value, false)
310
+ writeUInt16BE (...args) { return this.writeUint16BE(...args) }
311
+ writeUInt16LE (...args) { return this.writeUint16LE(...args) }
330
312
 
331
- return offset + 8
332
- }
333
-
334
- writeFloatBE (value, offset = 0) {
335
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
336
- view.setFloat32(offset, value, false)
337
-
338
- return offset + 4
339
- }
340
-
341
- writeUInt16BE (value, offset = 0) {
342
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
343
- view.setUint16(offset, value, false)
344
-
345
- return offset + 2
346
- }
347
-
348
- writeUInt32BE (value, offset = 0) {
349
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
350
- view.setUint32(offset, value, false)
351
-
352
- return offset + 4
353
- }
354
-
355
- writeInt32BE (value, offset = 0) {
356
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
357
- view.setInt32(offset, value, false)
358
-
359
- return offset + 4
360
- }
361
-
362
- readDoubleBE (offset = 0) {
363
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
364
-
365
- return view.getFloat64(offset, false)
366
- }
367
-
368
- readFloatBE (offset = 0) {
369
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
370
-
371
- return view.getFloat32(offset, false)
372
- }
373
-
374
- readUInt16BE (offset = 0) {
375
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
376
-
377
- return view.getUint16(offset, false)
378
- }
379
-
380
- readUInt32BE (offset = 0) {
381
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
382
-
383
- return view.getUint32(offset, false)
384
- }
385
-
386
- readInt32BE (offset = 0) {
387
- const view = new DataView(this.buffer, this.byteOffset, this.byteLength)
388
-
389
- return view.getInt32(offset, false)
390
- }
313
+ writeUInt32BE (...args) { return this.writeUint32BE(...args) }
314
+ writeUInt32LE (...args) { return this.writeUint32LE(...args) }
391
315
  }
392
316
 
393
317
  exports.Buffer = exports
@@ -412,6 +336,17 @@ function codecFor (encoding = 'utf8') {
412
336
  throw new Error(`Unknown encoding: ${encoding}`)
413
337
  }
414
338
 
339
+ const views = new WeakMap()
340
+
341
+ function viewOf (buffer) {
342
+ let view = views.get(buffer)
343
+ if (view === undefined) {
344
+ view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength)
345
+ views.set(buffer, view)
346
+ }
347
+ return view
348
+ }
349
+
415
350
  exports.isBuffer = function isBuffer (value) {
416
351
  if (typeof value !== 'object' || value === null) return false
417
352
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "2.5.10",
3
+ "version": "2.6.0",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -13,13 +13,7 @@
13
13
  "binding.js",
14
14
  "CMakeLists.txt",
15
15
  "lib",
16
- "prebuilds",
17
- "vendor/libbase64/include",
18
- "vendor/libbase64/src",
19
- "vendor/libhex/include",
20
- "vendor/libhex/src",
21
- "vendor/libutf/include",
22
- "vendor/libutf/src"
16
+ "prebuilds"
23
17
  ],
24
18
  "addon": true,
25
19
  "scripts": {
@@ -38,6 +32,7 @@
38
32
  "devDependencies": {
39
33
  "brittle": "^3.1.1",
40
34
  "cmake-bare": "^1.1.6",
35
+ "cmake-fetch": "^1.0.0",
41
36
  "standard": "^17.0.0"
42
37
  }
43
38
  }
@@ -1,70 +0,0 @@
1
- cmake_minimum_required(VERSION 3.22)
2
-
3
- project(base64 C)
4
-
5
- if(NOT TARGET utf)
6
- add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
7
- endif()
8
-
9
- add_library(base64 OBJECT)
10
-
11
- set_target_properties(
12
- base64
13
- PROPERTIES
14
- C_STANDARD 99
15
- POSITION_INDEPENDENT_CODE ON
16
- )
17
-
18
- target_sources(
19
- base64
20
- INTERFACE
21
- include/base64.h
22
- PRIVATE
23
- src/base64.c
24
- )
25
-
26
- target_include_directories(
27
- base64
28
- PUBLIC
29
- include
30
- $<TARGET_PROPERTY:utf,INTERFACE_INCLUDE_DIRECTORIES>
31
- )
32
-
33
- add_library(base64_shared SHARED)
34
-
35
- set_target_properties(
36
- base64_shared
37
- PROPERTIES
38
- OUTPUT_NAME base64
39
- WINDOWS_EXPORT_ALL_SYMBOLS ON
40
- )
41
-
42
- target_link_libraries(
43
- base64_shared
44
- PUBLIC
45
- base64
46
- )
47
-
48
- add_library(base64_static STATIC)
49
-
50
- set_target_properties(
51
- base64_static
52
- PROPERTIES
53
- OUTPUT_NAME base64
54
- PREFIX lib
55
- )
56
-
57
- target_link_libraries(
58
- base64_static
59
- PUBLIC
60
- base64
61
- )
62
-
63
- install(TARGETS base64_shared base64_static)
64
-
65
- install(FILES include/base64.h DESTINATION include)
66
-
67
- if(PROJECT_IS_TOP_LEVEL)
68
- enable_testing()
69
- add_subdirectory(test)
70
- endif()
@@ -1,128 +0,0 @@
1
- #ifndef BASE64_H
2
- #define BASE64_H
3
-
4
- #include <stdbool.h>
5
- #include <stddef.h>
6
- #include <stdint.h>
7
- #include <utf.h>
8
-
9
- #ifdef __cplusplus
10
- extern "C" {
11
- #endif
12
-
13
- static const char base64__alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14
-
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
- }
123
-
124
- #ifdef __cplusplus
125
- }
126
- #endif
127
-
128
- #endif // BASE64_H
@@ -1,10 +0,0 @@
1
- #include <stdint.h>
2
- #include <utf.h>
3
-
4
- #include "../include/base64.h"
5
-
6
- extern int
7
- base64_encode (const uint8_t *buffer, size_t buffer_len, utf8_t *string, size_t *string_len);
8
-
9
- extern int
10
- base64_decode (const utf8_t *string, size_t string_len, uint8_t *buffer, size_t *buffer_len);
@@ -1,71 +0,0 @@
1
- cmake_minimum_required(VERSION 3.22)
2
-
3
- project(hex C)
4
-
5
- if(NOT TARGET utf)
6
- add_subdirectory(vendor/libutf EXCLUDE_FROM_ALL)
7
- endif()
8
-
9
- add_library(hex OBJECT)
10
-
11
- set_target_properties(
12
- hex
13
- PROPERTIES
14
- C_STANDARD 99
15
- POSITION_INDEPENDENT_CODE ON
16
- )
17
-
18
- target_sources(
19
- hex
20
- INTERFACE
21
- include/hex.h
22
- PRIVATE
23
- src/hex.c
24
- )
25
-
26
- target_include_directories(
27
- hex
28
- PUBLIC
29
- include
30
- $<TARGET_PROPERTY:utf,INTERFACE_INCLUDE_DIRECTORIES>
31
- )
32
-
33
- add_library(hex_shared SHARED)
34
-
35
- set_target_properties(
36
- hex_shared
37
- PROPERTIES
38
- OUTPUT_NAME hex
39
- WINDOWS_EXPORT_ALL_SYMBOLS ON
40
- )
41
-
42
- target_link_libraries(
43
- hex_shared
44
- PUBLIC
45
- hex
46
- )
47
-
48
- add_library(hex_static STATIC)
49
-
50
- set_target_properties(
51
- hex_static
52
- PROPERTIES
53
- OUTPUT_NAME hex
54
- PREFIX lib
55
- )
56
-
57
- target_link_libraries(
58
- hex_static
59
- PUBLIC
60
- hex
61
- )
62
-
63
- install(TARGETS hex_shared hex_static)
64
-
65
- install(FILES include/hex.h DESTINATION include)
66
-
67
- if(PROJECT_IS_TOP_LEVEL)
68
- enable_testing()
69
-
70
- add_subdirectory(test)
71
- endif()