f249468ce6de4e7e7bc6837faac56ce9eb2334c3
[idzebra-moved-to-github.git] / util / su_codec.c
1 /* $Id: su_codec.c,v 1.2 2007-01-15 15:10:26 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <assert.h>
27
28 #include <yaz/xmalloc.h>
29 #include <su_codec.h>
30
31 int key_SU_encode (int ch, char *out)
32 {
33     int i;
34     for (i = 0; ch; i++)
35     {
36         if (ch >= 64)
37             out[i] = 65 + (ch & 63);
38         else
39             out[i] = 1 + ch;
40         ch = ch >> 6;
41     }
42     return i;
43     /* in   out
44        0     1
45        1     2
46        63    64
47        64    65, 2
48        65    66, 2
49        127   128, 2
50        128   65, 3
51        191   128, 3
52        192   65, 4
53     */
54 }
55
56 int key_SU_decode (int *ch, const unsigned char *out)
57 {
58     int len = 1;
59     int fact = 1;
60     *ch = 0;
61     for (len = 1; *out >= 65; len++, out++)
62     {
63         *ch += (*out - 65) * fact;
64         fact <<= 6;
65     }
66     *ch += (*out - 1) * fact;
67     return len;
68 }
69
70 /*
71  * Local variables:
72  * c-basic-offset: 4
73  * indent-tabs-mode: nil
74  * End:
75  * vim: shiftwidth=4 tabstop=8 expandtab
76  */
77