npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ctype

v0.5.5

Published

read and write binary structures and data types

Downloads

1,290,969

Readme

Node-CType is a way to read and write binary data in structured and easy to use format. Its name comes from the C header file.

To get started, simply clone the repository or use npm to install it. Once it is there, simply require it.

git clone git://github.com/rmustacc/node-ctype npm install ctype var mod_ctype = require('ctype')

There are two APIs that you can use, depending on what abstraction you'd like. The low level API let's you read and write individual integers and floats from buffers. The higher level API let's you read and write structures of these. To illustrate this, let's looks look at how we would read and write a binary encoded x,y point.

In C we would define this structure as follows:

typedef struct point { uint16_t p_x; uint16_t p_y; } point_t;

To read a binary encoded point from a Buffer, we first need to create a CType parser (where we specify the endian and other options) and add the typedef.

var parser = new mod_ctype.Parser({ endian: 'big' }); parser.typedef('point_t', [ { x: { type: 'uint16_t' } }, { y: { type: 'uint16_t' } } ]);

From here, given a buffer buf and an offset into it, we can read a point.

var out = parser.readData([ { point: { type: 'point_t' } } ], buffer, 0); console.log(out); { point: { x: 23, y: 42 } }

Another way to get the same information would be to use the low level methods. Note that these require you to manually deal with the offset. Here's how we'd get the same values of x and y from the buffer.

var x = mod_ctype.ruint16(buf, 'big', 0); var y = mod_ctype.ruint16(buf, 'big', 2); console.log(x + ', ' + y); 23, 42

The true power of this API comes from the ability to define and nest typedefs, just as you would in C. By default, the following types are defined by default. Note that they return a Number, unless indicated otherwise.

* int8_t
* int16_t
* int32_t
* int64_t (returns an array where val[0] << 32 + val[1] would be the value)
* uint8_t
* uint16_t
* uint32_t
* uint64_t (returns an array where val[0] << 32 + val[1] would be the value)
* float
* double
* char (either returns a buffer with that character or a uint8_t)
* char[] (returns an object with the buffer and the number of characters read which is either the total amount requested or until the first 0)

ctf2json integration:

Node-CType supports consuming the output of ctf2json. Once you read in a JSON file, all you have to do to add all the definitions it contains is:

var data, parser; data = JSON.parse(parsedJSONData); parser = mod_ctype.parseCTF(data, { endian: 'big' });

For more documentation, see the file README.old. Full documentation is in the process of being rewritten as a series of manual pages which will be available in the repository and online for viewing.

To read the ctio manual page simple run, from the root of the workspace:

man -Mman -s 3ctype ctio