🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #64307

Re: xBin library header

#64307
From: 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 */


Technical Details
Message-ID: <pan.2003.03.07.15.41.06.166515@rogers.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!news03.bloor.is.net.cable.rogers.com!news04.bloor.is.net.cable.rogers.com.POSTED!not-for-mail
References: <pan.2003.03.06.16.36.27.43643@rogers.com> <060320031937494307%planb@newsreaders.com>

Show Full Headers
Complete Article Headers
Message-ID: <pan.2003.03.07.15.41.06.166515@rogers.com>
Subject: Re: xBin library header
Date: Fri, 07 Mar 2003 15:41:06 GMT
From: "Chris" <nobody@rogers.com>
References: <pan.2003.03.06.16.36.27.43643@rogers.com> <060320031937494307%planb@newsreaders.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!cyclone.bc.net!sjc70.webusenet.com!news.webusenet.com!news03.bloor.is.net.cable.rogers.com!news04.bloor.is.net.cable.rogers.com.POSTED!not-for-mail
User-Agent: Pan/0.13.4 (She had eyes like strange sins. (Debian GNU/Linux))
Newsgroups: news.software.nntp
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Lines: 138
NNTP-Posting-Host: 24.114.32.59
X-Complaints-To: abuse@rogers.com
X-Trace: news04.bloor.is.net.cable.rogers.com 1047051666 24.114.32.59 (Fri, 07 Mar 2003 10:41:06 EST)
NNTP-Posting-Date: Fri, 07 Mar 2003 10:41:06 EST