Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kasefuchs committed Feb 3, 2023
0 parents commit c8bac6e
Show file tree
Hide file tree
Showing 17 changed files with 530 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Test

on:
- push:
- pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dart-lang/[email protected]
- name: Install dependencies
run: dart pub get
- name: Run tests
run: dart test -r github
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# See https://www.dartlang.org/guides/libraries/private-files

# Files and directories created by pub
.dart_tool/
.packages
build/
# If you're building an application, you may want to check-in your pubspec.lock
pubspec.lock

# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/

# dotenv environment variables file
.env*

# Avoid committing generated Javascript files:
*.dart.js
*.info.json # Produced by the --dump-info flag.
*.js # When generated by dart2js. Don't specify *.js if your
# project includes source files written in JavaScript.
*.js_
*.js.deps
*.js.map

.flutter-plugins
.flutter-plugins-dependencies

# IDE
.idea/
*.iml
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial port of Chromium Pickle in Dart
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Kasefuchs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Chromium Pickle in Dart

A Dart library to use the Chromium pickle.

This is a port of the [Chromium Pickle Node.JS library](https://github.com/electron/node-chromium-pickle-js/) in the [Dart language](https://www.dartlang.org/).
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
24 changes: 24 additions & 0 deletions example/pickle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:chromium_pickle/chromium_pickle.dart';

void main() {
// Create empty pickle
var pickle = Pickle.empty();

// Write value to pickle
pickle.writeInt(1024);

// Create iterator
var iterator = pickle.createIterator();

// Read value
print(iterator.readInt());

// Dump pickle to Uint8List
var pickleData = pickle.toUint8List();

// Create new pickle from Uint8List
var newIterator = Pickle.fromUint8List(pickleData).createIterator();

// There is it again!
print(newIterator.readInt());
}
6 changes: 6 additions & 0 deletions lib/chromium_pickle.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
library chromium_pickle;

export 'src/enums/enums.dart' show PickleSize, PickleConstants;
export 'src/iterator.dart' show PickleIterator;
export 'src/pickle.dart' show Pickle;
export 'src/utils.dart' show alignInt;
12 changes: 12 additions & 0 deletions lib/src/enums/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// [Pickle] constants.
enum PickleConstants {
/// Largest JS number.
capacityReadOnly(9007199254740992),

/// The allocation granularity of the payload.
payloadUnit(64);

const PickleConstants(this.value);

final int value;
}
2 changes: 2 additions & 0 deletions lib/src/enums/enums.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'constants.dart';
export 'size.dart';
24 changes: 24 additions & 0 deletions lib/src/enums/size.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// [Pickle] types size.
enum PickleSize {
/// Int32 length.
int32(4),

/// UInt32 length.
uint32(4),

/// Int64 length.
int64(8),

/// uInt64 length.
uint64(8),

/// Float length.
float(4),

/// Double length.
double(8);

const PickleSize(this.value);

final int value;
}
108 changes: 108 additions & 0 deletions lib/src/iterator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import 'dart:convert';
import 'dart:typed_data';

import 'enums/enums.dart';
import 'pickle.dart';
import 'utils.dart';

class PickleIterator {
late Uint8List payload;
late int payloadOffset;
late int readIndex;
late int endIndex;

/// Creates new [PickleIterator] instance that can be used to read data from [Pickle] objects.
PickleIterator(Pickle pickle) {
payload = pickle.header;
payloadOffset = pickle.headerSize;
readIndex = 0;
endIndex = pickle.getPayloadSize();
}

/// Returns the `bytes` value of the [Pickle] object at the specified buffer [length].
dynamic readBytes<ReturnType>(int length,
[ReturnType Function(int)? method]) {
int readPayloadOffset = getReadPayloadOffsetAndAdvance(length);
if (method != null) return method(readPayloadOffset);
return payload.sublist(readPayloadOffset, readPayloadOffset + length);
}

/// Returns current [Pickle] object value as `int32` and seeks to next data. A [RangeError] exception would be thrown when failed.
int readInt() {
return readBytes<int>(
PickleSize.int32.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getInt32(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `uint32` and seeks to next data. A [RangeError] exception would be thrown when failed.
int readUInt32() {
return readBytes<int>(
PickleSize.uint32.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getUint32(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `int64` and seeks to next data. A [RangeError] exception would be thrown when failed.
int readInt64() {
return readBytes<int>(
PickleSize.int64.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getInt64(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `uint64` and seeks to next data. A [RangeError] exception would be thrown when failed.
int readUInt64() {
return readBytes<int>(
PickleSize.uint64.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getUint64(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `float` and seeks to next data. A [RangeError] exception would be thrown when failed.
double readFloat() {
return readBytes<double>(
PickleSize.float.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getFloat32(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `double` and seeks to next data. A [RangeError] exception would be thrown when failed.
double readDouble() {
return readBytes<double>(
PickleSize.double.value,
(readPayloadOffset) => ByteData.view(payload.buffer)
.getFloat64(readPayloadOffset, Endian.little));
}

/// Returns current [Pickle] object value as `bool` and seeks to next data. A [RangeError] exception would be thrown when failed.
bool readBool() {
return readInt() != 0;
}

/// Returns current [Pickle] object value as `string` and seeks to next data. A [RangeError] exception would be thrown when failed.
String readString() {
return utf8.decode(readBytes(readInt()));
}

/// Gets read payload offset and seeks for next data in current [Pickle] object.
int getReadPayloadOffsetAndAdvance(int length) {
if (length > endIndex - readIndex) {
readIndex = endIndex;
throw RangeError('Failed to read data with length of $length');
}
int readPayloadOffset = payloadOffset + readIndex;
advance(length);
return readPayloadOffset;
}

/// Seeks for next data in current [Pickle] object.
void advance(int size) {
int alignedSize = alignInt(size, PickleSize.uint32.value);
if (endIndex - readIndex < alignedSize) {
readIndex = endIndex;
} else {
readIndex += alignedSize;
}
}
}
Loading

0 comments on commit c8bac6e

Please sign in to comment.