Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Parsing a Section of Binary String Data!

Reply
Thread Tools

Parsing a Section of Binary String Data!

 
 
zoltan
Guest
Posts: n/a
 
      03-22-2006
Hi,
The scenario is like this :


struct ns_rr {

const u_char* rdata;
};

The rdata field contains some fields such as :

char * flags;
char * services;
char * regexp;
char * replacement;

I want to parse the rdata section and obtain the individual string
fields as shown above. Can anyone suggest an efficient method? Are
these strings terminiated by '\0' within the rdata section?


Thanks and Regards,

Timmy Jose.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-22-2006
"zoltan" <(E-Mail Removed)> writes:
> Hi,
> The scenario is like this :
>
>
> struct ns_rr {
>
> const u_char* rdata;
> };


Is u_char a typedef for unsigned char? (If so, just using "unsigned
char" would be a lot clearer.)

> The rdata field contains some fields such as :
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;


No, the rdata member (not field) is a pointer; it doesn't contain
anything. Are you talking about something that rdata points to?

> I want to parse the rdata section and obtain the individual string
> fields as shown above. Can anyone suggest an efficient method? Are
> these strings terminiated by '\0' within the rdata section?


You want to parse the rdata section of what? Is this coming from a
file? Are we supposed to know what an "rdata section" is? A Google
search indicates that there is such a thing (or perhaps more than one
such things), but it's not part of the C programming language.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
dotnetdotcn@126.com
Guest
Posts: n/a
 
      03-22-2006

zoltan wrote:
> Hi,
> The scenario is like this :
>
>
> struct ns_rr {
>
> const u_char* rdata;
> };
>
> The rdata field contains some fields such as :
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
>
> I want to parse the rdata section and obtain the individual string
> fields as shown above. Can anyone suggest an efficient method? Are
> these strings terminiated by '\0' within the rdata section?
>
>
> Thanks and Regards,
>
> Timmy Jose.


look at this:
struct ns_rr * a;
puts(a->rdata->flags);

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-22-2006
Keith Thompson <(E-Mail Removed)> writes:
> "zoltan" <(E-Mail Removed)> writes:
>> Hi,
>> The scenario is like this :
>>
>>
>> struct ns_rr {
>>
>> const u_char* rdata;
>> };

>
> Is u_char a typedef for unsigned char? (If so, just using "unsigned
> char" would be a lot clearer.)
>
>> The rdata field contains some fields such as :
>>
>> char * flags;
>> char * services;
>> char * regexp;
>> char * replacement;

>
> No, the rdata member (not field) is a pointer; it doesn't contain
> anything. Are you talking about something that rdata points to?

[...]

I realize that my assumption that u_char is a typedef for unsigned
char may have been unwarranted. If u_char is a typedef for a struct
type that contains the flags, services, regexp, and replacement
members, the question makes a bit more sense. (Though the name
"u_char" certainly doesn't point in that direction.)

But it would still be very helful to show us the actual declarations
and a better idea of what you want to do with them.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
zoltan
Guest
Posts: n/a
 
      03-22-2006

Keith Thompson wrote:
> "zoltan" <(E-Mail Removed)> writes:
> > Hi,
> > The scenario is like this :
> >
> >
> > struct ns_rr {
> >
> > const u_char* rdata;
> > };

>
> Is u_char a typedef for unsigned char? (If so, just using "unsigned
> char" would be a lot clearer.)
>
> > The rdata field contains some fields such as :
> >
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;

>
> No, the rdata member (not field) is a pointer; it doesn't contain
> anything. Are you talking about something that rdata points to?
>
> > I want to parse the rdata section and obtain the individual string
> > fields as shown above. Can anyone suggest an efficient method? Are
> > these strings terminiated by '\0' within the rdata section?

>
> You want to parse the rdata section of what? Is this coming from a
> file? Are we supposed to know what an "rdata section" is? A Google
> search indicates that there is such a thing (or perhaps more than one
> such things), but it's not part of the C programming language.
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.



ns_rr is a structure which defines the type of the Resource Record (RR)
as used for DNS Queries. The structure is defined in <arpa/nameser.h> .

The rdata "member" points to memory where the various mentioned fields
are stored. This is because the contents of rdata depend on the type of
Resource Record.

So suppose I have a structure of this type ns_rr, how can I extract the
individual fields of the rdata member? Simple query.

 
Reply With Quote
 
zoltan
Guest
Posts: n/a
 
      03-22-2006

(E-Mail Removed) wrote:
> zoltan wrote:
> > Hi,
> > The scenario is like this :
> >
> >
> > struct ns_rr {
> >
> > const u_char* rdata;
> > };
> >
> > The rdata field contains some fields such as :
> >
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;
> >
> > I want to parse the rdata section and obtain the individual string
> > fields as shown above. Can anyone suggest an efficient method? Are
> > these strings terminiated by '\0' within the rdata section?
> >
> >
> > Thanks and Regards,
> >
> > Timmy Jose.

> look at this:
> struct ns_rr * a;
> puts(a->rdata->flags);


The problem is this :

ns_rr is the Standard type which is used to store the data.

I know that the various fields ( flag, services etc) are in the rdata
member. So to extract them, I define my own structure like this :

struct NAPTR
{

char * flags;
char * services;
char * regexp;
char * replacement;
};

So the compiler has no idea that there is a field called "flags" inside
the rdata member!!! That is the trouble... Any ideas?

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-22-2006
"zoltan" <(E-Mail Removed)> writes:
> Keith Thompson wrote:

[...]
>> You want to parse the rdata section of what? Is this coming from a
>> file? Are we supposed to know what an "rdata section" is? A Google
>> search indicates that there is such a thing (or perhaps more than one
>> such things), but it's not part of the C programming language.

>
> ns_rr is a structure which defines the type of the Resource Record (RR)
> as used for DNS Queries. The structure is defined in <arpa/nameser.h> .


You need to ask in a system-specific newsgroup, (perhaps
comp.unix.programmer).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-22-2006
zoltan wrote:
> (E-Mail Removed) wrote:
> > zoltan wrote:


> > > The scenario is like this :
> > >
> > > struct ns_rr {
> > > const u_char* rdata;
> > > };
> > >
> > > The rdata field contains some fields such as :
> > >
> > > char * flags;
> > > char * services;
> > > char * regexp;
> > > char * replacement;
> > >
> > > I want to parse the rdata section and obtain the individual string
> > > fields as shown above. Can anyone suggest an efficient method? Are
> > > these strings terminiated by '\0' within the rdata section?


you tell me.

<snip>

> The problem is this :
>
> ns_rr is the Standard type which is used to store the data.
>
> I know that the various fields ( flag, services etc) are in the rdata
> member. So to extract them, I define my own structure like this :
>
> struct NAPTR
> {
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
> };
>
> So the compiler has no idea that there is a field called "flags" inside
> the rdata member!!! That is the trouble... Any ideas?


I've read your posts and I still havn't a clue what you're talking
about.
Could you post a small, complete, compilable example. If it doesn't do
what you want then explain what you expected it to do and what it
actually does. Please include definitions of all data structures used.


--
Nick Keighley

 
Reply With Quote
 
Pedro Graca
Guest
Posts: n/a
 
      03-22-2006
zoltan wrote:
> Hi,
> The scenario is like this :
>
>
> struct ns_rr {
>
> const u_char* rdata;
> };


What is u_char?
I'll assume it's a struct with (possibly among others) these members:

> The rdata field contains some fields such as :
>
> char * flags;
> char * services;
> char * regexp;
> char * replacement;
>
> I want to parse the rdata section and obtain the individual string
> fields as shown above. Can anyone suggest an efficient method? Are
> these strings terminiated by '\0' within the rdata section?


I have absolutely no idea. If they are, you can use standard string
handling functions to mess with them; if they are not you should take
extra care when dealing with those members.

Here's a short program that tries to put together the information you
provided and uses the individual members of `rdata`.


#include <stdio.h>

struct u_char { /* bad choice of name */
char * flags;
char * services;
char * regexp;
char * replacement;
};

struct ns_rr {
const struct u_char * rdata;
};

int main(void) {
struct u_char dummy = {"flags", "services", "regexp", "replacement"};
struct ns_rr x;

x.rdata = &dummy;
printf("x.rdata->flags is '%s'.\n", x.rdata->flags);
printf("x.rdata->services is '%s'.\n", x.rdata->services);
printf("x.rdata->regexp is '%s'.\n", x.rdata->regexp);
printf("x.rdata->replacement is '%s'.\n", x.rdata->replacement);

return 0;
}


--
If you're posting through Google read <http://cfaj.freeshell.org/google>
 
Reply With Quote
 
zoltan
Guest
Posts: n/a
 
      03-22-2006

Nick Keighley wrote:
> zoltan wrote:
> > (E-Mail Removed) wrote:
> > > zoltan wrote:

>
> > > > The scenario is like this :
> > > >
> > > > struct ns_rr {
> > > > const u_char* rdata;
> > > > };
> > > >
> > > > The rdata field contains some fields such as :
> > > >
> > > > char * flags;
> > > > char * services;
> > > > char * regexp;
> > > > char * replacement;
> > > >
> > > > I want to parse the rdata section and obtain the individual string
> > > > fields as shown above. Can anyone suggest an efficient method? Are
> > > > these strings terminiated by '\0' within the rdata section?

>
> you tell me.
>
> <snip>
>
> > The problem is this :
> >
> > ns_rr is the Standard type which is used to store the data.
> >
> > I know that the various fields ( flag, services etc) are in the rdata
> > member. So to extract them, I define my own structure like this :
> >
> > struct NAPTR
> > {
> >
> > char * flags;
> > char * services;
> > char * regexp;
> > char * replacement;
> > };
> >
> > So the compiler has no idea that there is a field called "flags" inside
> > the rdata member!!! That is the trouble... Any ideas?

>
> I've read your posts and I still havn't a clue what you're talking
> about.
> Could you post a small, complete, compilable example. If it doesn't do
> what you want then explain what you expected it to do and what it
> actually does. Please include definitions of all data structures used.
>
>
> --
> Nick Keighley


Okay, Here are the actual structures :

I am supposed to implement a DNS Query. The Information to be processed
resides in RRs ( Resource Records). The header <arpa/nameser.h>
contains the following structure to store the data of these RRs as :

typedef struct __ns_rr {
char name[NS_MAXDNAME];
uint16_t type;
uint16_t rr_class;
uint32_t ttl;
uint16_t rdlength;
const uchar_t *rdata;
} ns_rr;

Now, the rdata part can have several fields depending on the specific
type of Record.
For instance, the SRV type has only the following fields : priority,
weight, port ( all of unsigned int type) and a target string ( the
hostname).

For my requirement, I have to use the data stored in a NAPTR record.
The "rdata" contains the following fields for this type of record :

order, preference ( unsigned int) and
flags, services, regexp and replacement ( all of type char *).

Now, because the definition of the rdata in the ns_rr structure is
generic, I have defined ( as I need to ) a structure as follows, to
store the actual data :

struct NAPTR
{
unsigned int order;
unsigned int preference;
char * flags;
char * services;
char * regexp;
char * replacement;
};


So my question is this : How can I extract the string fields from the
rdata portion of the ns_rr structure and store them into the respective
fields in my user-defined structure?

The integer fields ( order and preference ) are easy enough, being of
fixed size. What about the strings which can be of variable size?

I hope that is clear enough to elicit a suggestion or a positive
response in the least!

Regards,

Timmy Jose.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
section with in a section config file and reading that config file kampy Python 9 10-19-2012 10:59 PM
Does string contain A, and if so, does a section of string contain B Jason Carlton Javascript 11 12-08-2009 06:07 PM
K&R2, Binary-Tree, section 6.5 arnuld C Programming 7 05-15-2008 03:37 PM
Custom Section Handler XML parsing Hazzard ASP .Net 1 05-25-2004 11:43 AM
data conversion question (binary string to 'real string') Alexander Eisenhuth Python 5 07-25-2003 06:42 PM



Advertisments