rcsnum.c revision 1.5
1/*	$OpenBSD: rcsnum.c,v 1.5 2004/12/10 19:19:11 jfb 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 "rcs.h"
35#include "log.h"
36
37
38
39/*
40 * rcsnum_alloc()
41 *
42 * Allocate an RCS number structure.
43 */
44RCSNUM*
45rcsnum_alloc(void)
46{
47	RCSNUM *rnp;
48
49	rnp = (RCSNUM *)malloc(sizeof(*rnp));
50	if (rnp == NULL) {
51		cvs_log(LP_ERR, "failed to allocate RCS number");
52		return (NULL);
53	}
54	rnp->rn_len = 0;
55	rnp->rn_id = NULL;
56
57	return (rnp);
58}
59
60
61/*
62 * rcsnum_free()
63 *
64 * Free an RCSNUM structure previously allocated with rcsnum_alloc().
65 */
66
67void
68rcsnum_free(RCSNUM *rn)
69{
70	if (rn->rn_id != NULL)
71		free(rn->rn_id);
72	free(rn);
73}
74
75
76/*
77 * rcsnum_tostr()
78 * Returns a pointer to the start of <buf> on success, or NULL on failure.
79 */
80char*
81rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen)
82{
83	u_int i;
84	char tmp[8];
85
86	if (nump->rn_len == 0) {
87		buf[0] = '\0';
88		return (buf);
89	}
90
91	snprintf(buf, blen, "%u", nump->rn_id[0]);
92	for (i = 1; i < nump->rn_len; i++) {
93		snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]);
94		strlcat(buf, tmp, blen);
95	}
96
97	return (buf);
98}
99
100
101/*
102 * rcsnum_cpy()
103 *
104 * Copy the number stored in <nsrc> in the destination <ndst> up to <depth>
105 * numbers deep.
106 * Returns 0 on success, or -1 on failure.
107 */
108int
109rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth)
110{
111	u_int len;
112	size_t sz;
113	void *tmp;
114
115	len = nsrc->rn_len;
116	if ((depth != 0) && (len > depth))
117		len = depth;
118	sz = len * sizeof(u_int16_t);
119
120	tmp = realloc(ndst->rn_id, sz);
121	if (tmp == NULL) {
122		cvs_log(LP_ERR, "failed to reallocate RCSNUM");
123		return (-1);
124	}
125
126	ndst->rn_id = (u_int16_t *)tmp;
127	ndst->rn_len = len;
128	memcpy(ndst->rn_id, nsrc->rn_id, sz);
129	return (0);
130}
131
132
133/*
134 * rcsnum_cmp()
135 *
136 * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than
137 * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>.
138 * The <depth> argument specifies how many numbers deep should be checked for
139 * the result.  A value of 0 means that the depth will be the minimum of the
140 * two numbers.
141 */
142int
143rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth)
144{
145	int res;
146	u_int i;
147	size_t slen;
148
149	slen = MIN(n1->rn_len, n2->rn_len);
150	if ((depth != 0) && (slen > depth))
151		slen = depth;
152
153	for (i = 0; i < slen; i++) {
154		res = n1->rn_id[i] - n2->rn_id[i];
155		if (res < 0)
156			return (1);
157		else if (res > 0)
158			return (-1);
159	}
160
161	if (n1->rn_len > n2->rn_len)
162		return (-1);
163	else if (n2->rn_len > n1->rn_len)
164		return (1);
165
166	return (0);
167}
168
169
170/*
171 * rcsnum_aton()
172 *
173 * Translate the string <str> containing a sequence of digits and periods into
174 * its binary representation, which is stored in <nump>.  The address of the
175 * first byte not part of the number is stored in <ep> on return, if it is not
176 * NULL.
177 * Returns 0 on success, or -1 on failure.
178 */
179int
180rcsnum_aton(const char *str, char **ep, RCSNUM *nump)
181{
182	const char *sp;
183	void *tmp;
184
185	if (nump->rn_id == NULL) {
186		nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t));
187		if (nump->rn_id == NULL)
188			return (-1);
189	}
190
191	nump->rn_len = 0;
192	nump->rn_id[nump->rn_len] = 0;
193
194	for (sp = str;; sp++) {
195		if (!isdigit(*sp) && (*sp != '.'))
196			break;
197
198		if (*sp == '.') {
199			nump->rn_len++;
200			tmp = realloc(nump->rn_id,
201			    (nump->rn_len + 1) * sizeof(u_int16_t));
202			if (tmp == NULL) {
203				free(nump->rn_id);
204				nump->rn_len = 0;
205				nump->rn_id = NULL;
206				return (-1);
207			}
208			nump->rn_id = (u_int16_t *)tmp;
209			nump->rn_id[nump->rn_len] = 0;
210			continue;
211		}
212
213		nump->rn_id[nump->rn_len] *= 10;
214		nump->rn_id[nump->rn_len] += *sp - 0x30;
215	}
216
217	if (ep != NULL)
218		*(const char **)ep = sp;
219
220	nump->rn_len++;
221	return (nump->rn_len);
222}
223