Calculating file offset from RVA in .NET Assembly - clr

I'm trying to calculate the CLI Header file offset using the optional header, I manually checked a sample .NET Assembly and noticed that the optional header gives me the RVA for the CLI Header which is 0x2008 and the file offset of the CLI Header is 0x208. How can I calculate the file offset from the RVA?
Thanks.

The PE file contains a bunch of sections that get mapped to page aligned virtual addresses using the section table (just after the optional header).
So to read the CLI Header, you can either:
use something like LoadLibrary or LoadLibraryEx to map it into memory and then just add the RVA to the returned module base address,
or you can read the section table and use it to map the RVA to a file position.
/* pseudo code */
int GetFilePosition(int rva)
{
foreach (var section in Sections)
{
var pos = rva - section.VirtualAddress;
if (pos >= 0 && pos < section.VirtualSize)
{
return pos + section.PointerToRawData;
}
}
Explode();
}
The Section table is described in ECMA-335 Partition II Section 25.3

Related

How to setup pager in dart console

It is popular to show output per page at one time when content is huge in many console applications: git log/diff, more/less; but how do I do in a dart programming application?
I find a setup_pager method in git diff source code, but could not figure it out, do I have to do some linux programming? or start an other process to pass output to less command? Thanks!
If it's something simple like the more command then use the console package to get the no of rows for the terminal.
Enter a for loop from 1 to the row count - 1
Print a row of text on each iteration.
Then prompt the user to hit the space key.
When they do, restart the for loop.
Rinse and repeat.
If you want to scroll up then again the console package is you friend.
Similar process but when you prompt the user ask which direction.
If the direction is up then clear the screen and print the prior page of text.
The tricky bit is reading backwards through the file.
Have a look at the File class and the seek method. It will let you move backward in the file but you will have to find the line brakes yourself.
A very crude implementation:
#! /usr/bin/env dcli
// ignore: prefer_relative_imports
import 'dart:math';
import 'package:dcli/dcli.dart';
/// dcli script generated by:
/// dcli create %scriptname%
///
/// See
/// https://pub.dev/packages/dcli#-installing-tab-
///
/// For details on installing dcli.
///
void main(List<String> args) {
// -1 to allow for the 'Hit enter to continue' message
var rows = Terminal().rows - 1;
final lines = read('/var/log/syslog').toList();
rows = min(lines.length, rows);
var shown = 0;
while (shown < lines.length) {
for (var row = 0; row < rows; row++) {
print(lines[shown]);
shown++;
}
ask('Hit enter to continue', required: false);
}
print(orange('Done'));
}

Reading the file lines as locs in Rascal

In the file IO module there is a function that reads the lines from the file.
list[str] fileLines = readFileLines(fileLoc);
The function neatly returns the strings. Each of these strings are also denotable as a loc with the fileLoc(O, L, <BL, BC> , <EL,EC>) format. How can I load the lines from the file as loc resources?
Short answer: The library function readFiles takes a source location as argument and reads the contents of the file. If that source location contains position information then it will only read the part of the file corresponding to that position information.
Somewhat longer answer:
Here is an illustration:
module Example
void main() {
str text = "a\nbc\ndef\n";
loc tmpLoc = |home:///tmp.txt|;
writeFile(tmpLoc, text); // write example text to file
// Read lines back and print them
println("Using readFileLines:");
for(s <- readFileLines(tmpLoc)) println(s);
// Add position information to tmpLoc that corresponds to each of the lines,
// read it back and print it:
println("Using source location with position info:");
println(readFile(tmpLoc[offset=0][length=1]));
println(readFile(tmpLoc[offset=2][length=2]));
println(readFile(tmpLoc[offset=5][length=3]));
}
the expected output is:
Using readFileLines:
a
bc
def
Using source location with position info:
a
bc
def
Final remarks:
Usually position information is generated automatically
by tools (for example a parser); programmers mostly use that information, they don't have to create it (but they can).
Parsers usually generate full line and column information, here we only use offset and length.

How to Create a view that shows all existing attributes DOORS in all formal modules under a single project?

I have a project under which there are 50+ formal modules in IBM DOORS,
I want to create a single view for all modules ( As default view )
This view should display all the attributes that are available for that particular module when I open it.
And the number of attributes in some modules vary.
If anyone in stack-overflow knows a way on this, It would be really helpful!
Before anything, you should probably be aware that there is a maximum number of attributes that can be loaded into a view. You can check out this thread for more information regarding max columns:
https://www.ibm.com/developerworks/community/forums/html/topic?id=1861480b-7aa0-43b2-bf77-be677f5f778e
Now as for how to do this. If you're looking for an automated solution using DXL here's some sample code that you can modify for your purposes. The current code will add object-level attributes that aren't system attributes to the current view of the module you run this code from.
AttrDef ad
Module m = current Module
string sAttrName
int count = 0
Column col
for col in m do {count++}
for ad in m do
{
if ((ad.object) && (!ad.system))
{
sAttrName = ad.name
col = insert (column count)
attribute(col, sAttrName)
width(col, 200)
count++
}
}
Note: This code will only generate a view with all attributes in the module it is run from, it will not loop through all modules in a project or save the view.
To loop through a project and get all modules you'll need to create a recursive function using for itemRef in folder do {...}. Something like the following:
Folder f = current Folder
void recurseFolder(Folder f)
{
Item iRef
for iRef in f do
{
if (type(iRef) == "Formal")
(call your create views function here with parameter iRef)
else if (type(iRef) == "Folder" || type(iRef) == "Project")
recurseFolder(folder(iRef))
}
}
recurseFolder(f)
And then if you need additional code to save the view, you'll have to add appropriate code for that too using save(View v). You can look up additional information pertaining to setting view preferences and saving them in the DXL Reference Manual.

Increment +1 cells with True value

I'm trying to take a set of names with check boxes next to them and make a system so that you can check some of the names (mark them as "True") and click a button. It would then increment +1 the value next to the names of the people marked true.
Here is a link to a sample sheet:
https://docs.google.com/spreadsheets/d/1gf-BrXXR0cAYCn7bMkvvK65R290NXbP9D6aA68c06C8/edit?usp=sharing
If column A, row 2 (Tim's row) is marked true, I want to increment the value in column C, row 2 by one, so Tim would have a running total of tardies next to his name.
I hope this is do-able. Thanks!
(Now I know what you're trying to get)
In order to increment a value via the press of a button, as far as I know you have to use scripts (Tools -> Script Editor). Here's something I threw together:
// editCell takes the cell to edit and it's new value
function editCell(cellName, value) {
SpreadsheetApp.getActiveSheet().getRange(cellName).setValue(value);
}
// getCell takes the cell's value and returns it
function getCell(cellName) {
return SpreadsheetApp.getActiveSheet().getRange(cellName).getValue();
}
// plusOne adds one to the field supplied. It's linked to the button in the sheet
function plusOne() {
editCell("C2",getCell("C2")+1);
}
In order to make it work, you may need to change the targeted Cell (currently C2). You'll also need to create a drawing (Insert -> Drawing) which will act as the button you'll be able to press. Once inserted, click on the three dots on it and click on Link Script. Type in plusOne. When executing it the first time, it'll ask you to authenticate the use of scripts.
That should do the trick. I hope you have some understanding of Java Script though (to modify the code to your needs optimally).
Edit - Expandable version
So, to make every number behind a ticked field increase by one, you can use this version of the code:
// Adds one to every field within "AddArea" that has a tick in front of it. It's linked to the button in the sheet.
function plusOne() {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange("AddArea");
var values = range.getValues();
var newValues = [];
for (var i = 0; i < range.getNumRows(); ++i) {
var row = values[i];
if(row[0]) {
newValues.push([true, row[1]+1]);
}
else {
newValues.push([false, row[1]]);
}
}
range.setValues(newValues);
}
You need to define a custom named area, named "AddArea" (Data -> Labeled Areas [or similar]), link the script to a button and allow the script to be run. This was hard but very fun to figure out.
Example Sheet for reference (updated)
Can be achieved with just, for example for C2:
=A2+C2
but you would need to turn on iterative calculation (File > Spreadsheet settings... > Calculation [Max. 1 is adequate]) and I would not really recommend that over a trigger with Google Apps Script.

Pass null values to SVG path (using d3.js) to suppress missing data

Using jQuery Flot, I can pass a null value to the plotting mechanism so it just won't draw anything on the plot. See how the missing records are suppressed:
I'm looking to move to d3js, so that I can have deeper low level control of the graphics using SVG. However, I have yet to find out how to do that same process of suppressing missing records. The image below is an attempt to do this, using a value of 0 instead of null (where the d3 package breaks down). Here is some code to give you an idea of how I produced the graph below:
var line = d3.svg.line()
.x(function(d) {
var date = new Date(d[0]);
return x(date);
})
.y(function(d) {
var height = d[1];
if (no_record_exists) {
return y(0);
}
return y(height) + 0.5;
});
I looked up the SVG path element at the Mozilla Developer Network, and I found out that there is a MoveTo command, M x y, that only moves the "pen" to some point without drawing anything. Has this been implemented in the d3js package, so that I won't have to create several path elements every time I encounter a missing record?
The defined function of d3.svg.line() is the way to do this
Let's say we want to include a break in the chart if y is null:
line.defined(function(d) { return d.y!=null; })
Use line.defined or area.defined, and see the Area with Missing Data example.

Resources