Thread: How To Incorporate input_byte In For Loop Query ?

  1. #1
    Registered User
    Join Date
    Jan 2023
    Posts
    32

    How To Incorporate input_byte In For Loop Query ?

    I have the following part of a C# Code :-

    Code:
    while(current_input_position <= input_max_valid_position) {
    
                    input_byte = input_data[current_input_position];
                    current_input_position++;
    
    
                    for (int i = 0xFF; i >= 0x00; --i)
    
    
                        if (input_byte == 0xFF) {
                        //==============
                        // Fin de chunk
                        //==============
                        Save();
                        current_output_index++;
    
    
                        RLE_sumador            = 0;
                        line_min_length        = int.MaxValue;
                        line_max_length        = 0;
                        width_current        = 0;
                        image_height        = 0;
    
    
                    } else if(input_byte == 0xFF) {
                        //=================
                        // Siguiente linea
                        //=================
                        if(width_current < line_min_length) line_min_length = width_current;
                        if(width_current > line_max_length) line_max_length = width_current;
    
    
                        if(forced_image_width!=null) {
                            count = forced_image_width.Value - width_current;
                            if(count > 0) {
                                for(n=0; n<count; n++) {
                                    output_buffer.Add(0);
                                }
                            }
                        }
    
    
                        image_height++;
                        width_current = 0;
    I am adapting a Code to try to brute force all combinations of input_byte combinations, going backwards from 0xFF to 0x00, if possible I would like to exclude, all combinations of Hex Values for input_byte being numbers i.e 00 01 02 etc, as these will never produce Bitmaps.There are five input_byte == blocks of Code I have typed each one as 0xFF as that is the Hex Value, where the For Loop starts from. When I use --i in the for loop line of code, it runs for a few seconds, so not working for me.

    I tried --input_byte instead however it just runs and runs, and I know it isn't trying out all combinations of input_byte, as in the attached image, to this Thread, the number in the () stays at 636, the numbers should be a variety.

    The Program extracts images, from a DOS Games Graphics Files, and saves them as Greyscale Bitmap Files. I could provide more Code if needed, input_byte is declared as a byte higher up in the Code. Is the for loop in the wrong place ? How do I incorporate, input_byte, in the for loop ?

    Any help would be much appreciated.

    Regards

    Eddie Winch
    Attached Images Attached Images How To Incorporate input_byte In For Loop Query ?-unpac-program-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,464
    The first step would be to apply consistent indentation to your code.
    Code:
    while (current_input_position <= input_max_valid_position) {
      input_byte = input_data[current_input_position];
      current_input_position++;
    
      for (int i = 0xFF; i >= 0x00; --i)
        if (input_byte == 0xFF) {
          //==============
          // Fin de chunk
          //==============
          Save();
          current_output_index++;
    
          RLE_sumador = 0;
          line_min_length = int.MaxValue;
          line_max_length = 0;
          width_current = 0;
          image_height = 0;
        } else if (input_byte == 0xFF) {
          //=================
          // Siguiente linea
          //=================
          if (width_current < line_min_length)
            line_min_length = width_current;
          if (width_current > line_max_length)
            line_max_length = width_current;
    
          if (forced_image_width != null) {
            count = forced_image_width.Value - width_current;
            if (count > 0) {
              for (n = 0; n < count; n++) {
                output_buffer.Add(0);
              }
            }
          }
          image_height++;
          width_current = 0;
    Line 18 is nonsense, it will never happen as the same condition is already trapped by line 6.

    > if possible I would like to exclude, all combinations of Hex Values for input_byte being numbers i.e 00 01 02 etc,
    What does this mean?
    00 to 09
    10 to 19
    20 to 29
    ...
    90 to 99

    Are things like 0A and A0 also in the list, because they contain a numeric nibble?

    > I am adapting a Code to try to brute force all combinations of input_byte combinations
    Cite your source.
    It might be useful to know what you're trying to convert.

    Or even whether the code you have is a worthwhile place to start.
    If the code is a mess, it's probably better to understand the problem on a fundamental level and write the code to solve it, than struggle with trying to translate a mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Hi Salem

    Many thanks for your useful post, to my thread :-

    Yes when I say Number Hex Values, ones that are all Numbers, 0A etc, i.e. when numbers are with a letter that is okay.

    Here is the Full Program.cs Code :-

    Code:
    using System;using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
    
    
    
    namespace Unpac {
        class Program {
    
    
            //_____________________________________________________________________
            static string        input_filename_no_ext;
            static int            current_output_index;
            static string        output_directory;
            static List<byte>    output_buffer;
            static int            image_height;
            static int            line_min_length;
            static int            line_max_length;
    
    
            //_____________________________________________________________________
            static void Main(string[] args) {
    
    
                int?        forced_image_width;
                string        input_fullpath;
                string        input_directory;
                string        input_filename;
                byte[]        input_data;
                long        input_max_valid_position;
                long        current_input_position;
                byte        input_byte;
                byte        RLE_sumador;
                int            count;
                byte        b;
                int            n;
                bool        RLE_FC_swap;
                byte        b2;
                int            width_current;
    
    
    
    
                if(args.Length<2) {
                    Console.WriteLine("Use: unpac.exe <InputFile.pac> <OutputDir> [ImageWidth]");
                    return;
                }
    
    
                if(args.Length>2) {
                    forced_image_width = int.Parse(args[2]);
                } else {
                    forced_image_width = null;
                }
                
                try {
                    input_fullpath        = Path.GetFullPath(args[0]);
                    input_directory        = Path.GetDirectoryName(input_fullpath);
                    input_filename        = Path.GetFileName(input_fullpath);
    
    
                    input_fullpath        = Path.Combine(input_directory, input_filename);
                    if(!File.Exists(input_fullpath)) throw new Exception();
    
    
                    int p = input_filename.LastIndexOf('.');
                    if(p==-1) {
                        input_filename_no_ext = input_filename;
                    } else {
                        input_filename_no_ext = input_filename.Substring(0, p);
                    }
    
    
                } catch {
                    Console.WriteLine("ERROR: invalid input file");
                    return;
                }
    
    
                try {
                    output_directory = Path.GetFullPath(args[1]);
                    if(!Directory.Exists(output_directory)) {
                        Directory.CreateDirectory(output_directory);
                    }
                } catch {
                    Console.WriteLine("ERROR: invalid output directory");
                    return;
                }
    
    
                try {
                    input_data = File.ReadAllBytes(input_fullpath);
                } catch {
                    Console.WriteLine("ERROR: cannot read from input file");
                    return;
                }
    
    
                Console.WriteLine($"{input_filename}");
    
    
                input_max_valid_position    = input_data.Length - 1;
                current_input_position        = 0;
                current_output_index        = 1;
                output_buffer                = new List<byte>();
    
    
                RLE_sumador                    = 0;
                line_min_length                = int.MaxValue;
                line_max_length                = 0;
                width_current                = 0;
                image_height                = 0;
                count                       = 0;
                
                while(current_input_position <= input_max_valid_position) {
    
    
                    input_byte = input_data[current_input_position];
                    current_input_position++;
    
    
                    for (int i = 0xFF; i >= 0x00; --i)
    
    
                        if (input_byte == 0xFF) {
                        //==============
                        // Fin de chunk
                        //==============
                        Save();
                        current_output_index++;
    
    
                        RLE_sumador            = 0;
                        line_min_length        = int.MaxValue;
                        line_max_length        = 0;
                        width_current        = 0;
                        image_height        = 0;
    
    
                    } else if(input_byte == 0xFF) {
                        //=================
                        // Siguiente linea
                        //=================
                        if(width_current < line_min_length) line_min_length = width_current;
                        if(width_current > line_max_length) line_max_length = width_current;
    
    
                        if(forced_image_width!=null) {
                            count = forced_image_width.Value - width_current;
                            if(count > 0) {
                                for(n=0; n<count; n++) {
                                    output_buffer.Add(0);
                                }
                            }
                        }
    
    
                        image_height++;
                        width_current = 0;
    
    
                            try
                            {
    
    
                                if (input_byte == 0xFF)
                                    count = (int)(input_data[current_input_position]) + 1;
                                current_input_position++;
    
    
                                b = input_data[current_input_position];
                                current_input_position++;
    
    
                                for (n = 0; n < count; n++)
                                {
                                    output_buffer.Add(b);
                                }
    
    
                                width_current += count;
    
    
                            }
                            catch (IndexOutOfRangeException)
                            {
    
    
                            }
                        } else if(input_byte == 0xFF) {
                        b = input_data[current_input_position];
                        current_input_position++;
    
    
                        b2 = (byte)(b + 1);
    
    
                        count = (int) (input_data[current_input_position]) + 1;
                        current_input_position++;
    
    
                        RLE_FC_swap = false;
                        for(n=0; n<count; n++) {
                            output_buffer.Add(
                                RLE_FC_swap ? b2 : b
                            );
                            RLE_FC_swap = !RLE_FC_swap;
                        }
    
    
                        width_current += count;
    
    
                    } else if(input_byte == 0xFF) {
                        RLE_sumador = input_data[current_input_position];
                        current_input_position++;
    
    
                    } else {
                        b = (byte)(input_byte >> 2);
                        b += RLE_sumador;
                        count = (input_byte & 3) + 1;
    
    
                        for(n=0; n<count; n++) {
                            output_buffer.Add(b);
                        }
    
    
                        width_current += count;
                    }
                }
    
    
                Save();
            }
    
    
            //_____________________________________________________________________
            static void Save() {
    
    
                if(output_buffer.Count == 0) return;
    
    
                string    output_filename;
                string    output_fullpath;
                byte[]    output_array;
                int        output_length;
                bool    is_valid_image;
    
    
    
    
                output_filename    = $"{input_filename_no_ext}.{current_output_index:D3}";
                output_fullpath    = Path.Combine(output_directory, output_filename);
                output_array    = output_buffer.ToArray();
                output_length    = output_array.Length;
                is_valid_image    = false;
    
    
    
    
                if(image_height > 0) {
                    if(line_min_length != line_max_length) {
                        Console.WriteLine($" -> {output_filename} ({output_length}) (min width:{line_min_length}) (max width:{line_max_length}) (height:{image_height})");
                    } else {
                        Console.WriteLine($" -> {output_filename} ({output_length}) (width:{line_min_length}) (height:{image_height})");
                        is_valid_image = true;
                    }
                } else {
                    Console.WriteLine($" -> {output_filename} ({output_length})");
                }
    
    
                try {
                    if(is_valid_image) {
                        Bitmap.Save(output_fullpath + ".bmp", output_array, line_min_length, image_height);
                    
                    }
                } catch {
                    Console.WriteLine("ERROR: cannot save to output file");
                }
    
    
                output_buffer.Clear();
            }
    
    
        }
    }
    In the input_byte == the Values were different for all 5 blocks of Code i.e. 0xFF 0xFE 0XFD etc.

    Is the For Loop in the right place in the code ? could you suggest, changes I could make to the for loop ?

    Regards

    Eddie Winch

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,464
    Your main is 150+ lines long. It could do with some of the functionality moving out into separate functions.

    To check for 00 to 99 in hex, I would do

    Code:
    bool isNumeric ( byte b ) {
      byte low = (b & 0xf);
      byte high = (b & 0xf0) >>> 4;  // use the unsigned >>
      return low >= 0 && low <= 9 && high >= 0 && high <= 9;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Hi Salem,

    I am thinking maybe, that all combinations of Hex Values, are being tested, when I run the Code using --input_byte in the For Loop instead of --i ? But there will be millions of combinations, of 256 Hex Values in 5 Input_byte == blocks of code. I ran this for 12 Hours a few days ago and it still wasn't finished, maybe this would take days or longer ?

    How could I modify the For Loop, to exclude Numbers only Hex Value combinations where none of the 5 input_byte == have at least any letter with the number, i.e. A0 or two letters i.e. FE ? using the lines of Code, you posted in your last reply, or something similar ? I am hoping, that if this really is working, to narrow the combinations done, to reduce the time it takes to complete, that is why I wan't to exclude those values mentioned.

    Eddie
    Last edited by eddywinch82; 01-09-2023 at 05:23 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,464
    How To Incorporate input_byte In For Loop Query ?-eddie-cprog-png
    So you're searching your byte array for patterns like this?


    Code:
    // from the current position, check whether the next
    // 5 bytes match our magic
    bool result = true;
    for ( i = 0 ; result && i < 5 ; i++ ) {
      byte b = input_data[current_input_position+i]
      result = result && isNumeric(b);
    }
    
    if ( result ) {
      // yes, go do your thing now
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Hi Salem,

    Many apologies for not being clearer, when I say 5 input_byte == I mean there are 5 blocks of Code, in Program.cs where it says input_byte == i.e. in the if and else if blocks of Code.

    So how could I modify the For Loop, to do what say I wan't to exclude, in my last reply to you ?

    Eddie

  8. #8
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Can you help me Salem ? If that is okay ?

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,468
    It's hard to understand what you are trying to do.
    Your code doesn't help since it doesn't make sense to test the exact same condition over and over in an if/else if construct.

    Do you want all permutations of 5 byte values which exclude byte values whose hex representation doesn't contain a letter?
    Why do you only want those values?
    Why do you want them in descending order?
    What exactly do you want to do with them?

    One way to permute the values:
    Code:
        for (int a = 0xFF; a >= 0; --a) {
            if (a / 0x10 < 0xA && a % 0x10 < 0xA) continue;
            for (int b = 0xFF; b >= 0; --b) {
                if (b / 0x10 < 0xA && b % 0x10 < 0xA) continue;
                for (int c = 0xFF; c >= 0; --c) {
                    if (c / 0x10 < 0xA && c % 0x10 < 0xA) continue;
                    for (int d = 0xFF; d >= 0; --d) {
                        if (d / 0x10 < 0xA && d % 0x10 < 0xA) continue;
                        for (int e = 0xFF; e >= 0; --e) {
                            if (e / 0x10 < 0xA && e % 0x10 < 0xA) continue;
     
                            // use a,b,c,d,e here
     
                        }
                    }
                }
            }
        }
    I used the continues instead of the opposite condition and braces to limit the indentation and stair stepping.

    EDIT: It's not so bad with a 2-space tab and leaving out most of the braces when not needed:
    Code:
        for (int a = 0xFF; a >= 0xA; --a)
          if (a / 0x10 >= 0xA || a % 0x10 >= 0xA))
            for (int b = 0xFF; b >= 0xA; --b)
              if (b / 0x10 >= 0xA || b % 0x10 >= 0xA))
                for (int c = 0xFF; c >= 0xA; --c)
                  if (c / 0x10 >= 0xA || c % 0x10 >= 0xA))
                    for (int d = 0xFF; d >= 0xA; --d)
                      if (d / 0x10 >= 0xA || d % 0x10 >= 0xA))
                        for (int e = 0xFF; e >= 0xA; --e)
                          if (e / 0x10 >= 0xA || e % 0x10 >= 0xA)) {
     
                            // use a,b,c,d,e here
     
                          }
    Last edited by john.c; 01-13-2023 at 03:41 PM.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  10. #10
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Hi john.c

    I want to try out all combinations, of Hex Values 256 excluding what I say below , I don't mean 5 bytes. I mean there are 5 Blocks of Code In the Original Code I posted, where it says input_byte == i.e. the Hex Values go next to the ==

    I want to try out all combinations of individual bytes. except where all the Five input_byte == are just number hex values, i.e. they don't have any letter with them i.e. 00 01 etc, OA 1E etc are fine as are the two letter Hex Values FF FE FC etc, if at least one of the input_byte == has a letter with a number or The representation is two letters i.e. 0xFD, I would like to try those combinations.

    The 5 blocks of code where it says input_byte == when the Hex Values are used together, they may produce a greyscale .bmp image of the Dos Games Graphics. I say may, that is why I want to brute force all combinations of individual hex Values, bar what I know won't produce anything useful, if that can be done. There is only ever one Hex Value next to the ==

    The Original Code, which is compiled with another code bitmap.cs worked successfully on another Game, a later game in the series. Where I have put all the input_byte == as 0xFF, Originally first input_byte == was 0xFF then the next block of Code below was input_byte == 0xFE then 0xFD etc. My thinking behind using 0xFF, in all the input_byte == I say in my 1st post.

    Eddie
    Last edited by eddywinch82; 01-13-2023 at 04:52 PM.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,468
    There is only ever one Hex Value next to the ==
    It doesn't make sense to compare the same thing five times.
    I don't understand any of this.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  12. #12
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    the 0xFF for each input_byte == was based on the for loop I typed i.e. starting at 0xFF, going backwards, I see what you mean though. My thinking behind it was wrong.

    It says input_byte == five times in the Code, so one of the combinations could be input_byte == 0xFE then next block of code input_byte == 0x0A then next block of Code input_byte == 0x6E then 0xFD then 0x7F etc, you see what I mean John ?

    I want to work backwards, because I feel, that I am more likely to get lucky, with the .bmp's output, from the combinations of the Later Hex Value Combinations 0xFF backwards, than working from 0x00 upwards.
    Last edited by eddywinch82; 01-13-2023 at 05:14 PM.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,468
    Salem showed you how to do it for one variable.
    Something like this:
    Code:
        for (int i = 0xFF; i >= 0x00; --i) {
            if (i / 16 < 10 && i % 16 < 10) continue; // skip hex values without letters
     
            ...
     
        }
    You never actually use i anywhere in your code, though.
    The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts. - Bertrand Russell

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,464
    Do you have any primary sources of information you can show us?
    We need to understand the problem before we can fix your solution.

    > The Program extracts images, from a DOS Games Graphics Files,
    > and saves them as Greyscale Bitmap Files
    Like posting some examples in a zip file.

    I'm pretty sure whoever came up with the file format would have though about how to extract the information later.
    Certainly, they would have a better method than "brute force all the combinations".

    > The Original Code, which is compiled with another code bitmap.cs
    > worked successfully on another Game, a later game in the series.
    Or this code.

    > It says input_byte == five times in the Code
    So you keep saying, but no-one is any clearer on what that means.
    Code:
    // a linear chain
    if ( ) {
    } else if ( ) {
    } else if ( ) {
    } else if ( ) {
    } else {
    }
    vs
    Code:
    // a nested chain
    if ( ) {
      if ( ) {
        if ( ) {
          if ( ) {
            if ( ) {
            }
          }
        }
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jan 2023
    Posts
    32
    Hi Salem,

    Yes it's the else if's and the first one is an if. Sorry about this, I will try and think of a way to explain this better. And today will post, some of the Bitmap Files in a zip file for you. I appreciate yours and john.c 's help

    Eddie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While Loop Query - code displayed
    By darren78 in forum C Programming
    Replies: 13
    Last Post: 02-17-2009, 10:44 AM
  2. first look incorporate assembly?
    By stabu in forum C Programming
    Replies: 8
    Last Post: 09-25-2008, 08:53 AM
  3. Loop Query
    By (TNT) in forum C# Programming
    Replies: 5
    Last Post: 02-13-2006, 06:16 PM
  4. How can I incorporate this code into a web page?
    By MisterRob in forum C Programming
    Replies: 6
    Last Post: 11-02-2005, 05:43 PM
  5. Best way to incorporate upgrades
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-26-2002, 07:41 AM

Tags for this Thread