Files
onenote-tools/src/onenote/parser/detect-format.ts

71 lines
2.1 KiB
TypeScript

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
import { BinaryReader } from '../binary/BinaryReader.js';
import { NIL_GUID, readGuid } from '../binary/guid.js';
export const ONE_SECTION_FILE_TYPE = '{7B5C52E4-D88C-4DA7-AEB1-5378D02996D3}';
export const ONE_TOC_FILE_TYPE = '{43FF2FA1-EFD9-4C76-9EE2-10EA5722765F}';
export const DESKTOP_REVISION_STORE_FORMAT =
'{109ADD3F-911B-49F5-A5D0-1791EDC8AED8}';
export const FSSHTTP_PACKAGING_FORMAT =
'{638DE92F-A6D4-4BC1-9A36-B3FC2511A5B7}';
export type OneNoteFormatKind =
| 'desktop-one'
| 'desktop-onetoc2'
| 'fsshttp-one'
| 'not-onenote'
| 'unknown-onenote';
export interface OneNoteFormatDetection {
kind: OneNoteFormatKind;
fileType?: string;
legacyFileVersion?: string;
fileFormat?: string;
}
export function detectOneNoteFormat(
input: ArrayBuffer | Uint8Array
): OneNoteFormatDetection {
const bytes = toBytes(input);
if (bytes.byteLength < 64) return { kind: 'not-onenote' };
const reader = new BinaryReader(bytes, 0, 64, 'OneStore signature');
const fileType = readGuid(reader);
reader.skip(16);
const legacyFileVersion = readGuid(reader);
const fileFormat = readGuid(reader);
if (fileType !== ONE_SECTION_FILE_TYPE && fileType !== ONE_TOC_FILE_TYPE) {
return { kind: 'not-onenote', fileType, legacyFileVersion, fileFormat };
}
if (
fileFormat === FSSHTTP_PACKAGING_FORMAT ||
legacyFileVersion !== NIL_GUID
) {
return { kind: 'fsshttp-one', fileType, legacyFileVersion, fileFormat };
}
if (fileFormat !== DESKTOP_REVISION_STORE_FORMAT) {
return {
kind: 'unknown-onenote',
fileType,
legacyFileVersion,
fileFormat,
};
}
return {
kind:
fileType === ONE_SECTION_FILE_TYPE ? 'desktop-one' : 'desktop-onetoc2',
fileType,
legacyFileVersion,
fileFormat,
};
}
export function toBytes(input: ArrayBuffer | Uint8Array): Uint8Array {
return input instanceof Uint8Array ? input : new Uint8Array(input);
}