Sponsored Content
Top Forums Programming Private string embedded inside C code. Post 303031013 by wisecracker on Wednesday 20th of February 2019 12:03:51 PM
Old 02-20-2019
Private string embedded inside C code.

Private string inside C code.
After a lot of messing around, time consumption and jiggery-pokery I finally got a common piece of code for this thread working:

Embed text in C code

The OP wanted the string to be alone and as close to the top of the code as possible and not inside the data section.
Well, I wanted to make it possible for AMIGA users to have the same as we UNIX users, and upload to AMINET to suit.
But when it compiled in gcc 2.95.3 i got some warnings and errors, etc... When the warnings and errors went the test code similar to the URL above inverted everything and the data section was at the top and the private string was below the data section. After much thought this was the demo code that worked for the platforms tested on.

The assembly code does not need to be changed at all as the 2 instructions are the same for both architectures.

Code:
/* embed_private_string.c */
/* Guaranteed to allow up to 120 ASCII characters inside a function using gcc. */
/* It IS easily possible to get many, many more characters but this is a minimal approach. */
/* Without any modifications this will compile on these platforms:- */
/* Works on gcc 2.95.3 for the classic AMIGA OS 3.0.x using ADE the *NIX emulator, Motorola 68000+ architecture. */
/* Works on gcc 4.2.1 for OSX 10.14.x, 64 bit, Intel architecture. */
/* Works on gcc 7.3.0 for Linux Mint 19, 64 bit, Intel architecture. */
/* -- I have no idea whether this compiles on the current gcc 8.2.0, 20-02-2019. -- */

#include <stdio.h>

void Private_String(void)
{
    /* To keep this string near the top of the code this must be the first function. */
    /* This string will always be separate from the normal data section. */
    /* It MUST be an EVEN number of characters long, including a single */
    /* newline and the required NULL padding characters at the end. */
    asm("    jmp    exit_asm;"
    "string:"
    "    .asciz    \"'$VER (C)_Barry_Walker_CC0_Licence_[PD].'\\n\\0\";"
    "exit_asm:"
    "    nop;");
    /* IMPORTANT! DO NOT use the assembler RETURN instruction at all! */
    return;
}

/* All further strings will be stored in the data section area. */
void Test_Function(void)
{
    /* This function is just a test function. */
    printf("\nA test function to check that the strings are in the data section.\n"); 
    return;
}

int main(void)
{
    /* Note: Private_String() does absolutely NOTHING. */
    Private_String();
    Test_Function();
    printf("\nThese strings should be in the data section.\n");
    printf("The hidden string is inside the code section.\n");
    printf("'$VER (C)_Barry_Walker_CC0_Licence_[PD].'\n\n");
    return 0;
}

Results for OSX 10.14.x:
Code:
00000e00  55 48 89 e5 e9 2c 00 00  00 27 24 56 45 52 20 28  |UH...,...'$VER (|
00000e10  43 29 5f 42 61 72 72 79  5f 57 61 6c 6b 65 72 5f  |C)_Barry_Walker_|
00000e20  43 43 30 5f 4c 69 63 65  6e 63 65 5f 5b 50 44 5d  |CC0_Licence_[PD]|
00000e30  2e 27 0a 00 00 90 5d c3  0f 1f 84 00 00 00 00 00  |.'....].........|
00000e40  55 48 89 e5 48 83 ec 10  48 8d 3d 87 00 00 00 b0  |UH..H...H.=.....|
00000e50  00 e8 60 00 00 00 89 45  fc 48 83 c4 10 5d c3 90  |..`....E.H...]..|
00000e60  55 48 89 e5 48 83 ec 10  c7 45 fc 00 00 00 00 e8  |UH..H....E......|
00000e70  8c ff ff ff e8 c7 ff ff  ff 48 8d 3d 9b 00 00 00  |.........H.=....|
00000e80  b0 00 e8 2f 00 00 00 48  8d 3d bc 00 00 00 89 45  |.../...H.=.....E|
00000e90  f8 b0 00 e8 1e 00 00 00  48 8d 3d da 00 00 00 89  |........H.=.....|
00000ea0  45 f4 b0 00 e8 0d 00 00  00 31 c9 89 45 f0 89 c8  |E........1..E...|
00000eb0  48 83 c4 10 5d c3 ff 25  54 01 00 00 4c 8d 1d 45  |H...]..%T...L..E|
00000ec0  01 00 00 41 53 ff 25 35  01 00 00 90 68 00 00 00  |...AS.%5....h...|
00000ed0  00 e9 e6 ff ff ff 0a 41  20 74 65 73 74 20 66 75  |.......A test fu|
00000ee0  6e 63 74 69 6f 6e 20 74  6f 20 63 68 65 63 6b 20  |nction to check |
00000ef0  74 68 61 74 20 74 68 65  20 73 74 72 69 6e 67 73  |that the strings|
00000f00  20 61 72 65 20 69 6e 20  74 68 65 20 64 61 74 61  | are in the data|
00000f10  20 73 65 63 74 69 6f 6e  2e 0a 00 0a 54 68 65 73  | section....Thes|
00000f20  65 20 73 74 72 69 6e 67  73 20 73 68 6f 75 6c 64  |e strings should|
00000f30  20 62 65 20 69 6e 20 74  68 65 20 64 61 74 61 20  | be in the data |
00000f40  73 65 63 74 69 6f 6e 2e  0a 00 54 68 65 20 68 69  |section...The hi|
00000f50  64 64 65 6e 20 73 74 72  69 6e 67 20 69 73 20 69  |dden string is i|
00000f60  6e 73 69 64 65 20 74 68  65 20 63 6f 64 65 20 73  |nside the code s|
00000f70  65 63 74 69 6f 6e 2e 0a  00 27 24 56 45 52 20 28  |ection...'$VER (|

It looks much the same on all three OSes even though it was originally inverted on the AMIGA A1200.

It took wime to get it working, but this might be a better bet for drew77 as it is consistent on differing platforms and compiles on differing versions of gcc.

Have fun...

EDIT:
Off to give this to the AMINET site soon...

Last edited by wisecracker; 02-20-2019 at 01:07 PM.. Reason: See above...
These 2 Users Gave Thanks to wisecracker For This Post:
 

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search and Parse string from inside a File

Hello, I barely know the basics, but I am very determined to learn. I want to parse a few characters from each row, use that string to search another file and display the line number where I found the value in the file. I don't know if this can all be done on the command line, so I am creating a... (2 Replies)
Discussion started by: SSims
2 Replies

2. Shell Programming and Scripting

How to find a string inside files

Hi, I would like to know how to get a list of files that contain a specific string inside them. Thanks (12 Replies)
Discussion started by: yoavbe
12 Replies

3. Shell Programming and Scripting

how to delete blanks inside a quoted string

Hi I need to update a string inside a file which looks like the following: PX_LIST=" 4119 2390 2294 2776 2897 4099 " Is there a way to get rid of the blanks after the first quote mark and before the last quote mark. This needs to be done ONLY for the string named PX_LIST (there are some... (4 Replies)
Discussion started by: aoussenko
4 Replies

4. Shell Programming and Scripting

String handling is not working inside if loop

Hi All, I am comparing two strings inside an if condition if the strings are same then it should go inside the loop else it should execute code given in else part. But there is a but inside my script Even if the if condition is true it is not going inside the loop also it is executing... (4 Replies)
Discussion started by: usha rao
4 Replies

5. Shell Programming and Scripting

Finding a string inside A Tag

I have umpteen number of files containing HTML A tags in the below format or I want to find all the lines that contain the word Login= I used this command grep "Login=" * This gave me normal lines as well which contain the word Login= for example, it returned lines which... (2 Replies)
Discussion started by: dahlia84
2 Replies

6. Shell Programming and Scripting

Addition inside a string

hello, anyone knows how to do this: given a=0, i want to print in next line that a=1 so how do I do that inside the string? ex. a=0 b="value of a=$((a+1))"? <- Because this is incorrect.. Thanks! (1 Reply)
Discussion started by: h0ujun
1 Replies

7. UNIX for Dummies Questions & Answers

Finding files with a certain name string inside of another file.

Hi, I have a very large file that contains a listing of all files on the system. I need to create a listing from that file of all files that start with the following format: s???_*, whereas the '?' represents characters, so the file name begins with an 's' followed by three other characters and... (4 Replies)
Discussion started by: tes218
4 Replies

8. Shell Programming and Scripting

Cutting string inside if

I was trying the below statement if It is working fine if I run it in a test file. but not working, when I am trying in my actual script. Error: : "${FXML_line:1129:1}": bad substitution Thanks in advance :) PS: Above if block I have a while loop which is reading a... (4 Replies)
Discussion started by: ezee
4 Replies
All times are GMT -4. The time now is 12:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy