summaryrefslogtreecommitdiffstats
path: root/transform/absurlreplacer.go
diff options
context:
space:
mode:
authorbep <bjorn.erik.pedersen@gmail.com>2015-05-04 12:51:48 +0200
committerbep <bjorn.erik.pedersen@gmail.com>2015-05-04 12:51:52 +0200
commitbef3df7481027254a6711965828cd120fa5deb7c (patch)
treea0cc0e65b86122c0f6faaaa2effa5d47f544067f /transform/absurlreplacer.go
parentbe918d71457407b4d6649ab2cb6940c153b40bab (diff)
Polish absurlreplacer
Diffstat (limited to 'transform/absurlreplacer.go')
-rw-r--r--transform/absurlreplacer.go42
1 files changed, 24 insertions, 18 deletions
diff --git a/transform/absurlreplacer.go b/transform/absurlreplacer.go
index 40c9be75e..e1ba9e54c 100644
--- a/transform/absurlreplacer.go
+++ b/transform/absurlreplacer.go
@@ -17,8 +17,11 @@ const (
matchStateFull
)
-type contentlexer struct {
+type absurllexer struct {
+ // the source to absurlify
content []byte
+ // the target for the new absurlified content
+ w io.Writer
pos int // input position
start int // item start position
@@ -31,16 +34,19 @@ type contentlexer struct {
matches [3]bool // track matches of the 3 prefixes
idx int // last index in matches checked
- w io.Writer
}
-type stateFunc func(*contentlexer) stateFunc
+type stateFunc func(*absurllexer) stateFunc
+// prefix is how to identify and which func to handle the replacement.
type prefix struct {
r []rune
- f func(l *contentlexer)
+ f func(l *absurllexer)
}
+// new prefixes can be added below, but note:
+// - the matches array above must be expanded.
+// - the prefix must with the current logic end with '='
var prefixes = []*prefix{
&prefix{r: []rune{'s', 'r', 'c', '='}, f: checkCandidateBase},
&prefix{r: []rune{'h', 'r', 'e', 'f', '='}, f: checkCandidateBase},
@@ -53,7 +59,8 @@ type absURLMatcher struct {
replacementURL []byte
}
-func (l *contentlexer) match(r rune) {
+// match check rune inside word. Will be != ' '.
+func (l *absurllexer) match(r rune) {
var found bool
@@ -68,8 +75,6 @@ func (l *contentlexer) match(r rune) {
if l.checkMatchState(r, j) {
return
}
- } else {
- l.matches[j] = false
}
}
@@ -95,14 +100,12 @@ func (l *contentlexer) match(r rune) {
}
}
- if found {
- return
+ if !found {
+ l.ms = matchStateNone
}
-
- l.ms = matchStateNone
}
-func (l *contentlexer) checkMatchState(r rune, idx int) bool {
+func (l *absurllexer) checkMatchState(r rune, idx int) bool {
if r == '=' {
l.ms = matchStateFull
for k := range l.matches {
@@ -118,12 +121,13 @@ func (l *contentlexer) checkMatchState(r rune, idx int) bool {
return false
}
-func (l *contentlexer) emit() {
+func (l *absurllexer) emit() {
l.w.Write(l.content[l.start:l.pos])
l.start = l.pos
}
-func checkCandidateBase(l *contentlexer) {
+// handle URLs in src and href.
+func checkCandidateBase(l *absurllexer) {
for _, m := range l.matchers {
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
continue
@@ -148,7 +152,8 @@ func checkCandidateBase(l *contentlexer) {
}
}
-func checkCandidateSrcset(l *contentlexer) {
+// handle URLs in srcset.
+func checkCandidateSrcset(l *absurllexer) {
// special case, not frequent (me think)
for _, m := range l.matchers {
if !bytes.HasPrefix(l.content[l.pos:], m.match) {
@@ -201,7 +206,8 @@ func checkCandidateSrcset(l *contentlexer) {
}
}
-func (l *contentlexer) replace() {
+// main loop
+func (l *absurllexer) replace() {
contentLength := len(l.content)
var r rune
@@ -227,8 +233,8 @@ func (l *contentlexer) replace() {
for i, m := range l.matches {
if m {
p = prefixes[i]
+ l.matches[i] = false
}
- l.matches[i] = false
}
if p == nil {
panic("illegal state: curr is nil when state is full")
@@ -246,7 +252,7 @@ func (l *contentlexer) replace() {
}
func doReplace(ct contentTransformer, matchers []absURLMatcher) {
- lexer := &contentlexer{
+ lexer := &absurllexer{
content: ct.Content(),
w: ct,
matchers: matchers}
class="cm">/** * Low level command for setting/getting UWB radio's addresses * * @hwarc: HWA Radio Control interface instance * @bmOperationType: * Set/get, MAC/DEV (see WUSB1.0[8.6.2.2]) * @baAddr: address buffer--assumed to have enough data to hold * the address type requested. * @reply: Pointer to reply buffer (can be stack allocated) * @returns: 0 if ok, < 0 errno code on error. * * @cmd has to be allocated because USB cannot grok USB or vmalloc * buffers depending on your combination of host architecture. */ static int uwb_rc_dev_addr_mgmt(struct uwb_rc *rc, u8 bmOperationType, const u8 *baAddr, struct uwb_rc_evt_dev_addr_mgmt *reply) { int result; struct uwb_rc_cmd_dev_addr_mgmt *cmd; result = -ENOMEM; cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); if (cmd == NULL) goto error_kzalloc; cmd->rccb.bCommandType = UWB_RC_CET_GENERAL; cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_DEV_ADDR_MGMT); cmd->bmOperationType = bmOperationType; if (baAddr) { size_t size = 0; switch (bmOperationType >> 1) { case 0: size = 2; break; case 1: size = 6; break; default: BUG(); } memcpy(cmd->baAddr, baAddr, size); } reply->rceb.bEventType = UWB_RC_CET_GENERAL; reply->rceb.wEvent = UWB_RC_CMD_DEV_ADDR_MGMT; result = uwb_rc_cmd(rc, "DEV-ADDR-MGMT", &cmd->rccb, sizeof(*cmd), &reply->rceb, sizeof(*reply)); if (result < 0) goto error_cmd; if (result < sizeof(*reply)) { dev_err(&rc->uwb_dev.dev, "DEV-ADDR-MGMT: not enough data replied: " "%d vs %zu bytes needed\n", result, sizeof(*reply)); result = -ENOMSG; } else if (reply->bResultCode != UWB_RC_RES_SUCCESS) { dev_err(&rc->uwb_dev.dev, "DEV-ADDR-MGMT: command execution failed: %s (%d)\n", uwb_rc_strerror(reply->bResultCode), reply->bResultCode); result = -EIO; } else result = 0; error_cmd: kfree(cmd); error_kzalloc: return result; } /** * Set the UWB RC MAC or device address. * * @rc: UWB Radio Controller * @_addr: Pointer to address to write [assumed to be either a * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *']. * @type: Type of address to set (UWB_ADDR_DEV or UWB_ADDR_MAC). * @returns: 0 if ok, < 0 errno code on error. * * Some anal retentivity here: even if both 'struct * uwb_{dev,mac}_addr' have the actual byte array in the same offset * and I could just pass _addr to hwarc_cmd_dev_addr_mgmt(), I prefer * to use some syntatic sugar in case someday we decide to change the * format of the structs. The compiler will optimize it out anyway. */ static int uwb_rc_addr_set(struct uwb_rc *rc, const void *_addr, enum uwb_addr_type type) { int result; u8 bmOperationType = 0x1; /* Set address */ const struct uwb_dev_addr *dev_addr = _addr; const struct uwb_mac_addr *mac_addr = _addr; struct uwb_rc_evt_dev_addr_mgmt reply; const u8 *baAddr; result = -EINVAL; switch (type) { case UWB_ADDR_DEV: baAddr = dev_addr->data; break; case UWB_ADDR_MAC: baAddr = mac_addr->data; bmOperationType |= 0x2; break; default: return result; } return uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &reply); } /** * Get the UWB radio's MAC or device address. * * @rc: UWB Radio Controller * @_addr: Where to write the address data [assumed to be either a * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *']. * @type: Type of address to get (UWB_ADDR_DEV or UWB_ADDR_MAC). * @returns: 0 if ok (and *_addr set), < 0 errno code on error. * * See comment in uwb_rc_addr_set() about anal retentivity in the * type handling of the address variables. */ static int uwb_rc_addr_get(struct uwb_rc *rc, void *_addr, enum uwb_addr_type type) { int result; u8 bmOperationType = 0x0; /* Get address */ struct uwb_rc_evt_dev_addr_mgmt evt; struct uwb_dev_addr *dev_addr = _addr; struct uwb_mac_addr *mac_addr = _addr; u8 *baAddr; result = -EINVAL; switch (type) { case UWB_ADDR_DEV: baAddr = dev_addr->data; break; case UWB_ADDR_MAC: bmOperationType |= 0x2; baAddr = mac_addr->data; break; default: return result; } result = uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &evt); if (result == 0) switch (type) { case UWB_ADDR_DEV: memcpy(&dev_addr->data, evt.baAddr, sizeof(dev_addr->data)); break; case UWB_ADDR_MAC: memcpy(&mac_addr->data, evt.baAddr, sizeof(mac_addr->data)); break; default: /* shut gcc up */ BUG(); } return result; } /** Get @rc's MAC address to @addr */ int uwb_rc_mac_addr_get(struct uwb_rc *rc, struct uwb_mac_addr *addr) { return uwb_rc_addr_get(rc, addr, UWB_ADDR_MAC); } EXPORT_SYMBOL_GPL(uwb_rc_mac_addr_get); /** Get @rc's device address to @addr */ int uwb_rc_dev_addr_get(struct uwb_rc *rc, struct uwb_dev_addr *addr) { return uwb_rc_addr_get(rc, addr, UWB_ADDR_DEV); } EXPORT_SYMBOL_GPL(uwb_rc_dev_addr_get); /** Set @rc's address to @addr */ int uwb_rc_mac_addr_set(struct uwb_rc *rc, const struct uwb_mac_addr *addr) { int result = -EINVAL; mutex_lock(&rc->uwb_dev.mutex); result = uwb_rc_addr_set(rc, addr, UWB_ADDR_MAC); mutex_unlock(&rc->uwb_dev.mutex); return result; } /** Set @rc's address to @addr */ int uwb_rc_dev_addr_set(struct uwb_rc *rc, const struct uwb_dev_addr *addr) { int result = -EINVAL; mutex_lock(&rc->uwb_dev.mutex); result = uwb_rc_addr_set(rc, addr, UWB_ADDR_DEV); rc->uwb_dev.dev_addr = *addr; mutex_unlock(&rc->uwb_dev.mutex); return result; } /* Returns !0 if given address is already assigned to device. */ int __uwb_mac_addr_assigned_check(struct device *dev, void *_addr) { struct uwb_dev *uwb_dev = to_uwb_dev(dev); struct uwb_mac_addr *addr = _addr; if (!uwb_mac_addr_cmp(addr, &uwb_dev->mac_addr)) return !0; return 0; } /* Returns !0 if given address is already assigned to device. */ int __uwb_dev_addr_assigned_check(struct device *dev, void *_addr) { struct uwb_dev *uwb_dev = to_uwb_dev(dev); struct uwb_dev_addr *addr = _addr; if (!uwb_dev_addr_cmp(addr, &uwb_dev->dev_addr)) return !0; return 0; } /** * uwb_dev_addr_assign - assigned a generated DevAddr to a radio controller * @rc: the (local) radio controller device requiring a new DevAddr * * A new DevAddr is required when: * - first setting up a radio controller * - if the hardware reports a DevAddr conflict * * The DevAddr is randomly generated in the generated DevAddr range * [0x100, 0xfeff]. The number of devices in a beacon group is limited * by mMaxBPLength (96) so this address space will never be exhausted. * * [ECMA-368] 17.1.1, 17.16. */ int uwb_rc_dev_addr_assign(struct uwb_rc *rc) { struct uwb_dev_addr new_addr; do { get_random_bytes(new_addr.data, sizeof(new_addr.data)); } while (new_addr.data[0] == 0x00 || new_addr.data[0] == 0xff || __uwb_dev_addr_assigned(rc, &new_addr)); return uwb_rc_dev_addr_set(rc, &new_addr); } /** * uwbd_evt_handle_rc_dev_addr_conflict - handle a DEV_ADDR_CONFLICT event * @evt: the DEV_ADDR_CONFLICT notification from the radio controller * * A new (non-conflicting) DevAddr is assigned to the radio controller. * * [ECMA-368] 17.1.1.1. */ int uwbd_evt_handle_rc_dev_addr_conflict(struct uwb_event *evt) { struct uwb_rc *rc = evt->rc; return uwb_rc_dev_addr_assign(rc); } /* * Print the 48-bit EUI MAC address of the radio controller when * reading /sys/class/uwb_rc/XX/mac_address */ static ssize_t uwb_rc_mac_addr_show(struct device *dev, struct device_attribute *attr, char *buf) { struct uwb_dev *uwb_dev = to_uwb_dev(dev); struct uwb_rc *rc