ber.h

Go to the documentation of this file.
00001 //
00002 // Part of the ht://Dig package   <http://www.htdig.org/>
00003 // Copyright (c) 1999, 2000, 2001 The ht://Dig Group
00004 // For copyright details, see the file COPYING in your distribution
00005 // or the GNU General Public License version 2 or later 
00006 // <http://www.gnu.org/copyleft/gpl.html>
00007 //
00008 
00009 #ifndef _ber_h
00010 #define _ber_h
00011 
00012 #include <stdio.h>
00013 #include <errno.h>
00014 
00015 typedef unsigned int ber_t;
00016 
00017 #define BER_MAX_BYTES (sizeof(ber_t) + 1)
00018 
00019 inline int ber_buf2value(const unsigned char* buf, int buf_len, ber_t& result) {
00020   result = 0;
00021   unsigned int bits = 0;
00022   int length = 1;
00023   while(*buf & 0x80) {
00024     if(bits > sizeof(ber_t) * 8) return EINVAL;
00025     result |= (*buf & 0x7f) << bits;
00026     bits += 7;
00027     buf++;
00028     length++;
00029     if(length > buf_len) return EINVAL;
00030   }
00031   result |= (*buf & 0x7f) << bits;
00032 
00033   return length;
00034 }
00035 
00036 inline int ber_file2value(FILE* fp, ber_t& result) {
00037   result = 0;
00038   unsigned int bits = 0;
00039   int c;
00040   int length = 1;
00041   while((c = fgetc(fp)) != EOF && (c & 0x80)) {
00042     if(bits > sizeof(ber_t) * 8) return EINVAL;
00043     result |= (c & 0x7f) << bits;
00044     bits += 7;
00045     length++;
00046   }
00047 
00048   if(c == EOF) return EINVAL;
00049 
00050   result |= (c & 0x7f) << bits;
00051 
00052   return length;
00053 }
00054 
00055 inline int ber_value2buf(unsigned char* buf, int buf_len, ber_t value)
00056 {
00057   if(buf_len <= 0) return EINVAL;
00058   int buf_idx = 0;
00059   buf[buf_idx++] = (value & 0x7f);
00060   while(value >>= 7) {
00061     if(buf_idx >= buf_len) return EINVAL;
00062     buf[buf_idx - 1] |= 0x80;
00063     buf[buf_idx++] = (value & 0x7f);
00064   }
00065   return buf_idx;
00066 }
00067 
00068 inline int ber_value2file(FILE* fp, ber_t value)
00069 {
00070   int length = 1;
00071   unsigned char current;
00072   current = (value & 0x7f);
00073   while(value >>= 7) {
00074     current |= 0x80;
00075     if(fputc(current, fp) == EOF) return EINVAL;
00076     current = (value & 0x7f);
00077     length++;
00078   }
00079   
00080   if(fputc(current, fp) == EOF) return EINVAL;
00081 
00082   return length;
00083 }
00084 
00085 #endif /* _ber_h */

Generated on Sun Jun 8 10:56:39 2008 for GNUmifluz by  doxygen 1.5.5