rcsnum.c revision 1.15
1/*	$OpenBSD: rcsnum.c,v 1.15 2005/08/02 11:48:56 joris Exp $	*/
2/*
3 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/param.h>
28
29#include <ctype.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#include "cvs.h"
35#include "log.h"
36#include "rcs.h"
37
38
39static int	rcsnum_setsize(RCSNUM *, u_int);
40
41
42/*
43 * rcsnum_alloc()
44 *
45 * Allocate an RCS number structure and return a pointer to it on success,
46 * or NULL on failure.
47 */
48RCSNUM *
49rcsnum_alloc(void)
50{
51	RCSNUM *rnp;
52
53	rnp = (RCSNUM *)malloc(sizeof(*rnp));
54	if (rnp == NULL) {
55		rcs_errno = RCS_ERR_ERRNO;
56		return (NULL);
57	}
58	rnp->rn_len = 0;
59	rnp->rn_id = NULL;
60
61	return (rnp);
62}
63
64/*
65 * rcsnum_parse()
66 *
67 * Parse a string specifying an RCS number and return the corresponding RCSNUM.
68 */
69RCSNUM *
70rcsnum_parse(const char *str)
71{
72	char *ep;
73	RCSNUM *num;
74
75	if ((num = rcsnum_alloc()) == NULL)
76		return (NULL);
77
78	if ((rcsnum_aton(str, &ep, num) < 0) || (*ep != '\0')) {
79		rcsnum_free(num);
80		num = NULL;
81		if (*ep != '\0')
82			rcs_errno = RCS_ERR_BADNUM;
83	}
84
85	return (num);
86}
87
88/*
89 * rcsnum_free()
90 *
91 * Free an RCSNUM structure previously allocated with rcsnum_alloc().
92 */
93void
94rcsnum_free(RCSNUM *rn)
95{
96	if (rn->rn_id != NULL)
97		free(rn->rn_id);
98	free(rn);
99}
100
101/*
102 * rcsnum_tostr()
103 *
104 * Format the RCS number <nump> into a human-readable dot-separated
105 * representation and store the resulting string in <buf>, which is of size
106 * <blen>.
107 * Returns a pointer to the start of <buf> on success, or NULL on failure.
108 */
109char *
110rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
111{
112	u_int i;
113	char tmp[8];
114
115	if ((nump == NULL) || (nump->rn_len == 0)) {
116		buf[0] = '\0';
117		return (buf);
118	}
119
120	snprintf(buf, blen, "%u", nump->rn_id[0]);
121	for (i = 1; i < nump->rn_len; i++) {
122		snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]);
123		strlcat(buf, tmp, blen);
124	}
125
126	return (buf);
127}
128
129/*
130 * rcsnum_cpy()
131 *
132 * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
133 * numbers deep.
134 * Returns 0 on success, or -1 on failure.
135 */
136int
137rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
138{
139	u_int len;
140	size_t sz;
141	void *tmp;
142
143	len = nsrc->rn_len;
144	if ((depth != 0) && (len > depth))
145		len = depth;
146	sz = len * sizeof(u_int16_t);
147
148	tmp = realloc(ndst->rn_id, sz);
149	if (tmp == NULL) {
150		rcs_errno = RCS_ERR_ERRNO;
151		return (-1);
152	}
153
154	ndst->rn_id = (u_int16_t *)tmp;
155	ndst->rn_len = len;
156	memcpy(ndst->rn_id, nsrc->rn_id, sz);
157	return (0);
158}
159
160/*
161 * rcsnum_cmp()
162 *
163 * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
164 * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
165 * The <depth> argument specifies how many numbers deep should be checked for
166 * the result.  A value of 0 means that the depth will be the minimum of the
167 * two numbers.
168 */
169int
170rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
171{
172	int res;
173	u_int i;
174	size_t slen;
175
176	slen = MIN(n1->rn_len, n2->rn_len);
177	if ((depth != 0) && (slen > depth))
178		slen = depth;
179
180	for (i = 0; i < slen; i++) {
181		res = n1->rn_id[i] - n2->rn_id[i];
182		if (res < 0)
183			return (1);
184		else if (res > 0)
185			return (-1);
186	}
187
188	if (n1->rn_len > n2->rn_len)
189		return (-1);
190	else if (n2->rn_len > n1->rn_len)
191		return (1);
192
193	return (0);
194}
195
196/*
197 * rcsnum_aton()
198 *
199 * Translate the string <str> containing a sequence of digits and periods into
200 * its binary representation, which is stored in <nump>.  The address of the
201 * first byte not part of the number is stored in <ep> on return, if it is not
202 * NULL.
203 * Returns 0 on success, or -1 on failure.
204 */
205int
206rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
207{
208	u_int32_t val;
209	const char *sp;
210	void *tmp;
211	char *s;
212
213	if (nump->rn_id == NULL) {
214		nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
215		if (nump->rn_id == NULL) {
216			rcs_errno = RCS_ERR_ERRNO;
217			return (-1);
218		}
219	}
220
221	nump->rn_len = 0;
222	nump->rn_id[0] = 0;
223
224	for (sp = str;; sp++) {
225		if (!isdigit(*sp) && (*sp != '.'))
226			break;
227
228		if (*sp == '.') {
229			if (nump->rn_len >= RCSNUM_MAXLEN - 1) {
230				rcs_errno = RCS_ERR_BADNUM;
231				goto rcsnum_aton_failed;
232			}
233
234			nump->rn_len++;
235			tmp = realloc(nump->rn_id,
236			    (nump->rn_len + 1) * sizeof(u_int16_t));
237			if (tmp == NULL)
238				goto rcsnum_aton_failed;
239			nump->rn_id = (u_int16_t *)tmp;
240			nump->rn_id[nump->rn_len] = 0;
241			continue;
242		}
243
244		val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30);
245		if (val > RCSNUM_MAXNUM) {
246			cvs_log(LP_ERR, "RCSNUM overflow");
247			goto rcsnum_aton_failed;
248		}
249
250		nump->rn_id[nump->rn_len] = val;
251	}
252
253	if (ep != NULL)
254		*(const char **)ep = sp;
255
256	/*
257	 * Handle "magic" RCS branch numbers.
258	 *
259	 * What are they?
260	 *
261	 * Magic branch numbers have an extra .0. at the second farmost
262	 * rightside of the branch number, so instead of having an odd
263	 * number of dot-separated decimals, it will have an even number.
264	 *
265	 * Now, according to all the documentation i've found on the net
266	 * about this, cvs does this for "efficiency reasons", i'd like
267	 * to hear one.
268	 *
269	 * We just make sure we remove the .0. from in the branch number.
270	 *
271	 * XXX - for compatibility reasons with GNU cvs we _need_
272	 * to skip this part for the 'log' command, apparently it does
273	 * show the magic branches for an unknown and probably
274	 * completely insane and not understandable reason in that output.
275	 *
276	 */
277	if ((nump->rn_len > 2) && (nump->rn_id[nump->rn_len - 1] == 0)
278	    && (cvs_cmdop != CVS_OP_LOG)) {
279		/*
280		 * Look for ".0.x" at the end of the branch number.
281		 */
282		if ((s = strrchr(str, '.')) != NULL) {
283			*s--;
284			while (*s != '.')
285				*s--;
286
287			/*
288			 * If we have a "magic" branch, adjust it
289			 * so the .0. is removed.
290			 */
291			if (!strncmp(s, RCS_MAGIC_BRANCH,
292			    strlen(RCS_MAGIC_BRANCH))) {
293				nump->rn_id[nump->rn_len - 1] =
294				    nump->rn_id[nump->rn_len];
295				nump->rn_len--;
296			}
297		}
298	}
299
300	nump->rn_len++;
301	return (nump->rn_len);
302
303rcsnum_aton_failed:
304	nump->rn_len = 0;
305	free(nump->rn_id);
306	nump->rn_id = NULL;
307	return (-1);
308}
309
310/*
311 * rcsnum_inc()
312 *
313 * Increment the revision number specified in <num>.
314 * Returns a pointer to the <num> on success, or NULL on failure.
315 */
316RCSNUM *
317rcsnum_inc(RCSNUM *num)
318{
319	if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM)
320		return (NULL);
321	num->rn_id[num->rn_len - 1]++;
322	return (num);
323}
324
325/*
326 * rcsnum_revtobr()
327 *
328 * Retrieve the branch number associated with the revision number <num>.
329 * If <num> is a branch revision, the returned value will be the same
330 * number as the argument.
331 */
332RCSNUM *
333rcsnum_revtobr(const RCSNUM *num)
334{
335	RCSNUM *brnum;
336
337	if (num->rn_len < 2)
338		return (NULL);
339
340	if ((brnum = rcsnum_alloc()) == NULL)
341		return (NULL);
342
343	rcsnum_cpy(num, brnum, 0);
344
345	if (!RCSNUM_ISBRANCH(brnum))
346		brnum->rn_len--;
347
348	return (brnum);
349}
350
351/*
352 * rcsnum_brtorev()
353 *
354 * Retrieve the initial revision number associated with the branch number <num>.
355 * If <num> is a revision number, an error will be returned.
356 */
357RCSNUM *
358rcsnum_brtorev(const RCSNUM *brnum)
359{
360	RCSNUM *num;
361
362	if (!RCSNUM_ISBRANCH(brnum)) {
363		return (NULL);
364	}
365
366	if ((num = rcsnum_alloc()) == NULL)
367		return (NULL);
368
369	if (rcsnum_setsize(num, brnum->rn_len + 1) < 0) {
370		rcsnum_free(num);
371		return (NULL);
372	}
373
374	rcsnum_cpy(brnum, num, brnum->rn_len);
375	num->rn_id[num->rn_len++] = 1;
376
377	return (num);
378}
379
380static int
381rcsnum_setsize(RCSNUM *num, u_int len)
382{
383	void *tmp;
384
385	tmp = realloc(num->rn_id, len * sizeof(u_int16_t));
386	if (tmp == NULL) {
387		rcs_errno = RCS_ERR_ERRNO;
388		return (-1);
389	}
390
391	num->rn_id = (u_int16_t *)tmp;
392	num->rn_len = len;
393	return (0);
394}
395