Skip to content

Commit

Permalink
Name change
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrswift committed Jun 25, 2016
1 parent cf8879f commit 8227ee6
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 112 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ Swift-Cardinal Object Notation (SCON)
A minimal node module allowing the reading and writing of data in the SCON format.
The SCON format, created by Aritz J Beobide-Cardinal (ARitz Cracker) and James R Swift, is an extendable binary file format that was created to store data more efficiantly than the popular JSON standard.

Get the module here [![npm version](https://badge.fury.io/js/sbtag.svg)](https://badge.fury.io/js/sbtag)
Get the module here [![npm version](https://badge.fury.io/js/scon.svg)](https://badge.fury.io/js/scon)

## Installation

```shell
npm install sbtag --save
npm install scon --save
```

## Usage

```js
var sbtag = require('sbtag');
var scon = require('scon');

var encoded = sbtag.encode( { hello: "world!", five: 5 } ).result;
var decoded = sbtag.decode( encoded ).result;
var encoded = scon.encode( { hello: "world!", five: 5 } ).result;
var decoded = scon.decode( encoded ).result;
```

## Contributing
Expand Down
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Swift Binary Tag
* https://github.com/JamesxX/sbtag
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
* Licensed under the GNU GPLv3 license.
Expand All @@ -10,20 +10,20 @@

var Buffer = require('buffer').Buffer;

var sbtag = {}
var sbtagUtil = require( "./lib/util.js" );
var scon = {}
var sconUtil = require( "./lib/util.js" );

sbtagUtil.loadSubmodule( sbtag, "lib/conf.js" );
sbtagUtil.loadSubmodule( sbtag, "lib/error.js" );
sbtagUtil.loadSubmodule( sbtag, "lib/encode.js" );
sbtagUtil.loadSubmodule( sbtag, "lib/decode.js" );
sconUtil.loadSubmodule( scon, "lib/conf.js" );
sconUtil.loadSubmodule( scon, "lib/error.js" );
sconUtil.loadSubmodule( scon, "lib/encode.js" );
sconUtil.loadSubmodule( scon, "lib/decode.js" );

if ( true ){
var encoded = sbtag.encode( { hello: "world!!", hi: "bye", five: 5, pi: 3.14159, object:{amazing:true,true:{"mind":"fuck"}}, six: 6 ,arr:["wan","too","free",{"for":4},[1,2,3,4,5]]});
var encoded = scon.encode( { hello: "world!!", hi: "bye", five: 5, pi: 3.14159, object:{amazing:true,true:{"mind":"fuck"}}, six: 6 ,arr:["wan","too","free",{"for":4},[1,2,3,4,5]]});
console.log( "encoded:", encoded );

var decoded = sbtag.decode( encoded.result );
var decoded = scon.decode( encoded.result );
console.log( "decoded:", JSON.stringify(decoded));
}

module.exports = sbtag;
module.exports = scon;
30 changes: 15 additions & 15 deletions lib/conf.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/**
* Swift Binary Tag
* https://github.com/JamesxX/sbtag
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
* Licensed under the GNU GPLv3 license.
*/

"use strict";

var sbtagConf = {}
var sconConf = {}

sbtagConf.magicNumber = "sbtag"
sbtagConf.bufferSize = 50 * 1024 // 50kb
sconConf.magicNumber = "scon"
sconConf.bufferSize = 50 * 1024 // 50kb

sbtagConf.endBlock = 0;
sbtagConf.int32 = 1;
sbtagConf.float32 = 2;
sbtagConf.string = 3;
sbtagConf.compound = 4;
sbtagConf.array = 5;
sbtagConf.null = 6;
sbtagConf.undefined = 7;
sbtagConf.boolean = 8;
sbtagConf.reserved = 255;
sconConf.endBlock = 0;
sconConf.int32 = 1;
sconConf.float32 = 2;
sconConf.string = 3;
sconConf.compound = 4;
sconConf.array = 5;
sconConf.null = 6;
sconConf.undefined = 7;
sconConf.boolean = 8;
sconConf.reserved = 255;

module.exports = sbtagConf;
module.exports = sconConf;
54 changes: 27 additions & 27 deletions lib/decode.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
/**
* Swift Binary Tag
* https://github.com/JamesxX/sbtag
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
* Licensed under the GNU GPLv3 license.
*/

"use strict";

var sbtag = {};
var sbtagUtil = require( "./util.js" );
var scon = {};
var sconUtil = require( "./util.js" );

sbtagUtil.loadSubmodule( sbtag, "lib/conf.js" );
sbtagUtil.loadSubmodule( sbtag, "lib/error.js" );
sconUtil.loadSubmodule( scon, "lib/conf.js" );
sconUtil.loadSubmodule( scon, "lib/error.js" );

sbtag.decodeValue = function(buf,type,_offset){
scon.decodeValue = function(buf,type,_offset){

var result;

switch( type ){

// int32
case sbtag.int32:
case scon.int32:
result = buf.readInt32BE(_offset); _offset += 4;
break;

// float32
case sbtag.float32:
case scon.float32:
result = buf.readFloatBE(_offset); _offset += 4;
break;

// string <n>
case sbtag.string:
case scon.string:
var varLength = buf.readUInt32BE(_offset); _offset += 4;
result = buf.toString(null, _offset, _offset + varLength); _offset += varLength;
break;

// compound
case sbtag.compound:
var compound = sbtag.decode( buf, false, _offset );
case scon.compound:
var compound = scon.decode( buf, false, _offset );
result = compound.result; _offset = compound.offset;
break;

case sbtag.array:
case scon.array:
result = [];
var len = buf.readUInt32BE(_offset); _offset += 4;
for (var i=0;i<len;i+=1){
var itype = buf.readUInt8(_offset); _offset += 1;
var a = sbtag.decodeValue(buf,itype,_offset)
var a = scon.decodeValue(buf,itype,_offset)
result[ i ] = a[0];
_offset = a[1];
}
break;

case sbtag.null:
case scon.null:
result = null;
break;

case sbtag.endBlock:
case sbtag.undefined:
case scon.endBlock:
case scon.undefined:
break;

case sbtag.boolean:
case scon.boolean:
result = buf.readUInt8(_offset) !== 0; _offset += 1;
break;

// end block
case sbtag.endBlock:
case scon.endBlock:
break;

default:
throw new sbtag.Exception( sbtag.errorCodes.unknownObjectType, type );
throw new scon.Exception( scon.errorCodes.unknownObjectType, type );
break;

}
Expand All @@ -79,25 +79,25 @@ sbtag.decodeValue = function(buf,type,_offset){

}

sbtag.decode = function ( string, verify, offset ){
scon.decode = function ( string, verify, offset ){

var result = {};
var buf = new Buffer( string ,'binary');

// confirm we are dealing with an sbtag file
if ( verify && buf.toString( null, 0,sbtag.magicNumber.length ) != sbtag.magicNumber ){
// confirm we are dealing with an scon file
if ( verify && buf.toString( null, 0,scon.magicNumber.length ) != scon.magicNumber ){

throw new sbtag.Exception( sbtag.errorCodes.noMagicNumber );
throw new scon.Exception( scon.errorCodes.noMagicNumber );
return false;

}

var _offset = offset || sbtag.magicNumber.length;
var _offset = offset || scon.magicNumber.length;

while ( _offset < buf.length && _offset >= 0){

var type = buf.readUInt8(_offset); _offset += 1;
if (type==sbtag.endBlock){
if (type==scon.endBlock){

//_offset += 1;
return { result: result, offset: _offset };
Expand All @@ -107,7 +107,7 @@ sbtag.decode = function ( string, verify, offset ){
var nameLength = buf.readUInt16BE(_offset); _offset += 2;

var name = buf.toString(null, _offset, _offset + nameLength); _offset += nameLength;
var a = sbtag.decodeValue(buf,type,_offset)
var a = scon.decodeValue(buf,type,_offset)

result[ name ] = a[0];
_offset = a[1];
Expand All @@ -119,4 +119,4 @@ sbtag.decode = function ( string, verify, offset ){

}

module.exports = sbtag;
module.exports = scon;
58 changes: 29 additions & 29 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/**
* Swift Binary Tag
* https://github.com/JamesxX/sbtag
* https://github.com/JamesxX/scon
*
* Copyright (c) Aritz Beobide-Cardinal, 2016 James R Swift
* Licensed under the GNU GPLv3 license.
*/

"use strict";

var sbtag = {};
var sbtagUtil = require( "./util.js" );
var scon = {};
var sconUtil = require( "./util.js" );

sbtagUtil.loadSubmodule( sbtag, "lib/conf.js" );
sbtagUtil.loadSubmodule( sbtag, "lib/error.js" );
sconUtil.loadSubmodule( scon, "lib/conf.js" );
sconUtil.loadSubmodule( scon, "lib/error.js" );

sbtag.writeKey = function(buf,key,offset){
scon.writeKey = function(buf,key,offset){

if (key!=null){

Expand All @@ -27,7 +27,7 @@ sbtag.writeKey = function(buf,key,offset){
return offset;
}

sbtag.writeValue = function(buf,key,value,offset){
scon.writeValue = function(buf,key,value,offset){

switch( typeof value ){

Expand All @@ -36,24 +36,24 @@ sbtag.writeValue = function(buf,key,value,offset){
// Integer
if ( value % 1 === 0 ){

buf.writeUInt8( sbtag.int32, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
buf.writeUInt8( scon.int32, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeInt32BE( value, offset ); offset += 4;

// Float
} else {

buf.writeUInt8( sbtag.float32, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
buf.writeUInt8( scon.float32, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeFloatBE( value, offset ); offset += 4;

}

break;

case "string":
buf.writeUInt8( sbtag.string, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
buf.writeUInt8( scon.string, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeUInt32BE( value.length, offset ); offset += 4;
buf.write( value, offset ); offset += value.length;
break;
Expand All @@ -62,35 +62,35 @@ sbtag.writeValue = function(buf,key,value,offset){

if (Array.isArray(value)){

buf.writeUInt8( sbtag.array, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
buf.writeUInt8( scon.array, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeUInt32BE( value.length, offset ); offset += 4;
for (var i=0;i<value.length;i+=1){
offset = sbtag.writeValue(buf,null,value[i],offset)
offset = scon.writeValue(buf,null,value[i],offset)
}
//null is a type of object!

}else if (value == null){

buf.writeUInt8( sbtag.null, offset ); offset += 1;
buf.writeUInt8( scon.null, offset ); offset += 1;

}else{

buf.writeUInt8( sbtag.compound, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
offset = sbtag.encode( value, buf, offset ).offset;
buf.writeUInt8( scon.compound, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
offset = scon.encode( value, buf, offset ).offset;

}
break;

case "boolean":
buf.writeUInt8( sbtag.boolean, offset ); offset += 1;
offset = sbtag.writeKey(buf,key,offset);
buf.writeUInt8( scon.boolean, offset ); offset += 1;
offset = scon.writeKey(buf,key,offset);
buf.writeUInt8( value | 0, offset ); offset += 1;//A boolean is 87.5% wasted space :^)
break;

case "undefined":
buf.writeUInt8( sbtag.undefined, offset ); offset += 1;
buf.writeUInt8( scon.undefined, offset ); offset += 1;
break;

}
Expand All @@ -99,29 +99,29 @@ sbtag.writeValue = function(buf,key,value,offset){

}

sbtag.encode = function ( object, buf, offset, bsize ){
scon.encode = function ( object, buf, offset, bsize ){

if (buf==null){
buf = new Buffer(bsize || sbtag.bufferSize);
buf = new Buffer(bsize || scon.bufferSize);
buf.fill(255);
}

offset = offset | 0;

// write magic number
if ( offset == 0 ){
buf.write( sbtag.magicNumber, offset ); offset += sbtag.magicNumber.length;
buf.write( scon.magicNumber, offset ); offset += scon.magicNumber.length;
}

Object.keys(object).forEach(function(key) {

var value = object[ key ];
offset=sbtag.writeValue(buf,key,value,offset);
offset=scon.writeValue(buf,key,value,offset);
});

buf.writeUInt8( sbtag.endBlock, offset ); offset += 1;
buf.writeUInt8( scon.endBlock, offset ); offset += 1;
return { result: buf.toString( 'binary', 0, offset ), offset: offset };

}

module.exports = sbtag;
module.exports = scon;
Loading

0 comments on commit 8227ee6

Please sign in to comment.