Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Little endian C array

Name: Anonymous 2008-12-23 7:18


/*
 * Copyright 2008 Anonymous
 *
 * This snippet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

/* The parse function allows the extraciton of various length word data from
 * a little endian buffer
 *
 * @note This function doesn't care about the buffer length, as it should be
 *       correctly specified into the fmt field.
 *
 * @param buffer The buffer containing the data;
 * @param fmt The extraction format to use
 * @param offset A internal use offset that must be initialized to zero
 * @param ret A pointer to the memory area that will contain the extracted
 *            data
 * @return true if there's still something to extract, false otherwise
 */
bool parse(const uint8_t *buffer, const char *fmt,
           uint32_t *offset, uint32_t *ret)
{
    /* Two separated words: 0 for the format; 1 for the buffer */
    uint16_t *offs;
    uint32_t shifts;
    const char *f;

    offs = (uint16_t *)offset;
    if (*(f = fmt + offs[0]) == 0)
        return false;
    buffer += offs[1];
    switch (*f) {
    case 'b':   /* Byte (8) */
        *ret = *buffer;
        offs[1] ++;
        break;
    case 'h':   /* Half word (16) */
        *ret = *(uint16_t *)buffer;
        offs[1] += 2;
        break;
    case 'w':   /* Word (32) */
        *ret = *(uint32_t *)buffer;
        offs[1] += 4;
        break;
    }
    offs[0]++;
    return true;
}


const char *format = "bhwb";
const uint8_t buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

int main(int argc, char **argv)
{
    uint32_t offset;
    uint32_t val;
    uint8_t *p;

    offset = 0;
    p = (uint8_t *) &val;
    while (parse(buf, format, &offset, &val)) {
        printf("0x%08x (", val);
        printf("0x%02x ", p[0]);
        printf("0x%02x ", p[1]);
        printf("0x%02x ", p[2]);
        printf("0x%02x)\n", p[3]);
    }
    exit(0);
}

Name: Anonymous 2008-12-23 7:38

fixed it for you:
/*
 * Copyright 2008 Anonymous
 *
 * This snippet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 666 of the License, or
 * (at your option) any later version.
 *
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

/* The parse function allows the extraciton of various length word data from
 * a little endian buffer
 *
 * @note This function doesn't care about the buffer length, as it should be
 *       correctly specified into the fmt field.
 *
 * @param buffer The buffer containing the data;
 * @param fmt The extraction format to use
 * @param offset A internal use offset that must be initialized to zero
 * @param ret A pointer to the memory area that will contain the extracted
 *            data
 * @return true if there's still something to extract, false otherwise
 */
bool parse(const uint8_t *buffer, const char *fmt,
           uint32_t *offset, uint32_t *ret) {
 /* Two separated words: 0 for the format; 1 for the buffer */
 uint16_t *offs;
 uint32_t shifts;
 const char *f;

 offs = (uint16_t *)offset;
 if (*(f = fmt + offs[0]) == 0)
  return false;
 buffer += offs[1];
 switch (*f) {
  case 'b':   /* Byte (8) */
   *ret = *buffer;
   offs[1] ++;
   break;
  case 'h':   /* Half word (16) */
   *ret = *(uint16_t *)buffer;
   offs[1] += 2;
   break;
  case 'w':   /* Word (32) */
   *ret = *(uint32_t *)buffer;
   offs[1] += 4;
   break;
 }
 offs[0]++;
 return true;
}

int main(int argc, char **argv) {
 const char *format = "bhwb";
 const uint8_t buf[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
 uint32_t offset;
 uint32_t val;
 uint8_t *p;

 offset = 0;
 p = (uint8_t *) &val;
 while (parse(buf, format, &offset, &val)) {
  printf("0x%08x (", val);
  printf("0x%02x ", p[0]);
  printf("0x%02x ", p[1]);
  printf("0x%02x ", p[2]);
  printf("0x%02x)\n", p[3]);
 }
 exit(EXIT_SUCCESS);
}

Name: Anonymous 2008-12-23 7:47

>>2
Why didn't you just post a patch?

Name: Anonymous 2008-12-23 7:49

if (*(f = fmt + offs[0]) == 0)
  return false;
 buffer += offs[1];

Name: Anonymous 2008-12-23 7:52

>>4
+= considered harmful.

Name: Anonymous 2008-12-23 8:01

>>5
I don't think so, dear Anonix developer.

Name: Anonymous 2008-12-23 8:02

>>3
because then you might notice that i decided to distribute my modified version under the terms of a later version of the license.

Name: Anonymous 2008-12-23 8:13

>>7
Ok, you gained my respect.

Name: Anonymous 2008-12-23 9:37

Name: Anonymous 2008-12-23 18:39

Name: Anonymous 2008-12-23 18:41

Fucking fuck. I hate shiichan.

Name: Anonymous 2008-12-24 0:04

>>11
They should replace this with a REAL forum like PHPNUKE.

Name: Anonymous 2008-12-24 2:07

>>12
He's right you know.

Name: Anonymous 2008-12-24 12:32

i can't believe you copyright code like that, and to anonymous, lolu

Name: Anonymous 2008-12-24 15:32

This won't work on most non-x86 architectures where accesses must typically be aligned.  You can do an 8-bit read at any address, ad 16-bit read at any even address, a 32-bit read at any address that's a multiple of 4.  Even on x86, ignoring these alignments may cause performance penalties.

Of course, many other architectures use big endian anyway, so the code's of limited use.

Name: Anonymous 2008-12-24 15:39

>>14,1
If the copyright belongs to Anonymous, we are all free to copy, modify, sell, and relicense it as we please.  Only those who are not Anonymous are bound to the harsh terms below.

Name: Anonymous 2008-12-24 20:02

>>16
interesting.

Name: Anonymous 2011-02-04 16:30

Name: Anonymous 2011-02-04 18:13

Name: Anonymous 2013-09-01 14:34




          r‐- 、                    /
         、.  _|___/ ̄`ヽ            /      い
   ',  /`ヽ_,X´ |-、/   ̄`ヽノ             |   か  い
       |   _r‐< ̄>ー-、.イ__ \        __|.   し  の
    ',  /「,>''"´/`ヽー-く⌒l_`ヽヽ     /   \   ら
 \  ', lア´  /./      \-'ヽ. ハ   ./   私    ?
 .  \  /   メ、|   ,ハ    ヽ/、  |   |    の   ト-----
    ∠!   ./rト八  l/ -|‐ ハ  |,ハ/ |    |    土   |
 ‐ ─  、  /|  j_r!\| -rァテ、|  ト、| /   |    下   |
    / \.ハ"  ,    j__rソ,  /.) Yl   <. 見. 座.  |
 //   ./ 人  、_   "//|  八    | た. を   |
     /   ,' .| r|>、__ ,,. イ /ヽ| ハ  ヽ   | 時      |   ____
   /   .| ∨/::::::/ |__,. イレ' r' \ |  )  、        ´       `ヽ
     .r-'∨.,':::i::::|_/\  rソ/:::::::::Y ∧   ` ー---‐/      最
      ∨  |::/::::(/ム)ヽrノ:::i::::::::::::::∨  \  /L  /      謝.  終
      | r/、!:::::::く人):::::::::/:::::::::::::::::|     ヽ/_/  |  貴   る   的
      ,ハ /ヽ,ヘ、::::::::l:::::::::::::ヽ_r- 、__ノ ヽ、 く_」    |  方   事  に
     ( ./  ヽ,ハー::::::::::::::::イ|  ̄ |   ',   |.     |  達   に
      ノ7  //rく::::r-、:::::::::::::;| _  |    |  /     |  な   .な
     |./  ./_,.へ.  ,.ヘ. ̄ /´   ,ハ_   } ̄`Y´{  |  の  る
     ,'  /::::::::::::::::Y::::::::ヽ/   / ∧`ヽノレ' ハ」  |  よ.   の
   r/|  'J::::::::::::::::/:::::::::::/  ,イ_/|:::::ヾ]_)ハ   /   |   :  .は
   く∧__j:::::::::::::::::;'::::::::::::(|  _ノ:::::::::,':::::::::/」 |/     |   :
     |:::::::::::::::::::::::::::::::::::::::し'::::::::::::/:;'::/´        '、 ?
     ヽ:;____、:::::::::::::::::::::::::::/::/´             \
             ̄`ヽ:;____/               ̄ ̄ ̄

Name: Anonymous 2013-09-01 16:06



                             ミ
                             Ξ この程度なら
       , ‐ ''"´ ̄ ̄ ̄ ̄`゙'' 、-- 、     ニ   私が動く程の
     ,「 `7  _,r─-、__.、__)  |, ヘ    Ξ     事態じゃない
   /´\| / _/,ゞ'"´ ̄`ー--、へ.,__ノ./┐  彡
   \  ∨ r'/ /  ./|    `'ー-、ヾ-、」    '/ リl || |l| l l| ll |l| |l| l l| l
   ./ >-r´/  / -|-/、|   , ,ハ   ヽ }   /
   { r‐''´ ,'  .,' |斤テr‐|__/|_/__.|   | |/  /  /
  r∨ ,  |   |八弋_ソ    ィテ、|/  // |/ /
== く.'Y i   |_//⊂⊃     , り /、/`    二ニ====---
   `| |     |' |       ⊂| ||     |` \
    .| |     |   、     ´  人  、   |    \
      、ヽ  .!   \ 、.,_,. イ   \ ` ー-、/7
      \  人     ヽト、_|人     ー--} }ー-、
       ン´  \ ヽ   )__]\_>-- 、_.ンノ、  )
      /      )ノ rイ-┐、| ハ \  く_r'  |/
     ./  /   }ン|__]┬ ' __|__ ヽ、 `ヽ
    rヘ. /     /  |」 ||||::::::::::::::::::ト、ハ   ト、
    \ \     | /|  .||||::;: -、‐-、|_.り   ノ )
     l__rヘ.__r-、_∨ /} lア ̄ヽ }  } ノ| /レ'
       \ ヽ--<> '´     }__} __ノ  }

Don't change these.
Name: Email:
Entire Thread Thread List