rcsnum.c revision 1.16
1/* $OpenBSD: rcsnum.c,v 1.16 2005/09/19 15:45:16 niallo 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 !defined(RCSPROG) 278 if ((nump->rn_len > 2) && (nump->rn_id[nump->rn_len - 1] == 0) 279 && (cvs_cmdop != CVS_OP_LOG)) { 280#else 281 if ((nump->rn_len > 2) && (nump->rn_id[nump->rn_len - 1] == 0)) { 282#endif 283 /* 284 * Look for ".0.x" at the end of the branch number. 285 */ 286 if ((s = strrchr(str, '.')) != NULL) { 287 *s--; 288 while (*s != '.') 289 *s--; 290 291 /* 292 * If we have a "magic" branch, adjust it 293 * so the .0. is removed. 294 */ 295 if (!strncmp(s, RCS_MAGIC_BRANCH, 296 strlen(RCS_MAGIC_BRANCH))) { 297 nump->rn_id[nump->rn_len - 1] = 298 nump->rn_id[nump->rn_len]; 299 nump->rn_len--; 300 } 301 } 302 } 303 304 nump->rn_len++; 305 return (nump->rn_len); 306 307rcsnum_aton_failed: 308 nump->rn_len = 0; 309 free(nump->rn_id); 310 nump->rn_id = NULL; 311 return (-1); 312} 313 314/* 315 * rcsnum_inc() 316 * 317 * Increment the revision number specified in <num>. 318 * Returns a pointer to the <num> on success, or NULL on failure. 319 */ 320RCSNUM * 321rcsnum_inc(RCSNUM *num) 322{ 323 if (num->rn_id[num->rn_len - 1] == RCSNUM_MAXNUM) 324 return (NULL); 325 num->rn_id[num->rn_len - 1]++; 326 return (num); 327} 328 329/* 330 * rcsnum_revtobr() 331 * 332 * Retrieve the branch number associated with the revision number <num>. 333 * If <num> is a branch revision, the returned value will be the same 334 * number as the argument. 335 */ 336RCSNUM * 337rcsnum_revtobr(const RCSNUM *num) 338{ 339 RCSNUM *brnum; 340 341 if (num->rn_len < 2) 342 return (NULL); 343 344 if ((brnum = rcsnum_alloc()) == NULL) 345 return (NULL); 346 347 rcsnum_cpy(num, brnum, 0); 348 349 if (!RCSNUM_ISBRANCH(brnum)) 350 brnum->rn_len--; 351 352 return (brnum); 353} 354 355/* 356 * rcsnum_brtorev() 357 * 358 * Retrieve the initial revision number associated with the branch number <num>. 359 * If <num> is a revision number, an error will be returned. 360 */ 361RCSNUM * 362rcsnum_brtorev(const RCSNUM *brnum) 363{ 364 RCSNUM *num; 365 366 if (!RCSNUM_ISBRANCH(brnum)) { 367 return (NULL); 368 } 369 370 if ((num = rcsnum_alloc()) == NULL) 371 return (NULL); 372 373 if (rcsnum_setsize(num, brnum->rn_len + 1) < 0) { 374 rcsnum_free(num); 375 return (NULL); 376 } 377 378 rcsnum_cpy(brnum, num, brnum->rn_len); 379 num->rn_id[num->rn_len++] = 1; 380 381 return (num); 382} 383 384static int 385rcsnum_setsize(RCSNUM *num, u_int len) 386{ 387 void *tmp; 388 389 tmp = realloc(num->rn_id, len * sizeof(u_int16_t)); 390 if (tmp == NULL) { 391 rcs_errno = RCS_ERR_ERRNO; 392 return (-1); 393 } 394 395 num->rn_id = (u_int16_t *)tmp; 396 num->rn_len = len; 397 return (0); 398} 399