fd1b4feff368e1efd9dccd83940d97e9526ae686
[idzebra-moved-to-github.git] / util / zint.c
1 /* $Id: zint.c,v 1.4 2006-12-22 13:48:20 adam Exp $
2    Copyright (C) 1995-2006
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 <idzebra/util.h>
25
26 void zebra_zint_encode(char **dst, zint pos)
27 {
28     unsigned char *bp = (unsigned char*) *dst;
29
30     while (pos > 127)
31     {
32          *bp++ = (unsigned char) (128 | (pos & 127));
33          pos = pos >> 7;
34     }
35     *bp++ = (unsigned char) pos;
36     *dst = (char *) bp;
37 }
38
39 void zebra_zint_decode(const char **src, zint *pos)
40 {
41     const unsigned char **bp = (const unsigned char **) src;
42     zint d = 0;
43     unsigned char c;
44     unsigned r = 0;
45
46     while (((c = *(*bp)++) & 128))
47     {
48         d += ((zint) (c & 127) << r);
49         r += 7;
50     }
51     d += ((zint) c << r);
52     *pos = d;
53 }
54
55 zint atozint(const char *src)
56 {
57 #if HAVE_ATOLL
58     return atoll(src);
59 #else
60     return atoi(src);
61 #endif
62 }
63
64 /*
65  * Local variables:
66  * c-basic-offset: 4
67  * indent-tabs-mode: nil
68  * End:
69  * vim: shiftwidth=4 tabstop=8 expandtab
70  */
71