Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6092143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:24:31+00:00 2026-05-23T12:24:31+00:00

Suppose I need to parse a binary file, which starts with three 4-byte magic

  • 0

Suppose I need to parse a binary file, which starts with three 4-byte magic numbers. Two of them are fixed strings. The other, however, is the length of the file.

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Data.Attoparsec
import Data.Attoparsec.Enumerator
import Data.Enumerator hiding (foldl, foldl', map, head)
import Data.Enumerator.Binary hiding (map)
import qualified Data.ByteString as S
import System

main = do
    f:_ <- getArgs
    eitherStat <- run (enumFile f $$ iterMagics)
    case eitherStat of
        Left _err -> putStrLn $ "Not a beam file: " ++ f
        Right _ -> return ()

iterMagics :: Monad m => Iteratee S.ByteString m ()
iterMagics = iterParser parseMagics

parseMagics :: Parser ()
parseMagics = do
    _ <- string "FOR1"
    len <- big_endians 4 -- need to compare with actual file length
    _ <- string "BEAM"
    return ()

big_endians :: Int -> Parser Int
big_endians n = do
    ws <- count n anyWord8
    return $ foldl1 (\a b -> a * 256 + b) $ map fromIntegral ws

If the stated length doesn’t match the actual length, ideally iterMagics should return an error. But how? Is the only way to pass the actual length in as an argument? Is this the iteratee-ish way to do so? Not very incremental for me 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:24:31+00:00Added an answer on May 23, 2026 at 12:24 pm

    This can easily be done with enumeratees. First you read the three 4-byte magic numbers, then run an inner iteratee over the remainder. If you’re using iteratee, it would look like more-or-less like this:

    parseMagics :: Parser ()
    parseMagics = do
        _ <- string "FOR1"
        len <- big_endians 4 -- need to compare with actual file length
        _ <- string "BEAM"
        return len
    
    iterMagics :: Monad m => Iteratee S.ByteString m (Either String SomeResult)
    iterMagics = do
      len <- iterParser parseMagics
      (result, bytesConsumed) <- joinI $ takeUpTo len (enumWith iterData I.length)
      if len == bytesConsumed
        then return $ Right result
        else return $ Left "Data too short"
    

    In this case it won’t throw an error if the file is too long, but it will stop reading. You can modify it to check for that condition fairly easily. I don’t think Enumerator has an analog function to enumWith, so you’d probably need to count the bytes manually, but the same principle would apply.

    Possibly a more pragmatic approach is to check the filesize before running the enumerator, and then just compare that to the value in the header. You’ll need to either pass the filesize, or the filepath, as an argument to the iteratee (but not the parser).

    import System.Posix
    
    iterMagics2 filepath = do
      fsize <- liftIO . liftM fileSize $ getFileStatus filepath
      len <- iterParser parseMagics
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you need to run a program on the world’s fastest supercomputer which will
Suppose I need to break out of three or four nested for loops at
Suppose we need to check three boolean conditions to perform a select query. Let
Suppose you have two seperate ASP.NET Web Application projects that both need to use
I need to parse some text file, create objects for various entities encountered in
Suppose you are working with some data, which comes from a JSON file. You
I need to parse a lot of html files in order to know which
Suppose you are making a GUI application, and you need to load/parse/calculate a bunch
I have several strings that I need to parse. The string is supposed to
Suppose you have a collection of a few hundred in-memory objects and you need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.