-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.asm
More file actions
171 lines (139 loc) · 4.06 KB
/
response.asm
File metadata and controls
171 lines (139 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
; AsmGrm
;
; Copyright (C) 2014 Rob Hardwick
; string.h
extern strlen
; time.h
extern clock_gettime
%define tv_nsec 8 ; struct timespec -> long int tv_nsec
%define CLOCK_PROCESS_CPUTIME_ID 2
; fcgiapp.h
extern FCGX_GetParam
extern FCGX_FPrintF
extern FCGX_PutS
; permutations.asm
extern asmgrm_permutations
; Constants
%define MAX_LENGTH 6
; Stack
%define response_out 0 ; FCGX_Stream *
%define response_envp 8 ; FCGX_ParamArray
%define response_start 24 ; struct timespec
%define response_end 40 ; struct timespec
%define response_stack 56
;
; Data
;
section .data
request_uri: db `REQUEST_URI\0`
response_head: db `Content-type: text/html\r\n\r\n`, \
`<!DOCTYPE html>\n`, \
`<html lang="en">\n<head>\n`, \
`<meta charset="utf-8">\n`, \
`<meta http-equiv="X-UA-Compatible" content="IE=edge">\n`, \
`<title>AsmGrm</title>\n`, \
`<meta name="viewport" content="width=device-width, initial-scale=1">\n`, \
`<style type="text/css">\n`, \
`html { font-family: Georgia,Cambria,"Times New Roman",Times,serif; font-size: 2em; }\n`, \
`h1 { margin: 20px; }\n`, \
`h1 a { color: black; text-decoration: none; }\n`, \
`p { float: left; margin: 5px 20px; }\n`, \
`small { float: right; padding: 20px; font-size: 0.5em; }\n`, \
`</style>\n</head>\n<body>\n`, \
`<h1><a href="/">AsmGrm</a></h1>\n\0`
response_foot: db `</body>\n</html>\n\0`
response_home: db `<p>Find anagrams by entering a word into the URL (maximum 6 characters). `, \
`Why not try <a href="/apes">apes</a>, <a href="/pandas">pandas</a> `, \
`or <a href="/zebras">zebras</a>?<p>\n\0`
response_error: db `Status: 500\r\nContent-type: text/plain\r\n\r\nAn error occured :(\n\0`
time_fmt: db `<small>%ldns</small>\n\0`
;
; Code
;
section .text
global asmgrm_response
asmgrm_response:
sub rsp, response_stack
; Setup stack
mov qword[rsp+response_out], rdi
mov qword[rsp+response_envp], rsi
; Get request URI
mov rsi, qword[rsp+response_envp]
mov edi, request_uri
call FCGX_GetParam
test rax, rax
je .error
; Remove leading slash
mov r14, rax
inc r14
; Get length
mov rdi, r14
call strlen
mov ebx, eax
; Check length < MAX_LENGTH
.maxlen:
cmp rbx, MAX_LENGTH
jbe .minlen
; Truncate string
mov byte[r14+MAX_LENGTH], 0
mov ebx, MAX_LENGTH
jmp .response
; Check length > 1
.minlen:
test rbx, rbx
je .home
; Print permutations header
.response:
mov rsi, qword[rsp+response_out]
mov edi, response_head
call FCGX_PutS
; Get start time
lea rsi, [rsp+response_start]
mov edi, CLOCK_PROCESS_CPUTIME_ID
call clock_gettime
; Print permutations
mov rdi, qword[rsp+response_out]
mov ecx, ebx
xor edx, edx
mov rsi, r14
call asmgrm_permutations
; Get end time
lea rsi, [rsp+response_end]
mov edi, CLOCK_PROCESS_CPUTIME_ID
call clock_gettime
; Calculate elapsed time
mov rdx, qword[rsp+response_end+tv_nsec]
sub rdx, qword[rsp+response_start+tv_nsec]
; Print elapsed time
mov esi, time_fmt
mov rdi, qword[rsp+response_out]
xor eax, eax
call FCGX_FPrintF
; Print permutations footer
mov rsi, qword[rsp+response_out]
mov edi, response_foot
call FCGX_PutS
jmp .ret
; Print home page header
.home:
mov rsi, qword[rsp+response_out]
mov edi, response_head
call FCGX_PutS
; Print home page
mov rsi, qword[rsp+response_out]
mov edi, response_home
call FCGX_PutS
; Print home page footer
mov rsi, qword[rsp+response_out]
mov edi, response_foot
call FCGX_PutS
jmp .ret
; Print error page
.error:
mov rsi, qword[rsp+response_out]
mov edi, response_error
call FCGX_PutS
.ret:
xor eax, eax
add rsp, response_stack
ret