aboutsummaryrefslogtreecommitdiff
path: root/sys/ki/kiencode.x
blob: 463579e4aab5bfe87a2d1f0c02ea385837c484f1 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# KI_ENCODE -- Encode an integer into the indicated number of chars for
# transmission between machines via a byte stream.

procedure ki_encode (data, str, nchars)

long	data			# data value to be encoded
char	str[nchars]		# output chars
int	nchars			# number of chars to be encoded

int	i
long	v, nv

begin
	v = abs (data)

	do i = 1, nchars {
	    nv = v / 128
	    str[i] = v - (nv * 128)
	    v = nv
	}

	if (data < 0)
	    if (str[1] == 0)
		str[1] = -128
	    else
		str[1] = -str[1]
end


# KI_DECODE -- Decode the long integer value encoded by ki_encode, returning
# the long integer value as the function value.

long procedure ki_decode (str, nchars)

char	str[ARB]		# string to be decoded
int	nchars			# number of chars to decode

bool	neg
int	pow, i
long	sum

begin
	sum = str[1]
	neg = (sum < 0)
	if (neg)
	    if (sum == -128)
		sum = 0
	    else
		sum = -sum

	pow = 1

	do i = 2, nchars {
	    pow = pow * 128
	    sum = sum + (str[i] * pow)
	}

	if (neg)
	    return (-sum)
	else
	    return (sum)
end