Skip to content

Commit

Permalink
Improved some the file validation and normalised some exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bramp committed Feb 1, 2024
1 parent 0eae972 commit fec17b5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions packages/pmtiles/lib/src/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ class PmTilesArchive {
static Future<PmTilesArchive> fromReadAt(ReadAt f) async {
final headerAndRoot =
await (await f.readAt(0, headerAndRootMaxLength)).toBytes();

if (headerAndRoot.length < headerLength) {
throw CorruptArchiveException('Header is too short.');
}

final header = Header(
ByteData.view(
// Make a copy of the first headerLength (127) bytes.
Expand All @@ -297,7 +302,7 @@ class PmTilesArchive {
}

if (header.clustered == Clustered.notClustered) {
throw UnimplementedError('Unclustered archives are not supported.');
throw UnsupportedError('Unclustered archives.');
}

final root = Uint8List.view(
Expand Down Expand Up @@ -390,6 +395,6 @@ extension CompressionDecoder on Compression {
// TODO Add support for the following:
// Compression.brotli,
// Compression.zstd,
_ => throw UnimplementedError('$this compression is not supported.'),
_ => throw UnsupportedError('$this compression.'),
};
}
2 changes: 1 addition & 1 deletion packages/pmtiles/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class CorruptArchiveException implements Exception {

@override
String toString() {
return 'CorruptArchive: $message';
return 'CorruptArchiveException: $message';
}
}
5 changes: 4 additions & 1 deletion packages/pmtiles/lib/src/header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import 'convert.dart';
import 'exceptions.dart';
import 'types.dart';

// Minimum valid header length.
const headerLength = 127;

// Max header + root length.
const headerAndRootMaxLength = 16384;

/// PMTiles Header
Expand Down Expand Up @@ -89,7 +92,7 @@ class Header {
}

if (version != 3) {
throw UnsupportedError('Unsupported version "$version"');
throw UnsupportedError('Version "$version" files');
}

if (!strict) {
Expand Down
4 changes: 4 additions & 0 deletions packages/pmtiles/lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export 'io_http.dart' if (dart.library.node) 'io_http_fake.dart';
/// Simple interface so we can abstract reading from Files, or Http.
abstract interface class ReadAt {
/// Read [length] bytes from [offset].
///
/// If offset+length is beyond the end of the file as many bytes as possible
/// are returned.
// TODO Test the edge case behaviours.
Future<ByteStream> readAt(int offset, int length);

/// Close any resources.
Expand Down
4 changes: 2 additions & 2 deletions packages/pmtiles/lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension TileTypeMime on TileType {
TileType.jpeg => 'image/jpeg',
TileType.webp => 'image/webp',
TileType.avif => 'image/avif',
_ => throw UnimplementedError('Unknown tile type $this'),
_ => throw UnsupportedError('Unknown tile type $this'),
};

/// Returns the file extension for this tile type.
Expand All @@ -40,6 +40,6 @@ extension TileTypeMime on TileType {
TileType.jpeg => 'jpg',
TileType.webp => 'webp',
TileType.avif => 'avif',
_ => throw UnimplementedError('Unknown tile type $this'),
_ => throw UnsupportedError('Unknown tile type $this'),
};
}

0 comments on commit fec17b5

Please sign in to comment.