Thread: Deciphering a code example, string class confusion

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    Question Deciphering a code example, string class confusion

    A little bit of a backstory....I am making a program in Visual Basic. The program basically opens up a compressed archive, finds a file inside it and then I THINK it modifies some data at a byte level(either a 4-byte or an 2-byte unsigned shorts), but I cannot be certain. Anyways, I have the program with a working implementation of what I am wanting to do (its in C++ and I am using Visual Basic, thus I need to convert the code and I can't copy/paste) and I have the code pulled up.

    For the past several days I have been pouring over the code, going line by line commenting out what I am pretty sure the code is doing. It's odd because I can tell what the code is doing but I don't know how or why it does this. I have the binary of the file the code is for pulled up so I am able to locate where I think the data is being copied, replaced, assigned...etc. Also, not quite sure why some values and parameters are used. Mainly the issue is with the way the string class is used in the code.

    My skills are extremely limited and I am learning....I'm afraid I don't understand the string class at all. I need help figuring out what the following 4 lines of code are doing. This code is fragmented and taken completely out of context so if I have missing information please let me know.

    Code:
      //Variables:
         std::string sBuffer
         char *srcScen
         DWORD dwSrcSize
         size_t strPos
         unsigned short strCount
         int strSize
    The following is the codes with a brief description of what I think it is doing.

    Code:
    sBuffer.assign(srcScen, dwSrcSize)
    This appears to assign the value of dwSrcSize to srcScen? How can this even be possible? DWORD is an typedef of INT and srcScen is a char???

    Code:
    strPos = sBuffer.find ("LSX ")
    Is it safe to assume that each time strPos is used, it calls the .find string method and always searches for "LSX "? Also, it appears to be searching for a string of text/characters, why then is strPos size_t...shouldn't it be some sort of char/string?

    Code:
    sBuffer.copy((char*)&strCount, 2, strPos+8)
    As far as I can tell, it finds a location at strPos+8, copies 2 characaters(bytes, in this case....i think), and stores the value in the strCount pointer? Also, strCount is an unsigned short....how can it be a reference to a pointer char?

    Code:
    sBuffer.replace(strPos+4, 4, (char*)&strSize, 4)
    Really uncertain here. The only thing I may know for certain, is that the number 4 being used is because these are 4bytes of data.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Can you post the entire C++ program? Or put up a link to it?

    Code:
    sBuffer.assign(srcScen, dwSrcSize)
    srcScen is a POINTER to char. It points to a position inside an allocated piece of memory (somewhere, you don't show where it gets a value). The assign function copies dwSrcSize chars from the memory pointed to by srcScen to sBuffer.
    Code:
    strPos = sBuffer.find ("LSX ")
    No, find is just a function. It returns a value. That value is being assigned to strPos. The find function ("method") is being called on the sBuffer object, so that is the "string" that it searches.
    Code:
    sBuffer.copy((char*)&strCount, 2, strPos+8)
    Copies 2 chars from position strPos+8 of sBuffer to the (presumably 2-byte short variable) strCount, which is interpretted as a character pointer (by taking its address, yielding a short pointer, and then converting that to a char pointer).
    Code:
    sBuffer.replace(strPos+4, 4, (char*)&strSize, 4)
    replace 4 chars starting at strPos+4 in sBuffer with the first 4 characters starting at the address of the strSize variable (again interpretted as a char*). strSize is probably a 4-byte int.
    Last edited by john.c; 03-16-2018 at 12:41 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Thank you for the clarification!

    As far as posting the code, I wouldn't feel comfortable posting the code here as none of it is mine. Even though the program is freely available at a particular site. However, if you care to the take the time to look over the code and help decipher, I can shoot you a PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Okay, send me the PM. Or just post the link here so anybody can follow it.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help deciphering a line
    By Polaris84 in forum C Programming
    Replies: 2
    Last Post: 09-08-2015, 09:27 AM
  2. Unbounded deciphering delay
    By Rob4226 in forum C Programming
    Replies: 0
    Last Post: 10-18-2009, 09:15 PM
  3. C++ Noob Class Confusion
    By Freestyler in forum C++ Programming
    Replies: 16
    Last Post: 06-11-2009, 03:19 AM
  4. class header confusion
    By te5la in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 02:39 PM
  5. help deciphering code
    By tlovaj in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 09:48 PM

Tags for this Thread