rcsnum.c revision 1.7
1/* $OpenBSD: rcsnum.c,v 1.7 2005/02/25 20:05:42 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_parse() 63 * 64 * Parse a string specifying an RCS number and return the corresponding RCSNUM. 65 */ 66RCSNUM* 67rcsnum_parse(const char *str) 68{ 69 char *ep; 70 RCSNUM *num; 71 72 if ((num = rcsnum_alloc()) == NULL) 73 return (NULL); 74 75 if (rcsnum_aton(str, &ep, num) < 0) { 76 rcsnum_free(num); 77 return (NULL); 78 } 79 80 return (num); 81} 82 83 84/* 85 * rcsnum_free() 86 * 87 * Free an RCSNUM structure previously allocated with rcsnum_alloc(). 88 */ 89void 90rcsnum_free(RCSNUM *rn) 91{ 92 if (rn->rn_id != NULL) 93 free(rn->rn_id); 94 free(rn); 95} 96 97 98/* 99 * rcsnum_tostr() 100 * Returns a pointer to the start of <buf> on success, or NULL on failure. 101 */ 102char* 103rcsnum_tostr(const RCSNUM *nump, char *buf, size_t blen) 104{ 105 u_int i; 106 char tmp[8]; 107 108 if (nump->rn_len == 0) { 109 buf[0] = '\0'; 110 return (buf); 111 } 112 113 snprintf(buf, blen, "%u", nump->rn_id[0]); 114 for (i = 1; i < nump->rn_len; i++) { 115 snprintf(tmp, sizeof(tmp), ".%u", nump->rn_id[i]); 116 strlcat(buf, tmp, blen); 117 } 118 119 return (buf); 120} 121 122 123/* 124 * rcsnum_cpy() 125 * 126 * Copy the number stored in <nsrc> in the destination <ndst> up to <depth> 127 * numbers deep. 128 * Returns 0 on success, or -1 on failure. 129 */ 130int 131rcsnum_cpy(const RCSNUM *nsrc, RCSNUM *ndst, u_int depth) 132{ 133 u_int len; 134 size_t sz; 135 void *tmp; 136 137 len = nsrc->rn_len; 138 if ((depth != 0) && (len > depth)) 139 len = depth; 140 sz = len * sizeof(u_int16_t); 141 142 tmp = realloc(ndst->rn_id, sz); 143 if (tmp == NULL) { 144 cvs_log(LP_ERR, "failed to reallocate RCSNUM"); 145 return (-1); 146 } 147 148 ndst->rn_id = (u_int16_t *)tmp; 149 ndst->rn_len = len; 150 memcpy(ndst->rn_id, nsrc->rn_id, sz); 151 return (0); 152} 153 154 155/* 156 * rcsnum_cmp() 157 * 158 * Compare the two numbers <n1> and <n2>. Returns -1 if <n1> is larger than 159 * <n2>, 0 if they are both the same, and 1 if <n2> is larger than <n1>. 160 * The <depth> argument specifies how many numbers deep should be checked for 161 * the result. A value of 0 means that the depth will be the minimum of the 162 * two numbers. 163 */ 164int 165rcsnum_cmp(const RCSNUM *n1, const RCSNUM *n2, u_int depth) 166{ 167 int res; 168 u_int i; 169 size_t slen; 170 171 slen = MIN(n1->rn_len, n2->rn_len); 172 if ((depth != 0) && (slen > depth)) 173 slen = depth; 174 175 for (i = 0; i < slen; i++) { 176 res = n1->rn_id[i] - n2->rn_id[i]; 177 if (res < 0) 178 return (1); 179 else if (res > 0) 180 return (-1); 181 } 182 183 if (n1->rn_len > n2->rn_len) 184 return (-1); 185 else if (n2->rn_len > n1->rn_len) 186 return (1); 187 188 return (0); 189} 190 191 192/* 193 * rcsnum_aton() 194 * 195 * Translate the string <str> containing a sequence of digits and periods into 196 * its binary representation, which is stored in <nump>. The address of the 197 * first byte not part of the number is stored in <ep> on return, if it is not 198 * NULL. 199 * Returns 0 on success, or -1 on failure. 200 */ 201int 202rcsnum_aton(const char *str, char **ep, RCSNUM *nump) 203{ 204 u_int32_t val; 205 const char *sp; 206 void *tmp; 207 208 if (nump->rn_id == NULL) { 209 nump->rn_id = (u_int16_t *)malloc(sizeof(u_int16_t)); 210 if (nump->rn_id == NULL) { 211 cvs_log(LP_ERRNO, "failed to allocate RCSNUM"); 212 return (-1); 213 } 214 } 215 216 nump->rn_len = 0; 217 nump->rn_id[0] = 0; 218 219 for (sp = str;; sp++) { 220 if (!isdigit(*sp) && (*sp != '.')) 221 break; 222 223 if (*sp == '.') { 224 if (nump->rn_len >= RCSNUM_MAXLEN - 1) { 225 cvs_log(LP_ERR, 226 "RCSNUM exceeds maximum length"); 227 goto rcsnum_aton_failed; 228 } 229 230 nump->rn_len++; 231 tmp = realloc(nump->rn_id, 232 (nump->rn_len + 1) * sizeof(u_int16_t)); 233 if (tmp == NULL) { 234 goto rcsnum_aton_failed; 235 } 236 nump->rn_id = (u_int16_t *)tmp; 237 nump->rn_id[nump->rn_len] = 0; 238 continue; 239 } 240 241 val = (nump->rn_id[nump->rn_len] * 10) + (*sp - 0x30); 242 if (val > RCSNUM_MAXNUM) { 243 cvs_log(LP_ERR, "RCSNUM overflow"); 244 goto rcsnum_aton_failed; 245 } 246 247 nump->rn_id[nump->rn_len] = val; 248 } 249 250 if (ep != NULL) 251 *(const char **)ep = sp; 252 253 nump->rn_len++; 254 return (nump->rn_len); 255 256rcsnum_aton_failed: 257 nump->rn_len = 0; 258 free(nump->rn_id); 259 nump->rn_id = NULL; 260 return (-1); 261} 262