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