/* * Copyright (C) 2006 Richard Kotal * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ static char * Ns_StrCopy2 (char *src, int len) { char *out = NULL; if (src == NULL) return out; out = (char *)Ns_Malloc(len); memset(out,'\0',len); memcpy(out,src,len); return out; } static int ns_mcrypt_IsMcryptObj (Tcl_Obj * obj, char *name) { int ret = NS_FALSE; if (obj == NULL || name == NULL) return ret; if (obj->length == 0) return ret; if (obj->bytes == NULL) return ret; if (!STREQ (obj->bytes, name)) return ret; return NS_TRUE; } char * ns_mcrypt_utils_unasciify(char *in, int insize) { char *ptrIn = in; char *buffer = NULL; char *ptrOut = buffer; long loop; int len = 0; int buf; if (ns_mcrypt_utils_ishexstring(in,insize) != 1) return buffer; len = insize/2; buffer = Ns_Malloc(len); memset(buffer, '\0',len); ptrOut = buffer; for (loop = 0; loop < len; loop++, ptrIn+=2) { sscanf(ptrIn,"%2x",&buf); *ptrOut++=(char)buf; } return buffer; } int ns_mcrypt_utils_ishexstring(char *x, int insize) { int out = 0; int i = 0; if (x == NULL || insize == 0) return out; if ((insize % 2) != 0) return out; for (i = 0; i < insize ; i++) if (!((x[i]>='0' && x[i]<='9') || (x[i]>='a' && x[i]<='f'))) return out; return 1; } static int ns_mcrypt_Min(int i1, int i2) { return (i2 < i1)?i2:i1; } /* * Copyright (C) 1998 Nikos Mavroyanopoulos * Copyright (C) 1999,2000 Sascha Schumman, Nikos Mavroyanopoulos * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ /* $Id: stdfns.c,v 1.2 2006/01/10 03:47:18 imipak Exp $ */ /** * Some of these are wrappers. The idea is to eventually produce an extremely * lightweight set of robust, portable functions that are guaranteed to produce * a "safe" result, even with bogus inputs. We can't trust native C libraries * to validate inputs. */ unsigned char ns_mcrypt_utils_val2char(unsigned char x) { unsigned char out; switch(x) { case 0x0 : { out = '0'; break; } case 0x1 : { out = '1'; break; } case 0x2 : { out = '2'; break; } case 0x3 : { out = '3'; break; } case 0x4 : { out = '4'; break; } case 0x5 : { out = '5'; break; } case 0x6 : { out = '6'; break; } case 0x7 : { out = '7'; break; } case 0x8 : { out = '8'; break; } case 0x9 : { out = '9'; break; } case 0xa : { out = 'a'; break; } case 0xb : { out = 'b'; break; } case 0xc : { out = 'c'; break; } case 0xd : { out = 'd'; break; } case 0xe : { out = 'e'; break; } case 0xf : { out = 'f'; break; } } return(out); } char * ns_mcrypt_utils_asciify(char *in, long len) { char *ptrIn = in; char *buffer = Ns_Malloc((2 * len) + 1); char *ptrOut = buffer; long loop; memset(buffer, '\0',(2 * len) + 1); for (loop = 0; loop < len; loop++, ptrIn++) { *ptrOut++ = ns_mcrypt_utils_val2char((*ptrIn & 0xf0) >> 4); *ptrOut++ = ns_mcrypt_utils_val2char((*ptrIn & 0x0f)); } return(buffer); }