Skip to content

Commit

Permalink
Deploying to gh-pages from @ 892956d 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
tniessen committed Sep 6, 2024
0 parents commit 01b0b79
Show file tree
Hide file tree
Showing 7 changed files with 1,039 additions and 0 deletions.
156 changes: 156 additions & 0 deletions app-resources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
window.appResources = {
softwareInfo: [
{
name: 'littlefs disk image viewer',
version: '892956d9',
repo: 'https://github.com/tniessen/littlefs-disk-img-viewer',
license: `MIT License
Copyright (c) 2021-2023 Tobias Nießen
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.
`
},
{
name: 'littlefs',
version: 'v2.9.3',
repo: 'https://github.com/littlefs-project/littlefs.git',
license: `Copyright (c) 2022, The littlefs authors.
Copyright (c) 2017, Arm Limited. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
- Neither the name of ARM nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
`
},
{
name: 'tailwindcss',
version: '2.2.2',
repo: 'https://github.com/tailwindlabs/tailwindcss',
license: `MIT License
Copyright (c) Adam Wathan <[email protected]>
Copyright (c) Jonathan Reinink <[email protected]>
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.
`
},
{
name: 'Heroicons',
version: 'v2.1.5',
repo: 'https://github.com/tailwindlabs/heroicons.git',
license: `MIT License
Copyright (c) Tailwind Labs, Inc.
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.
`
}
],
definitions: {
MAX_LOOKAHEAD_SIZE: 128,
MAX_CACHE_SIZE: 4096,
MAX_PATH_SIZE: 1024,
ASYNCIFY_BUFFER_SIZE: 8192,
FILE_READ_BUFFER_SIZE: 8192,
},
icons: {
'outline/arrow-down-tray': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5M16.5 12 12 16.5m0 0L7.5 12m4.5 4.5V3"/>
</svg>
`,
'outline/circle-stack': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="M20.25 6.375c0 2.278-3.694 4.125-8.25 4.125S3.75 8.653 3.75 6.375m16.5 0c0-2.278-3.694-4.125-8.25-4.125S3.75 4.097 3.75 6.375m16.5 0v11.25c0 2.278-3.694 4.125-8.25 4.125s-8.25-1.847-8.25-4.125V6.375m16.5 0v3.75m-16.5-3.75v3.75m16.5 0v3.75C20.25 16.153 16.556 18 12 18s-8.25-1.847-8.25-4.125v-3.75m16.5 0c0 2.278-3.694 4.125-8.25 4.125s-8.25-1.847-8.25-4.125"/>
</svg>
`,
'outline/folder': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12.75V12A2.25 2.25 0 0 1 4.5 9.75h15A2.25 2.25 0 0 1 21.75 12v.75m-8.69-6.44-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z"/>
</svg>
`,
'outline/folder-open': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 9.776c.112-.017.227-.026.344-.026h15.812c.117 0 .232.009.344.026m-16.5 0a2.25 2.25 0 0 0-1.883 2.542l.857 6a2.25 2.25 0 0 0 2.227 1.932H19.05a2.25 2.25 0 0 0 2.227-1.932l.857-6a2.25 2.25 0 0 0-1.883-2.542m-16.5 0V6A2.25 2.25 0 0 1 6 3.75h3.879a1.5 1.5 0 0 1 1.06.44l2.122 2.12a1.5 1.5 0 0 0 1.06.44H18A2.25 2.25 0 0 1 20.25 9v.776"/>
</svg>
`,
'outline/information-circle': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="m11.25 11.25.041-.02a.75.75 0 0 1 1.063.852l-.708 2.836a.75.75 0 0 0 1.063.853l.041-.021M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Zm-9-3.75h.008v.008H12V8.25Z"/>
</svg>
`,
'outline/tag': `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" aria-hidden="true" data-slot="icon">
<path stroke-linecap="round" stroke-linejoin="round" d="M9.568 3H5.25A2.25 2.25 0 0 0 3 5.25v4.318c0 .597.237 1.17.659 1.591l9.581 9.581c.699.699 1.78.872 2.607.33a18.095 18.095 0 0 0 5.223-5.223c.542-.827.369-1.908-.33-2.607L11.16 3.66A2.25 2.25 0 0 0 9.568 3Z"/>
<path stroke-linecap="round" stroke-linejoin="round" d="M6 6h.008v.008H6V6Z"/>
</svg>
`,
'solid/document': `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" data-slot="icon">
<path d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625Z"/>
<path d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"/>
</svg>
`,
}
};
43 changes: 43 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>littlefs disk image viewer</title>
<meta charset="utf-8">
<link href="tailwind.min.css" rel="stylesheet">
</head>
<body class="overflow-y-scroll">
<div class="container mx-auto px-2">
<div class="flex justify-between items-center my-4">
<h1 class="text-xl my-2">littlefs disk image viewer</h1>
<a href="#" id="about-button">About</a>
</div>
<div id="setup-box">
<div class="rounded border p-8">
<div class="grid grid-cols-2 lg:grid-cols-4 xl:grid-cols-6 gap-4 text-sm">
<label for="img-block-size-input" class="text-right">Block size:</label>
<input type="number" id="img-block-size-input" class="border text-right">
<label for="img-block-count-input" class="text-right">Block count:</label>
<input type="number" id="img-block-count-input" class="border text-right" placeholder="Auto">
<label for="img-read-size-input" class="text-right">Read size:</label>
<input type="number" id="img-read-size-input" class="border text-right" placeholder="Auto">
<label for="img-cache-size-input" class="text-right">Cache size:</label>
<input type="number" id="img-cache-size-input" class="border text-right" placeholder="Auto">
<label for="img-lookahead-size-input" class="text-right">Lookahead size:</label>
<input type="number" id="img-lookahead-size-input" class="border text-right" placeholder="Auto">
<label for="max-cached-blocks-input" class="text-right">Cache blocks:</label>
<input type="number" id="max-cached-blocks-input" class="border text-right" placeholder="Auto">
</div>
<div class="text-center mt-10 mb-4">
<label for="img-file-input" class="bg-blue-600 text-white py-2 px-4 border shadow-lg text-lg uppercase">Open disk image</label>
<input type="file" id="img-file-input" class="opacity-0 w-px h-px">
</div>
</div>
</div>
<div id="content"></div>
<script type="application/javascript" src="app-resources.js"></script>
<script type="application/javascript" src="lib/ui.js"></script>
<script type="application/javascript" src="lib/lfs-reader.js"></script>
<script type="application/javascript" src="lib/main.js"></script>
</div>
</body>
</html>
181 changes: 181 additions & 0 deletions lib/lfs-reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
'use strict';

(typeof module !== 'undefined' && module.exports || window).createLfsImageReader = async (opts) => {
const { read } = opts;

const { MAX_PATH_SIZE, FILE_READ_BUFFER_SIZE } = appResources.definitions;

let rewindCallback = undefined;
function onAsyncRewind(fn) {
if (rewindCallback !== undefined) {
throw new Error('Another callback is already registered');
}
rewindCallback = fn;
}

let traverseCallback;

const bytes = await (typeof fetch === 'function' ? fetch('main.async.wasm').then((res) => res.arrayBuffer()) : require('fs').promises.readFile(__dirname + '/../main.async.wasm'));
const { instance: wasm } = await WebAssembly.instantiate(bytes, {
env: {
readFileRange(offset, ptr, size) {
if (wasm.exports.asyncify_get_state() !== 0) {
wasm.exports.asyncify_stop_rewind();
return;
}

const ret = read(offset, wasm.exports.memory.buffer, ptr, size);
if (ret === undefined) {
rewindCallback = undefined;
} else {
wasm.exports.prepare_asyncify_unwind();
const dataAddr = wasm.exports.get_asyncify_info();
wasm.exports.asyncify_start_unwind(dataAddr);

ret.then(() => {
wasm.exports.asyncify_start_rewind(dataAddr);
const fn = rewindCallback;
rewindCallback = undefined;
fn();
});
}
},
traverseCallback(_, block) {
traverseCallback(block);
}
}
});

function x(ret, message) {
if (ret < 0) {
throw new Error(`${message}: ${ret}`);
}
return ret;
}

// Pointers to statically allocated memory.
const img = wasm.exports.get_lfs_image();
const dir = wasm.exports.get_lfs_dir();
const file = wasm.exports.get_lfs_file();
const fileConfig = wasm.exports.get_lfs_file_config();
const info = wasm.exports.get_lfs_info();
const pathBuf = wasm.exports.get_lfs_path_buffer();
const fileReadBuffer = wasm.exports.get_file_read_buffer();
const attrSizes = wasm.exports.get_attr_sizes();

function callAsync(fn) {
let ret = fn();
if (wasm.exports.asyncify_get_state() === 0) {
return Promise.resolve(ret);
}

if (ret !== 0) {
throw new Error(`Async operation should have returned 0, but got ${ret}`);
}

return new Promise((resolve, reject) => {
onAsyncRewind(() => callAsync(fn).then(resolve, reject));
});
}

async function openImage(readSize, cacheSize, lookaheadSize, blockSize, blockCount) {
const config = x(wasm.exports.configure(readSize, cacheSize, lookaheadSize, blockSize, blockCount), 'configure');
x(await callAsync(() => wasm.exports.lfs_mount(img, config)), 'lfs_mount');
}

function writePathBuffer(path) {
// TODO: Check overflow
const enc = new TextEncoder();
const out = new Uint8Array(wasm.exports.memory.buffer, pathBuf, MAX_PATH_SIZE);
out[enc.encodeInto(path, out).written] = 0;
}

async function openDir(path) {
writePathBuffer(path);
x(await callAsync(() => wasm.exports.lfs_dir_open(img, dir, pathBuf)), 'lfs_dir_open');
}

async function readDir() {
let ret = x(await callAsync(() => wasm.exports.lfs_dir_read(img, dir, info), 'lfs_dir_read'));
if (ret === 0) return null;
const dec = new TextDecoder();
return {
type: wasm.exports.get_lfs_info_type(),
size: wasm.exports.get_lfs_info_size(),
name: dec.decode(new Uint8Array(wasm.exports.memory.buffer, wasm.exports.get_lfs_info_name(), wasm.exports.get_lfs_info_name_length()))
};
}

function closeDir() {
x(wasm.exports.lfs_dir_close(img, dir), 'lfs_dir_close');
}

async function openFile(path) {
writePathBuffer(path);
x(await callAsync(() => wasm.exports.lfs_file_opencfg(img, file, pathBuf, 1, fileConfig)), 'lfs_file_open');
}

async function readFile() {
const n = x(await callAsync(() => wasm.exports.lfs_file_read(img, file, fileReadBuffer, FILE_READ_BUFFER_SIZE)), 'lfs_file_read');
return new Uint8Array(wasm.exports.memory.buffer, fileReadBuffer, n);
}

function closeFile() {
x(wasm.exports.lfs_file_close(img, file), 'lfs_file_close');
}

async function traverse(callback) {
traverseCallback = callback;
x(await callAsync(() => wasm.exports.do_lfs_traverse()));
}

async function countAttributes(path) {
writePathBuffer(path);
return x(await callAsync(() => wasm.exports.list_lfs_attr()), 'list_lfs_attr');
}

async function listAttributes(path) {
const n = await countAttributes(path);
const array = new Uint16Array(wasm.exports.memory.buffer, attrSizes, 256);
const attrs = [];
for (let i = 0; i < 256; i++) {
if (array[i] !== 0xffff) {
attrs.push([i, array[i]]);
}
}
if (attrs.length !== n) {
throw new Error('Reported number of attributes does not match.');
}
return attrs;
}

async function readAttribute(path, tag) {
writePathBuffer(path);
const size = x(await callAsync(() => wasm.exports.lfs_getattr(img, pathBuf, tag, fileReadBuffer, FILE_READ_BUFFER_SIZE)), 'lfs_getattr');
return new Uint8Array(wasm.exports.memory.buffer, fileReadBuffer, size);
}

console.log('data end', wasm.exports.__data_end.valueOf());
console.log('global', wasm.exports.__global_base.valueOf());
console.log('heap', wasm.exports.__heap_base.valueOf());
console.log('memory', wasm.exports.__memory_base.valueOf());
console.log('table', wasm.exports.__table_base.valueOf());

if (wasm.exports.memory.buffer.byteLength !== 64 * 1024) {
throw new Error(`WebAssembly should allocate exactly one page, but allocated ${wasm.exports.memory.buffer.byteLength}`);
}

return {
openImage,
openDir,
readDir,
closeDir,
openFile,
readFile,
closeFile,
traverse,
countAttributes,
listAttributes,
readAttribute
};
};
Loading

0 comments on commit 01b0b79

Please sign in to comment.