Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chs

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chs - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

175

lib/chs.js

@@ -1,10 +0,4 @@

var inherit = require( 'bloodline' )
var util = require( 'util' )
var tty = require( 'tty' )
var isBuffer = Buffer.isBuffer
const initial = [ 0xFE, 0xFF, 0xFF ]
/**
* CHS
* Cylinder-Head-Sector Address
* @constructor
* @param {Number} cylinder

@@ -16,28 +10,44 @@ * @param {Number} head

function CHS( cylinder, head, sector ) {
if( !(this instanceof CHS) )
return new CHS( cylinder, head, sector )
Buffer.call( this, 3 )
this[0] = 0xFE
this[1] = 0xFF
this[2] = 0xFF
// ( buffer, from, to )
if( isBuffer( cylinder ) ) {
cylinder.copy( this, 0, head, sector )
} else {
this.cylinder = cylinder
this.head = head
this.sector = sector
}
if( Buffer.isBuffer( cylinder ) )
return CHS.fromBuffer( cylinder )
if( cylinder != null && !Number.isFinite( cylinder ) )
throw new TypeError( 'Cylinder must be a number' )
if( head != null && !Number.isFinite( head ) )
throw new TypeError( 'Head must be a number' )
if( sector != null && !Number.isFinite( sector ) )
throw new TypeError( 'Sector must be a number' )
this.cylinder = cylinder != null ?
( cylinder | 0 ) : 1023
this.head = head != null ?
( head | 0 ) : 254
this.sector = sector != null ?
( sector | 0 ) : 63
}
/**
* Set to the CHS of a Logical Block Address
* @param {Number} lba
* Create a CHS Address from a given buffer
* @param {Buffer} buffer
* @return {CHS}
*/
CHS.fromBuffer = function( buffer ) {
return new CHS().parse( buffer )
}
/**
* Create a CHS Address from a Logical Block Address (LBA)
* @param {Number} lba Logical Block Address
* @param {Number} hpt Heads per Track
* @param {Number} spt Sectors per Track
* @return {CHS}
*/

@@ -53,41 +63,18 @@ CHS.fromLBA = function( lba, hpt, spt ) {

CHS.prototype = {
constructor: CHS,
get head() {
return this[0]
},
// Sector in bits 5–0;
// bits 7–6 are high bits of cylinder
// 00111111b
get sector() {
return this[1] & 0x3F
},
// Bits 7-6 from sector & bits 7–0 of cylinder
// 11000000b
get cylinder() {
return ( ( this[1] & 0xC0 ) << 2 ) | this[2]
},
set head( value ) {
this[0] = value
},
set sector( value ) {
var sector = ( value >> 2 ) & 0xC0
sector = sector ^ value
this[1] = sector
},
set cylinder( value ) {
this[2] = value & 0xFF
},
/**
* Set to the CHS of a Logical Block Address
* @param {Number} lba
* Get/set values from/to a Buffer
* @property {Buffer} buffer
*/
set buffer( value ) { return this.parse( value ) },
get buffer() { return this.toBuffer() },
/**
* Set CHS to a Logical Block Address (LBA)
* @param {Number} lba Logical Block Address
* @param {Number} hpt Heads per Track
* @param {Number} spt Sectors per Track
* @return {CHS}
*/

@@ -100,8 +87,9 @@ setLBA: function( lba, hpt, spt ) {

},
/**
* Get the Logical Block Address corresponding
* to the given disk geometry
* Get the Logical Block Address (LBA)
* corresponding to the given disk geometry
* @param {Number} hpt Heads per Track
* @param {Number} spt Sectors per Track
* @return {Number} lba
*/

@@ -112,3 +100,3 @@ getLBA: function( hpt, spt ) {

},
/**

@@ -118,2 +106,3 @@ * @see #getLBA()

* @param {Number} spt Sectors per Track
* @return {Number} lba
*/

@@ -123,23 +112,45 @@ toLBA: function( hpt, spt ) {

},
toJSON: function() {
return {
cylinder: this.cylinder,
head: this.head,
sector: this.sector,
}
/**
* Parse a given Buffer
* @param {Buffer} buffer
* @return {CHS}
*/
parse: function( buffer ) {
if( !Buffer.isBuffer( buffer ) )
throw new TypeError( 'Value must be a buffer' )
this.head = buffer[0]
// Sector in bits 5–0;
// bits 7–6 are high bits of cylinder
// 00111111b
this.sector = buffer[1] & 0x3F
// Bits 7-6 from sector & bits 7–0 of cylinder
// 11000000b
this.cylinder = ( ( buffer[1] & 0xC0 ) << 2 ) | buffer[2]
return this
},
inspect: function() {
return '<' + this.constructor.name + ' ' +
util.inspect( this.toJSON(), {
colors: tty.isatty()
}) + '>'
/**
* Create a Buffer representation of the CHS Address
* @return {Buffer}
*/
toBuffer: function() {
var cylinder = this.cylinder & 0xFF
var head = this.head
var sector = (( this.sector >> 2 ) & 0xC0) ^ this.sector
return new Buffer([ head, sector, cylinder ])
},
}
// Inherit from Buffer
inherit( CHS, Buffer )
// Exports
module.exports = CHS

@@ -21,2 +21,1 @@ # The MIT License (MIT)

OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "chs",
"version": "0.2.1",
"version": "0.3.0",
"description": "Cylinder-Head-Sector Address (CHS)",
"author": "Jonas Hermsmeier <jhermsmeier@gmail.com> (https://jhermsmeier.de/)",
"license": "MIT",

@@ -12,23 +13,28 @@ "keywords": [

"sector",
"mbr"
"mbr",
"lba",
"block",
"hdd",
"gpt",
"disk"
],
"main": "lib/chs",
"author": "Jonas Hermsmeier <jhermsmeier@gmail.com> (https://jhermsmeier.de/)",
"dependencies": {
"bloodline": "~1.0.0"
},
"main": "lib/chs.js",
"dependencies": {},
"devDependencies": {
"mocha": "~2.2.4"
"mocha": "~2.5.3"
},
"scripts": {
"test": "mocha"
},
"homepage": "https://github.com/jhermsmeier/node-chs",
"repository": {
"type": "git",
"url": "https://github.com/jhermsmeier/node-chs.git"
"url": "git+https://github.com/jhermsmeier/node-chs.git"
},
"bugs": {
"url": "https://github.com/jhermsmeier/node-chs/issues"
},
"directories": {
"test": "test"
},
"scripts": {
"test": "mocha"
}
}

@@ -1,13 +0,19 @@

# chs
[![npm](http://img.shields.io/npm/v/chs.svg?style=flat-square)](https://npmjs.com/chs)
[![npm downloads](http://img.shields.io/npm/dm/chs.svg?style=flat-square)](https://npmjs.com/chs)
[![build status](http://img.shields.io/travis/jhermsmeier/node-chs.svg?style=flat-square)](https://travis-ci.org/jhermsmeier/node-chs)
# CHS (Cylinder-Head-Sector) Address
[![npm](https://img.shields.io/npm/v/chs.svg?style=flat-square)](https://npmjs.com/package/chs)
[![npm license](https://img.shields.io/npm/l/chs.svg?style=flat-square)](https://npmjs.com/package/chs)
[![npm downloads](https://img.shields.io/npm/dm/chs.svg?style=flat-square)](https://npmjs.com/package/chs)
[![build status](https://img.shields.io/travis/jhermsmeier/node-chs.svg?style=flat-square)](https://travis-ci.org/jhermsmeier/node-chs)
## Install via [npm](https://npmjs.com)
[CHS addressing] is an early method for giving addresses to each physical block of data on a hard disk drive,
identifying individual sectors on a disk by their position in a track, where the track is determined by the head and cylinder numbers.
[CHS addressing]: https://en.wikipedia.org/wiki/Cylinder-head-sector
# Install via [npm](https://npmjs.com)
```sh
$ npm install chs
$ npm install --save chs
```
## Usage
# Usage

@@ -17,4 +23,8 @@ ```js

var CHS = require( 'chs' )
```
```js
// Create a CHS address
var addr = new CHS( 5, 20, 8 )
```
```js
// Properties:

@@ -24,12 +34,146 @@ var c = addr.cylinder

var s = addr.sector
```
```js
// Convert to an LBA (Logical Block Address)
var lba = addr.toLBA( headsPerTrack, sectorsPerTrack )
var lba = addr.toLBA( 12, 32 )
```
```js
// Set it to an LBA
addr.setLBA( lba, headsPerTrack, sectorsPerTrack )
addr.setLBA( 3150, 16, 63 )
```
```js
// Get it as a buffer
var buf = addr.buffer
var buf = addr.toBuffer()
```
```js
// Set from buffer
addr.buffer = new Buffer([ 0xFE, 0xFF, 0xFF ])
addr.parse( new Buffer([ 0xFE, 0xFF, 0xFF ]) )
```
# API Reference
<a name="CHS"></a>
## CHS
**Kind**: global class
* [CHS](#CHS)
* [new CHS(cylinder, head, sector)](#new_CHS_new)
* _instance_
* [.buffer](#CHS+buffer)
* [.setLBA(lba, hpt, spt)](#CHS+setLBA) ⇒ <code>[CHS](#CHS)</code>
* [.getLBA(hpt, spt)](#CHS+getLBA) ⇒ <code>Number</code>
* [.toLBA(hpt, spt)](#CHS+toLBA) ⇒ <code>Number</code>
* [.parse(buffer)](#CHS+parse) ⇒ <code>[CHS](#CHS)</code>
* [.toBuffer()](#CHS+toBuffer) ⇒ <code>Buffer</code>
* _static_
* [.fromBuffer(buffer)](#CHS.fromBuffer) ⇒ <code>[CHS](#CHS)</code>
* [.fromLBA(lba, hpt, spt)](#CHS.fromLBA) ⇒ <code>[CHS](#CHS)</code>
<a name="new_CHS_new"></a>
### new CHS(cylinder, head, sector)
Cylinder-Head-Sector Address
| Param | Type |
| --- | --- |
| cylinder | <code>Number</code> |
| head | <code>Number</code> |
| sector | <code>Number</code> |
<a name="CHS+buffer"></a>
### chS.buffer
Get/set values from/to a Buffer
**Kind**: instance property of <code>[CHS](#CHS)</code>
**Properties**
| Name | Type |
| --- | --- |
| buffer | <code>Buffer</code> |
<a name="CHS+setLBA"></a>
### chS.setLBA(lba, hpt, spt) ⇒ <code>[CHS](#CHS)</code>
Set CHS to a Logical Block Address (LBA)
**Kind**: instance method of <code>[CHS](#CHS)</code>
| Param | Type | Description |
| --- | --- | --- |
| lba | <code>Number</code> | Logical Block Address |
| hpt | <code>Number</code> | Heads per Track |
| spt | <code>Number</code> | Sectors per Track |
<a name="CHS+getLBA"></a>
### chS.getLBA(hpt, spt) ⇒ <code>Number</code>
Get the Logical Block Address (LBA)
corresponding to the given disk geometry
**Kind**: instance method of <code>[CHS](#CHS)</code>
**Returns**: <code>Number</code> - lba
| Param | Type | Description |
| --- | --- | --- |
| hpt | <code>Number</code> | Heads per Track |
| spt | <code>Number</code> | Sectors per Track |
<a name="CHS+toLBA"></a>
### chS.toLBA(hpt, spt) ⇒ <code>Number</code>
**Kind**: instance method of <code>[CHS](#CHS)</code>
**Returns**: <code>Number</code> - lba
**See**: #getLBA()
| Param | Type | Description |
| --- | --- | --- |
| hpt | <code>Number</code> | Heads per Track |
| spt | <code>Number</code> | Sectors per Track |
<a name="CHS+parse"></a>
### chS.parse(buffer) ⇒ <code>[CHS](#CHS)</code>
Parse a given Buffer
**Kind**: instance method of <code>[CHS](#CHS)</code>
| Param | Type |
| --- | --- |
| buffer | <code>Buffer</code> |
<a name="CHS+toBuffer"></a>
### chS.toBuffer() ⇒ <code>Buffer</code>
Create a Buffer representation of the CHS Address
**Kind**: instance method of <code>[CHS](#CHS)</code>
<a name="CHS.fromBuffer"></a>
### CHS.fromBuffer(buffer) ⇒ <code>[CHS](#CHS)</code>
Create a CHS Address from a given buffer
**Kind**: static method of <code>[CHS](#CHS)</code>
| Param | Type |
| --- | --- |
| buffer | <code>Buffer</code> |
<a name="CHS.fromLBA"></a>
### CHS.fromLBA(lba, hpt, spt) ⇒ <code>[CHS](#CHS)</code>
Create a CHS Address from a Logical Block Address (LBA)
**Kind**: static method of <code>[CHS](#CHS)</code>
| Param | Type | Description |
| --- | --- | --- |
| lba | <code>Number</code> | Logical Block Address |
| hpt | <code>Number</code> | Heads per Track |
| spt | <code>Number</code> | Sectors per Track |
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc