/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1993-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* David Korn <[email protected]> *17* *18***********************************************************************/19#pragma prototyped20/*21* huffman coding routine to write pack format file header22*23* David Korn24* AT&T Laboratories25*/2627#include "huffman.h"2829/*30* This routine outputs a pack format header to <outfile> and returns31* the number of bytes in the header32*/3334int huffputhdr(register Huff_t *hp,Sfio_t *outfile)35{36register int i, c;37register Sfio_t *fp = outfile;38/* output magic number */39sfputc(fp,HUFFMAG1);40sfputc(fp,HUFFMAG2);41if(sizeof(Sfoff_t)>4 && hp->insize >= ((Sfoff_t)1)<<(4*CHAR_BIT))42{43sfputc(fp,hp->insize>>(7*CHAR_BIT));44sfputc(fp,hp->insize>>(6*CHAR_BIT));45sfputc(fp,hp->insize>>(5*CHAR_BIT));46sfputc(fp,hp->insize>>(4*CHAR_BIT));47sfputc(fp,0);48}49/* output the length and the dictionary */50sfputc(fp,hp->insize>>(3*CHAR_BIT));51sfputc(fp,hp->insize>>(2*CHAR_BIT));52sfputc(fp,hp->insize>>(CHAR_BIT));53sfputc(fp,hp->insize);54/* output number of levels and count for each level */55sfputc(fp,hp->maxlev);56for (i=1; i<hp->maxlev; i++)57sfputc(fp,hp->levcount[i]);58sfputc(fp,hp->levcount[hp->maxlev]-2);59/* output the characters */60for (i=1; i<=hp->maxlev; i++)61for (c=0; c < (1<<CHAR_BIT); c++)62if (hp->length[c] == i)63sfputc(fp,c);64return(huffhsize(hp));65}666768