vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi Is there a quick and safe way/function to read an unsigned string buffer that might contain '\0' in the middle of the string in order to return its full length, including the null-terminator and the rest of the string? Thanks in advance! i386 OpenBSD 3.3 |
| |||
| On Thu, 03 Jul 2003 08:28:21 GMT, Tom <fake23lk1@3jk23.akk> wrote: > Is there a quick and safe way/function to read an unsigned string buffer that > might contain '\0' in the middle of the string in order to return its full > length, including the null-terminator and the rest of the string? By definition, no. The length needs to be transported Out Of Band. |
| |||
| > char StrBuffer[1024]; > strncpy(StrBuffer,"Hello, World",sizeof(StrBuffer)-1); > strncpy(StrBuffer,"Foo",sizeof(StrBuffer)-1); > > After this, the buffer has "Foo\0o, World\0" in it and possibly > some more junk after that-- note that it wasn't memset initially. No, this is wrong. strncpy fills the remaining space with \0. |
| |||
| Tom wrote: > Hi > > Is there a quick and safe way/function to read an unsigned string buffer that > might contain '\0' in the middle of the string in order to return its full > length, including the null-terminator and the rest of the string? > > Thanks in advance! > > i386 OpenBSD 3.3 For strlen use pointer arithmetic on rindex(str, '\0'). I think the new way to do that is strrchr see man strrchr(3). I'm not sure of safety, it has to be managed externally with say, ensuring that address returned is within sizeof of the buffer, which means you can only do this on stack arrays, or on heap objects if you maintain the allocated size somewhere else. Somewhere among the cobwebs I have pointer swizzling utilities that do this (strlen, that is) for char and int arrays used in extended precision arithmetic registers, where '\0' is valid non-termination value. I could dig them up if to see if they apply to your question if you're curious. Your subject only refers strlen, but your question regards 'reads', which is a bit different. For that I think you will want to go forwards with strchr(3) and read the separate pieces like ordinary strings. A poor man's strtok that's safe but not necessarily fast. MK |
| |||
| > Somewhere among the cobwebs I have pointer swizzling utilities that do > this (strlen, that is) for char and int arrays used in extended > precision arithmetic registers, where '\0' is valid non-termination > value. I could dig them up if to see if they apply to your question if > you're curious. My use with reading bytes containing '\0' is encryption. Since I have access to the size of the cipher-text, I can always store it in reference to the cipher-text. I just wanted to know another fast and safe way to read such data, if there is any. If it's no bother to you MK, please post your "pointer swizzling utilities." I think others will appreciate that; I know I will. Thanks again to everyone who replied! p.s. You'll get a cipher-text with '\0' before the string ends, if anyone is curious to try these parameters out - found in man, but slightly modified. unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; char intext[] = "Some Crypto Text"; EVP_EncryptInit_ex(&ctx, EVP_bf_cfb(), NULL, key, iv); |
| ||||
| M Khomo wrote: > Tom wrote: >> If it's no bother to you MK, please post your "pointer swizzling >> utilities." I >> think others will appreciate that; I know I will. >> > Will do. > Here we go. Just the integer versions of strcmp, strchr and strlen (istrcmp, strint and istrln respectively), and other obvious utilities. They do linear searches only. ::::Begin Excerpts:::: int istrcmp(str1,str2) int *str1, *str2; { while(*str1&&*str2&&*str1==*str2) str1++,str2++; return (0+*str1)-*str2; } int *strint(str,chr) int *str, chr; { while(*str&&*str!=chr) str++; return !chr||*str ? str : (int*) 0; } istrln(str) int *str; { int num = 0; while(*str++) num++; return num; } adigit(num) int num; {return num>='0'&&num<='9';} alower(let) int let; {return (let>='a'&&let<='i')||(let>='j'&&let<='r')||(let>= 's'&&let<='z');} anupper(let) int let; {return (let>='A'&&let<='I')||(let>='J'&&let<='R')||(let>= 'S'&&let<='Z');} analpha(let) int let; {return alower(let)||anupper(let);} analnum(chr) int chr; {return analpha(chr)||adigit(chr);} ::::End Excerpts:::: NB: There are many more non-obvious ones including conversions from int to char strings and back, but they have application context that would require too much explaining. The other option would be to just donate the entire package to obsd after normalizing the copyright to the obsd license. The actual application is 'Arbitrary Precision Arithmetic Expression APAX(), written long ago in K&R C. Perhaps the sampling of conversions below should satisfy the curiosity so the package becomes unnecessary. ::::::Begin Conversions:::::::: int *cstois(str) char *str; { int *int1ptr(), *irun, *istr; irun = istr = int1ptr(strlen(str)+1); while(*irun++=(int)*str++); return istr; } char *istocs(str) int *str; { int nmem = istrln(str); char *chr1ptr(), *crun, *cstr = chr1ptr(nmem+1); crun = cstr; while(*crun++=(char)*str++); return cstr; } ::::::: Allocation Utils:::::::: int *cpystr(str) int *str; { int *copied, *run, *int1ptr(); run = copied = int1ptr(istrln(str)+1); while(*run++ = *str++); return copied; } char *chr1ptr(nmem) int nmem; { char *ptr; if((ptr = (char *) calloc(nmem,sizeof(char)))==NULL){ err_prnt("insufficient storage space for character string"); exit(1); } return ptr; } int *int1ptr(nmem) int nmem; { int *ptr; if((ptr = (int *) calloc(nmem,sizeof(int)))==(int*)0){ err_prnt("insufficient storage space for integer array"); exit(1); } return ptr; } ::::::: Miscellaneous I/O:::::::: void err_prnt(msg) char *msg;{fprintf(stderr,"%s\n",msg);} :::End Excerpts::: |