Micheal  Block

Micheal Block

1604098800

Formatting Lisp

Credits: Google Common Lisp Style Guide, Emacs 21.2 Manual, Strandh’s Tutorial on Indentation, Rainer Joswig, ACow_Adonis, following Reddit users: defunkydrummer, lispm, KaranasToll, kazkylheku, theangreymacsshibe, xach, zulu-inoe

Lisp is infamous for its reliance on parentheses ( ) to delimit code and is notorious for being difficult to read because of this. As many experienced lisp programmers have pointed out, and as this article will hopefully show, properly indented code makes reading lisp much easier and the parentheses disappear into the background.

In this article, we will discuss some basic concepts for formatting Lisp code and I will share with you my personal notes, as a professional designer of presentations and reports. I hope you find it useful and that it makes it easier for you to read, write and enjoy lisp code.

1: Executive Summary

Lisp code should be formatted according to the following guidelines.

Indenting

Indent your code the way a properly configured GNU Emacs does. In practise, this means relying on a lisp editor (such as Emacs) that indents code automatically.

  • Function ArgumentsAligned with the first argument; if the first argument is on its own line, it is aligned with the function name
  • Body of FormsIndented two spaces
  • Distinguished (“special”) ArgumentsIndented four spaces

Examples

;; Align arguments to a function with the first argument:
(my-function arg-one
             arg-two
             arg-three)
;; Or under the function name:
(my-function
 arg-one
 arg-two
 arg-three)
;; Body should be nested two spaces:
(when something
  (do-this)
  (and-this)
  (and-also-this))
;; Distinguished forms should be nested four spaces:
(with-slots (a b)
    distinguished-form
  (print a)
  (print b))

Line Spacing / Newlines

When an lisp form does not fit on one line, consider inserting newlines between the arguments so that each one is on a separate line. However, do not insert newlines in a way that makes it hard to tell how many arguments the function takes or where an argument form starts and ends.

;; Bad:
(do-something first-argument second-argument (lambda (x)
    (frob x)) fourth-argument last-argument)

;; Good:
(do-something first-argument
              second-argument
              #'(lambda (x) (frob x))
              fourth-argument
              last-argument)

Spacing of Parentheses

Always space elements like this: (+ (1 2) 3) and never like this (+(1 2)3) or (+ ( 1 2 ) 3).

Do not put ending parentheses on newlines

Always put ending parentheses on the last line, and not seperately on a newline. Lisp programmers read code by indentation and not by parentheses so we do not need to allocate extra lines for closing parentheses.

;; Good:
(defun our-equal (x y)
  (or (eql x y)
      (and (consp x)
           (consp y)
           (our-equal (car x) (car y))
           (our-equal (cdr x) (cdr y)))))
;; Bad:
(defun our-equal (x y)
  (or (eql x y)
      (and (consp x)
           (consp y)
           (our-equal (car x) (car y))
           (our-equal (cdr x) (cdr y))
      )
  )
)

Vertical lines between top-line forms

We should keep one blank line between top-level forms, unless they relate to definitions that are related to each other, e.g.:

;; Good:

(defun function-one
  ...)
(defun function-two 
  ...)
;; Bad:
(defun function-one
  ...)
(defun function-two 
  ...)
;; This is okay:
(defvar font "Arial")
(defvar bg-color "Black")
(defvar text-color "White")
(defvar font-size "12pt")

#code #programming #programming-languages #lisp

What is GEEK

Buddha Community

Formatting Lisp
August  Larson

August Larson

1624357980

String Format() Function in Python

To control and handle complex string formatting more efficiently

What is formatting, why is it used?

In python, there are several ways to present output. String formatting using python is one such method where it allows the user to control and handle complex string formatting more efficiently than simply printing space-separated values.There are many types of string formatting, such as padding and alignment, using dictionaries, etc. The usage of formatting techniques is not only subjected to strings. It also formats dates, numbers, signed digits, etc.

Structure of format() method

Let us look at the basic structure of how to write in string format method.

Syntax: ‘String {} value’.format(value)

Let us look at an example:
‘Welcome to the {} world.’.format(“python”)

Here, we have defined a string( ‘’) with a placeholder( {} ) and assigned the argument of the parameter as “python.” On executing the program, the value will be assigned to the placeholder, showing the output as:

#python #programming #string format() function in python #string format() function #format() #format() function

Micheal  Block

Micheal Block

1604098800

Formatting Lisp

Credits: Google Common Lisp Style Guide, Emacs 21.2 Manual, Strandh’s Tutorial on Indentation, Rainer Joswig, ACow_Adonis, following Reddit users: defunkydrummer, lispm, KaranasToll, kazkylheku, theangreymacsshibe, xach, zulu-inoe

Lisp is infamous for its reliance on parentheses ( ) to delimit code and is notorious for being difficult to read because of this. As many experienced lisp programmers have pointed out, and as this article will hopefully show, properly indented code makes reading lisp much easier and the parentheses disappear into the background.

In this article, we will discuss some basic concepts for formatting Lisp code and I will share with you my personal notes, as a professional designer of presentations and reports. I hope you find it useful and that it makes it easier for you to read, write and enjoy lisp code.

1: Executive Summary

Lisp code should be formatted according to the following guidelines.

Indenting

Indent your code the way a properly configured GNU Emacs does. In practise, this means relying on a lisp editor (such as Emacs) that indents code automatically.

  • Function ArgumentsAligned with the first argument; if the first argument is on its own line, it is aligned with the function name
  • Body of FormsIndented two spaces
  • Distinguished (“special”) ArgumentsIndented four spaces

Examples

;; Align arguments to a function with the first argument:
(my-function arg-one
             arg-two
             arg-three)
;; Or under the function name:
(my-function
 arg-one
 arg-two
 arg-three)
;; Body should be nested two spaces:
(when something
  (do-this)
  (and-this)
  (and-also-this))
;; Distinguished forms should be nested four spaces:
(with-slots (a b)
    distinguished-form
  (print a)
  (print b))

Line Spacing / Newlines

When an lisp form does not fit on one line, consider inserting newlines between the arguments so that each one is on a separate line. However, do not insert newlines in a way that makes it hard to tell how many arguments the function takes or where an argument form starts and ends.

;; Bad:
(do-something first-argument second-argument (lambda (x)
    (frob x)) fourth-argument last-argument)

;; Good:
(do-something first-argument
              second-argument
              #'(lambda (x) (frob x))
              fourth-argument
              last-argument)

Spacing of Parentheses

Always space elements like this: (+ (1 2) 3) and never like this (+(1 2)3) or (+ ( 1 2 ) 3).

Do not put ending parentheses on newlines

Always put ending parentheses on the last line, and not seperately on a newline. Lisp programmers read code by indentation and not by parentheses so we do not need to allocate extra lines for closing parentheses.

;; Good:
(defun our-equal (x y)
  (or (eql x y)
      (and (consp x)
           (consp y)
           (our-equal (car x) (car y))
           (our-equal (cdr x) (cdr y)))))
;; Bad:
(defun our-equal (x y)
  (or (eql x y)
      (and (consp x)
           (consp y)
           (our-equal (car x) (car y))
           (our-equal (cdr x) (cdr y))
      )
  )
)

Vertical lines between top-line forms

We should keep one blank line between top-level forms, unless they relate to definitions that are related to each other, e.g.:

;; Good:

(defun function-one
  ...)
(defun function-two 
  ...)
;; Bad:
(defun function-one
  ...)
(defun function-two 
  ...)
;; This is okay:
(defvar font "Arial")
(defvar bg-color "Black")
(defvar text-color "White")
(defvar font-size "12pt")

#code #programming #programming-languages #lisp

[ExpDev] Exploit Exercise | Protostar | Format 1

Format 1 (Format String Basic 1)

The goal of this challenge is to leverage format strings to modify arbitrary memory locations to print the winning statement.

Things to note

  • **printf(string);**: This is the vulnerable function in this code. The printf() will not check whether the supplied inputs expected format strings or not since it is coded to accept any string values. So what we can do is simply to verify if we can leak memory addresses and also write arbitrary code onto the stack ([READ] %p → [WRITE] %n).
  • **if(target) {**: The target variable is what we need to find. Then, by leveraging a Format String vulnerability, we will write the address onto the stack to print out the winning statement by specifying the right offset value.

Disassemble (GDB)

Let’s disassemble the binary to see what is doing at the ASM-level:

$ gdb -q format1
Reading symbols from /opt/protostar/bin/format1...done.
(gdb) set disassembly-flavor intel
(gdb) disassemble vuln

The program is pretty simple. One interesting thing is that mov eax,ds:0x8049638 moves the following address from the .bss (part of the .data segment) into EAX register.

Below is to dump the binary’s headers using objdump:

$ objdump -h /opt/protostar/bin/format1
...(snip)...

23 .data         00000008  08049628  08049628  00000628  2**2
                  CONTENTS, ALLOC, LOAD, DATA
24 .bss          0000000c  08049630  08049630  00000630  2**2
                  ALLOC
...(snip)...

Then, the test eax,eax creates a conditional jump by setting eflags value to ZF (=Zero Flag) if the EAX is equal to “0.” If “0,” it jumps; otherwise, it continues its execution flow.


Exploit

Initial Recon

As discussed, the vulnerable function is the printf(string). When we supply a random string, it just echoes back:

However, if we supply some format string parameters, we get interesting outputs 😮:

What this is doing is basically printing out some random addresses in stack memory. But more interesting thing is if we enter enough format string parameters, we can actually find the strings that we enter on the stack.

$ ./format1 $(python -c 'print "AAAA" + "%p|" * 156') && echo

To abuse this: (1) Find an offset where we can control → (2) Write an arbitrary code we wish to execute. In this case, we will just supply an address for target variable to print the winning statement.

#format-string-attack #string-format #protostar-format1 #exploit-exercise-format1 #protostar-walkthrough

[ExpDev] Exploit Exercise | Protostar | Format 2

The goal of this challenge is to find the location where we can write our supplied input in memory and find the correct offset value to print the winning statement.

Image for post

Things to note

  • **char buffer[512]**: Setting the buffer size to 512.
  • **fgets(buffer, sizeof(buffer), stdin)**: Getting a user supplied-input. And it limits the buffer size to size of the buffer, which is 512. We can max input with 511 bytes because C always add 0x00 at the end as a terminator.
  • **printf(buffer);**: This is the vulnerable function in this code. The printf() will not check whether the supplied inputs expected format strings or not since it is coded to accept any string values. So what we can do is simply to verify if we can leak the memory addresses and also write arbitrary code onto the stack ([READ] %p or %x → [WRITE] %n).
  • **if(target == 64) {**: The target variable is the address where we need to find. Then, by leveraging the Format String vulnerability, we will write arbitrary bytes to the target to match with 64 to print out the winning statement.

Disassemble (GDB)

Let’s disassemble the binary to see what is doing at the ASM-level:

$ gdb -q format2
Reading symbols from /opt/protostar/bin/format2...done.
(gdb) set disassembly-flavor intel
(gdb) disassemble vuln

Image for post

#format-string-attack #exploit-exercise-format2 #protostar-format2 #protostar-walkthrough #string-format

Formatting the Essential JavaScript Spreadsheet: Cell Formatting

This is the second blog in our series on formatting the Syncfusion JavaScript Spreadsheet. For this one, we’ll explain the cell formatting options like number formatting, text formatting, and conditional formatting. If you missed the first blog, you can check it out at “Formatting the Essential JavaScript Spreadsheet: Number Formatting.”

The cell formatting features in the Syncfusion JavaScript Spreadsheet allow you to change the font, font size, color, attributes such as bold or italics, and more for a cell or range of cells.

Formatting text helps you enhance the visibility of the data when you are working on a large worksheet. The different types of cell formatting options available in the spreadsheet control are:

Let’s learn about them!

#excel #javascript #syncfusion #tips and tricks #web #formatting #spreadsheet