Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The codepage npm package is a library for converting data between different character encodings. It supports a wide range of code pages and provides functionality for encoding and decoding text.
Encoding Conversion
Convert strings between different encodings. In this example, we convert a binary string to UTF-8 and vice versa using code page 437.
var cp = require('codepage');
var utf8String = cp.utils.decode(437, Buffer.from('Hello World', 'binary'));
var binaryString = cp.utils.encode(437, 'Hello World');
Stream Transformation
Create a stream that transforms data from one encoding to another. Here, we create a transform stream that encodes incoming data to code page 437.
var cp = require('codepage');
var stream = require('stream');
var converter = new stream.Transform();
converter._transform = function(chunk, encoding, done) {
this.push(cp.utils.encode(437, chunk.toString()));
done();
};
Handling Code Pages
Access information about different code pages. This code sample retrieves information about code page 437.
var cp = require('codepage');
var cpInfo = cp[437];
console.log(cpInfo.name);
iconv-lite is a popular character encoding conversion library that works similarly to codepage. It supports many encodings and is often used for its simplicity and lightweight nature. Unlike codepage, iconv-lite does not rely on native Node.js modules, making it more portable across different platforms.
buffer-encoding is another package that offers encoding and decoding of text in Node.js. It provides a simple API for converting between different character encodings. It is similar to codepage but has a smaller footprint and fewer dependencies.
text-encoding is a polyfill for the Encoding Living Standard API provided by the WHATWG. It allows for encoding and decoding of text in the browser and Node.js. It is more comprehensive than codepage as it aims to provide a polyfill for the standard API, but it might be heavier in terms of size and complexity.
Codepages are character encodings. In many contexts, single-byte character sets are used in lieu of standard multibyte Unicode encodings. They use 256 characters with a simple mapping.
unicode.org hosts lists of mappings.
The build script automatically downloads and parses the mappings in order to
generate the full script. The pages.csv
description in codepage.md
controls
which codepages are used.
In the browser:
<script src="cptable.js"></script>
<script src="cputils.js"></script>
The complete set of codepages is large because of some Double Byte Character Set
encodings. A much smaller file that just includes SBCS codepages is provided in
this repo (sbcs.js
).
If you know which codepages you need, you can include individual scripts for
each codepage. The individual files are provided in the bits/
directory.
For example, to include only the Mac codepages:
<script src="bits/10000.js"></script>
<script src="bits/10006.js"></script>
<script src="bits/10007.js"></script>
<script src="bits/10029.js"></script>
<script src="bits/10079.js"></script>
<script src="bits/10081.js"></script>
All of the browser scripts define and append to the cptable
object. To rename
the object, edit the JSVAR
shell variable in make.sh
and run the script.
The utilities functions are contained in cputils.js
, which assumes that the
appropriate codepage scripts were loaded.
In node:
var cptable = require('codepage');
The codepages are indexed by number. To get the unicode character for a given
codepoint, use the dec
property:
var unicode_cp10000_255 = cptable[10000].dec[255]; // ˇ
To get the codepoint for a given character, use the enc
property:
var cp10000_711 = cptable[10000].enc[String.fromCharCode(711)]; // 255
There are a few utilities that deal with strings and buffers:
var 汇总 = cptable.utils.decode(936, [0xbb,0xe3,0xd7,0xdc]);
var buf = cptable.utils.encode(936, 汇总);
This script uses voc. The script to build the codepage tables and
the JS source is codepage.md
, so building is as simple as voc codepage.md
.
The standard Windows codepages are supported:
The full collection of ISO-8859
codepages are also supported. The East-Asian
Double Byte Character Sets are also supported:
The complete list of supported codepages can be found in the file pages.csv
.
The following codepages are not implemented. Normative references may not be available in all cases. Furthermore, other software packages are known to hack certain codepages (for example, Mozilla treats ASMO-708 as an alias of Arabic ISO-8869-6 when in fact there are many differences), so all implementations should be cleanroom when possible.
FAQs
pure-JS library to handle codepages
The npm package codepage receives a total of 2,442,299 weekly downloads. As such, codepage popularity was classified as popular.
We found that codepage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.