Skip to content
This repository
tree: 4035ad9049
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 115 lines (90 sloc) 4.096 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
#!/usr/bin/python

"""
This file is in charge of generating the path based watermark template
"""

import random

class SimplePBWMTemplate:
    
    _predicate_list = \
                    [
                        ["1", 0],\
                        ["0", 1]
                    ]

    def __init__(self):
        self._code_list = []
        random.seed()
        self._namesource = "0123456789"
        
        self._wmid_name = "PBWM_" + "".join(random.sample(self._namesource, 10))
        
        self._wmval_name = "PBWM_" + "".join(random.sample(self._namesource, 10))
        while(self._wmval_name == self._wmid_name):
            self._wmval_name = "PBWM_" + "".join(random.sample(self._namesource, 10))

        self._wmbuf_name = "PBWM_" + "".join(random.sample(self._namesource, 10))
        while(self._wmbuf_name == self._wmid_name or \
              self._wmbuf_name == self._wmval_name):
            self._wmbuf_name = "PBWM_" + "".join(random.sample(self._namesource, 10))

    def _negate_bit(self, bit):
        if bit == '0':
            return '1'
        elif bit == '1':
            return '0'
        else:
            return 'X'

    def get_template(self, bitstr):
        """
bitstr: bit string to be embedded
"""
        length = len(bitstr)
        if length <= 0:
            return [[],""]

        full_code_header = []
        full_code_header.append("void pbwm_stub()\n")
        full_code_header.append("{\n")
        
        full_code_footer = []
        full_code_footer.append("}\n")

        self._code_list.extend(full_code_header)
        
        self._code_list.append("int " + self._wmid_name + ";\n");
        self._code_list.append("int " + self._wmval_name + ";\n");
        self._code_list.append("char " + self._wmbuf_name + ";\n");

        self._code_list.append(self._wmid_name + " = request_initialise_wm();\n\n");

        i = 0
        while i < length:
            predicate_str = "/* embed bit " + bitstr[i] + " */\n"
            predicate_choice = random.randrange(len(self._predicate_list))
            predicate_str += "if(" + self._predicate_list[predicate_choice][0] \
                            + ")\n" + "{\n"
            if self._predicate_list[predicate_choice][1] == 0:
                predicate_str += self._wmbuf_name + " = \'" + bitstr[i] + "\';\n"
            elif self._predicate_list[predicate_choice][1] == 1:
                predicate_str += self._wmbuf_name + " = \'" + \
                                 self._negate_bit(bitstr[i]) + "\';\n"
            else:
                pass
            predicate_str += "request_update_wm(" + self._wmid_name + ", &" + \
                             self._wmbuf_name + ", 1);\n"
            predicate_str += "}\nelse\n{\n"
            if self._predicate_list[predicate_choice][1] == 0:
                predicate_str += self._wmbuf_name + " = \'" + \
                                 self._negate_bit(bitstr[i]) + "\';\n"
            elif self._predicate_list[predicate_choice][1] == 1:
                predicate_str += self._wmbuf_name + " = \'" + bitstr[i] + "\';\n"
            else:
                pass
            predicate_str += "request_update_wm(" + self._wmid_name + ", &" + \
                             self._wmbuf_name + ", 1);\n"
            predicate_str += "}\n\n"

            self._code_list.append(predicate_str)

            i += 1

        self._code_list.append("request_retrieval_wm(" + self._wmid_name +
                               ", &" + self._wmval_name + ");\n")
        self._code_list.append("printf(\"Next block's ID is %d\\n\", " +
                               self._wmval_name + ");\n")
        self._code_list.append("request_release_wm(" + self._wmid_name + ");\n")

        self._code_list.extend(full_code_footer)
    
        fullcode = "".join(self._code_list)

        return [self._code_list, fullcode]

if __name__ == "__main__":
    spbwmt = SimplePBWMTemplate()
    [codelist, fullcode] = spbwmt.get_template("001011101001")
    for line in codelist:
        print line

    print
    print fullcode
Something went wrong with that request. Please try again.