rcsnum.c revision 1.38
1/* $OpenBSD: rcsnum.c,v 1.38 2006/05/31 18:24:55 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 "includes.h" 28 29#include "cvs.h" 30#include "log.h" 31#include "rcs.h" 32 33static void rcsnum_setsize(RCSNUM *, u_int); 34static char *rcsnum_itoa(u_int16_t, char *, size_t); 35 36int rcsnum_flags; 37 38/* 39 * rcsnum_alloc() 40 * 41 * Allocate an RCS number structure and return a pointer to it. 42 */ 43RCSNUM * 44rcsnum_alloc(void) 45{ 46 RCSNUM *rnp; 47 48 rnp = xmalloc(sizeof(*rnp)); 49 rnp->rn_len = 0; 50 rnp->rn_id = NULL; 51 52 return (rnp); 53} 54 55/* 56 * rcsnum_parse() 57 * 58 * Parse a string specifying an RCS number and return the corresponding RCSNUM. 59 */ 60RCSNUM * 61rcsnum_parse(const char *str) 62{ 63 char *ep; 64 RCSNUM *num; 65 66 num = rcsnum_alloc(); 67 if (rcsnum_aton(str, &ep, num) < 0 || *ep != '\0') { 68 rcsnum_free(num); 69 num = NULL; 70 if (*ep != '\0') 71 rcs_errno = RCS_ERR_BADNUM; 72 } 73 74 return (num); 75} 76 77/* 78 * rcsnum_free() 79 * 80 * Free an RCSNUM structure previously allocated with rcsnum_alloc(). 81 */ 82void 83rcsnum_free(RCSNUM *rn) 84{ 85 if (rn->rn_id != NULL) 86 xfree(rn->rn_id); 87 xfree(rn); 88} 89 90/* 91 * rcsnum_tostr() 92 * 93 * Format the RCS number <nump> into a human-readable dot-separated 94 * representation and store the resulting string in <buf>, which is of size 95 * <blen>. 96 * Returns a pointer to the start of <buf>. On failure <buf> is set to 97 * an empty string. 98 */ 99char * 100rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen) 101{ 102 u_int i; 103 char tmp[8]; 104 size_t len; 105 106 if (nump == NULL || nump->rn_len == 0) { 107 buf[0] = '\0'; 108 return (buf); 109 } 110 111 strlcpy(buf, rcsnum_itoa(nump->rn_id[0], buf, blen), blen); 112 for (i = 1; i < nump->rn_len; i++) { 113 len = strlcat(buf, ".", blen); 114 if (len >= blen) 115 fatal("rcsnum_tostr: overflow 1"); 116 117 len = strlcat(buf, 118 rcsnum_itoa(nump->rn_id[i], tmp, sizeof(tmp)), blen); 119 if (len >= blen) 120 fatal("rcsnum_tostr: overflow 2"); 121 } 122 123 return (buf); 124} 125 126static char * 127rcsnum_itoa(u_int16_t num, char *buf, size_t len) 128{ 129 u_int16_t i; 130 char *p; 131 132 if (num == 0) 133 return "0"; 134 135 p = buf + len - 1; 136 i = num; 137 bzero(buf, len); 138 while (i) { 139 *--p = '0' + (i % 10); 140 i /= 10; 141 } 142 return (p); 143} 144 145/* 146 * rcsnum_cpy() 147 * 148 * Copy the number stored in <nsrc> in the destination <ndst> up to <depth> 149 * numbers deep. If <depth> is 0, there is no depth limit. 150 */ 151void 152rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth) 153{ 154 u_int len; 155 void *tmp; 156 157 len = nsrc->rn_len; 158 if (depth != 0 && len > depth) 159 len = depth; 160 161 tmp = xrealloc(ndst->rn_id, len, sizeof(len)); 162 ndst->rn_id = tmp; 163 ndst->rn_len = len; 164 /* Overflow checked in xrealloc(). */ 165 (void)memcpy(ndst->rn_id, nsrc->rn_id, len * sizeof(len)); 166} 167 168/* 169 * rcsnum_cmp() 170 * 171 * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than 172 * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>. 173 * The <depth> argument specifies how many numbers deep should be checked for 174 * the result. A value of 0 means that the depth will be the minimum of the 175 * two numbers. 176 */ 177int 178rcsnum_cmp(RCSNUM *n1, RCSNUM *n2, u_int depth) 179{ 180 int res; 181 u_int i; 182 size_t slen; 183 184 if (!rcsnum_differ(n1, n2)) 185 return (0); 186 187 slen = MIN(n1->rn_len, n2->rn_len); 188 if (depth != 0 && slen > depth) 189 slen = depth; 190 191 for (i = 0; i < slen; i++) { 192 res = n1->rn_id[i] - n2->rn_id[i]; 193 if (res < 0) 194 return (1); 195 else if (res > 0) 196 return (-1); 197 } 198 199 if (n1->rn_len > n2->rn_len) 200 return (-1); 201 else if (n2->rn_len > n1->rn_len) 202 return (1); 203 204 return (0); 205} 206 207/* 208 * rcsnum_aton() 209 * 210 * Translate the string <str> containing a sequence of digits and periods into 211 * its binary representation, which is stored in <nump>. The address of the 212 * first byte not part of the number is stored in <ep> on return, if it is not 213 * NULL. 214 * Returns 0 on success, or -1 on failure. 215 */ 216int 217rcsnum_aton(const char *str, char **ep, RCSNUM *nump) 218{ 219 u_int32_t val; 220 const char *sp; 221 void *tmp; 222 char *s; 223 224 if (nump->rn_id == NULL) 225 nump->rn_id = xmalloc(sizeof(*(nump->rn_id))); 226 227 nump->rn_len = 0; 228 nump->rn_id[0] = 0; 229 230 for (sp = str;; sp++) { 231 if (!isdigit(*sp) && (*sp != '.')) 232 break; 233 234 if (*sp == '.') { 235 if (nump->rn_len >= RCSNUM_MAXLEN - 1) { 236 rcs_errno = RCS_ERR_BADNUM; 237 goto rcsnum_aton_failed; 238 } 239 240 nump->rn_len++; 241 tmp = xrealloc(nump->rn_id, 242 nump->rn_len + 1, sizeof(*(nump->rn_id))); 243 nump->rn_id = tmp; 244 nump->rn_id[nump->rn_len] = 0; 245 continue; 246 } 247 248 val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30); 249 if (val > RCSNUM_MAXNUM) 250 fatal("RCSNUM overflow!"); 251 252 nump->rn_id[nump->rn_len] = val; 253 } 254 255 if (ep != NULL) 256 *(const char **)ep = sp; 257 258 /* 259 * Handle "magic" RCS branch numbers. 260 * 261 * What are they? 262 * 263 * Magic branch numbers have an extra .0. at the second farmost 264 * rightside of the branch number, so instead of having an odd 265 * number of dot-separated decimals, it will have an even number. 266 * 267 * Now, according to all the documentation i've found on the net 268 * about this, cvs does this for "efficiency reasons", i'd like 269 * to hear one. 270 * 271 * We just make sure we remove the .0. from in the branch number. 272 * 273 * XXX - for compatibility reasons with GNU cvs we _need_ 274 * to skip this part for the 'log' command, apparently it does 275 * show the magic branches for an unknown and probably 276 * completely insane and not understandable reason in that output. 277 * 278 */ 279 if (nump->rn_len > 2 && nump->rn_id[nump->rn_len - 1] == 0 280 && !(rcsnum_flags & RCSNUM_NO_MAGIC)) { 281 /* 282 * Look for ".0.x" at the end of the branch number. 283 */ 284 if ((s = strrchr(str, '.')) != NULL) { 285 s--; 286 while (*s != '.') 287 s--; 288 289 /* 290 * If we have a "magic" branch, adjust it 291 * so the .0. is removed. 292 */ 293 if (!strncmp(s, RCS_MAGIC_BRANCH, 294 strlen(RCS_MAGIC_BRANCH))) { 295 nump->rn_id[nump->rn_len - 1] = 296 nump->rn_id[nump->rn_len]; 297 nump->rn_len--; 298 } 299 } 300 } 301 302 /* We can't have a single-digit rcs number. */ 303 if (nump->rn_len == 0) { 304 tmp = xrealloc(nump->rn_id, 305 nump->rn_len + 1, sizeof(*(nump->rn_id))); 306 nump->rn_id = tmp; 307 nump->rn_id[nump->rn_len + 1] = 0; 308 nump->rn_len++; 309 } 310 311 nump->rn_len++; 312 return (nump->rn_len); 313 314rcsnum_aton_failed: 315 nump->rn_len = 0; 316 xfree(nump->rn_id); 317 nump->rn_id = NULL; 318 return (-1); 319} 320 321/* 322 * rcsnum_inc() 323 * 324 * Increment the revision number specified in <num>. 325 * Returns a pointer to the <num> on success, or NULL on failure. 326 */ 327RCSNUM * 328rcsnum_inc(RCSNUM *num) 329{ 330 if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM) 331 return (NULL); 332 num->rn_id[num->rn_len - 1]++; 333 return (num); 334} 335 336/* 337 * rcsnum_dec() 338 * 339 * Decreases the revision number specified in <num>, if doing so will not 340 * result in an ending value below 1. E.g. 4.2 will go to 4.1 but 4.1 will 341 * be returned as 4.1. 342 */ 343RCSNUM * 344rcsnum_dec(RCSNUM *num) 345{ 346 /* XXX - Is it an error for the number to be 0? */ 347 if (num->rn_id[num->rn_len - 1] <= 1) 348 return (num); 349 num->rn_id[num->rn_len - 1]--; 350 return (num); 351} 352 353/* 354 * rcsnum_revtobr() 355 * 356 * Retrieve the branch number associated with the revision number <num>. 357 * If <num> is a branch revision, the returned value will be the same 358 * number as the argument. 359 */ 360RCSNUM * 361rcsnum_revtobr(const RCSNUM *num) 362{ 363 RCSNUM *brnum; 364 365 if (num->rn_len < 2) 366 return (NULL); 367 368 brnum = rcsnum_alloc(); 369 rcsnum_cpy(num, brnum, 0); 370 371 if (!RCSNUM_ISBRANCH(brnum)) 372 brnum->rn_len--; 373 374 return (brnum); 375} 376 377/* 378 * rcsnum_brtorev() 379 * 380 * Retrieve the initial revision number associated with the branch number <num>. 381 * If <num> is a revision number, an error will be returned. 382 */ 383RCSNUM * 384rcsnum_brtorev(const RCSNUM *brnum) 385{ 386 RCSNUM *num; 387 388 if (!RCSNUM_ISBRANCH(brnum)) { 389 return (NULL); 390 } 391 392 num = rcsnum_alloc(); 393 rcsnum_setsize(num, brnum->rn_len + 1); 394 rcsnum_cpy(brnum, num, brnum->rn_len); 395 num->rn_id[num->rn_len++] = 1; 396 397 return (num); 398} 399 400static void 401rcsnum_setsize(RCSNUM *num, u_int len) 402{ 403 void *tmp; 404 405 tmp = xrealloc(num->rn_id, len, sizeof(*(num->rn_id))); 406 num->rn_id = tmp; 407 num->rn_len = len; 408} 409 410int 411rcsnum_differ(RCSNUM *r1, RCSNUM *r2) 412{ 413 int i, len; 414 415 if (r1->rn_len != r2->rn_len) 416 return (1); 417 418 len = MIN(r1->rn_len, r2->rn_len); 419 for (i = 0; i < len; i++) { 420 if (r1->rn_id[i] != r2->rn_id[i]) 421 return (1); 422 } 423 424 return (0); 425} 426