Modifying app.config through installscript - installshield-2010

How to modify app.config files through installscript in installshield 2010.
I want to modify the sql connection string in app.config file based on user selected server.
Early help should be appreciated.

I have written own installscript to change the connection string.
Code snippet :
function Update_AppConfig(hMSI)
STRING EXAMPLE_FILE, EXAMPLE_DIR;
STRING svLine;
STRING svContent;
number nvBuff,temp,nvFileHandle ;
string szConnection,szServer,szFileName,szUser,szFilePath;
long cchValueBuf;
long length;
begin
EXAMPLE_FILE = "Example.exe.config";
EXAMPLE_DIR = INSTALLDIR;
cchValueBuf = 2;
MsiGetProperty(hMSI,"IS_SQLSERVER_SERVER",szServer,cchValueBuf);
OpenFileMode (FILE_MODE_NORMAL);
if (OpenFile (nvFileHandle, EXAMPLE_DIR, EXAMPLE_FILE) < 0) then
MessageBox ("OpenFile failed.", SEVERE);
abort;
endif;
while GetLine (nvFileHandle, svLine) = 0
svContent = svContent + svLine + "\r\n";
endwhile;
if (CloseFile (nvFileHandle) < 0) then
MessageBox ("CloseFile failed.", SEVERE);
endif;
StrReplace (svContent, "{{SQLSERVERSETTING}}", szServer, 0);
// Close the file.
OpenFileMode (FILE_MODE_BINARY);
if (OpenFile (nvFileHandle, EXAMPLE_DIR, EXAMPLE_FILE) < 0) then
MessageBox ("OpenFile failed.", SEVERE);
abort;
endif;
length = StrLength (svContent);
if (WriteBytes (nvFileHandle, svContent, 0, length) < 0) then
MessageBox ("WriteBytes failed.", SEVERE);
endif;
if (CloseFile (nvFileHandle) < 0) then
MessageBox ("CloseFile failed.", SEVERE);
endif;
end;

Related

ADA - Records in a hashmap. Problems with printing the hash map

I'm new to programming and this is my first attempt at a data container with records. I'm having difficulty with printing the hashmap between line 65-70. I'm guessing I need to break down the record and print each of its attributes individually but I'm not sure on the best way to do that. The error state 'no candidates match the actuals: missing argument for parameter.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Hash;
with Ada.Containers.Hashed_Maps;
with Ada.Characters.Handling;
procedure Hashmap_Records is
type Guest is record
Name : Unbounded_String := Null_Unbounded_String;
Attending : Boolean := False;
Vegitarian : Boolean := False;
Kids : Natural := 0;
end record;
function Equivalent_Keys(Left : Unbounded_String;
Right : Unbounded_String)
return Boolean is
begin
return Left = Right;
end Equivalent_Keys;
function U_To_L(Key : in Unbounded_String)
return Unbounded_String is
begin
return To_Unbounded_String(Ada.Characters.Handling.To_Lower(To_String(Key)));
end U_To_L;
function Hash_Func(Key : in Unbounded_String)
return Ada.Containers.Hash_Type is
begin
return Ada.Strings.Hash(To_String(Key));
end Hash_Func;
package Guest_Tracker is new Ada.Containers.Hashed_Maps(Key_Type => Unbounded_String,
Element_Type => Guest,
Hash => Hash_Func,
Equivalent_Keys => Equivalent_Keys);
Tracked_Guests : Guest_Tracker.Map;
User_Input : Natural := 0;
Name_Input : Unbounded_String := Null_Unbounded_String;
Name : Unbounded_String := Null_Unbounded_String;
Attendance_Input : Unbounded_String := Null_Unbounded_String;
Vegitarian_Input : Unbounded_String := Null_Unbounded_String;
Kids_Input : Natural := 0;
Temp_Attendance : Boolean := False;
Temp_Vegi : Boolean := False;
Procedure Populate_Hash_Map is
begin
Tracked_Guests.Insert(Key => To_Unbounded_String("John Smith"),
New_Item => (To_Unbounded_String("John"), True, False, 1));
Tracked_Guests.Insert(Key => To_Unbounded_String("Robert Johnson"),
New_Item => (To_Unbounded_String("Rob"), True, True, 2));
end Populate_Hash_Map;
procedure Print_Hash_Map(Position : Guest_Tracker.Cursor) is ---
begin
Put_Line("The key: " & To_String(Guest_Tracker.Key(Position)) &
" the data item: ");
Put(Guest_Tracker.Element(Position)); ----THIS IS WHERE ERRORS OCCUR
end Print_Hash_Map; ----
begin
Populate_Hash_Map;
loop
Put_Line(" - Menu - ");
Put_Line(" - 1 - Enter new value.");
Put_Line(" - 2 - Delete Existing Value.");
Put_Line(" - 3 - Print entire hashmap.");
Put_Line(" - 4 - Exit Application.");
New_Line;
Put(" - > ");
declare
begin
Get(User_Input);
exception
when Data_Error =>
Put_Line("ERROR : The entered value is not an integer, please try again!");
User_Input := 0;
when others =>
Put_Line("ERROR: An unknown error has occured!");
end;
Skip_Line;
New_Line;
if User_Input = 1 then
Put_Line("Enter a new value.");
Put(" Name - > ");
Name_Input := Get_Line;
Name := Name_Input;
New_Line;
Put(" Attending? (yes/y/no/n) - > ");
Attendance_Input := Get_Line;
New_Line;
Put(" Vegitarian? (yes/y/no/n) - > ");
Vegitarian_Input := Get_Line;
New_Line;
Put("How many children attending? - > ");
Get(Kids_Input);
New_Line;
if (U_To_L(Attendance_Input) = To_Unbounded_String("no"))
or (U_To_L(Attendance_Input) = To_Unbounded_String("n")) then
Temp_Attendance := False;
elsif (U_To_L(Attendance_Input) = To_Unbounded_String("y"))
or (U_To_L(Attendance_Input) = To_Unbounded_String("yes")) then
Temp_attendance := True;
else
Put_Line("WARNING: The confirmation that you entered is not recognized.");
end if;
if (U_To_L(Vegitarian_Input) = To_Unbounded_String("no"))
or (U_To_L(Vegitarian_Input) = To_Unbounded_String("n")) then
Temp_Vegi := False;
elsif (U_To_L(Vegitarian_Input) = To_Unbounded_String("y"))
or (U_To_L(Vegitarian_Input) = To_Unbounded_String("yes")) then
Temp_Vegi := True;
else
Put_Line("WARNING: The confirmation that you entered is not recognized.");
end if;
Guest_Tracker.Insert(Container => Tracked_Guests,
Key => Name_Input,
New_item => (Name, Temp_Attendance, Temp_Vegi, Kids_Input));
elsif User_Input = 2 then
Put("Delete a value - > ");
Name_Input := Get_Line;
New_Line;
declare
begin
Guest_Tracker.Delete(Container => Tracked_Guests,
Key => Name_Input);
exception
when Constraint_Error =>
Put_Line("The name: '" & To_String(Name_Input) & "' is not found.");
when others =>
Put_Line("ERROR: Another error has been discovered!");
end;
elsif User_Input = 3 then
Tracked_Guests.Iterate(Print_Hash_Map'access);
New_Line;
elsif User_Input = 4 then
exit;
end if;
end loop;
end Hashmap_Records;
This has nothing to do with the hash map. You are assuming that there is a Put procedure that outputs your record type Guest but there is none. You only have the Put subroutines from Ada.Text_IO, Ada.Integer_Text_IO and Ada.Text_IO.Unbounded_IO available, none of which take a value of type Guest as parameter.
Ada does not automatically generate a subroutine to pretty-print a record value when you define the record type. You have to do it yourself, e.g.
procedure Put (Value : Guest) is
begin
Put ("Guest(Name: "); -- supplied by Ada.Text_IO
Put (Value.Name); -- supplied by Ada.Text_IO.Unbounded_IO
Put (", Attending: ");
Put (Value.Attending'Img); -- 'Img is GNAT-specific; standard is
-- Boolean'Image (Value.Attending)
Put (", Vegitarian: ");
Put (Value.Vegitarian'Img);
Put (", Kids: ");
Put (Value.Kids); -- supplied by Ada.Integer_Text_IO
Put (")");
end Put;

How do I use escapeshellarg() on Windows but "aimed for Linux" (and vice versa)?

If PHP is running on Windows, escapeshellarg() escapes file names (for example) in a certain way and then adds " (DOUBLE) quotes around it.
If PHP is running on Linux, escapeshellarg() uses Linux-based escaping and then adds ' (SINGLE) quotes around it.
In my situation, I'm generating a SHA256SUMS file on Windows, but aimed for Linux. Since I use escapeshellarg() to escape the file name, I end up with a file like:
cabcdccas12exdqdqadanacvdkjsc123ccfcfq3rdwcndwf2qefcf "cool filename with spaces.zip"
However, Linux tools probably expect:
cabcdccas12exdqdqadanacvdkjsc123ccfcfq3rdwcndwf2qefcf 'cool filename with spaces.zip'
Looking in the manual, there seems to be no way to do something like: escapeshellarg($blabla, TARGET_OS_LINUX); in order for it to use the rules for Linux instead of the OS running the script (Windows).
I can't just str_replace the quotes because it would not take into consideration all the platform-specific rules.
Also, yes, I need spaces in the file name (and any other cross-platform-valid character).
I sadly found no mention whatsoever about the preferred quote style on the only source of information I have for this: https://help.ubuntu.com/community/HowToSHA256SUM
Maybe the SHA256 security verification tools which read that SHA256SUMS file understand and can parse both kinds?
The behavior of escapeshellarg() is hard-coded depending on whether PHP is running on Windows or any other operating system. You should reimplement escapeshellarg() for consistent behavior.
Here is my attempt at reimplementing escapeshellarg() with a Windows/other-OS toggle in PHP:
<?php namespace polyfill;
const TARGET_OS_WINDOWS = 1;
const TARGET_OS_UNIX = 2;
function escapeshellarg(string $input, int $os_mode = 0): string
{
if (false !== strpos($input, "\x00"))
{
throw new \UnexpectedValueException(__FUNCTION__ . '(): Argument #1 ($input) must not contain any null bytes');
}
if ($os_mode == 0)
{
$os_mode = TARGET_OS_UNIX;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
$os_mode = TARGET_OS_WINDOWS;
}
$maxlen = 4096;
if ($os_mode === TARGET_OS_WINDOWS) $maxlen = 8192;
if (strlen($input) > $maxlen - 2) return "";
if ($os_mode === TARGET_OS_WINDOWS)
{
$output =
str_replace(['"', '%', '!'],
[' ', ' ', ' '],
$input);
# https://bugs.php.net/bug.php?id=69646
if (substr($output, -1) === "\\")
{
$k = 0; $n = strlen($output) - 1;
for (; $n >= 0 && substr($output, $n, 1) === "\\"; $n--, $k++);
if ($k % 2) $output .= "\\";
}
$output = "\"$output\"";
}
else
{
$output = str_replace("'", "'\''", $input);
$output = "'$output'";
}
if (strlen($output) > $maxlen) return "";
return $output;
}
It should be almost functionally equivalent to the native PHP escapeshellarg(), except that:
it takes a second argument that sets whether you want the output in Windows mode or not Windows mode,
it raises an \UnexpectedValueException instead of some kind of PHP error if the input string contains null bytes,
it doesn't emit errors due to the input being too long, and
it has 4096 hard-coded as the maximum argument length on Unix-like platforms.
To use this replacement function:
# In Unix/Linux/macOS mode
\polyfill\escapeshellarg($blabla, \polyfill\TARGET_OS_UNIX);
# In Windows mode
\polyfill\escapeshellarg($blabla, \polyfill\TARGET_OS_WINDOWS);
# In auto-detect (running OS) mode
\polyfill\escapeshellarg($blabla);
Reference
Here is the full C implementation from PHP 7.3.10 (./ext/standard/exec.c):
PHPAPI zend_string *php_escape_shell_arg(char *str)
{
size_t x, y = 0;
size_t l = strlen(str);
zend_string *cmd;
uint64_t estimate = (4 * (uint64_t)l) + 3;
/* max command line length - two single quotes - \0 byte length */
if (l > cmd_max_len - 2 - 1) {
php_error_docref(NULL, E_ERROR, "Argument exceeds the allowed length of %zu bytes", cmd_max_len);
return ZSTR_EMPTY_ALLOC();
}
cmd = zend_string_safe_alloc(4, l, 2, 0); /* worst case */
#ifdef PHP_WIN32
ZSTR_VAL(cmd)[y++] = '"';
#else
ZSTR_VAL(cmd)[y++] = '\'';
#endif
for (x = 0; x < l; x++) {
int mb_len = php_mblen(str + x, (l - x));
/* skip non-valid multibyte characters */
if (mb_len < 0) {
continue;
} else if (mb_len > 1) {
memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
y += mb_len;
x += mb_len - 1;
continue;
}
switch (str[x]) {
#ifdef PHP_WIN32
case '"':
case '%':
case '!':
ZSTR_VAL(cmd)[y++] = ' ';
break;
#else
case '\'':
ZSTR_VAL(cmd)[y++] = '\'';
ZSTR_VAL(cmd)[y++] = '\\';
ZSTR_VAL(cmd)[y++] = '\'';
#endif
/* fall-through */
default:
ZSTR_VAL(cmd)[y++] = str[x];
}
}
#ifdef PHP_WIN32
if (y > 0 && '\\' == ZSTR_VAL(cmd)[y - 1]) {
int k = 0, n = y - 1;
for (; n >= 0 && '\\' == ZSTR_VAL(cmd)[n]; n--, k++);
if (k % 2) {
ZSTR_VAL(cmd)[y++] = '\\';
}
}
ZSTR_VAL(cmd)[y++] = '"';
#else
ZSTR_VAL(cmd)[y++] = '\'';
#endif
ZSTR_VAL(cmd)[y] = '\0';
if (y > cmd_max_len + 1) {
php_error_docref(NULL, E_ERROR, "Escaped argument exceeds the allowed length of %zu bytes", cmd_max_len);
zend_string_release_ex(cmd, 0);
return ZSTR_EMPTY_ALLOC();
}
if ((estimate - y) > 4096) {
/* realloc if the estimate was way overill
* Arbitrary cutoff point of 4096 */
cmd = zend_string_truncate(cmd, y, 0);
}
ZSTR_LEN(cmd) = y;
return cmd;
}
// … [truncated] …
/* {{{ proto string escapeshellarg(string arg)
Quote and escape an argument for use in a shell command */
PHP_FUNCTION(escapeshellarg)
{
char *argument;
size_t argument_len;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(argument, argument_len)
ZEND_PARSE_PARAMETERS_END();
if (argument) {
if (argument_len != strlen(argument)) {
php_error_docref(NULL, E_ERROR, "Input string contains NULL bytes");
return;
}
RETVAL_STR(php_escape_shell_arg(argument));
}
}
/* }}} */
The logic is fairly simple. Here are some equivalent functional test cases in prose:
The input string cannot contain NUL characters.
Applied to the input string,
in Windows mode,
Prepend a " character.
Replace all ", %, and ! characters with .
If the end consists of an odd number of \ characters, add one \ character to the end. (Bug #69646)
Append a " character.
in other platforms mode,
Prepend a ' character.
Replace all ' characters with '\''
Append a ' character.
On Windows, if the output is longer than 8192 characters, emit an E_ERROR and return an empty string.
On other platforms, if the output is longer than 4096 characters (or whatever the overridden maximum is at compile time), emit an E_ERROR and return an empty string.

String search and replace in SystemVerilog

What's the easiest way to do string search and replace in SystemVerilog?
For example, I have:
string hdl_path = "DUT.my_red39";
How do I create a new string that replaces red with blue?
EDA Playground link
You can use one of the SystemVerilog utility libraries that have been created recently.
ClueLib
has cl::text::replace method
ClueLib replace example
svlib
has svlib_pkg::Str::replace method
svlib replace example
Alternatively, you could do it with plain SystemVerilog by comparing individual characters:
function automatic string search_replace(string original, string old, string replacement);
// First find the index of the old string
int start_index = 0;
int original_index = 0;
int replace_index = 0;
bit found = 0;
while(1) begin
if (original[original_index] == old[replace_index]) begin
if (replace_index == 0) begin
start_index = original_index;
end
replace_index++;
original_index++;
if (replace_index == old.len()) begin
found = 1;
break;
end
end else if (replace_index != 0) begin
replace_index = 0;
original_index = start_index + 1;
end else begin
original_index++;
end
if (original_index == original.len()) begin
// Not found
break;
end
end
if (!found) return original;
return {
original.substr(0, start_index-1),
replacement,
original.substr(start_index+old.len(), original.len()-1)
};
endfunction
SystemVerilog replace example on EDA Playground

How can I safely and simply read a line of text from a file or stdin?

Given that fgets only sometimes includes a linebreak, and fscanf is inherently unsafe, I would like a simple alternative to read text line-by-line from a file. Is this page a good place to find such a function?
Yes. The following function should satisfy this requirement without creating any damaging security flaws.
/* reads from [stream] into [buffer] until terminated by
* \r, \n or EOF, or [lastnullindex] is reached. Returns
* the number of characters read excluding the terminating
* character. [lastnullindex] refers to the uppermost index
* of the [buffer] array. If an error occurs or non-text
* characters (below space ' ' or above tilde '~') are
* detected, the buffer will be emptied and 0 returned.
*/
int readline(FILE *stream, char *buffer, int lastnullindex) {
if (!stream) return 0;
if (!buffer) return 0;
if (lastnullindex < 0) return 0;
int inch = EOF;
int chi = 0;
while (chi < lastnullindex) {
inch = fgetc(stream);
if (inch == EOF || inch == '\n' || inch == '\r') {
buffer[chi] = '\0';
break;
} else if (inch >= ' ' && inch <= '~') {
buffer[chi] = (char)inch;
chi++;
} else {
buffer[0] = '\0';
return 0;
}
}
if (chi < 0 || chi > lastnullindex) {
buffer[0] = '\0';
return 0;
} else {
buffer[chi] = '\0';
return chi;
}
}

Encode and decode an Adobe Dreamweaver password in *.ste file

How is a password encoded or decoded in order to retrieve a password from an Adobe Dreamweaver *.ste file, or to dynamically create a *.ste file designed to be imported into Dreamweaver?
This Javascript function can be used to encode the password:
function encodePassword(input)
{
var top = 0;
var output = '';
for(var i = 0; i < input.length; i++){
var currentChar = input.charCodeAt(i);
if(currentChar < 0 || currentChar > 0xFFFF){return(false);}
if(top != 0){
if(0xDC00 <= currentChar && currentChar <= 0xDFFF){
output += dec2hex(0x10000 + ((top - 0xD800) << 10) + (currentChar - 0xDC00) + i) + '';
top = 0;
continue;
// Insert alert for below failure
}else{return(false);}
}
if(0xD800 <= currentChar && currentChar <= 0xDBFF){top = currentChar;}
else{output += dec2hex(currentChar + i) + '';}
}
return(output);
}
function dec2hex(input){return(input+0).toString(16).toUpperCase();}
And this function can be used to decode the password:
function decodePassword(input)
{
var output = "";
if(input.length == 0){return("");}
for(var i = 0; i < input.length / 2; i++){
var currentHex = parseInt(input.substr(i * 2, 2), 16);
if(currentHex <= 0xFFFF){
output += String.fromCharCode(currentHex - i);
}else if(currentHex <= 0x10FFFF){
currentHex -= 0x10000
output += String.fromCharCode(0xD800 | (currentHex >> 10)) + String.fromCharCode(0xDC00 | (currentHex & 0x3FF) - i);
}else{
//Insert alert for below failure
return(false);
}
}
return(output);
}
You can also do this online without any code using this tool: http://blog.affirmix.com/2009/05/05/live-ste-dreamweaver-password-encoder-and-decoder/
To decode the Dreamweaver password you break the password into pairs of hexadecimal (0-9, A-F) digits then subtract from each hex digit based on it's position in the string, starting with 0, then convert back to ascii.
The encrypted password of 5470714865787F would be...
54-0 = 54 => T
70-1 = 6F => o
71-2 = 6F => o
48-3 = 45 => E
65-4 = 61 => a
78-5 = 73 => s
7F-6 = 79 => y
So 5470714865787F => 'TooEasy'
We have a working example of this on our website which allows you to decode your password. You can also copy/paste your entire .ste file and it will output the needed FTP details to connect, save some time...
http://www.mywebsitespot.com/dreamweaver-password-decode
Sorry to wake up an old thread but thought I'd post my solution. It's a single HTML5 page that can load and parse Dreamweaver STE files, decoding the password as part of that. I wanted something simple, offline/local (so details aren't transmitted when decoding) that I could to just load an STE file in to and it would give me all the FTP details:
Blog Post:
http://bobmckay.com/web/dreamweaver-password-ste-file-decoder
Decoder Page:
http://bobmckay.com/dreamweaver-password-decoder/
Hope it's useful to someone!
Bob
Waking an old thread, but in addition to the accepted JavaScript answer, I just wanted to leave two PHP functions for anyone interested:
To encode a password:
function dwste_encode( $pw ){
$output = '';
$split = str_split( $pw );
foreach( $split as $key => $value ){
$char = ord( $value );
$char = ( $char + $key );
$char = dechex( $char );
$output .= strtoupper( $char );
}
return $output;
}
To decode a password:
function dwste_decode( $pw ){
$output = '';
$split = str_split( $pw, 2 );
foreach( $split as $key => $value ){
$char = hexdec( $value );
$char = ( $char - $key );
$char = chr( $char );
$output .= $char;
}
return $output;
}
I needed to create many DW ste files for a client today. I used the following VBA to do it straight from Excel. I used the code from #andrew-odri as the basis.
Function decodeDreamWeaverPass(sHash$)
Dim sPass$, i&, lHash&, lChar&, sChars$
lHash = Len(sHash) - 1
For i = 0 To lHash Step 2
sChars = Mid(sHash, i + 1, 2)
lChar = CLng("&H" & sChars)
lChar = lChar - (i / 2)
sPass = sPass & Chr(CLng(lChar))
Next
decodeDreamWeaverPass = sPass
End Function
Function encodeDreamWeaverPass(sPassword$)
Dim lTop&, i&, sOutput$
Dim lPassword&, sChar$, lChar&
lTop = 0
lPassword = Len(sPassword) - 1
For i = 0 To lPassword
lChar = Asc(Mid(sPassword, i + 1, 1))
sChar = Chr(lChar)
If ((lChar < 0) Or (lChar > 65535)) Then
encodeDreamWeaverPass = ""
End If
If lTop > 0 Then
If (lChar >= 56320) And (lChar <= 57343) Then
sOutput = sOutput & Hex(65536 + ((lTop - 55296) Xor 10) + (lChar - 56320) + i)
lTop = 0
Else
encodeDreamWeaverPass = ""
End If
End If
If (lChar >= 55296) And (lChar <= 56319) Then
lTop = lChar
Else
sOutput = sOutput & Hex(lChar + i)
End If
Next
encodeDreamWeaverPass = sOutput
End Function

Resources