Skip to content

Instantly share code, notes, and snippets.

@isasmendiagus
Last active November 19, 2024 08:12
Show Gist options
  • Save isasmendiagus/e19b06c37c5b8e7cd43515ab0013ec44 to your computer and use it in GitHub Desktop.
Save isasmendiagus/e19b06c37c5b8e7cd43515ab0013ec44 to your computer and use it in GitHub Desktop.

SCANOSS Memory Scanning Example

This example demonstrates how to perform in-memory scanning using the SCANOSS SDK.

Pre-requisites

  • node
  • npm

Quick Start

  1. Download this Gist as a ZIP file using the "Download ZIP" button
  2. Extract the ZIP contents
  3. Install dependencies and run:
npm install
npm start

Code Explanation

This example demonstrates in-memory code scanning without the use of a file. The key method used is scanContents which allows direct scanning of code strings.

import { Scanner } from 'scanoss';
async function main() {
const scanner = new Scanner();
const results = await scanner.scanContents({
content: `// Compare words in given command against known command\n for (int j = 1; j <= limit; j++)\n {\n char *cword = ldb_extract_word(j, command);\n char *kword = ldb_extract_word(j, ldb_commands[i]);\n bool fulfilled = false;\n if (!strcmp(kword, "{hex}")) fulfilled = ldb_valid_hex(cword);\n else if (!strcmp(kword, "{ascii}")) fulfilled = ldb_valid_ascii(cword);\n else if (!strcmp(kword, cword)) fulfilled = true;\n free(cword);\n free(kword);\n\n if (!fulfilled) break;\n else if (j > hits)\n {\n closest = i;\n hits = j;\n *word_nr = hits;\n *command_nr = closest;\n }\n }\n if ((hits > 0) && (hits == known_words)) return true;\n }\n\n return false;`,
key: 'content_scanning',
});
console.log(JSON.stringify(results,null,2));
}
main();
{
"name": "scanoss-memory-example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "tsc && node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "SCANOSS memory scanning example",
"devDependencies": {
"@types/node": "^22.9.0",
"typescript": "^5.6.3"
},
"dependencies": {
"scanoss": "^0.15.2"
}
}
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["index.ts"],
"exclude": ["node_modules"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment