🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.nntp
5 messages
5 total messages Started by Chris Thu, 06 Mar 2003 16:36
xBin library header
#64289
Author: Chris
Date: Thu, 06 Mar 2003 16:36
138 lines
4692 bytes
To facilitate incorporation into existing news clients, I figured writing
an xBin library would be the best way to get xBin in use out there in the
real world.  Here is the interface I'm working with so far.  I have a few
concerns right now...It's obviously a c++ header, would there be much of a
need for a C library, or C wrappers for these functions?  Also I'd like
the return type of base64decode to be something like a
basic_string<unsigned char*>, but I couldn't get that to work.  Anyway,
here it is:

#ifndef __xbin_h__
#define __xbin_h__

#include <string>
#include <vector>

const unsigned int xBinBlockSize = 22500;
const unsigned int xBinArticleSize = 1024 * 512000;

//
// Probably need something more intelligent for other platforms
//
typedef unsigned long long int uint64_t;
typedef unsigned int uint32_t;

//
// xBin uses SHA1 hashes which are 160 bits (20 bytes) long
//
typedef unsigned char hashT[20];

//
// A HashNode is an individual node in a hash tree
//
class HashNode
{
public:
	//
	// Make this node using the given data to create the hash
	//
	HashNode(const unsigned char* data, int bytes);

	//
	// Make this node with the given hash
	//
	HashNode(const hashT hash);

	//
	// Make this node the parent of the two given nodes
	//
	HashNode(HashNode *lChild, HashNode *rChild);

	//
	// Deleting a HashNode deletes its children as well!
	//
	~HashNode();

	//
	// Get the node that covers this block
	//
	HashNode* getNode(uint64_t block);

	HashNode *lChild, *rChild, *parent;
	uint64_t firstBlock, lastBlock;
	hashT hash;
};

//
// encode data in base64, without = padding
//
std::string base64encode(const unsigned char* input, int bytes);
std::string base64encode(const uint64_t input);

//
// decode a base64 string
// returns a new[]'d buffer
// size of the buffer is placed in bytes
//
unsigned char* base64decode(const char* input, int& bytes);

//
// decode a base64 string as an integer
//
uint64_t base64decodeInt(const char* input, int bytes);

//
// MessageInfo contains all of the information that goes into a
// xBin Message-ID header
//
struct MessageInfo
{
	uint64_t fileSize; // size of file in bytes
	uint64_t offset; // offset of this article in blocks
	uint64_t nBlocks; // size of this article in blocks
	hashT hash; // root hash
};

//
// Generates an xBin message id string that can be included in the
// Message-ID header
//
std::string xBinGetMessageId(const MessageInfo& m);

//
// Parses Message-ID header for xBin tags
// Returns 1 on success, and fills in m appropriately
// Returns 0 on failure
//
int xBinParseMessageId(const std::string& header, MessageInfo& m);

//
// Builds a hashtree from the given file
//
HashNode* xBinBuildHashTree(const char* filename);

//
// Returns a list of base64 encoded hash values to include in the
// Content-SHA1-Tree header
//
std::string xBinBuildContentTree(const HashNode* root, uint64_t
	firstBlock, uint64_t lastblock);

//
// Verifies that a Content-SHA1-Tree header is valid
// Returns 1 on success
// Returns 0 on failure
//
// dataNodes is a list of hashnodes representing the hashes of the data
// being verified
//
// nBlocks is the total number of blocks in the file (leaves on the tree)
//  (nBlocks == root->lastBlock + 1)
//
// begin and end specify what range of data is covered by dataNodes
//
int xBinVerifyContentTree(const std::string& header, 
		std::vector<HashNode*> dataNodes, uint64_t nBlocks, 
		uint64_t begin, uint64_t end);
#endif // __xbin_h__
Re: xBin library header
#64293
Author: Russ Allbery
Date: Thu, 06 Mar 2003 18:12
24 lines
1873 bytes
Chris <nobody@rogers.com> writes:

> #ifndef __xbin_h__
> #define __xbin_h__

A side note not related to the rest of your post:

This is a common but technically incorrect way of doing header guards.  At
least in ISO C, and I believe in ISO C++ as well, all identifiers
beginning with __ or with _ and a capital letter are reserved for the
implementation.  That means that user code is not supposed to use such
identifiers anywhere, since you may be trampling on identifiers used by
the compiler.

_xbin_h_ would be okay.  I generally just use XBIN_H without any
underscores, relying on the _H to keep header guard macros out of the way
of other things.

-- 
Russ Allbery (rra@stanford.edu)             <http://www.eyrie.org/~eagle/>

          Please post questions rather than mailing me directly.
     <http://www.eyrie.org/~eagle/faqs/questions.html> explains why.
Re: xBin library header
#64294
Author: Chris
Date: Thu, 06 Mar 2003 18:34
15 lines
1784 bytes
On Thu, 06 Mar 2003 10:12:36 -0800, Russ Allbery wrote:
> This is a common but technically incorrect way of doing header guards.  At
> least in ISO C, and I believe in ISO C++ as well, all identifiers
> beginning with __ or with _ and a capital letter are reserved for the
> implementation.  That means that user code is not supposed to use such
> identifiers anywhere, since you may be trampling on identifiers used by
> the compiler.
> 
> _xbin_h_ would be okay.  I generally just use XBIN_H without any
> underscores, relying on the _H to keep header guard macros out of the way
> of other things.

Thanks for the tip!  I never knew that.

Re: xBin library header
#64304
Author: J.B. Moreno
Date: Fri, 07 Mar 2003 00:37
13 lines
1350 bytes
In article <pan.2003.03.06.16.36.27.43643@rogers.com>, 
 Chris <nobody@rogers.com> wrote:

> I have a few concerns right now...It's obviously a c++ header, would
> there be much of a need for a C library, or C wrappers for these
> functions?

I'd suspect so, so much that I'd suggest going with C and using a c++
wrapper if you could...

-- 
J.B. Moreno
Re: xBin library header
#64307
Author: Chris
Date: Fri, 07 Mar 2003 15:41
139 lines
5070 bytes
On Thu, 06 Mar 2003 19:37:49 -0500, J.B. Moreno wrote:
> I'd suspect so, so much that I'd suggest going with C and using a c++
> wrapper if you could...

No real need here for a C++ specific wrapper if a good C interface exists.  I
just prefer C++, and wasn't sure if others used C at all.

Here's my attempt at a C interface:

#ifndef XBIN_H_
#define XBIN_H_
#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

const uint64_t xbBlockSize;
const uint64_t xbArticleSize;

/*
 * xBin uses SHA-1 for its hashing
 * SHA-1 hashes are 160 bits = 20 bytes
 */
typedef unsigned char xbHashT[20];

/*
 * An individual node in a hash tree
 * lChild and rChild point to the children
 * firstBlock, lastBlock refer to the range of blocks this node covers
 */
typedef struct _xbHashNode
{
	struct _xbHashNode *lChild, *rChild, *parent;
	xbHashT hash;
	uint64_t firstBlock, lastBlock;
} xbHashNode;

/*
 * Information pertaining to one article
 * Specifies file size (in bytes), offset (in blocks), and
 * number of blocks covered by this article as well as first 72 bits
 * of the hash.
 *
 * if nBlocks == 0 then this article is assumed to be the last one
 */
typedef struct
{
	uint64_t fileSize, offset, nBlocks;
	xbHashT hash;
} xbMessageInfo;

/*
 * Base64 encoding / decoding stuff
 * xBin doesn't pad the end of base64 encoded data with =
 */

/*
 * Encodes n bytes of input and places result into output
 * If output == NULL then a new string is allocated and returned
 * If output != NULL then it must be at least 1.34n bytes large
 */
char* xbBase64Encode(const unsigned char* input, uint32_t n, char* output);

/*
 * Encodes input and places result into output
 * If output == NULL then a new string is allocated and returned
 * If output != NULL then it must be at least 11 bytes large
 */
char* xbBase64EncodeInt(uint64_t input, char* output);

/*
 * Decodes n bytes of input and places result in output
 * output must be at least 0.75n bytes large
 * The number of bytes decoded is returned
 */
int xbBase64Decode(const char* input, uint32_t n, unsigned char* output);

/*
 * Decodes n bytes of input and interprets result as an integer
 */
uint64_t xbBase64DecodeInt(const char* input, uint32_t n);

/*
 * Returns the root of the hashtree of the file specified, or NULL
 * if there was an error (couldn't open the file)
 */
xbHashNode* xbMakeTree(const char* filename);

/*
 * Deletes a tree and all its children
 */
void xbDeleteTree(xbHashNode* root);

/*
 * Makes a string suitable for a usenet Message-ID header.
 * 
 * If output == NULL then a new string is allocated and returned
 * If output != NULL then it must be at least 60 bytes large
 */
char* xbMakeMessageId(const xbMessageInfo* m, char* output);

/*
 * Parses a usenet Message-ID header and fills in m's fields
 * Returns 1 on success, 0 on failure
 */
int xbParseMessageId(const char* header, xbMessageInfo* m);

/*
 * Returns an allocated string representing the complement to the tree
 * spanned by [firstBlock, lastBlock]
 *
 * This string is suitable for an xBin Content-SHA1-Tree header
 */
char* xbMakeTreeComplement(const xbHashNode* root, uint64_t firstBlock,
		uint64_t lastBlock);

/*
 * Verifies that the hashes listed in dataHashes combined with the tree
 * complement as specified by header result in the root hash (first element
 * in the tree complement header)
 *
 * firstBlock is the first block that dataHashes covers, nBlocks is the
 * number of blocks covered.
 *
 * dataHashes is an array nBlocks long of hashes of the data to be verified
 *
 * Returns 1 on success, 0 on failure
 */
int xbVerifyTreeComplement(const char* header, uint64_t firstBlock, 
		uint64_t nBlocks, xbHashT dataHashes[]);

#ifdef __cplusplus
}
#endif
#endif /* XBIN_H */


Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads