Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. How do you understand cryptic code?

How do you understand cryptic code?

Scheduled Pinned Locked Moved The Lounge
csharpc++question
40 Posts 23 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Gary Wheeler

    I have this precise issue in a legacy project I have inherited. The author wrote the code in a fashion that virtually guaranteed he was the only possible maintainer [approx. 4,000 words of obscenity-laden rant omitted]. On occasion I have copied the relevant source files to another folder and the reformatted and refactored mercilessly. The reformatting is to correct layout issues since his brace style and tabs weren't consistent (I've found tabs of 2, 3, 4, and 8 with tab characters). The refactoring is to give values meaningful names. The reworked source code lets me understand how the original works when I need to make changes or understand how a feature works. Given the fragility of this towering pile of excreta, I don't use the reformatted code for anything other than my own understanding.

    Software Zen: delete this;

    H Offline
    H Offline
    honey the codewitch
    wrote on last edited by
    #31

    I have only one thing to say in response, but it's a mouthful:

    static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)
    {
    int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
    int has_subrs = 0, clear_stack;
    float s[48];
    stbtt__buf subr_stack[10], subrs = info->subrs, b;
    float f;

    #define STBTT__CSERR(s) (0)

    // this currently ignores the initial width value, which isn't needed if we have hmtx
    b = stbtt__cff_index_get(info->charstrings, glyph_index);
    while (b.cursor < b.size) {
    i = 0;
    clear_stack = 1;
    b0 = stbtt__buf_get8(&b);
    switch (b0) {
    // @TODO implement hinting
    case 0x13: // hintmask
    case 0x14: // cntrmask
    if (in_header)
    maskbits += (sp / 2); // implicit "vstem"
    in_header = 0;
    stbtt__buf_skip(&b, (maskbits + 7) / 8);
    break;

      case 0x01: // hstem
      case 0x03: // vstem
      case 0x12: // hstemhm
      case 0x17: // vstemhm
         maskbits += (sp / 2);
         break;
    
      case 0x15: // rmoveto
         in\_header = 0;
         if (sp < 2) return STBTT\_\_CSERR("rmoveto stack");
         stbtt\_\_csctx\_rmove\_to(c, s\[sp-2\], s\[sp-1\]);
         break;
      case 0x04: // vmoveto
         in\_header = 0;
         if (sp < 1) return STBTT\_\_CSERR("vmoveto stack");
         stbtt\_\_csctx\_rmove\_to(c, 0, s\[sp-1\]);
         break;
      case 0x16: // hmoveto
         in\_header = 0;
         if (sp < 1) return STBTT\_\_CSERR("hmoveto stack");
         stbtt\_\_csctx\_rmove\_to(c, s\[sp-1\], 0);
         break;
    
      case 0x05: // rlineto
         if (sp < 2) return STBTT\_\_CSERR("rlineto stack");
         for (; i + 1 < sp; i += 2)
            stbtt\_\_csctx\_rline\_to(c, s\[i\], s\[i+1\]);
         break;
    
      // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical
      // starting from a different place.
    
      case 0x07: // vlineto
         if (sp < 1) return STBTT\_\_CSERR("vlineto stack");
         goto vlineto;
      case 0x06: // hlineto
         if (sp < 1) return STBTT\_\_CSERR("hlineto stack");
         for (;;) {
            if (i >= sp) break;
            stbtt\_\_csctx\_rline\_to(c, s\[i\], 0);
            i++;
      vlineto:
            if (i >= sp) break;
            stbtt\_\_csctx\_rline\_to(c, 0, s\[i\]);
            i++;
         }
         break;
    
      case 0x1F: // hvcurveto
         if (sp < 4) return STBTT\_\_CSERR("hvcurveto stack");
    
    1 Reply Last reply
    0
    • H honey the codewitch

      I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)

      Real programmers use butterflies

      J Offline
      J Offline
      Julian Ragan
      wrote on last edited by
      #32

      I either write code analysis report, where I divide code into sections and write section by section, what I think it does, then verify with debugger, or I create a good old flow diagram, if it is really cryptic (even for OO this will work for extracting actual algorithms, if you can identify sequence first).

      H 1 Reply Last reply
      0
      • J Julian Ragan

        I either write code analysis report, where I divide code into sections and write section by section, what I think it does, then verify with debugger, or I create a good old flow diagram, if it is really cryptic (even for OO this will work for extracting actual algorithms, if you can identify sequence first).

        H Offline
        H Offline
        honey the codewitch
        wrote on last edited by
        #33

        I can totally see the sense in that, but I could never do it. Too rigorous for me. :laugh: I am good at improvisation and creativity, but I am no good at being methodical.

        Real programmers use butterflies

        J 1 Reply Last reply
        0
        • H honey the codewitch

          I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)

          Real programmers use butterflies

          J Offline
          J Offline
          JP Reyes
          wrote on last edited by
          #34

          Not exactly. I used to use C to spit out ARM16 Assembler so I got an idea how I could program what I needed in assembler...then I yanked out all the stuff I didn't need (Like when you throw out all the useless HTML an editor generates). Smaller, faster, optimized and straight to the point (well for me...anybody else would also have to be comfortable with ARM16 as well)

          H 1 Reply Last reply
          0
          • H honey the codewitch

            I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)

            Real programmers use butterflies

            E Offline
            E Offline
            englebart
            wrote on last edited by
            #35

            Do you port all pointers to array access? Pointers are always the pain for this type of exercise. Some algorithms are easier to understand that way. Multilevel pointers are the worst.

            H 1 Reply Last reply
            0
            • E englebart

              Do you port all pointers to array access? Pointers are always the pain for this type of exercise. Some algorithms are easier to understand that way. Multilevel pointers are the worst.

              H Offline
              H Offline
              honey the codewitch
              wrote on last edited by
              #36

              Whenever I can, that's what I do. I turn it to arrays and indices. It is difficult but actually that process is critical for me to understand it. It's one of the most important parts of the port. And yeah, double indirection and such gets tricky fast.

              Real programmers use butterflies

              1 Reply Last reply
              0
              • J JP Reyes

                Not exactly. I used to use C to spit out ARM16 Assembler so I got an idea how I could program what I needed in assembler...then I yanked out all the stuff I didn't need (Like when you throw out all the useless HTML an editor generates). Smaller, faster, optimized and straight to the point (well for me...anybody else would also have to be comfortable with ARM16 as well)

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #37

                I rarely write in assembly these days but I read it a lot. I've focused more on getting C and particularly C++ to generate the exact machine code I intended if I'd have written by hand, or as is often the case, better.

                Real programmers use butterflies

                1 Reply Last reply
                0
                • H honey the codewitch

                  I can totally see the sense in that, but I could never do it. Too rigorous for me. :laugh: I am good at improvisation and creativity, but I am no good at being methodical.

                  Real programmers use butterflies

                  J Offline
                  J Offline
                  Julian Ragan
                  wrote on last edited by
                  #38

                  Yeah, it is exhausting to do in a complex code base.

                  1 Reply Last reply
                  0
                  • H honey the codewitch

                    I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)

                    Real programmers use butterflies

                    N Offline
                    N Offline
                    NightPen
                    wrote on last edited by
                    #39

                    Start by determining what data it works with. Then split out the functions and start figuring out the inputs and outputs from each function. Finally, make a process and data flow diagrams for the code. This process makes short work of understanding even the most cryptic code. If you don't want to spend a day or two doing this process there are tools like sourcetrail that will do the work for you.

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)

                      Real programmers use butterflies

                      H Offline
                      H Offline
                      hur10forcer10
                      wrote on last edited by
                      #40

                      If the project is fairly significant in size with multiple .c and .h files with many functions, I will often turn doxygen (with graphviz) loose on it. Even though the code may not have any doxygen tags or doxygen-compatible comment blocks to generate function and API documentation, it can still give you an idea of "what calls what" in a graphical context.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups