File: address.c

package info (click to toggle)
pilot-link 0.12.5-dfsg-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 6,868 kB
  • ctags: 5,811
  • sloc: ansic: 53,153; sh: 10,459; java: 2,584; perl: 2,247; python: 1,044; makefile: 991; yacc: 662; cpp: 551; xml: 39
file content (313 lines) | stat: -rw-r--r-- 7,741 bytes parent folder | download | duplicates (5)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * $Id: address.c,v 1.30 2006/11/22 22:52:25 adridg Exp $
 *
 * address.c:  Translate Pilot address book data formats
 * (c) 1996, Kenneth Albanowski
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This library 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 Library
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pi-macros.h"
#include "pi-address.h"

#define hi(x) (((x) >> 4) & 0x0f)
#define lo(x) ((x) & 0x0f)
#define pair(x,y) (((x) << 4) | (y))

/***********************************************************************
 *
 * Function:    free_Address
 *
 * Summary:	Free the members of an address structure
 *
 * Parameters:  Address_t*
 *
 * Returns:     void
 *
 ***********************************************************************/
void
free_Address(Address_t *addr)
{
	int 	i;

	for (i = 0; i < 19; i++)
		if (addr->entry[i]) {
			free(addr->entry[i]);
			addr->entry[i] = NULL;
		}
}


/***********************************************************************
 *
 * Function:    unpack_Address
 *
 * Summary:     Fill in the address structure based on the raw record 
 *		data
 *
 * Parameters:  Address_t*, pi_buffer_t *buf
 *
 * Returns:     -1 on error, 0 on success
 *
 ***********************************************************************/
int
unpack_Address(Address_t *addr, const pi_buffer_t *buf, addressType type)
{
	unsigned long	contents,
			v;
	size_t		ofs;

	if (type != address_v1)
		/* Don't support anything else yet */
		return -1;

	if (buf == NULL || buf->data == NULL || buf->used < 9)
		return -1;

	/* get_byte(buffer); gapfill */
	addr->showPhone     = hi(get_byte(buf->data + 1));
	addr->phoneLabel[4] = lo(get_byte(buf->data + 1));
	addr->phoneLabel[3] = hi(get_byte(buf->data + 2));
	addr->phoneLabel[2] = lo(get_byte(buf->data + 2));
	addr->phoneLabel[1] = hi(get_byte(buf->data + 3));
	addr->phoneLabel[0] = lo(get_byte(buf->data + 3));

	contents = get_long(buf->data + 4);

	/* get_byte(buf->data+8) offset */

	ofs = 9;

	/* if(flag & 0x1) { 
	   addr->lastname = strdup((buf->data + ofs);
	   ofs += strlen((buf->data + ofs)) + 1;
	   } else {
	   addr->lastname = 0;
	   } */

	for (v = 0; v < 19; v++) {
		if (contents & (1 << v)) {
			if ((buf->used - ofs) < 1)
				return 0;
			addr->entry[v] = strdup((char *) (buf->data + ofs));
                  	ofs += strlen(addr->entry[v]) + 1;
		} else {
			addr->entry[v] = 0;
		}
	}

	return 0;
}


/***********************************************************************
 *
 * Function:    pack_Address
 *
 * Summary:     Fill in the raw address record data based on the 
 *		address structure
 *
 * Parameters:  Address_t*, pi_buffer_t *buf of record, record type
 *
 * Returns:     -1 on error, 0 on success.
 *
 ***********************************************************************/
int
pack_Address(const Address_t *addr, pi_buffer_t *buf, addressType type)
{
	unsigned int	l,
			destlen = 9;

	unsigned char *buffer;
	unsigned long 	contents,
			v,
			phoneflag;

	unsigned char offset;

	if (addr == NULL || buf == NULL)
		return -1;

	if (type != address_v1)
		/* Don't support anything else yet */
		return -1;

	for (v = 0; v < 19; v++)
		if (addr->entry[v] && strlen(addr->entry[v]))
			destlen += strlen(addr->entry[v]) + 1;

	pi_buffer_expect (buf, destlen);
	buf->used = destlen;

	buffer 		= buf->data + 9;
	phoneflag 	= 0;
	contents 	= 0;
	offset 		= 0;

	for (v = 0; v < 19; v++) {
		if (addr->entry[v] && strlen(addr->entry[v])) {
			if (v == entryCompany)
				offset =
				    (unsigned char) (buffer - buf->data) - 8;
			contents |= (1 << v);
			l = strlen(addr->entry[v]) + 1;
			memcpy(buffer, addr->entry[v], l);
			buffer += l;
		}
	}

	phoneflag = ((unsigned long) addr->phoneLabel[0]) << 0;
	phoneflag |= ((unsigned long) addr->phoneLabel[1]) << 4;
	phoneflag |= ((unsigned long) addr->phoneLabel[2]) << 8;
	phoneflag |= ((unsigned long) addr->phoneLabel[3]) << 12;
	phoneflag |= ((unsigned long) addr->phoneLabel[4]) << 16;
	phoneflag |= ((unsigned long) addr->showPhone) << 20;

	set_long(buf->data, phoneflag);
	set_long(buf->data + 4, contents);
	set_byte(buf->data + 8, offset);

	return 0;
}


/***********************************************************************
 *
 * Function:    unpack_AddressAppInfo
 *
 * Summary:     Fill in the app info structure based on the raw app 
 *		info data
 *
 * Parameters:  AddressAppInfo_t*, char * to record, record length
 *
 * Returns:     The necessary length of the buffer if record is NULL,
 *		or 0 on error, the length of the data used from the 
 *		buffer otherwise
 *
 ***********************************************************************/
int
unpack_AddressAppInfo(AddressAppInfo_t *ai, const unsigned char *record, size_t len)
{
	size_t 	i,
		destlen = 4 + 16 * 22 + 2 + 2;

	unsigned char *start = record;
	unsigned long r;

	ai->type = address_v1;

	i = unpack_CategoryAppInfo(&ai->category, record, len);
	if (!record)
		return i + destlen;
	if (!i)
		return i;
	record += i;
	len -= i;

	if (len < destlen)
		return 0;

	r = get_long(record);
	for (i = 0; i < 22; i++)
		ai->labelRenamed[i] = !!(r & (1 << i));

	record += 4;
	memcpy(ai->labels, record, 16 * 22);
	record += 16 * 22;
	ai->country = get_short(record);
	record += 2;
	ai->sortByCompany = get_byte(record);
	record += 2;

	for (i = 3; i < 8; i++)
		strcpy(ai->phoneLabels[i - 3], ai->labels[i]);
	for (i = 19; i < 22; i++)
		strcpy(ai->phoneLabels[i - 19 + 5], ai->labels[i]);

	return (record - start);
}

/***********************************************************************
 *
 * Function:    pack_AddressAppInfo
 *
 * Summary:     Fill in the raw app info record data based on the app
 *		info structure
 *
 * Parameters:  AddressAppInfo_t*, char * to record, record length
 *
 * Returns:     The length of the buffer required if record is NULL,
 *		or 0 on error, the length of the data used from the
 *		buffer otherwise
 *
 ***********************************************************************/
int
pack_AddressAppInfo(const AddressAppInfo_t *ai, unsigned char *record, size_t len)
{
	int 	i;
	size_t	destlen = 4 + 16 * 22 + 2 + 2;
	unsigned char *pos = record;
	unsigned long r;

	i = pack_CategoryAppInfo(&ai->category, record, len);
	if (!record)
		return destlen + i;
	if (!i)
		return i;

	pos += i;
	len -= i;

	for (i = 3; i < 8; i++)
		strcpy(ai->phoneLabels[i - 3], ai->labels[i]);
	for (i = 19; i < 22; i++)
		strcpy(ai->phoneLabels[i - 19 + 5], ai->labels[i]);

	memset(pos, 0, destlen);

	r = 0;
	for (i = 0; i < 22; i++)
		if (ai->labelRenamed[i])
			r |= (1 << i);
	set_long(pos, r);
	pos += 4;

	memcpy(pos, ai->labels, 16 * 22);
	pos += 16 * 22;
	set_short(pos, ai->country);
	pos += 2;
	set_byte(pos, ai->sortByCompany);
	pos += 2;

	for (i = 3; i < 8; i++)
		strcpy(ai->phoneLabels[i - 3], ai->labels[i]);
	for (i = 19; i < 22; i++)
		strcpy(ai->phoneLabels[i - 19 + 5], ai->labels[i]);

	return (pos - record);
}

/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */