LIB_EXPORT rc_t CC XFS_StrEndsWith ( const char * Str, const char * End ) { uint32_t StrLen, EndLen; if ( Str == NULL || End == NULL ) { return false; } StrLen = string_len ( Str, string_size ( Str ) ); EndLen = string_len ( End, string_size ( End ) ); if ( StrLen >= EndLen && EndLen > 0 ) { return string_cmp ( Str + ( StrLen - EndLen ), EndLen, End, EndLen, EndLen ) == 0; } return false; } /* XFS_StrEndsWith () */
/****************************************************************************** * concat_3() * Concatenate 3 strings * * Parameters * --------- * - 3 strings * * Returns * ------- * - a new string ******************************************************************************/ char *concat_3(char *s1, char *s2, char *s3) { char *s; int len1, len2, len3, len; int s1_ok = TRUE; int s2_ok = TRUE; int s3_ok = TRUE; if(s1 == NULL){ len1 = 0; s1_ok = FALSE; }else { len1 = string_len(s1); } if(s2 == NULL){ len2 = 0; s2_ok = FALSE; }else { len2 = string_len(s2); } if(s3 == NULL){ len3 = 0; s3_ok = FALSE; }else { len3 = string_len(s3); } len = len1 + len2 + len3; if (len == 0) return( (char *)NULL ); s = create_string(len); if (s1_ok) strcpy(s, s1); if (s2_ok) strcpy(s + len1, s2); if (s3_ok) strcpy(s + len1 + len2, s3); return (s); }
PUBLIC_KEY *publickey_make_rsa(SSH_SESSION *session, BUFFER *buffer, char *type){ STRING *e,*n; PUBLIC_KEY *key=malloc(sizeof(PUBLIC_KEY)); if(!strcmp(type,"ssh-rsa")) key->type=TYPE_RSA; else key->type=TYPE_RSA1; key->type_c=type; e=buffer_get_ssh_string(buffer); n=buffer_get_ssh_string(buffer); buffer_free(buffer); /* we don't need it anymore */ if(!e || !n){ ssh_set_error(session,SSH_FATAL,"Invalid RSA public key"); if(e) free(e); if(n) free(n); free(key); return NULL; } #ifdef HAVE_LIBGCRYPT gcry_sexp_build(&key->rsa_pub,NULL,"(public-key(rsa(n %b)(e %b)))",string_len(n),n->string,string_len(e),e->string); #elif HAVE_LIBCRYPTO key->rsa_pub=RSA_new(); key->rsa_pub->e=make_string_bn(e); key->rsa_pub->n=make_string_bn(n); #endif #ifdef DEBUG_CRYPTO ssh_print_hexa("e",e->string,string_len(e)); ssh_print_hexa("n",n->string,string_len(n)); #endif free(e); free(n); return key; }
static void build_session_id1(SSH_SESSION *session, STRING *servern, STRING *hostn){ MD5CTX md5=md5_init(); #ifdef DEBUG_CRYPTO ssh_print_hexa("host modulus",hostn->string,string_len(hostn)); ssh_print_hexa("server modulus",servern->string,string_len(servern)); #endif md5_update(md5,hostn->string,string_len(hostn)); md5_update(md5,servern->string,string_len(servern)); md5_update(md5,session->server_kex.cookie,8); md5_final(session->next_crypto->session_id,md5); #ifdef DEBUG_CRYPTO ssh_print_hexa("session_id",session->next_crypto->session_id,MD5_DIGEST_LEN); #endif }
static bool string_ends_in( const char * str, const char * end ) { bool res = false; if ( str != NULL && end != NULL ) { uint32_t l_str = string_len( str, string_size( str ) ); uint32_t l_end = string_len( end, string_size( end ) ); if ( l_str >= l_end && l_end > 0 ) { const char * p = str + ( l_str - l_end ); res = ( string_cmp ( p, l_end, end, l_end, l_end ) == 0 ); } } return res; }
int main(int argc, char *argv[]) { char *t; string *str; int i; t = getenv("SHSQL"); if(t != NULL) { if(!strcmp(t, "postgres")) mode = SHSQL_POSTGRES; else if(!strcmp(t, "mysql")) mode = SHSQL_MYSQL; else if(!strcmp(t, "sqlite3")) mode = SHSQL_SQLITE3; else if(!strcmp(t, "odbc")) mode = SHSQL_ODBC; else if(!strcmp(t, "freetds")) mode = SHSQL_FREETDS; else mode = SHSQL_POSTGRES; } else mode = SHSQL_POSTGRES; str = new_string(); for(i=1;i<argc;i++) { catstr(str, argv[i]); if(string_len(str) && i < argc - 1) string_cat_c(str, ' '); } if(string_len(str)) { fputc('\'', stdout); fputs(string_s(str), stdout); fputc('\'', stdout); } else fputs("NULL", stdout); string_delete(str); return 0; }
/***************************************************************************** * num_words_in_string * Return the number of words in a string * * Parameters * --------- * - a string * * Returns * ------- * - number of words * * Notes * ----- * Get the number of whitespace delimited words in a string. * this assumes that all the cntrl character (eg "\n", "\r" etc..) * have been stripped from str already ******************************************************************************/ int num_words_in_string(char *str) { int num=0; int j=0; int ok=TRUE; int len; if (str == NULL) return (0); len = string_len(str); /* skip through leading whitespace */ /* note is_white_space('\0') should eval to false */ while( is_white_space(str[j]) ) j++; if (str[j] == '\0') return(0); /* apparently we are at a word */ while (ok) { while( !is_white_space(str[j]) && (j<len) ) j++; num++; while ( is_white_space(str[j]) && (j<len) ) j++; if( (str[j] == '\0') || ( j>= len) ) ok=FALSE; } return num; }
static rc_t KNSProxiesAddHttpProxyPath ( KNSProxies * self, const char * proxy, size_t proxy_size, uint16_t proxy_port ) { const String * proxy_host = NULL; rc_t rc = 0; HttpProxy * new_proxy = NULL; BSTItem * node = NULL; HttpProxy add = { proxy_host, proxy_port, 0 }; assert ( self ); if ( proxy == NULL ) return 0; if ( rc == 0 ) { String tmp; StringInit ( & tmp, proxy, proxy_size, string_len ( proxy, proxy_size ) ); rc = StringCopy ( & proxy_host, & tmp ); if ( rc == 0 ) add . proxy_host = proxy_host; else return rc; } if ( BSTreeFind ( & self -> proxie_tree, & add, BSTItemCmp ) != NULL ) { DBGMSG ( DBG_KNS, DBG_FLAG ( DBG_KNS_PROXY ), ( "Ignored duplicate proxy '%S:%d'\n", proxy_host, proxy_port ) ); free ( ( void * ) proxy_host ); return 0; } new_proxy = calloc ( 1, sizeof * new_proxy ); if ( new_proxy == NULL ) return RC ( rcNS, rcMgr, rcAllocating, rcMemory, rcExhausted ); new_proxy -> proxy_host = proxy_host; new_proxy -> proxy_port = proxy_port; node = calloc ( 1, sizeof * node ); if ( node == NULL ) { free ( new_proxy ); return RC ( rcNS, rcMgr, rcAllocating, rcMemory, rcExhausted ); } node -> proxy = new_proxy; rc = BSTreeInsert ( & self -> proxie_tree, ( BSTNode * ) node, BSTreeSort ); DBGMSG ( DBG_KNS, DBG_FLAG ( DBG_KNS_PROXY ), ( "Added proxy '%S:%d'\n", proxy_host, proxy_port ) ); if ( ! self -> http_proxy_enabled ) self -> http_proxy_enabled = ( proxy_host != NULL ); return rc; }
/****************************************************************************** * print_error() * * Parameters * --------- * - error msg (NULL is OK). you can also pass a formatted string, e.g. * print_error("Reading file %s failed\n", fname); * * Returns * ------- * - None ******************************************************************************/ void print_error(char *msg,...) { va_list args; int max_err_msg = 500; char errbuf[501]; char *errmsg; int errlen; va_start(args, msg); // put va_ptr to the first arg after msg vsprintf(errbuf, msg, args); // put contents of msg and args into errbuf va_end(args); // set the va_ptr to NULL errlen = string_len(errbuf); if (errlen > max_err_msg) { fprintf(stderr, "Error: Error Message too long\n"); } errmsg = malloc(errlen + 1); if (errmsg == NULL) { errmsg = "No memory available"; } else { strcpy(errmsg, errbuf); } fprintf(stderr, "%s\n", errmsg); perror("Error: "); }
static void build_signature(struct signctx *senv, const char *method, const char *consumerSecret, const char *tokenSecret) { struct signature_param *sp; const size_t nParams = vector_size(&senv->signatureParams); uint32_t i; uint8_t digest[20]; qsort(vector_values(&senv->signatureParams), vector_size(&senv->signatureParams), senv->signatureParams.item_size, bySignatureParamName); string_append(&senv->signatureSeed, "&", 1); if (nParams) { i = 0; do { sp = (struct signature_param *)vector_value(&senv->signatureParams, i); string_appendfmt(&senv->signatureSeed, "%s%%3D", sp->name); string_append_urlencoded_rfc3986(&senv->signatureSeed, sp->value, sp->len); string_append(&senv->signatureSeed, "%26", 3); } while (++i != nParams); string_adjustlen(&senv->signatureSeed, 3); } char _k[256]; const size_t keyLen = sprintf(_k, "%s&%s", consumerSecret, tokenSecret); string_reset(&senv->signatureParamsBuf); hmac((uint8_t *)_k, keyLen, (uint8_t *)string_data(&senv->signatureSeed), string_len(&senv->signatureSeed), digest); base64_encode(digest, 20, &senv->signatureParamsBuf, 0); }
PUBLIC_KEY *publickey_from_string(SSH_SESSION *session, STRING *pubkey_s){ BUFFER *tmpbuf=buffer_new(); STRING *type_s; char *type; buffer_add_data(tmpbuf,pubkey_s->string,string_len(pubkey_s)); type_s=buffer_get_ssh_string(tmpbuf); if(!type_s){ buffer_free(tmpbuf); ssh_set_error(session,SSH_FATAL,"Invalid public key format"); return NULL; } type=string_to_char(type_s); free(type_s); if(!strcmp(type,"ssh-dss")){ free(type); return publickey_make_dss(session, tmpbuf); } if(!strcmp(type,"ssh-rsa")){ free(type); return publickey_make_rsa(session, tmpbuf,"ssh-rsa"); } if(!strcmp(type,"ssh-rsa1")){ free(type); return publickey_make_rsa(session, tmpbuf,"ssh-rsa1"); } ssh_set_error(session,SSH_FATAL,"unknown public key protocol %s",type); buffer_free(tmpbuf); free(type); return NULL; }
STRING *ssh_encrypt_rsa1(SSH_SESSION *session, STRING *data, PUBLIC_KEY *key){ int len=string_len(data); #ifdef HAVE_LIBGCRYPT STRING *ret; gcry_sexp_t ret_sexp; gcry_sexp_t data_sexp; const char *tmp; size_t size; gcry_sexp_build(&data_sexp,NULL,"(data(flags pkcs1)(value %b))",len,data->string); gcry_pk_encrypt(&ret_sexp,data_sexp,key->rsa_pub); gcry_sexp_release(data_sexp); data_sexp=gcry_sexp_find_token(ret_sexp,"a",0); tmp=gcry_sexp_nth_data(data_sexp,1,&size); if (*tmp == 0) { size--; tmp++; } ret=string_new(size); string_fill(ret,(char *)tmp,size); gcry_sexp_release(ret_sexp); #elif defined HAVE_LIBCRYPTO int flen=RSA_size(key->rsa_pub); STRING *ret=string_new(flen); RSA_public_encrypt(len,data->string,ret->string,key->rsa_pub, RSA_PKCS1_PADDING); #endif return ret; }
/****************************************************************************** * string_replace() * Replace a string * * Parameters * --------- * - a text string * - fstr: string to search for * - rstr: replacement string * - numrep: count on how many times the string was replaced * * Returns * ------- * - a new string * * Notes * ----- * This replaces all instances of fstr with rstr in str. * Note this Frees str!!!! * ******************************************************************************/ char *string_replace(char *str, char *fstr, char *rstr,int *num_rep) { char *tmp1, *tmp2, *tmp3; int p1=0, len, ok = TRUE; if ((str == NULL)||(fstr == NULL) ||(rstr == NULL)){ return(NULL); } if (strcmp(fstr,rstr)==0) return(str); len = string_len(fstr); *num_rep = 0; while (ok){ p1 = find_string(str,fstr,0); if (p1 < 0) { ok = FALSE; } else if (p1 > 0){ tmp1 = sub_string(str,0,p1-1); tmp2 = str + p1 + len; } else { tmp1 = NULL; tmp2 = str + len; } if(ok){ tmp3 = concat_3(tmp1,rstr,tmp2); free(tmp1); free(str); str = tmp3; *num_rep = *num_rep +1; } } return(str); }
LIB_EXPORT rc_t CC KNSManagerVSetHTTPProxyPath ( KNSManager * self, const char * fmt, va_list args ) { rc_t rc = 0; if ( self == NULL ) rc = RC ( rcNS, rcMgr, rcUpdating, rcSelf, rcNull ); else { uint16_t proxy_port = 0; const String * proxy = NULL; if ( fmt != NULL && fmt [ 0 ] != 0 ) { size_t psize; char path [ 4096 ]; rc = string_vprintf ( path, sizeof path, & psize, fmt, args ); if ( rc == 0 && psize != 0 ) { char * colon = string_rchr ( path, psize, ':' ); if ( colon != NULL ) { char * end; const char * port_spec = colon + 1; /* it is true that some day we might read symbolic port names... */ long port_num = strtol ( port_spec, & end, 10 ); if ( port_num <= 0 || port_num >= 0x10000 || end [ 0 ] != 0 ) rc = RC ( rcNS, rcMgr, rcUpdating, rcPath, rcInvalid ); else { proxy_port = ( uint64_t ) port_num; psize = colon - path; } } if ( rc == 0 ) { String tmp; StringInit ( & tmp, path, psize, string_len ( path, psize ) ); rc = StringCopy ( & proxy, & tmp ); } } } if ( rc == 0 ) { if ( self -> http_proxy != NULL ) { StringWhack ( self -> http_proxy ); self -> http_proxy_port = 0; } self -> http_proxy = proxy; self -> http_proxy_enabled = ( proxy != NULL ); self -> http_proxy_port = proxy_port; } } return rc; }
/****************************************************************************** * copy_string() * Copy a string * * Parameters * --------- * - a string * * Returns * ------- * - a new string * * Notes * ----- * copy_string copies the string s into dynamically allocated * storage and returns the new string. ******************************************************************************/ char *copy_string(char *s) { char *newstr; //if (s == NULL) print_error("NULL string passed to copy_string"); if (s == NULL) return (NULL); newstr = create_string(string_len(s)); strcpy(newstr, s); return (newstr); }
/****************************************************************************** * find_string_skip_blocks * * Parameters * --------- * - string to search in * - string to search for * - start position for search * * Returns * ------- * Same as find_string except it ignores occurences of str * within the blocks (),{},[], "" * * This needs some testing, at least with "" blocks * ******************************************************************************/ int find_string_skip_blocks(char *s, char *str, int p1){ int j, b, btype; int p2, ok, s_len, s2_len; if (s == NULL) return(-1); //print_error("NULL string in find_string_skip_blocks"); if (str == NULL) return(-1); //print_error("NULL string in find_string_skip_blocks"); s_len = string_len(s); s2_len = string_len(str); if (p1 < 0) p1 = 0; if (p1 > s_len - 1) return(-1); b = 0; p2 = -1; j = p1; ok = 1; while ( (ok) && (s[j] != '\0') ) { if ( strncmp( s+j, str, s2_len) == 0 ) { p2 = j; ok = 0; } else if ( btype=is_open_block(s[j]) ) { //buff_print("%d\n",btype); b = 1; j++; while( (b != 0 ) && ( s[j] != '\0') ){ // look for closed first // this should allow for quotes to be used as // open and closed blocks ? if ( is_close_block(s[j]) == btype ){ b--; } else if ( is_open_block(s[j]) == btype ){ b++; } j++; } } else { j++; } } return( p2 ); }
/* * Question 3: * * Write a function reverse_string that takes a string argument, a start index, * and an end index, and reverses the order of the letters in the string. From * start to end. This function should change the letters of the string in place, * and should not return anything. If the indices are out of bounds, don't * do anything. * * WARNING: Make sure the string terminates with the nill ('\0') character! * * Examples: * "apple", start = 0, end = 4 -> "elppa" * "apple", start = 1, end = 2 -> "apple" * "apple", start = 1, end = 3 -> "alppe" * "apple", start = -25, end = 61 -> "apple" */ void reverse_string(string s, int start, int end) { if (start < 0 || end > string_len(s)) return; char temp; int mid = (end - start) / 2 + 1; for (int i = 0; i < mid; i++) { temp = s[start + i]; s[start + i] = s[end - i]; s[end - i] = temp; } }
void *sftp_handle(SFTP_SESSION *sftp, STRING *handle) { u32 val; if(!sftp->handles) return NULL; if(string_len(handle)!=sizeof(val)) return NULL; memcpy(&val,handle->string,sizeof(u32)); if(val>SFTP_HANDLES) return NULL; return sftp->handles[val]; }
/****************************************************************************** * find_string() * Find a string in a string * * Parameters * --------- * - string to search in * - string to search for * - start position for search * * Returns * ------- * Beginning at position start in the string text, this * function searches for the string str and returns the * first index at which it appears or -1 if no match is * found. ******************************************************************************/ int find_string(char *text, char *str, int start) { char *cptr; if (str == NULL) return(-1); //print_error("NULL pattern string in find_string"); if (text == NULL) return(-1); //print_error("NULL text string in find_string"); if (start < 0) start = 0; if (start > string_len(text)) return (-1); cptr = strstr(text + start, str); if (cptr == NULL) return (-1); return ((int) (cptr - text)); }
struct string *copy_string(struct string *s) { struct string *newp; uvalue size = string_len(s); GCPRO1(s); newp = alloc_string_n(size); memcpy(newp->str, s->str, size * sizeof(*s->str)); GCPOP(1); return newp; }
/****************************************************************************** * find_char * Find the index of a character in a string * * Parameters * --------- * - character * - string * - position to start search * * Returns * ------- * Beginning at position start in the string text, this * function searches for the character ch and returns the * first index at which it appears or -1 if no match is * found. ******************************************************************************/ int find_char(char ch, char *text, int start) { char *cptr; //if (text == NULL) print_error("NULL string passed to find_char"); if (text == NULL) return(-1); if (start < 0) start = 0; if (start > string_len(text)) return (-1); cptr = strchr(text + start, ch); if (cptr == NULL) return (-1); return ((int) (cptr - text)); }
/****************************************************************************** * to_upper * Convert a string to upper * * Parameters * --------- * - a string * * Returns * ------- * This function returns a new string with all * alphabetic characters converted to upper case. ******************************************************************************/ char *to_upper(char *s) { char *result; int i; if (s == NULL) { print_error("NULL string passed to to_upper"); } result = create_string(string_len(s)); for (i = 0; s[i] != '\0'; i++) result[i] = toupper(s[i]); result[i] = '\0'; return (result); }
static rc_t aws_extract_key_value_pair ( const String *source, String *key, String *val ) { String k, v; const char *start = source -> addr; const char *end = start + source -> size; char *eql = string_chr ( start, source -> size, '=' ); if ( eql == NULL ) return RC ( rcKFG, rcChar, rcSearching, rcFormat, rcInvalid ); /* key */ StringInit ( &k, start, eql - start, string_len ( start, eql - start ) ); StringTrim ( key, &k ); start = eql + 1; /* value */ StringInit ( &v, start, end - start, string_len ( start, end - start ) ); StringTrim ( val, &v ); return 0; }
/****************************************************************************** * strip_white_space() * Strip white space from a string * * Parameters * --------- * - a string * * Returns * ------- * - a new string * * Notes * ----- * This function returns a ptr to the first non-whitespace char of a string * and insert a '\0' after the last non-whitespace char of string * This function creates a new string. * ******************************************************************************/ char *strip_white_space (char *in_string) { char *s, *t, *string, *ret_str; if(in_string == NULL) return(NULL); string = copy_string(in_string); for (s = string; is_white_space(*s); s++); if (*s == 0) return (NULL); t = s + string_len(s) - 1; while (t > s && is_white_space (*t)) t--; *++t = '\0'; ret_str = copy_string(s); free(string); return (ret_str); }
static int channel_rcv_data1(ssh_session session, int is_stderr) { ssh_channel channel = session->channels; ssh_string str = NULL; str = buffer_get_ssh_string(session->in_buffer); if (str == NULL) { ssh_log(session, SSH_LOG_FUNCTIONS, "Invalid data packet !\n"); return -1; } ssh_log(session, SSH_LOG_RARE, "Adding %zu bytes data in %d", string_len(str), is_stderr); if (channel_default_bufferize(channel, string_data(str), string_len(str), is_stderr) < 0) { string_free(str); return -1; } string_free(str); return 0; }
/* return true unless sym can be found in data->table */ static bool recurse_symbol(struct symbol *sym, void *_data) { struct table_data *data = _data; if (sym->data != NULL) ++data->nentries; value val = NULL; struct symbol *symval; if (table_lookup_len(data->table, sym->name->str, string_len(sym->name), &symval)) val = symval->data; return !recurse(sym->data, val, data->seen); }
/* * Question 0: * * Write a program where the function main prints out all the command line * arguments in order, one per line. */ int main(int argc, string argv[]) { for (int i = 0; i < argc; i++) { printf("arg[%d]: %s\n", i, argv[i]); printf("length: %d\n", string_len(argv[i])); printf("num occurrences of %c: %d\n", 'e', num_occur(argv[i],'e')); reverse_string(argv[i],0,4); printf("reverse, start = %d, end = %d: %s", 0, 4, argv[i]); printf("\n"); } char s[] = "be sure to drink your ovaltine"; printf("%s\n", s); reverse_words(s); printf("%s\n", s); return 0; }
/* reverse a string * works for any string of size 0 to INT_MAX - 1 */ void crazy_reverse_string(char *s) { int l; int i; /* you are not allowed to declare new variables */ l = string_len(s); i = 0; while (i < l / 2) { s[l] = s[i]; /* Using the Null terminator as temp variable. */ s[i] = s[l - i - 1]; /* Assigning the each item to its opposite. */ s[l - i -1 ] = s[l]; /* Retrieving the saved value from the Null. */ s[l] = 0; /* Reassigning the 0 value to last its place. */ i++; } }
ssh_public_key publickey_from_string(ssh_session session, ssh_string pubkey_s) { ssh_buffer tmpbuf = NULL; ssh_string type_s = NULL; char *type_c = NULL; int type; tmpbuf = buffer_new(); if (tmpbuf == NULL) { return NULL; } if (buffer_add_data(tmpbuf, string_data(pubkey_s), string_len(pubkey_s)) < 0) { goto error; } type_s = buffer_get_ssh_string(tmpbuf); if (type_s == NULL) { ssh_set_error(session,SSH_FATAL,"Invalid public key format"); goto error; } type_c = string_to_char(type_s); string_free(type_s); if (type_c == NULL) { goto error; } type = ssh_type_from_name(type_c); SAFE_FREE(type_c); switch (type) { case TYPE_DSS: return publickey_make_dss(session, tmpbuf); case TYPE_RSA: case TYPE_RSA1: return publickey_make_rsa(session, tmpbuf, type); } ssh_set_error(session, SSH_FATAL, "Unknown public key protocol %s", ssh_type_to_char(type)); error: buffer_free(tmpbuf); return NULL; }
static void write_string(struct oport *p, prt_level level, struct string *print) { uvalue l = string_len(print); if (level == prt_display) pswrite(p, print, 0, l); else { unsigned char *str = (unsigned char *)alloca(l + 1); unsigned char *endstr; memcpy((char *)str, print->str, l + 1); GCPRO1(p); /* The NULL byte at the end doesn't count */ endstr = str + l; pputc('"', p); while (str < endstr) { unsigned char *pos = str; while (pos < endstr && writable(*pos)) pos++; opwrite(p, (char *)str, pos - str); if (pos < endstr) /* We stopped for a \ */ { pputc('\\', p); switch (*pos) { case '\\': case '"': pputc(*pos, p); break; case '\n': pputc('n', p); break; case '\r': pputc('r', p); break; case '\t': pputc('t', p); break; case '\f': pputc('f', p); break; default: pprintf(p, "%o", *pos); break; } str = pos + 1; } else str = pos; } pputc('"', p); GCPOP(1); } }