File: util.c

package info (click to toggle)
coldsync 3.0%2Bpre3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,188 kB
  • ctags: 2,033
  • sloc: ansic: 20,386; perl: 2,302; cpp: 1,640; yacc: 1,102; lex: 802; makefile: 533; sh: 177
file content (290 lines) | stat: -rw-r--r-- 6,005 bytes parent folder | download | duplicates (6)
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
/* util.c
 *
 * Misc. utility functions.
 *
 *	Copyright (C) 1999, Andrew Arensburger.
 *	You may distribute this file under the terms of the Artistic
 *	License, as specified in the README file.
 *
 * The get_*() functions are used to extract values out of strings of
 * ubytes and convert them to the native format.
 * The put_*() functions, conversely, are used to take a value in the
 * native format, convert them to Palm (big-endian) format, and write
 * them to a ubyte string.
 *
 * $Id: util.c,v 1.4 2001/07/06 05:42:26 arensb Exp $
 */

#include "config.h"
#include <stdio.h>
#include <ctype.h>	/* For isprint() */
#include <pconn/util.h>

#ifndef EPOCH_1904
#  define EPOCH_1904	2082844800L	/* Difference, in seconds, between
					 * Palm's epoch (Jan. 1, 1904) and
					 * Unix's epoch (Jan. 1, 1970).
					 */
#endif	/* EPOCH_1904 */

/* XXX - Most of the functions below really ought to be inlined. Not sure
 * how to do this portably, though.
 */

INLINE ubyte
peek_ubyte(const ubyte *buf)
{
	return buf[0];
}

INLINE uword
peek_uword(const ubyte *buf)
{
	return ((uword) buf[0] << 8) |
		buf[1];
}

INLINE udword
peek_udword(const ubyte *buf)
{
	return ((uword) buf[0] << 24) |
		((uword) buf[1] << 16) |
		((uword) buf[2] << 8) |
		buf[3];
}

INLINE ubyte
get_ubyte(const ubyte **buf)
{
	ubyte retval;

	retval = peek_ubyte(*buf);
	*buf += SIZEOF_UBYTE;

	return retval;
}

INLINE void
put_ubyte(ubyte **buf, ubyte value)
{
	**buf = value;
	++(*buf);
}

INLINE uword
get_uword(const ubyte **buf)
{
	uword retval;

	retval = peek_uword(*buf);
	*buf += SIZEOF_UWORD;

	return retval;
}

INLINE void
put_uword(ubyte **buf, uword value)
{
	**buf = (value >> 8) & 0xff;
	++(*buf);
	**buf = value & 0xff;
	++(*buf);
}

INLINE udword
get_udword(const ubyte **buf)
{
	udword retval;

	retval = peek_udword(*buf);
	*buf += SIZEOF_UDWORD;

	return retval;
}

INLINE void
put_udword(ubyte **buf, udword value)
{
	**buf = (value >> 24) & 0xff;
	++(*buf);
	**buf = (value >> 16) & 0xff;
	++(*buf);
	**buf = (value >>  8) & 0xff;
	++(*buf);
	**buf = value & 0xff;
	++(*buf);
}

/* XXX - Figure out the timezone hairiness:
 * Palms don't have timezones. Hence, the Palm's epoch is Jan. 1, 1904 in
 * the local timezone.
 * Unless you're syncing across the network, in which case its epoch is
 * Jan. 1, 1904 in the timezone it happens to be in (which may not be the
 * same as the desktop's timezone).
 * Except that there are (I'm sure) tools that add timezones to the Palm.
 * These should be consulted.
 * Times generated locally are in the local timezone (i.e., the one that
 * the desktop machine is in).
 */

/* time_dlp2time_t
 * Convert the DLP time structure into a Unix time_t, and return it.
 */
time_t
time_dlp2time_t(const struct dlp_time *dlpt)
{
	struct tm tm;

	/* Convert the dlp_time into a struct tm, then just use mktime() to
	 * do the conversion.
	 */
	tm.tm_sec = dlpt->second;
	tm.tm_min = dlpt->minute;
	tm.tm_hour = dlpt->hour;
	tm.tm_mday = dlpt->day;
	tm.tm_mon = dlpt->month - 1;
	tm.tm_year = dlpt->year - 1900;
	tm.tm_wday = 0;
	tm.tm_yday = 0;
	tm.tm_isdst = 0;
#if HAVE_TM_ZONE
	tm.tm_gmtoff = 0;
	tm.tm_zone = NULL;
#else
/* XXX - ANSI doesn't allow #warning, and we're not using the timezone for
 * anything yet.
 */
/*  #warning You do not have tm_zone */
#endif

	return mktime(&tm);
}

/* time_dlp2palmtime
 * Convert a DLP time structure into a Palm time_t (number of seconds since
 * Jan. 1. 1904), and return it.
 */
udword
time_dlp2palmtime(const struct dlp_time *dlpt)
{
	time_t now;		/* The time, as a Unix time_t */
	struct tm tm;

	/* Convert the dlp_time into a struct tm, use mktime() to do the
	 * conversion, and add the difference in epochs.
	 */
	tm.tm_sec = dlpt->second;
	tm.tm_min = dlpt->minute;
	tm.tm_hour = dlpt->hour;
	tm.tm_mday = dlpt->day;
	tm.tm_mon = dlpt->month - 1;
	tm.tm_year = dlpt->year - 1900;
	tm.tm_wday = 0;
	tm.tm_yday = 0;
	tm.tm_isdst = 0;
#if HAVE_TM_ZONE
	tm.tm_gmtoff = 0;
	tm.tm_zone = NULL;
#endif

	now = mktime(&tm);
	now += EPOCH_1904;

	return now;
}

/* time_time_t2dlp
 * Convert a Unix time_t into a DLP time structure. Put the result in
 * 'dlpt'.
 */
void
time_time_t2dlp(const time_t t,
		struct dlp_time *dlpt)
{
	struct tm *tm;

	tm = localtime(&t);	/* Break 't' down into components */

	/* Copy the relevant fields over to 'dlpt' */
	dlpt->year = tm->tm_year + 1900;
	dlpt->month = tm->tm_mon + 1;
	dlpt->day = tm->tm_mday;
	dlpt->hour = tm->tm_hour;
	dlpt->minute = tm->tm_min;
	dlpt->second = tm->tm_sec;
}

/* time_palmtime2dlp

 * Convert a Palm time (seconds since the Jan. 1, 1904) to a DLP time
 * structure. Put the result in 'dlpt'.
 */
void
time_palmtime2dlp(const udword palmt,
		  struct dlp_time *dlpt)
{
	struct tm *tm;
	time_t t;

	/* Convert the Palm time to a Unix time_t */
	t = palmt - EPOCH_1904;

	/* Break the Unix time_t into components */
	tm = localtime(&t);

	/* Copy the relevant fields over to 'dlpt' */
	dlpt->year = tm->tm_year + 1900;
	dlpt->month = tm->tm_mon + 1;
	dlpt->day = tm->tm_mday;
	dlpt->hour = tm->tm_hour;
	dlpt->minute = tm->tm_min;
	dlpt->second = tm->tm_sec;
}

/* debug_dump
 * Dump the contents of an array of ubytes to stderr, for debugging.
 */
void
debug_dump(FILE *outfile, const char *prefix,
	   const ubyte *buf, const udword len)
{
	int lineoff;

	for (lineoff = 0; lineoff < len; lineoff += 16)
	{
		int i;

		fprintf(outfile, "%s ", prefix);
		for (i = 0; i < 16; i++)
		{
			if (lineoff + i < len)
			{
				/* Regular bytes */
				fprintf(outfile, "%02x ", buf[lineoff+i]);
			} else {
				/* Filler at the end of the line */
				fprintf(outfile, "   ");
			}
		}
		fprintf(outfile, "  | ");
		for (i = 0; i < 16; i++)
		{
			if (lineoff + i < len)
			{
				/* Regular bytes */
				if (isprint(buf[lineoff+i]))
					fprintf(outfile, "%c", buf[lineoff+i]);
				else
					fprintf(outfile, ".");
			} else
				break;
		}
		fprintf(outfile, "\n");
	}
}

/* This is for Emacs's benefit:
 * Local Variables: ***
 * fill-column:	75 ***
 * End: ***
 */