/* Read and write "compressed" signed but positive longs; these are stored
** as follows: MSB first, only 7 bits used per int.  High bit == 1 means
** there are more bytes coming to this integer; high bit==0 means this is
** the last one.
*/

#include <stdio.h>
#include <assert.h>

long CompressedIntRead(FILE *fp)
{
    long value = 0;
    int c;
    do
    {
	if((c = getc(fp)) == EOF)
	    return EOF;
	value = (value << 7) | (c&127);
    } while(c & 128);

    return value;
}

long CompressedIntWrite(long value, FILE *fp)
{
    char buf[sizeof(long)+1];
    int i = 0;
    long storeValue = value;
    assert(value >= 0);

    /* generate the characters low byte first, then output backwards */
    do
    {
	buf[i++] = value & 127;
	value >>= 7;
    } while(value);

    while(--i)
	if(putc(buf[i] | 128, fp) == EOF)
	    return EOF;
    if(putc(buf[0], fp) == EOF)
	return EOF;
    return storeValue;
}
