Skip to content

Commit

Permalink
Restricted use of EncodedString. (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Sep 24, 2023
1 parent ae390ab commit f3b8e5f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/src/auth/sasl_authenticator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SaslClientFirstMessage extends ClientMessage {
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.PasswordIdentifier);

final encodedMechanismName = buffer.prepareString(mechanismName);
final encodedMechanismName = buffer.encodeString(mechanismName);
final msgLength = bytesToSendToServer.length;
// No Identifier bit + 4 byte counts (for whole length) + mechanism bytes + zero byte + 4 byte counts (for msg length) + msg bytes
final length = 4 + encodedMechanismName.bytesLength + 1 + 4 + msgLength;
Expand Down
14 changes: 8 additions & 6 deletions lib/src/buffer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import 'dart:convert';

import 'package:buffer/buffer.dart';

/// This class doesn't add much over using `List<int>` instead, however,
/// it creates a nice explicit type difference from both `String` and `List<int>`,
/// and it allows better use for the string encoding that delimits the value with `0`.
class EncodedString {
final List<int> _bytes;
EncodedString.fromValue(String value, {Encoding encoding = utf8})
: _bytes = encoding.encode(value);
EncodedString._(this._bytes);

int get bytesLength => _bytes.length;
}
Expand All @@ -18,10 +20,10 @@ class PgByteDataWriter extends ByteDataWriter {
Encoding encoding = utf8,
}) : _encoding = encoding;

late final encodingName = prepareString(_encoding.name);
late final encodingName = encodeString(_encoding.name);

EncodedString prepareString(String value) {
return EncodedString.fromValue(value, encoding: _encoding);
EncodedString encodeString(String value) {
return EncodedString._(_encoding.encode(value));
}

void writeEncodedString(EncodedString value) {
Expand All @@ -30,7 +32,7 @@ class PgByteDataWriter extends ByteDataWriter {
}

void writeLengthEncodedString(String value) {
final encoded = EncodedString.fromValue(value, encoding: _encoding);
final encoded = encodeString(value);
writeUint32(5 + encoded.bytesLength);
write(encoded._bytes);
writeInt8(0);
Expand Down
23 changes: 11 additions & 12 deletions lib/src/client_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ class StartupMessage extends ClientMessage {

@override
void applyToBuffer(PgByteDataWriter buffer) {
final databaseName = buffer.prepareString(_databaseName);
final timeZone = buffer.prepareString(_timeZone);
final username =
_username == null ? null : buffer.prepareString(_username!);
final replication = buffer.prepareString(_replication);
final databaseName = buffer.encodeString(_databaseName);
final timeZone = buffer.encodeString(_timeZone);
final username = _username == null ? null : buffer.encodeString(_username!);
final replication = buffer.encodeString(_replication);
var fixedLength = 44 + buffer.encodingName.bytesLength;
var variableLength = databaseName.bytesLength + timeZone.bytesLength + 2;

Expand Down Expand Up @@ -134,8 +133,8 @@ class ParseMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.ParseIdentifier);
final statement = buffer.prepareString(_statement);
final statementName = buffer.prepareString(_statementName);
final statement = buffer.encodeString(_statement);
final statementName = buffer.encodeString(_statementName);
final length = 8 +
statement.bytesLength +
statementName.bytesLength +
Expand Down Expand Up @@ -168,7 +167,7 @@ class DescribeMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.DescribeIdentifier);
final name = buffer.prepareString(_name);
final name = buffer.encodeString(_name);
final length = 6 + name.bytesLength;
buffer.writeUint32(length);
buffer.writeUint8(_isPortal ? $P : $S);
Expand All @@ -189,8 +188,8 @@ class BindMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.BindIdentifier);
final portalName = buffer.prepareString(_portalName);
final statementName = buffer.prepareString(_statementName);
final portalName = buffer.encodeString(_portalName);
final statementName = buffer.encodeString(_statementName);

final parameterBytes = _parameters.map((p) => p.encodeAsBytes()).toList();
final typeSpecCount = _parameters.where((p) => p.hasKnownType).length;
Expand Down Expand Up @@ -258,7 +257,7 @@ class ExecuteMessage extends ClientMessage {
@override
void applyToBuffer(PgByteDataWriter buffer) {
buffer.writeUint8(ClientMessage.ExecuteIdentifier);
final portalName = buffer.prepareString(_portalName);
final portalName = buffer.encodeString(_portalName);
buffer.writeUint32(9 + portalName.bytesLength);
buffer.writeEncodedString(portalName);
buffer.writeUint32(0);
Expand All @@ -279,7 +278,7 @@ class CloseMessage extends ClientMessage {

@override
void applyToBuffer(PgByteDataWriter buffer) {
final name = buffer.prepareString(_name);
final name = buffer.encodeString(_name);
final length = 6 + name.bytesLength;

buffer
Expand Down

0 comments on commit f3b8e5f

Please sign in to comment.