String parsing in Kernel

Hey guys,

I’ve got a device driver that I wrote that needs to parse a file for some configuration settings. I am using ZwCreateFile() and ZwReadFile() to open and read the file. It is a relatively short file so I read it in as one string and then I want to break that string up and extract the configuration values (half of which are ints and the other half floats) and store them into two different arrays. I am currently using strtok() to break up the string and then I was planning on using atoi()/atof() OR stoi()/stof() to convert the string representations of the configuration values into the int and float values respectively. I get a linker error, however, when I try to use the conversion functions. It lets me use strtok() just fine though. I #include <stdlib.h> and #include <string.h> in my main header file and declare the functions in my source file with the ‘extern’ keyword but it still gives me a linker error saying that I have unresolved externals (referring to the stoi()/stof() but not strtok()). I’ve done some hunting but can’t figure this out. Any ideas? Or just a point in the right direction? Thanks

Regards</string.h></stdlib.h>

Why don’t you store the settings in the registry?

Why have a config file at all? Just put each config setting as a reg value and you don’t have anything to parse. Remember, regardless of where the float comes from, you need to wrap any float use (init, read or write) with a KeSave/RestoreFloatingPointState call pair.

d

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Friday, January 03, 2014 4:04 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] String parsing in Kernel

Hey guys,

I’ve got a device driver that I wrote that needs to parse a file for some configuration settings. I am using ZwCreateFile() and ZwReadFile() to open and read the file. It is a relatively short file so I read it in as one string and then I want to break that string up and extract the configuration values (half of which are ints and the other half floats) and store them into two different arrays. I am currently using strtok() to break up the string and then I was planning on using atoi()/atof() OR stoi()/stof() to convert the string representations of the configuration values into the int and float values respectively. I get a linker error, however, when I try to use the conversion functions. It lets me use strtok() just fine though. I #include <stdlib.h> and #include <string.h> in my main header file and declare the functions in my source file with the ‘extern’ keyword but it still gives me a linker error saying that I have unresolved externals (referring to the stoi()/stof() but not strtok()). I’ve done some hunting but can’t figure this out. Any ideas? Or just a point in the right direction? Thanks

Regards


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</string.h></stdlib.h>

xxxxx@gmail.com wrote:

I am currently using strtok() to break up the string and then I was planning on using atoi()/atof() OR stoi()/stof() to convert the string representations of the configuration values into the int and float values respectively.

stoi and stof are not part of the C runtime library. They were added to
the C++ standard starting with C++ 11, but the kernel C runtime library
supports only the older C standard. atoi and atof should be there, however.

I get a linker error, however, when I try to use the conversion functions. It lets me use strtok() just fine though. I #include <stdlib.h> and #include <string.h> in my main header file and declare the functions in my source file with the ‘extern’ keyword but it still gives me a linker error saying that I have unresolved externals (referring to the stoi()/stof() but not strtok()).

The other advice to do this via the registry is the right advice, but as
as long as you have libcntpr.lib in your library list, it should be able
to find all three of those routines.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.</string.h></stdlib.h>

I would prefer that strings do not exist, and certainly do not need to be
parsed. strtok() is one of those functions that is so badly designed that
if it did not already exist, no one would be stupid enough to invent it.
It retains global state in static variables, which means that if two
instances or your decice exist, you have a time bomb waiting to detonate.
Use the Registry; that’s what it is for. Text configuration files are SO
1960s!. Store the floats as REG_BINARY. But what good is a float in the
kernel? You do understand that you can’t do floating point operations in
the kernel? At least, not without doing an expensive save/restore of the
floating point state.

I wouldn’t consider for a second using a text file to configure a driver.
strtok() is a function whose use should be declared illegal by Federal law
(note that there is a new, stateless, strtok to which you provide a
pointer to the retained state structure. This one is thread-safe in the
kernel. In user space, the horrible artifacts of the original defective
library design are handled by thread-local variables, a concept not
available in the kernel).

Why do you think a text file is a reasonable alternative? (The
explanation “I don’t understand the Registry” is not a valid answer).
joe

Hey guys,

I’ve got a device driver that I wrote that needs to parse a file
for
some configuration settings. I am using ZwCreateFile() and
ZwReadFile() to open and read the file. It is a relatively short file
so I read it in as one string and then I want to break that string up
and extract the configuration values (half of which are ints and the
other half floats) and store them into two different arrays. I am
currently using strtok() to break up the string and then I was planning
on using atoi()/atof() OR stoi()/stof() to
convert the string representations of the configuration values into the
int and float values respectively. I get a linker error,
however, when I try to use the conversion functions. It lets me use
strtok() just fine though. I #include <stdlib.h> and #include
> <string.h> in my main header file and declare the functions in my source
file with the ‘extern’ keyword but it still gives me a linker error
saying that I have unresolved externals (referring to the
> stoi()/stof() but not strtok()). I’ve done some hunting but can’t
figure this out. Any ideas? Or just a point in the right direction?
Thanks
>
> Regards
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
></string.h></stdlib.h>

> xxxxx@gmail.com wrote:

> I am currently using strtok() to break up the string and then I was
> planning on using atoi()/atof() OR stoi()/stof() to convert the string
> representations of the configuration values into the int and float
> values respectively.

stoi and stof are not part of the C runtime library. They were added to
the C++ standard starting with C++ 11, but the kernel C runtime library
supports only the older C standard. atoi and atof should be there,
however.

> I get a linker error, however, when I try to use the conversion
> functions. It lets me use strtok() just fine though. I #include
> <stdlib.h> and #include <string.h> in my main header file and declare
>> the functions in my source file with the ‘extern’ keyword but it still
>> gives me a linker error saying that I have unresolved externals
>> (referring to the stoi()/stof() but not strtok()).
>

Your problem is that you are trying to solve the wrong problem. Instead
of trying to link CRT functions, you should be trying to come up with a
way to not need them. It’s called “the Registry”. Learn it. Use it.
Stop trying to emulate MS-DOS.
joe
> The other advice to do this via the registry is the right advice, but as
> as long as you have libcntpr.lib in your library list, it should be able
> to find all three of those routines.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
></string.h></stdlib.h>

Iirc the strtok in kernel requires you to pass in the state vars, no static state kept underneath the covers

d

Bent from my phone


From: xxxxx@flounder.commailto:xxxxx
Sent: ?1/?3/?2014 7:00 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] String parsing in Kernel

I would prefer that strings do not exist, and certainly do not need to be
parsed. strtok() is one of those functions that is so badly designed that
if it did not already exist, no one would be stupid enough to invent it.
It retains global state in static variables, which means that if two
instances or your decice exist, you have a time bomb waiting to detonate.
Use the Registry; that’s what it is for. Text configuration files are SO
1960s!. Store the floats as REG_BINARY. But what good is a float in the
kernel? You do understand that you can’t do floating point operations in
the kernel? At least, not without doing an expensive save/restore of the
floating point state.

I wouldn’t consider for a second using a text file to configure a driver.
strtok() is a function whose use should be declared illegal by Federal law
(note that there is a new, stateless, strtok to which you provide a
pointer to the retained state structure. This one is thread-safe in the
kernel. In user space, the horrible artifacts of the original defective
library design are handled by thread-local variables, a concept not
available in the kernel).

Why do you think a text file is a reasonable alternative? (The
explanation “I don’t understand the Registry” is not a valid answer).
joe

> Hey guys,
>
> I’ve got a device driver that I wrote that needs to parse a file
for
> some configuration settings. I am using ZwCreateFile() and
> ZwReadFile() to open and read the file. It is a relatively short file
so I read it in as one string and then I want to break that string up
and extract the configuration values (half of which are ints and the
other half floats) and store them into two different arrays. I am
currently using strtok() to break up the string and then I was planning
on using atoi()/atof() OR stoi()/stof() to
> convert the string representations of the configuration values into the
int and float values respectively. I get a linker error,
> however, when I try to use the conversion functions. It lets me use
strtok() just fine though. I #include <stdlib.h> and #include
> <string.h> in my main header file and declare the functions in my source
file with the ‘extern’ keyword but it still gives me a linker error
saying that I have unresolved externals (referring to the
> stoi()/stof() but not strtok()). I’ve done some hunting but can’t
figure this out. Any ideas? Or just a point in the right direction?
Thanks
>
> Regards
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</string.h></stdlib.h></mailto:xxxxx></mailto:xxxxx>

On 04-Jan-2014 02:07, xxxxx@broadcom.com wrote:

Why don’t you store the settings in the registry?

Because Registry is one of those proprietary (ugh!) MS things…
Files are handy for simulations and porting from/to other platforms.

So what one can do to consume an ini file in kernel driver?

a. Convert the ini file into .reg or .inf format, then import into the
registry. The driver reads from the registry as usual; no parsing!

b. Google for “portable” libraries for ini files.
There are about half dozen of such opensource projects, one or two may
be suitable. Most of these support writing or updating the files as
well. Again, no parsing.

– pa

> On 04-Jan-2014 02:07, xxxxx@broadcom.com wrote:

> Why don’t you store the settings in the registry?

Because Registry is one of those proprietary (ugh!) MS things…
Files are handy for simulations and porting from/to other platforms.

Andva Windows device driver is portable to other platforms’ native
environment? If you are sharing code, the right abstraction is “Get me
the configuration parameters” and whether it goes to the Registry or reads
a file is a platform-specific subroutine.

Create a configuration spec in XML. Write an XML-to-whatever program (a
tiny number of lines of Perl, Python, or whatever your favorite scripting
language is). I’ve done this many times when I needed portability between
Mac, Windows, and Unix (twenty years ago!).

You should not be attempting a one-solution-fits-all solution at the code
level. Move up a level, and your “one-solution” is just something that
compiles into the appropriate native representation. You have spent more
time trying to get this to link than the more general solution would have
cost.
joe

So what one can do to consume an ini file in kernel driver?

a. Convert the ini file into .reg or .inf format, then import into the
registry. The driver reads from the registry as usual; no parsing!

b. Google for “portable” libraries for ini files.
There are about half dozen of such opensource projects, one or two may
be suitable. Most of these support writing or updating the files as
well. Again, no parsing.

– pa


NTDEV is sponsored by OSR

Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Sorry for dropping out of the loop on this the past couple of days. I was away for the weekend. I guess I was going to use a text file is because this driver will only be used/configured by a small number of users who would know how to edit the file properly and because I am still learning my way around working with the Windows kernel and driver programming so I wasn’t sure what the best approach was. Registry seems to be overwhelmingly supported as the better option so I suppose I will go that route. Thanks for all the responses.