Coder Social home page Coder Social logo

mhx / convert-binary-c Goto Github PK

View Code? Open in Web Editor NEW
7.0 6.0 8.0 3.95 MB

Binary Data Conversion using C Types

XS 0.90% Perl 59.12% Makefile 0.25% Shell 0.01% C 34.50% TeX 0.14% CSS 0.08% Yacc 1.40% Lex 0.09% HTML 0.01% Terra 0.16% Roff 0.06% Raku 2.95% CMake 0.33%

convert-binary-c's Introduction

Convert-Binary-C

Binary Data Conversion using C Types

Description

Convert::Binary::C is a preprocessor and parser for C type definitions. It is highly configurable and should support arbitrarily complex data structures. Its object-oriented interface has pack and unpack methods that act as replacements for Perl's pack and unpack and allow to use the C types instead of a string representation of the data structure for conversion of binary data from and to Perl's complex data structures.

Actually, what Convert::Binary::C does is not very different from what a C compiler does, just that it doesn't compile the source code into an object file or executable, but only parses the code and allows Perl to use the enumerations, structs, unions and typedefs that have been defined within your C source for binary data conversion, similar to Perl's pack and unpack.

Beyond that, the module offers a lot of convenience methods to retrieve information about the C types that have been parsed.

Installation

Installation of the Convert::Binary::C module follows the standard Perl Way and should not be harder than:

perl Makefile.PL
make
make test
make install

Note that you may need to become superuser to make install.

If you're building the module under Windows, you may need to use a different make program, such as nmake, instead of make.

When running 'make test' on on slower systems please be patient, since some of the tests are quite time consuming. The time required for running the whole test suite depends on your Perl version, the features you're building the module with, and of course your machine.

Upgrading

If you are upgrading from a previous release of Convert::Binary::C, please check the Changes file. In its current state, the module is still subject to changes that may affect compatibility with older releases.

Documentation

To see the documentation, use the perldoc command:

perldoc Convert::Binary::C
perldoc Convert::Binary::C::Cached

You can also visit CPAN Search and see the documentation online as pretty nice HTML. This is also where you will find the most recent version of this module:

https://metacpan.org/release/Convert-Binary-C

Even though the documentation contains a large amount of tested example code, you might want some working example scripts. You can find them in the

examples

subdirectory after you've built the module. These scripts normally require Convert::Binary::C to be installed on your system. If you want to test the examples prior to installing Convert::Binary::C, you can start the examples like this after building the module:

perl -Mblib examples/script.pl

Otherwise just run the example scripts like any other Perl script.

Configuration

Configuring a Convert::Binary::C object correctly can be quite painful if you don't know every little detail about your compiler.

However, if you're lucky, you can use the 'ccconfig' tool that comes with this package. It was written to automatically retrieve the complete compiler configuration. This may not work always, or retrieve the complete configuration, but it should at least give you some point to start from.

Just run

ccconfig -c compiler

with compiler being the name of your compiler executable.

You can see the documentation for 'ccconfig' using the perldoc command:

perldoc ccconfig

As the tool is very experimental, any feedback on 'ccconfig' is really appreciated!

Compatibility

The module should build on most of the platforms that Perl runs on. I have tested it on:

  • Various Linux systems
  • Various BSD systems
  • HP-UX
  • Compaq/HP Tru64 Unix
  • Mac-OS X
  • Cygwin
  • Windows 98/NT/2000/XP

Also, most architectures should be supported. Tests were done on:

  • Various Intel Pentium and Itanium systems
  • Various Alpha systems
  • HP PA-RISC
  • Power-PC
  • StrongARM (the module worked fine on an IPAQ system)

The module should build with any perl from 5.005 up to the latest development version. It will also build with perl 5.004, but then the test suite cannot be run completely.

Multithreaded perl binaries are explicitly supported, as the module is intended to be thread-safe.

Features

You can enable or disable certain features at compile time by adding options to the Makefile.PL call. However, you can safely leave them at their default.

Available features are debug to build the module with debugging support and ieeefp to explicitly enable or disable IEEE floating point support.

The debug feature depend on how your perl binary was built. If it was built with the DEBUGGING flag, the debug feature is enabled, otherwise it is disabled by default.

The ieeefp feature depends on how your machine stores floating point values. If they are stored in IEEE format, this feature will be enabled automatically. You should really only change the default if you know what you're doing.

You can enable or disable features explicitly by adding the arguments

enable-feature
disable-feature

to the Makefile.PL call. To explicitly build the module with debugging enabled, you would say:

perl Makefile.PL enable-debug

This will still allow you to pass other standard arguments to Makefile.PL, like

perl Makefile.PL enable-debug OPTIMIZE=-O3

Copyright

Copyright (c) Marcus Holland-Moritz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The ucpp library is (c) 1998-2002 Thomas Pornin. For license and redistribution details refer to 'ctlib/ucpp/README'.

Portions copyright (c) 1989, 1990 James A. Roskind.

convert-binary-c's People

Contributors

gregoa avatar kentfredric avatar mhx avatar plicease avatar s-nez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

convert-binary-c's Issues

WARNING: The following autogenerated files are missing

Right after cloning - I ran perl Makefile.PL but it says some files are missing:

$ perl Makefile.PL enable-debug
Building with feature 'debug'
Building with feature 'ieeefp'
Finding dependencies...
Setting license tag...

--> WARNING: The following autogenerated files are missing:
-->
--> token/t_parser.c token/t_keywords.c token/t_ckeytok.c token/t_basic.c token/t_pragma.c token/t_config.c token/t_sourcify.c token/t_tag.h token/t_tag.c token/t_hookid.h token/t_hookid.c token/t_blproperty.h token/t_blproperty.c
-->
--> Please run:
-->
--> make regen
-->
--> If you just extracted the source distribution and did
--> not modify or delete any files, something is seriously
--> wrong.

Generating a Unix-style Makefile
Writing Makefile for Convert::Binary::C
Writing MYMETA.yml and MYMETA.json

can't tell typedef linked list

#failed

my $c = Convert::Binary::C->new->parse(<<ENDC);
  typedef struct Node {
        struct Node *link;
        int value;
} Node;
ENDC

#correct

my $c = Convert::Binary::C->new->parse(<<ENDC);
typedef struct Node Node;
struct Node {
    Node*       link;
    int                 value;
    };
ENDC

Is it possible to support the first expression?

packed structures too small; sizeof doesn't report the right size

One of the main reasons to use C::B::C is to handle padding, yet C::B::C doesn't reproduce the trailing padding C adds. sizeof doesn't report the right size, and packed structures too small.

#include <stdio.h>

struct Foo {
   short s;
   char  c;
};

int main(void) {
   printf("%zu\n", sizeof(struct Foo));
}
use 5.014;
use warnings;

use Convert::Binary::C qw( );

my $c = Convert::Binary::C->new->parse(<<'__EOS__');
   struct Foo {
      short s;
      char  c;
   };
__EOS__

say $c->sizeof('Foo');
say length($c->pack('Foo', { s => 0, c => 0 }));
$ gcc -Wall -Wextra -pedantic a.c -o a && ./a
4

$ perl a.pl
3
3

trivial change, fix for old perl before introduction of defined-or operator

The patch/diff:

diff --git a/tests/include/stuff/generate.pl b/tests/include/stuff/generate.pl
index e1572e3..06a1baf 100644
--- a/tests/include/stuff/generate.pl
+++ b/tests/include/stuff/generate.pl
@@ -4,7 +4,8 @@ use warnings;
 use IO::File;
 use File::Spec;

-my $dir = shift // '.';
+my $v = shift;
+my $dir = defined($v) ? $v : '.';

 for my $f ('aa' .. 'az') {
   my $fh = IO::File->new(">" . File::Spec->catfile($dir, "$f.h")) or die;

Thanks for the very nice and useful project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.