blob: fc0af86da69605c9f5d7fbc968654d5dd67f78a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
#ifndef _ber_h
#define _ber_h
#include <stdio.h>
#include <errno.h>
typedef unsigned int ber_t;
#define BER_MAX_BYTES (sizeof(ber_t) + 1)
inline int ber_buf2value(const unsigned char* buf, int buf_len, ber_t& result) {
result = 0;
unsigned int bits = 0;
int length = 1;
while(*buf & 0x80) {
if(bits > sizeof(ber_t) * 8) return EINVAL;
result |= (*buf & 0x7f) << bits;
bits += 7;
buf++;
length++;
if(length > buf_len) return EINVAL;
}
result |= (*buf & 0x7f) << bits;
return length;
}
inline int ber_file2value(FILE* fp, ber_t& result) {
result = 0;
unsigned int bits = 0;
int c;
int length = 1;
while((c = fgetc(fp)) != EOF && (c & 0x80)) {
if(bits > sizeof(ber_t) * 8) return EINVAL;
result |= (c & 0x7f) << bits;
bits += 7;
length++;
}
if(c == EOF) return EINVAL;
result |= (c & 0x7f) << bits;
return length;
}
inline int ber_value2buf(unsigned char* buf, int buf_len, ber_t value)
{
if(buf_len <= 0) return EINVAL;
int buf_idx = 0;
buf[buf_idx++] = (value & 0x7f);
while(value >>= 7) {
if(buf_idx >= buf_len) return EINVAL;
buf[buf_idx - 1] |= 0x80;
buf[buf_idx++] = (value & 0x7f);
}
return buf_idx;
}
inline int ber_value2file(FILE* fp, ber_t value)
{
int length = 1;
unsigned char current;
current = (value & 0x7f);
while(value >>= 7) {
current |= 0x80;
if(fputc(current, fp) == EOF) return EINVAL;
current = (value & 0x7f);
length++;
}
if(fputc(current, fp) == EOF) return EINVAL;
return length;
}
#endif /* _ber_h */
|