source: trunk/src/data_sources/scream2ew/gputil.c @ 2148

Revision 2148, 2.8 KB checked in by paulf, 14 years ago (diff)

MKIII DM24 patch

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * gputil.c:
3 *
4 * Copyright (c) 2003 Guralp Systems Limited
5 * Author James McKenzie, contact <software@guralp.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23static char rcsid[] = "$Id$";
24
25/*
26 * $Log$
27 * Revision 1.2  2006/04/28 20:24:31  paulf
28 * MKIII DM24 patch
29 *
30 * Revision 1.1  2003/03/27 18:07:18  alex
31 * Initial revision
32 *
33 * Revision 1.6  2003/02/28 17:05:37  root
34 * #
35 *
36 */
37
38#include "project.h"
39
40int
41gp_uint8 (uint8_t * buf)
42{
43  return *buf;
44}
45
46int
47gp_uint16 (uint8_t * buf)
48{
49  return (buf[0] << 8) | buf[1];
50}
51
52int
53gp_uint24 (uint8_t * buf)
54{
55  return (buf[0] << 16) | (buf[1] << 8) | buf[2];
56}
57
58int
59gp_uint32 (uint8_t * buf)
60{
61  return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
62}
63
64int
65gp_int8 (uint8_t * buf)
66{
67  if ((*buf) & 0x80)
68    {
69      return ((int) *buf) - 0x100;
70    }
71  return gp_uint8 (buf);
72}
73
74int
75gp_int16 (uint8_t * buf)
76{
77  if (buf[0] & 0x80)
78    {
79      return ((buf[0] << 8) | buf[1]) - 0x10000;
80    }
81  return gp_uint16 (buf);
82}
83
84
85int
86gp_int24 (uint8_t * buf)
87{
88
89  if (buf[0] & 0x80)
90    {
91      return ((buf[0] << 16) | (buf[1] << 8) | buf[2]) - 0x1000000;
92    }
93  return gp_uint24 (buf);
94}
95
96int
97gp_int32 (uint8_t * buf)
98{
99  int ret;
100  if (buf[0] & 0x80)
101    {
102
103/*Bad voodoo below*/
104      ret = ((buf[1] << 16) | (buf[2] << 8) | buf[3]) - 0x1000000;
105      ret -= ((0xff - buf[0]) << 24);
106
107      return ret;
108    }
109  return gp_uint32 (buf);
110}
111
112
113int
114gp_a_to_base36 (char *a)
115{
116  int ret = 0;
117
118  while (*a)
119    {
120      ret *= 36;
121      if (((*a) >= '0') && ((*a) <= '9'))
122        ret += (*a) - '0';
123      if (((*a) >= 'a') && ((*a) <= 'z'))
124        ret += 10 + (*a) - 'a';
125      if (((*a) >= 'A') && ((*a) <= 'Z'))
126        ret += 10 + (*a) - 'A';
127      a++;
128    }
129
130  return ret;
131}
132
133
134char *
135gp_base36_to_a (int i)
136{
137  static char ret[8];
138  int j, k, pos = 7;
139
140  ret[pos] = 0;
141
142  /* bit 31 is the 'extended sysid' bit, if it is set then the next
143     5 bits are used for other purposes and we only have 26 bits of
144     base-36 characters */
145  if(i & 0x80000000) i &= 0x03FFFFFF;
146
147  while(i) {
148    k = (int) i / 36;
149    j = i - (k * 36);
150
151    ret[--pos] = (j < 10) ? (j + '0') : (j - 10 + 'A');
152    i = k;
153  }
154
155  return ret + pos;
156}
Note: See TracBrowser for help on using the repository browser.