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