cheksum function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cranium.2003@gmail.com

    #1

    cheksum function

    hello,
    I want a checksum function that operates on unsigned char *
    string. Is there any ready function avail to me on linux? Or if not how
    to write a function in C that generate cheksum in unsigned short
    variable from any length text string?

  • Flash Gordon

    #2
    Re: cheksum function

    cranium.2003@gm ail.com wrote:[color=blue]
    > hello,
    > I want a checksum function that operates on unsigned char *
    > string. Is there any ready function avail to me on linux?[/color]

    The C standard does not include one. For any that Linux may (or may not)
    provide you will have to ask in a Linux group.
    [color=blue]
    > Or if not how
    > to write a function in C that generate cheksum in unsigned short
    > variable from any length text string?[/color]

    Iterate over the string and apply whatever checksum algorithm you choose
    to each byte. Post your attempt and people will comment on it.
    Alternatively post me 250UKP in used notes and I will send you an
    implementation by return of post.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.

    Comment

    • marty

      #3
      Re: cheksum function

      Flash Gordon wrote:
      [color=blue]
      > cranium.2003@gm ail.com wrote:[color=green]
      >> hello,
      >> I want a checksum function that operates on unsigned char *
      >> string. Is there any ready function avail to me on linux?[/color][/color]

      An unsigned char * is not a string in C in the usual sense.

      A char * which is terminated by '\0' is a string in C. If you want your
      function to operation on an unsigned char * then you'll have to tell it how
      long the sequence of bytes are.

      unsigned short checkSum(unsign ed char *toBeChecked, int toBeCheckedLeng th);

      Martin

      Comment

      • cranium.2003@gmail.com

        #4
        Re: cheksum function

        hello marty,
        I am getting unsigned char * from other function. i want to
        ask is there any difference to apply strlen on unsigned char * and get
        false results as you said that[color=blue]
        > an unsigned char * is not a string in C in the usual sense.[/color]
        if it gives correct results then no problem regarding getting and
        sending length of that string to checksum function.
        Does I require to convert unsigned char * string to any other
        data type to checksum it?

        Comment

        • Joe Wright

          #5
          Re: cheksum function

          marty wrote:[color=blue]
          > Flash Gordon wrote:
          >
          >[color=green]
          >>cranium.2003@ gmail.com wrote:
          >>[color=darkred]
          >>>hello,
          >>> I want a checksum function that operates on unsigned char *
          >>>string. Is there any ready function avail to me on linux?[/color][/color]
          >
          >
          > An unsigned char * is not a string in C in the usual sense.
          >
          > A char * which is terminated by '\0' is a string in C. If you want your
          > function to operation on an unsigned char * then you'll have to tell it how
          > long the sequence of bytes are.
          >
          > unsigned short checkSum(unsign ed char *toBeChecked, int toBeCheckedLeng th);
          >
          > Martin[/color]

          No Marty.

          The definition of a C string is indeed in terms of char but there is
          nothing said about whether char is signed or unsigned. This is an
          implementation detail.

          Alone among integer types, there are three char types: 1. char, 2.
          signed char and 3. unsigned char. The first, char, is identical to one
          of the other two. Which one is implementation defined.

          ASCII is a 7-bit character set and all its characters will be
          'non-negative' even with 'char' being 'signed char'. EBCDIC on the other
          hand is an 8-bit character set. For its characters to be 'non-negative'
          the implementation of 'char' must be 'unsigned char'.

          --
          Joe Wright mailto:joewwrig ht@comcast.net
          "Everything should be made as simple as possible, but not simpler."
          --- Albert Einstein ---

          Comment

          • Peter Nilsson

            #6
            Re: cheksum function

            marty wrote:[color=blue]
            > Flash Gordon wrote:[color=green]
            > > cranium.2003@gm ail.com wrote:[color=darkred]
            > >> hello,
            > >> I want a checksum function that operates on unsigned char[/color][/color][/color]
            *[color=blue][color=green][color=darkred]
            > >> string. Is there any ready function avail to me on linux?[/color][/color]
            >
            > An unsigned char * is not a string in C in the usual sense.[/color]

            7.1.1p1:

            "A string is a contiguous sequence of characters terminated by and
            including the first null character."

            There is no mention of strings requiring char type. Indeed character
            codings are all positive. It's only when viewed through a signed plain
            char (or a signed char) that the bytes have negative values.
            [color=blue]
            > A char * which is terminated by '\0' is a string in C.[/color]

            As is an unsigned char which is terminated by a zero byte.

            String comparison functions (for instance) will interpret bytes as
            unsigned char values, not char values.
            [color=blue]
            > If you want your function to operation on an unsigned char * then
            > you'll have to tell it how long the sequence of bytes are.[/color]

            Not if it's null terminated...

            size_t foo(const void *msg)
            {
            return strlen(msg);
            }

            ....or...

            size_t foo(const unsigned char *msg)
            {
            return strlen((const char *) msg);
            }

            --
            Peter

            Comment

            • Ben Pfaff

              #7
              Re: cheksum function

              Joe Wright <joewwright@com cast.net> writes:
              [color=blue]
              > The definition of a C string is indeed in terms of char but there is
              > nothing said about whether char is signed or unsigned. This is an
              > implementation detail.[/color]

              This is the definition of a C string:

              "A string is a contiguous sequence of characters terminated
              by and including the first null character."

              Thus, the definition of a C string is not in terms of char. It
              is in terms of characters. C has three character types, so
              elements of a string can have any of those types.
              [color=blue]
              > Alone among integer types, there are three char types: 1. char,
              > 2. signed char and 3. unsigned char. The first, char, is identical to
              > one of the other two. Which one is implementation defined.[/color]

              It is notable that these are three distinct types, even though
              two of them have the same range.
              --
              "The lusers I know are so clueless, that if they were dipped in clue
              musk and dropped in the middle of pack of horny clues, on clue prom
              night during clue happy hour, they still couldn't get a clue."
              --Michael Girdwood, in the monastery

              Comment

              • marty

                #8
                Re: cheksum function

                > Not if it's null terminated...[color=blue]
                >
                > size_t foo(const void *msg)
                > {
                > return strlen(msg);
                > }
                >
                > ...or...
                >
                > size_t foo(const unsigned char *msg)
                > {
                > return strlen((const char *) msg);
                > }
                >[/color]

                Yes but how you'll need to start using casts in your program. I compiled
                the following with gcc -Wall -pedantic test.c
                and it gave me.

                test.c: In function `main':
                test.c:11: warning: pointer targets in initialization differ in signedness


                #include <stdio.h>
                #include <string.h>

                size_t foo(void *msg)
                {
                return strlen( msg);
                }

                int main(int argc, char *argv[])
                {
                unsigned char *string = "marty was here";
                printf("%d\n",f oo(string));
                return 0;
                }

                Your correct about strlen not seeming to mind.
                My own last post did not appear in my Newsreader (Knode). Don't know what I
                done wrong.

                Martin.

                Comment

                • Peter Nilsson

                  #9
                  Re: cheksum function

                  marty wrote:[color=blue][color=green]
                  > > Not if it's null terminated...
                  > >
                  > > size_t foo(const void *msg)
                  > > {
                  > > return strlen(msg);
                  > > }
                  > >
                  > > ...or...
                  > >
                  > > size_t foo(const unsigned char *msg)
                  > > {
                  > > return strlen((const char *) msg);
                  > > }
                  > >[/color]
                  >
                  > Yes but how you'll need to start using casts in your program.[/color]

                  The first shows that it isn't. But casts are not always wrong.
                  [color=blue]
                  > I compiled the following with gcc -Wall -pedantic test.c
                  > and it gave me.
                  >
                  > test.c: In function `main':
                  > test.c:11: warning: pointer targets in initialization differ in[/color]
                  signedness[color=blue]
                  >
                  > #include <stdio.h>
                  > #include <string.h>
                  >
                  > size_t foo(void *msg)
                  > {
                  > return strlen( msg);
                  > }
                  >
                  > int main(int argc, char *argv[])
                  > {
                  > unsigned char *string = "marty was here";[/color]

                  This _requires_ a diagnostic becuase char * and unsigned char * are
                  not compatible, even though they are convertable.

                  Try...

                  unsigned char *string = (void *) "marty was here";

                  ....or...

                  void *string = "marty was here";
                  [color=blue]
                  > printf("%d\n",f oo(string));[/color]

                  %d is not correct for size_t. Since you know the size to be small in
                  this case, you can simply do...

                  printf("%u\n", (unsigned) foo(string));
                  [color=blue]
                  > return 0;
                  > }[/color]

                  --
                  Peter

                  Comment

                  Working...