File: wi_xid.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (234 lines) | stat: -rw-r--r-- 7,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 *  wi_xid.c
 *
 *  $Id$
 *
 *  Functions to deal with XID structures
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <stdio.h>

#include "Dk.h"
#include "Wi/widv.h"
#include "Wi/wi_xid.h"


#define XID_DEBUG


char* uuid_bin_encode (void* uuid)
{
  char* encoded_str = dk_alloc_box (37, DV_STRING);

#ifdef XID_DEBUG
  if (box_length (uuid) != sizeof(uuid_t))
    GPF_T1 ("wrong uuid object received");
#endif
  uuid_unparse ((unsigned char *) uuid, encoded_str);
  return encoded_str;
}


void* uuid_bin_decode (const char* uuid_str)
{
  uuid_t * xid = (uuid_t *) dk_alloc_box (sizeof (uuid_t), DV_BIN);
#ifdef XID_DEBUG
  if (strlen (uuid_str) != 37)
    GPF_T1 ("wrong uuid string received");
#endif

  if (0 == uuid_parse ((const char*)uuid_str, (unsigned char *) xid))
    return xid;
  dk_free_box ((box_t) xid);
  return 0;
}

static char char_16_table[] = "0123456789abcdef";
static int char_r_16_table[256] =
/*	0	1	2	3	4	5	6	7	8	9	a	b	c       d        e       f */
/* 0 */{-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	     -1,       -1,      -1,
/* 1 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 2 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 3 */	0,	1,	2,	3,	4,	5,	6,	7,	8,	9,	-1,	-1,      -1,      -1,       -1,      -1,
/* 4 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 5 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 6 */	-1,	10,	11,	12,	13,	14,	15,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 7 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 8 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* 9 */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* a */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* b */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* c */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* d */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* e */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1,
/* f */	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,	-1,      -1,      -1,       -1,      -1
};




/* encoded xid string is sequence of 3 encoded longs in
   network byte order + remains encoded data... */
#if 1 /* Aleksey, enable it when the java code will be updated */

static
int encode_ptr (unsigned char * ptr, int length, unsigned char * target)
{
  unsigned char * xid_ptr = ptr;
  unsigned char * xid_end = ptr+length;
  unsigned char * where = target;

  while (xid_ptr != xid_end)
    {
      where[0] = char_16_table [xid_ptr[0] / 16];
      where[1] = char_16_table [xid_ptr[0] % 16];
      where+=2;
      xid_ptr++;
    }
  return (int) (where - target);
}

static
int decode_ptr (const unsigned char * from, int length, unsigned char * where)
{
  const unsigned char * str_ptr = from;
  const unsigned char * str_e = from + 2 * ( length / 2 ); /* only even numbers are allowed */
  unsigned char * xid_ptr = where;

  while (str_ptr != str_e)
    {
#ifdef XID_DEBUG
      if (char_r_16_table[(int)str_ptr[0]] == -1 ||
	  char_r_16_table[(int)str_ptr[1]] == -1)
	GPF_T1 ("wrong xid string");
#endif
      xid_ptr[0] = (char) char_r_16_table[(int)str_ptr[0]] * 16 + (char) char_r_16_table[(int)str_ptr[1]];

      str_ptr+=2;
      xid_ptr++;
    }
  return length;
}


char*
xid_bin_encode (void* _xid)
{
  unsigned char* str_ptr;
  unsigned char* encoded_str = str_ptr = (unsigned char *) dk_alloc_box (sizeof(virtXID)*2 + 1 ,DV_STRING);
  virtXID * xid = (virtXID *) _xid;
  int length;
  int32 tmp_l;

  /*log_debug (">> %d %d %d", xid->formatID, xid->gtrid_length, xid->bqual_length); */

  LONG_SET_NA (&tmp_l, xid->formatID);
  length = encode_ptr ((unsigned char *) &tmp_l, sizeof (tmp_l), encoded_str);

  LONG_SET_NA (&tmp_l, xid->gtrid_length);
  length += encode_ptr ((unsigned char *) &tmp_l, sizeof (tmp_l), encoded_str + length);

  LONG_SET_NA (&tmp_l, xid->bqual_length);
  length += encode_ptr ((unsigned char *) &tmp_l, sizeof (tmp_l), encoded_str + length);

  length += encode_ptr ((unsigned char *) xid->data, XIDDATASIZE, encoded_str + length);

  encoded_str[length] = 0;

  /*log_debug (">> %s", encoded_str);*/
  return (char *) encoded_str;
}

void*
xid_bin_decode (const char* xid_str)
{
  int length = 0;
  virtXID * xid;
  caddr_t xid_ptr;
  int32 tmp;
  if (strlen(xid_str) != (sizeof(virtXID)*2))
    return 0;

  xid = (virtXID *) (xid_ptr = dk_alloc_box (sizeof (virtXID), DV_BIN));

  length = decode_ptr ((unsigned char*)xid_str, sizeof (int32) * 2, (unsigned char*) &tmp);
  xid->formatID = LONG_REF_NA (&tmp);

  length += decode_ptr ((unsigned char*) (xid_str + length), sizeof (int32) * 2, (unsigned char*) &tmp);
  xid->gtrid_length = LONG_REF_NA (&tmp);

  length += decode_ptr ((unsigned char*) (xid_str + length), sizeof (int32) * 2, (unsigned char*) &tmp);
  xid->bqual_length = LONG_REF_NA (&tmp);

  decode_ptr ((unsigned char*) (xid_str + length), (int) (strlen (xid_str) - length), (unsigned char*) xid->data);

  /*log_debug ("%s << %d %d %d", xid_str, xid->formatID, xid->gtrid_length, xid->bqual_length);*/

  return xid;

}


#else
char*
xid_bin_encode (void* xid)
{
  char* str_ptr;
  char* encoded_str = str_ptr = dk_alloc_box (sizeof(XID)*2 + 1 ,DV_STRING);
  unsigned char * xid_ptr = (unsigned char*)xid;
  unsigned char * xid_end = (unsigned char*)(xid) + sizeof(XID);
  while (xid_ptr != xid_end)
    {
      str_ptr[0] = char_16_table [xid_ptr[0] / 16];
      str_ptr[1] = char_16_table [xid_ptr[0] % 16];
      str_ptr+=2;
      xid_ptr++;
    }
  str_ptr[0]=0;
  return encoded_str;
}

void*
xid_bin_decode (const char* xid_str)
{
  XID * xid;
  caddr_t xid_ptr;
  const char * str_ptr = xid_str;
  if (strlen(xid_str) != (sizeof(XID)*2))
    return 0;

  xid = (XID *) (xid_ptr = dk_alloc_box (sizeof (XID), DV_BIN));
  while (str_ptr[0])
    {
#ifdef XID_DEBUG
      if (char_r_16_table[(int)str_ptr[0]] == -1 ||
	  char_r_16_table[(int)str_ptr[1]] == -1)
	GPF_T1 ("wrong xid string");
#endif
      xid_ptr[0] = (char) char_r_16_table[(int)str_ptr[0]] * 16 + (char) char_r_16_table[(int)str_ptr[1]];

      str_ptr+=2;
      xid_ptr++;
    }
  return xid;
}
#endif